From c9ee849a2c03e17941fd1b650461bd7b0ed64b15 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 12 May 2015 18:02:14 +0800 Subject: [PATCH] Add localStorage.clear in JSB --- .../js_bindings_system_functions.cpp | 10 +++++++++ .../js_bindings_system_functions.h | 1 + cocos/storage/local-storage/LocalStorage.cpp | 22 ++++++++++++++++++- cocos/storage/local-storage/LocalStorage.h | 3 +++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/cocos/scripting/js-bindings/manual/localstorage/js_bindings_system_functions.cpp b/cocos/scripting/js-bindings/manual/localstorage/js_bindings_system_functions.cpp index 6fe52144c3..590e26f198 100644 --- a/cocos/scripting/js-bindings/manual/localstorage/js_bindings_system_functions.cpp +++ b/cocos/scripting/js-bindings/manual/localstorage/js_bindings_system_functions.cpp @@ -69,5 +69,15 @@ bool JSB_localStorageSetItem(JSContext *cx, uint32_t argc, jsval *vp) { return true; } +// Arguments: char*, char* +// Ret value: void +bool JSB_localStorageClear(JSContext *cx, uint32_t argc, jsval *vp) { + JSB_PRECONDITION2( argc == 0, cx, false, "Invalid number of arguments" ); + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + + localStorageClear(); + args.rval().setUndefined(); + return true; +} //#endif // JSB_INCLUDE_SYSTEM diff --git a/cocos/scripting/js-bindings/manual/localstorage/js_bindings_system_functions.h b/cocos/scripting/js-bindings/manual/localstorage/js_bindings_system_functions.h index 85f9ea55c3..6560d0b2d3 100644 --- a/cocos/scripting/js-bindings/manual/localstorage/js_bindings_system_functions.h +++ b/cocos/scripting/js-bindings/manual/localstorage/js_bindings_system_functions.h @@ -12,6 +12,7 @@ extern "C" { bool JSB_localStorageGetItem(JSContext *cx, uint32_t argc, jsval *vp); bool JSB_localStorageRemoveItem(JSContext *cx, uint32_t argc, jsval *vp); bool JSB_localStorageSetItem(JSContext *cx, uint32_t argc, jsval *vp); +bool JSB_localStorageClear(JSContext *cx, uint32_t argc, jsval *vp); #ifdef __cplusplus } diff --git a/cocos/storage/local-storage/LocalStorage.cpp b/cocos/storage/local-storage/LocalStorage.cpp index 4580015617..da61f960ac 100644 --- a/cocos/storage/local-storage/LocalStorage.cpp +++ b/cocos/storage/local-storage/LocalStorage.cpp @@ -42,6 +42,7 @@ static sqlite3 *_db; static sqlite3_stmt *_stmt_select; static sqlite3_stmt *_stmt_remove; static sqlite3_stmt *_stmt_update; +static sqlite3_stmt *_stmt_clear; static void localStorageCreateTable() @@ -80,6 +81,10 @@ void localStorageInit( const std::string& fullpath/* = "" */) // DELETE const char *sql_remove = "DELETE FROM data WHERE key=?;"; ret |= sqlite3_prepare_v2(_db, sql_remove, -1, &_stmt_remove, nullptr); + + // Clear + const char *sql_clear = "DELETE FROM data;"; + ret |= sqlite3_prepare_v2(_db, sql_clear, -1, &_stmt_clear, nullptr); if( ret != SQLITE_OK ) { printf("Error initializing DB\n"); @@ -130,11 +135,15 @@ bool localStorageGetItem( const std::string& key, std::string *outItem ) ok |= sqlite3_step(_stmt_select); const unsigned char *text = sqlite3_column_text(_stmt_select, 0); - if( (ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW) || !text) + if ( ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW ) { printf("Error in localStorage.getItem()\n"); return false; } + else if (!text) + { + return false; + } else { outItem->assign((const char*)text); @@ -157,4 +166,15 @@ void localStorageRemoveItem( const std::string& key ) printf("Error in localStorage.removeItem()\n"); } +/** 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"); +} + #endif // #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) diff --git a/cocos/storage/local-storage/LocalStorage.h b/cocos/storage/local-storage/LocalStorage.h index 642451a221..2c8dd1b8a1 100644 --- a/cocos/storage/local-storage/LocalStorage.h +++ b/cocos/storage/local-storage/LocalStorage.h @@ -51,6 +51,9 @@ bool CC_DLL localStorageGetItem( const std::string& key, std::string *outItem ); /** Removes an item from the JS. */ void CC_DLL localStorageRemoveItem( const std::string& key ); +/** Removes all items from the JS. */ +void CC_DLL localStorageClear(); + // end group /// @}