static struct {
const char* pPath;
sqlite3* pHandle;
+ bool need_reset;
} databaseInfo = {
- DB_PATH, NULL
+ DB_PATH, NULL, false
};
+static inline int _db_disconnect(void);
+
static inline int _begin_transaction(void)
{
sqlite3_stmt* pStmt = NULL;
return EXIT_SUCCESS;
}
+static inline int _db_integrity_check_cb(void *NotUsed, int argc, char **argv, char **azColName)
+{
+ std::string result_DB_check = argv[0];
+ std::transform(result_DB_check.begin(), result_DB_check.end(), result_DB_check.begin(), ::tolower);
+ if (result_DB_check != "ok") {
+ databaseInfo.need_reset = true;
+ LOGE ("DB integrity check failed\n");
+ }
+
+ return 0;
+}
+
static inline int _db_create_ime_info(void)
{
char* pException = NULL;
if (databaseInfo.pHandle)
db_util_close(databaseInfo.pHandle);
databaseInfo.pHandle = NULL;
+ databaseInfo.need_reset = true;
return -EIO;
}
static const char* pQuery = "PRAGMA journal_mode = WAL";
int ret = sqlite3_exec(databaseInfo.pHandle, pQuery, NULL, NULL, &pException);
if (ret != SQLITE_OK) {
+ databaseInfo.need_reset = true;
LOGE ("sqlite3_exec returned %d: %s\n", ret, pException);
}
}
+ if (SQLITE_CORRUPT == sqlite3_exec(databaseInfo.pHandle, "pragma integrity_check",_db_integrity_check_cb, NULL, NULL)) {
+ databaseInfo.need_reset = true;
+ LOGE ("DB has been corrupted.\n");
+ }
+
return 0;
}
{
if (!databaseInfo.pHandle) {
int ret = _db_init();
+ if (databaseInfo.need_reset) {
+ _db_disconnect();
+ databaseInfo.need_reset = false;
+ remove(databaseInfo.pPath);
+ ret = _db_init();
+ }
+
if (ret < 0)
{
LOGE ("_db_init failed. error code=%d\n", ret);