Commit 559c7277 authored by Chen Bill's avatar Chen Bill Committed by GitHub

read bit field with sqlite3_column_int64 (#2704)

* read bit field with sqlite3_column_int64

follow the standard conversion

* handle all error code in v2

* always finalize stmt in ReadDB

* fix loop condition
parent d06a9096
...@@ -13,23 +13,21 @@ DataManager::DataManager() : _datas(32768), _strings(32768) { ...@@ -13,23 +13,21 @@ DataManager::DataManager() : _datas(32768), _strings(32768) {
extra_setcode = { {8512558u, {0x8f, 0x54, 0x59, 0x82, 0x13a}}, }; extra_setcode = { {8512558u, {0x8f, 0x54, 0x59, 0x82, 0x13a}}, };
} }
bool DataManager::ReadDB(sqlite3* pDB) { bool DataManager::ReadDB(sqlite3* pDB) {
sqlite3_stmt* pStmt{}; sqlite3_stmt* pStmt = nullptr;
const char* sql = "select * from datas,texts where datas.id=texts.id"; const char* sql = "select * from datas,texts where datas.id=texts.id";
if (sqlite3_prepare_v2(pDB, sql, -1, &pStmt, 0) != SQLITE_OK) if (sqlite3_prepare_v2(pDB, sql, -1, &pStmt, 0) != SQLITE_OK)
return Error(pDB); return Error(pDB, pStmt);
wchar_t strBuffer[4096]; wchar_t strBuffer[4096];
int step = 0; int step = 0;
do { do {
CardDataC cd; CardDataC cd;
CardString cs; CardString cs;
step = sqlite3_step(pStmt); step = sqlite3_step(pStmt);
if (step == SQLITE_BUSY || step == SQLITE_ERROR || step == SQLITE_MISUSE) if (step == SQLITE_ROW) {
return Error(pDB, pStmt);
else if (step == SQLITE_ROW) {
cd.code = sqlite3_column_int(pStmt, 0); cd.code = sqlite3_column_int(pStmt, 0);
cd.ot = sqlite3_column_int(pStmt, 1); cd.ot = sqlite3_column_int(pStmt, 1);
cd.alias = sqlite3_column_int(pStmt, 2); cd.alias = sqlite3_column_int(pStmt, 2);
auto setcode = sqlite3_column_int64(pStmt, 3); uint64_t setcode = static_cast<uint64_t>(sqlite3_column_int64(pStmt, 3));
if (setcode) { if (setcode) {
auto it = extra_setcode.find(cd.code); auto it = extra_setcode.find(cd.code);
if (it != extra_setcode.end()) { if (it != extra_setcode.end()) {
...@@ -42,7 +40,7 @@ bool DataManager::ReadDB(sqlite3* pDB) { ...@@ -42,7 +40,7 @@ bool DataManager::ReadDB(sqlite3* pDB) {
else else
cd.set_setcode(setcode); cd.set_setcode(setcode);
} }
cd.type = sqlite3_column_int(pStmt, 4); cd.type = static_cast<decltype(cd.type)>(sqlite3_column_int64(pStmt, 4));
cd.attack = sqlite3_column_int(pStmt, 5); cd.attack = sqlite3_column_int(pStmt, 5);
cd.defense = sqlite3_column_int(pStmt, 6); cd.defense = sqlite3_column_int(pStmt, 6);
if (cd.type & TYPE_LINK) { if (cd.type & TYPE_LINK) {
...@@ -51,13 +49,13 @@ bool DataManager::ReadDB(sqlite3* pDB) { ...@@ -51,13 +49,13 @@ bool DataManager::ReadDB(sqlite3* pDB) {
} }
else else
cd.link_marker = 0; cd.link_marker = 0;
unsigned int level = sqlite3_column_int(pStmt, 7); uint32_t level = static_cast<uint32_t>(sqlite3_column_int(pStmt, 7));
cd.level = level & 0xff; cd.level = level & 0xff;
cd.lscale = (level >> 24) & 0xff; cd.lscale = (level >> 24) & 0xff;
cd.rscale = (level >> 16) & 0xff; cd.rscale = (level >> 16) & 0xff;
cd.race = sqlite3_column_int(pStmt, 8); cd.race = static_cast<decltype(cd.race)>(sqlite3_column_int64(pStmt, 8));
cd.attribute = sqlite3_column_int(pStmt, 9); cd.attribute = static_cast<decltype(cd.attribute)>(sqlite3_column_int64(pStmt, 9));
cd.category = sqlite3_column_int(pStmt, 10); cd.category = static_cast<decltype(cd.category)>(sqlite3_column_int64(pStmt, 10));
_datas[cd.code] = cd; _datas[cd.code] = cd;
if (const char* text = (const char*)sqlite3_column_text(pStmt, 12)) { if (const char* text = (const char*)sqlite3_column_text(pStmt, 12)) {
BufferIO::DecodeUTF8(text, strBuffer); BufferIO::DecodeUTF8(text, strBuffer);
...@@ -76,7 +74,9 @@ bool DataManager::ReadDB(sqlite3* pDB) { ...@@ -76,7 +74,9 @@ bool DataManager::ReadDB(sqlite3* pDB) {
} }
_strings[cd.code] = cs; _strings[cd.code] = cs;
} }
} while (step != SQLITE_DONE); else if (step != SQLITE_DONE)
return Error(pDB, pStmt);
} while (step == SQLITE_ROW);
sqlite3_finalize(pStmt); sqlite3_finalize(pStmt);
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment