15.【cocos2d-x 源码分析】:localStorage的详细分析

对应源码位置:cocos2d-x-3.3\cocos\storage\local-storage\localStorage

localStorage 的接口

cocos2d-x提供了简单的本地数据存储的功能,其主要是由对sqlite的封装来实现。

//初始化 数据库
void CC_DLL localStorageInit( const std::string& fullpath = "");
//释放数据库
void CC_DLL localStorageFree();
//存一个key-value
void CC_DLL localStorageSetItem( const std::string& key, const std::string& value);
//根据key获取value
std::string CC_DLL localStorageGetItem( const std::string& key );
//删除 key对应的 项
void CC_DLL localStorageRemoveItem( const std::string& key );

localStorage 的接口的实现

//是否初始化的标志
static int _initialized = 0;
static sqlite3 *_db;
//查询
static sqlite3_stmt *_stmt_select;
//删除
static sqlite3_stmt *_stmt_remove;
//更新
static sqlite3_stmt *_stmt_update;

//static表明  别处不能用 
static void localStorageCreateTable()
{	//创建 data表 两个属性 key为主键
	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);
	
	if( ok != SQLITE_OK && ok != SQLITE_DONE)
		printf("Error in CREATE TABLE\n");
}

void localStorageInit( const std::string& fullpath/* = "" */)
{
	//如果没有初始化
	if( ! _initialized ) {
		int ret = 0;
		//未给路径 则创建内存数据库
		if (fullpath.empty())
			ret = sqlite3_open(":memory:",&_db);
		else
			ret = sqlite3_open(fullpath.c_str(), &_db);
		//建表
		localStorageCreateTable();
		//先准备语句
		// SELECT
		const char *sql_select = "SELECT value FROM data WHERE key=?;";
		ret |= sqlite3_prepare_v2(_db, sql_select, -1, &_stmt_select, nullptr);

		// REPLACE
		const char *sql_update = "REPLACE INTO data (key, value) VALUES (?,?);";
		ret |= sqlite3_prepare_v2(_db, sql_update, -1, &_stmt_update, nullptr);

		// DELETE
		const char *sql_remove = "DELETE FROM data WHERE key=?;";
		ret |= sqlite3_prepare_v2(_db, sql_remove, -1, &_stmt_remove, nullptr);

		if( ret != SQLITE_OK ) {
			printf("Error initializing DB\n");
			// report error
		}
		
		_initialized = 1;
	}
}

void localStorageFree()
{
	//已经初始化了 就都关闭了
	if( _initialized ) {
		sqlite3_finalize(_stmt_select);
		sqlite3_finalize(_stmt_remove);
		sqlite3_finalize(_stmt_update);		
		//关闭数据库
		sqlite3_close(_db);
		_initialized = 0;
	}
}

/** sets an item in the LS */
void localStorageSetItem( const std::string& key, const std::string& value)
{
	//一定要确保已经初始化了
	assert( _initialized );
	//配置 查询
	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);
	ok |= sqlite3_step(_stmt_update);
	ok |= sqlite3_reset(_stmt_update);
	
	if( ok != SQLITE_OK && ok != SQLITE_DONE)
		printf("Error in localStorage.setItem()\n");
}

//select
std::string localStorageGetItem( const std::string& key )
{
	assert( _initialized );

	std::string ret;
	int ok = sqlite3_reset(_stmt_select);

	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);
	if (text)
		ret = (const char*)text;

	if( ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW)
		printf("Error in localStorage.getItem()\n");
	return ret;
}

//删除对应项
void localStorageRemoveItem( const std::string& key )
{
	assert( _initialized );

	int ok = sqlite3_bind_text(_stmt_remove, 1, key.c_str(), -1, SQLITE_TRANSIENT);
	
	ok |= sqlite3_step(_stmt_remove);
	
	ok |= sqlite3_reset(_stmt_remove);

	if( ok != SQLITE_OK && ok != SQLITE_DONE)
		printf("Error in localStorage.removeItem()\n");
}

最后

下一篇分析 HttpClient 的实现。

你可能感兴趣的:(cocos2d-x,c++,cocos2d-x,源码分析)