2013-01-06 11:57:10 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2012 - Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2013-01-06 11:57:10 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Local Storage support for the JS Bindings for iOS.
|
|
|
|
Works on cocos2d-iphone and cocos2d-x.
|
|
|
|
*/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "storage/local-storage/LocalStorage.h"
|
2014-09-10 08:17:07 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2013-01-07 17:22:06 +08:00
|
|
|
|
2013-08-07 18:01:41 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
|
2013-01-07 17:22:06 +08:00
|
|
|
|
2013-01-06 11:57:10 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
|
|
|
static int _initialized = 0;
|
|
|
|
static sqlite3 *_db;
|
|
|
|
static sqlite3_stmt *_stmt_select;
|
|
|
|
static sqlite3_stmt *_stmt_remove;
|
|
|
|
static sqlite3_stmt *_stmt_update;
|
2015-05-12 18:02:14 +08:00
|
|
|
static sqlite3_stmt *_stmt_clear;
|
2013-01-06 11:57:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void localStorageCreateTable()
|
|
|
|
{
|
2016-04-15 02:42:16 +08:00
|
|
|
const char *sql_createtable = "CREATE TABLE IF NOT EXISTS data(key TEXT PRIMARY KEY,value TEXT);";
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
int ok = sqlite3_prepare_v2(_db, sql_createtable, -1, &stmt, nullptr);
|
|
|
|
ok |= sqlite3_step(stmt);
|
|
|
|
ok |= sqlite3_finalize(stmt);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
if (ok != SQLITE_OK && ok != SQLITE_DONE)
|
|
|
|
printf("Error in CREATE TABLE\n");
|
2013-01-06 11:57:10 +08:00
|
|
|
}
|
|
|
|
|
2014-01-20 22:31:56 +08:00
|
|
|
void localStorageInit( const std::string& fullpath/* = "" */)
|
2013-01-06 11:57:10 +08:00
|
|
|
{
|
2016-04-15 02:42:16 +08:00
|
|
|
if (!_initialized) {
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
int ret = 0;
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
if (fullpath.empty())
|
|
|
|
ret = sqlite3_open(":memory:", &_db);
|
|
|
|
else
|
|
|
|
ret = sqlite3_open(fullpath.c_str(), &_db);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
localStorageCreateTable();
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
// SELECT
|
|
|
|
const char *sql_select = "SELECT value FROM data WHERE key=?;";
|
|
|
|
ret |= sqlite3_prepare_v2(_db, sql_select, -1, &_stmt_select, nullptr);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
// REPLACE
|
|
|
|
const char *sql_update = "REPLACE INTO data (key, value) VALUES (?,?);";
|
|
|
|
ret |= sqlite3_prepare_v2(_db, sql_update, -1, &_stmt_update, nullptr);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
// DELETE
|
|
|
|
const char *sql_remove = "DELETE FROM data WHERE key=?;";
|
|
|
|
ret |= sqlite3_prepare_v2(_db, sql_remove, -1, &_stmt_remove, nullptr);
|
2015-05-12 18:02:14 +08:00
|
|
|
|
|
|
|
// Clear
|
|
|
|
const char *sql_clear = "DELETE FROM data;";
|
|
|
|
ret |= sqlite3_prepare_v2(_db, sql_clear, -1, &_stmt_clear, nullptr);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
printf("Error initializing DB\n");
|
|
|
|
// report error
|
|
|
|
}
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
_initialized = 1;
|
|
|
|
}
|
2013-01-06 11:57:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void localStorageFree()
|
|
|
|
{
|
2016-04-15 02:42:16 +08:00
|
|
|
if (_initialized) {
|
|
|
|
sqlite3_finalize(_stmt_select);
|
|
|
|
sqlite3_finalize(_stmt_remove);
|
|
|
|
sqlite3_finalize(_stmt_update);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
sqlite3_close(_db);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
_initialized = 0;
|
|
|
|
}
|
2013-01-06 11:57:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** sets an item in the LS */
|
2014-01-20 22:31:56 +08:00
|
|
|
void localStorageSetItem( const std::string& key, const std::string& value)
|
2013-01-06 11:57:10 +08:00
|
|
|
{
|
2016-04-15 02:42:16 +08:00
|
|
|
assert( _initialized );
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
int ok = sqlite3_bind_text(_stmt_update, 1, key.c_str(), -1, SQLITE_TRANSIENT);
|
|
|
|
ok |= sqlite3_bind_text(_stmt_update, 2, value.c_str(), -1, SQLITE_TRANSIENT);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
ok |= sqlite3_step(_stmt_update);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
ok |= sqlite3_reset(_stmt_update);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
if (ok != SQLITE_OK && ok != SQLITE_DONE)
|
|
|
|
printf("Error in localStorage.setItem()\n");
|
2013-01-06 11:57:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** gets an item from the LS */
|
2015-04-23 19:06:06 +08:00
|
|
|
bool localStorageGetItem( const std::string& key, std::string *outItem )
|
2013-01-06 11:57:10 +08:00
|
|
|
{
|
2016-04-15 02:42:16 +08:00
|
|
|
assert( _initialized );
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
int ok = sqlite3_reset(_stmt_select);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
ok |= sqlite3_bind_text(_stmt_select, 1, key.c_str(), -1, SQLITE_TRANSIENT);
|
|
|
|
ok |= sqlite3_step(_stmt_select);
|
|
|
|
const unsigned char *text = sqlite3_column_text(_stmt_select, 0);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
if (ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW)
|
2015-05-12 18:02:14 +08:00
|
|
|
{
|
2016-04-15 02:42:16 +08:00
|
|
|
printf("Error in localStorage.getItem()\n");
|
2015-05-12 18:02:14 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-15 02:42:16 +08:00
|
|
|
else if (!text)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
2015-04-23 19:06:06 +08:00
|
|
|
{
|
|
|
|
outItem->assign((const char*)text);
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-06 11:57:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** removes an item from the LS */
|
2014-01-20 22:31:56 +08:00
|
|
|
void localStorageRemoveItem( const std::string& key )
|
2013-01-06 11:57:10 +08:00
|
|
|
{
|
2016-04-15 02:42:16 +08:00
|
|
|
assert( _initialized );
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
int ok = sqlite3_bind_text(_stmt_remove, 1, key.c_str(), -1, SQLITE_TRANSIENT);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
ok |= sqlite3_step(_stmt_remove);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
ok |= sqlite3_reset(_stmt_remove);
|
2013-01-06 11:57:10 +08:00
|
|
|
|
2016-04-15 02:42:16 +08:00
|
|
|
if (ok != SQLITE_OK && ok != SQLITE_DONE)
|
|
|
|
printf("Error in localStorage.removeItem()\n");
|
2013-01-06 11:57:10 +08:00
|
|
|
}
|
2013-01-07 17:22:06 +08:00
|
|
|
|
2015-05-12 18:02:14 +08:00
|
|
|
/** removes all items from the LS */
|
|
|
|
void localStorageClear()
|
|
|
|
{
|
|
|
|
assert( _initialized );
|
|
|
|
|
|
|
|
int ok = sqlite3_step(_stmt_clear);
|
|
|
|
|
|
|
|
if( ok != SQLITE_OK && ok != SQLITE_DONE)
|
|
|
|
printf("Error in localStorage.clear()\n");
|
|
|
|
}
|
|
|
|
|
2013-01-07 17:22:06 +08:00
|
|
|
#endif // #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
|