-
-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error messages and check for more return codes in various database subroutines #117
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ bool dbopen(void) | |
pthread_mutex_lock(&dblock); | ||
int rc = sqlite3_open_v2(FTLfiles.db, &db, SQLITE_OPEN_READWRITE, NULL); | ||
if( rc ){ | ||
logg("Cannot open database: %s", sqlite3_errmsg(db)); | ||
logg("dbopen() - SQL error (%i): %s", rc, sqlite3_errmsg(db)); | ||
dbclose(); | ||
return false; | ||
} | ||
|
@@ -70,7 +70,6 @@ bool dbquery(const char *format, ...) | |
if(query == NULL) | ||
{ | ||
logg("Memory allocation failed in dbquery()"); | ||
va_end(args); | ||
return false; | ||
} | ||
|
||
|
@@ -96,7 +95,7 @@ bool db_create(void) | |
bool ret; | ||
int rc = sqlite3_open_v2(FTLfiles.db, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); | ||
if( rc ){ | ||
logg("Can't create database: %s", sqlite3_errmsg(db)); | ||
logg("db_create() - SQL error (%i): %s", rc, sqlite3_errmsg(db)); | ||
dbclose(); | ||
return false; | ||
} | ||
|
@@ -131,7 +130,7 @@ void db_init(void) | |
{ | ||
int rc = sqlite3_open_v2(FTLfiles.db, &db, SQLITE_OPEN_READWRITE, NULL); | ||
if( rc ){ | ||
logg("Cannot open database: %s", sqlite3_errmsg(db)); | ||
logg("db_init() - Cannot open database (%i): %s", rc, sqlite3_errmsg(db)); | ||
dbclose(); | ||
|
||
logg("Creating new (empty) database"); | ||
|
@@ -171,16 +170,16 @@ int db_get_FTL_property(unsigned int ID) | |
|
||
rc = sqlite3_prepare(db, querystring, -1, &dbstmt, NULL); | ||
if( rc ){ | ||
logg("Cannot read from database: %s", sqlite3_errmsg(db)); | ||
logg("db_get_FTL_property() - SQL error prepare (%i): %s", rc, sqlite3_errmsg(db)); | ||
dbclose(); | ||
return -1; | ||
} | ||
free(querystring); | ||
|
||
// Evaluate SQL statement | ||
sqlite3_step(dbstmt); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forgot to check for this return value here, Thanks @Mcat12 |
||
if( rc ){ | ||
logg("Cannot evaluate in database: %s", sqlite3_errmsg(db)); | ||
rc = sqlite3_step(dbstmt); | ||
if( rc != SQLITE_ROW ){ | ||
logg("db_get_FTL_property() - SQL error step (%i): %s", rc, sqlite3_errmsg(db)); | ||
dbclose(); | ||
return -1; | ||
} | ||
|
@@ -200,15 +199,23 @@ bool db_set_FTL_property(unsigned int ID, int value) | |
int number_of_queries_in_DB(void) | ||
{ | ||
sqlite3_stmt* stmt; | ||
int result = -1; | ||
|
||
// Count number of rows using the index timestamp is faster than select(*) | ||
sqlite3_prepare_v2(db, "SELECT COUNT(timestamp) FROM queries", -1, &stmt, NULL); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a check for this return value |
||
int rc = sqlite3_step(stmt); | ||
if (rc == SQLITE_ROW) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disentangled this slightly |
||
result = sqlite3_column_int(stmt, 0); | ||
else | ||
logg("get_number_of_queries_in_DB() - SQL error: %s", sqlite3_errmsg(db)); | ||
int rc = sqlite3_prepare_v2(db, "SELECT COUNT(timestamp) FROM queries", -1, &stmt, NULL); | ||
if( rc ){ | ||
logg("number_of_queries_in_DB() - SQL error prepare (%i): %s", rc, sqlite3_errmsg(db)); | ||
dbclose(); | ||
return -1; | ||
} | ||
|
||
rc = sqlite3_step(stmt); | ||
if( rc != SQLITE_ROW ){ | ||
logg("number_of_queries_in_DB() - SQL error step (%i): %s", rc, sqlite3_errmsg(db)); | ||
dbclose(); | ||
return -1; | ||
} | ||
|
||
int result = sqlite3_column_int(stmt, 0); | ||
|
||
sqlite3_finalize(stmt); | ||
|
||
|
@@ -238,20 +245,37 @@ void save_to_DB(void) | |
// Open database | ||
if(!dbopen()) | ||
{ | ||
logg("Failed to open DB in save_to_DB()"); | ||
logg("save_to_DB() - failed to open DB"); | ||
return; | ||
} | ||
|
||
int lasttimestamp = db_get_FTL_property(DB_LASTTIMESTAMP); | ||
if(lasttimestamp < 0) | ||
{ | ||
logg("save_to_DB() - error in trying to get last time stamp from database"); | ||
return; | ||
} | ||
int newlasttimestamp = lasttimestamp; | ||
|
||
unsigned int saved = 0, saved_error = 0; | ||
long int i; | ||
sqlite3_stmt* stmt; | ||
|
||
bool ret = dbquery("BEGIN TRANSACTION"); | ||
if(!ret){ dbclose(); return; } | ||
sqlite3_prepare_v2(db, "INSERT INTO queries VALUES (NULL,?,?,?,?,?,?)", -1, &stmt, NULL); | ||
if(!ret) | ||
{ | ||
logg("save_to_DB() - unable to begin transaction (%i): %s", ret, sqlite3_errmsg(db)); | ||
dbclose(); | ||
return; | ||
} | ||
|
||
int rc = sqlite3_prepare_v2(db, "INSERT INTO queries VALUES (NULL,?,?,?,?,?,?)", -1, &stmt, NULL); | ||
if( rc ) | ||
{ | ||
logg("save_to_DB() - error in preparing SQL statement (%i): %s", ret, sqlite3_errmsg(db)); | ||
dbclose(); | ||
return; | ||
} | ||
|
||
for(i = lastdbindex; i < counters.queries; i++) | ||
{ | ||
|
@@ -298,12 +322,12 @@ void save_to_DB(void) | |
} | ||
|
||
// Step and check if successful | ||
int rc = sqlite3_step(stmt); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
rc = sqlite3_step(stmt); | ||
sqlite3_clear_bindings(stmt); | ||
sqlite3_reset(stmt); | ||
|
||
if( rc != SQLITE_DONE ){ | ||
logg("save_to_DB() - SQL error: %s", sqlite3_errmsg(db)); | ||
logg("save_to_DB() - SQL error (%i): %s", rc, sqlite3_errmsg(db)); | ||
saved_error++; | ||
continue; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already called before in line 68. No need to do it again.