LocalStorage.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. Copyright (c) 2012 - Zynga Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /*
  21. Local Storage support for the JS Bindings for iOS.
  22. Works on cocos2d-iphone and cocos2d-x.
  23. */
  24. #include "storage/local-storage/LocalStorage.h"
  25. #include "platform/CCPlatformMacros.h"
  26. #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <assert.h>
  30. #include <sqlite3.h>
  31. static int _initialized = 0;
  32. static sqlite3 *_db;
  33. static sqlite3_stmt *_stmt_select;
  34. static sqlite3_stmt *_stmt_remove;
  35. static sqlite3_stmt *_stmt_update;
  36. static sqlite3_stmt *_stmt_clear;
  37. static void localStorageCreateTable()
  38. {
  39. const char *sql_createtable = "CREATE TABLE IF NOT EXISTS data(key TEXT PRIMARY KEY,value TEXT);";
  40. sqlite3_stmt *stmt;
  41. int ok = sqlite3_prepare_v2(_db, sql_createtable, -1, &stmt, nullptr);
  42. ok |= sqlite3_step(stmt);
  43. ok |= sqlite3_finalize(stmt);
  44. if (ok != SQLITE_OK && ok != SQLITE_DONE)
  45. printf("Error in CREATE TABLE\n");
  46. }
  47. void localStorageInit( const std::string& fullpath/* = "" */)
  48. {
  49. if (!_initialized) {
  50. int ret = 0;
  51. if (fullpath.empty())
  52. ret = sqlite3_open(":memory:", &_db);
  53. else
  54. ret = sqlite3_open(fullpath.c_str(), &_db);
  55. localStorageCreateTable();
  56. // SELECT
  57. const char *sql_select = "SELECT value FROM data WHERE key=?;";
  58. ret |= sqlite3_prepare_v2(_db, sql_select, -1, &_stmt_select, nullptr);
  59. // REPLACE
  60. const char *sql_update = "REPLACE INTO data (key, value) VALUES (?,?);";
  61. ret |= sqlite3_prepare_v2(_db, sql_update, -1, &_stmt_update, nullptr);
  62. // DELETE
  63. const char *sql_remove = "DELETE FROM data WHERE key=?;";
  64. ret |= sqlite3_prepare_v2(_db, sql_remove, -1, &_stmt_remove, nullptr);
  65. // Clear
  66. const char *sql_clear = "DELETE FROM data;";
  67. ret |= sqlite3_prepare_v2(_db, sql_clear, -1, &_stmt_clear, nullptr);
  68. if (ret != SQLITE_OK) {
  69. printf("Error initializing DB\n");
  70. // report error
  71. }
  72. _initialized = 1;
  73. }
  74. }
  75. void localStorageFree()
  76. {
  77. if (_initialized) {
  78. sqlite3_finalize(_stmt_select);
  79. sqlite3_finalize(_stmt_remove);
  80. sqlite3_finalize(_stmt_update);
  81. sqlite3_close(_db);
  82. _initialized = 0;
  83. }
  84. }
  85. /** sets an item in the LS */
  86. void localStorageSetItem( const std::string& key, const std::string& value)
  87. {
  88. assert( _initialized );
  89. int ok = sqlite3_bind_text(_stmt_update, 1, key.c_str(), -1, SQLITE_TRANSIENT);
  90. ok |= sqlite3_bind_text(_stmt_update, 2, value.c_str(), -1, SQLITE_TRANSIENT);
  91. ok |= sqlite3_step(_stmt_update);
  92. ok |= sqlite3_reset(_stmt_update);
  93. if (ok != SQLITE_OK && ok != SQLITE_DONE)
  94. printf("Error in localStorage.setItem()\n");
  95. }
  96. /** gets an item from the LS */
  97. bool localStorageGetItem( const std::string& key, std::string *outItem )
  98. {
  99. assert( _initialized );
  100. int ok = sqlite3_reset(_stmt_select);
  101. ok |= sqlite3_bind_text(_stmt_select, 1, key.c_str(), -1, SQLITE_TRANSIENT);
  102. ok |= sqlite3_step(_stmt_select);
  103. const unsigned char *text = sqlite3_column_text(_stmt_select, 0);
  104. if (ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW)
  105. {
  106. printf("Error in localStorage.getItem()\n");
  107. return false;
  108. }
  109. else if (!text)
  110. {
  111. return false;
  112. }
  113. else
  114. {
  115. outItem->assign((const char*)text);
  116. return true;
  117. }
  118. }
  119. /** removes an item from the LS */
  120. void localStorageRemoveItem( const std::string& key )
  121. {
  122. assert( _initialized );
  123. int ok = sqlite3_bind_text(_stmt_remove, 1, key.c_str(), -1, SQLITE_TRANSIENT);
  124. ok |= sqlite3_step(_stmt_remove);
  125. ok |= sqlite3_reset(_stmt_remove);
  126. if (ok != SQLITE_OK && ok != SQLITE_DONE)
  127. printf("Error in localStorage.removeItem()\n");
  128. }
  129. /** removes all items from the LS */
  130. void localStorageClear()
  131. {
  132. assert( _initialized );
  133. int ok = sqlite3_step(_stmt_clear);
  134. if( ok != SQLITE_OK && ok != SQLITE_DONE)
  135. printf("Error in localStorage.clear()\n");
  136. }
  137. #endif // #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)