Skip to content

Commit

Permalink
Improve SQLite error messages (#269)
Browse files Browse the repository at this point in the history
Signed-off-by: Zachary Michaels <zmichaels11@gmail.com>
  • Loading branch information
zmichaels11 committed Jan 31, 2020
1 parent 1c63b6e commit c349100
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <cstring>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
Expand All @@ -33,8 +34,11 @@ SqliteStatementWrapper::SqliteStatementWrapper(sqlite3 * database, const std::st
sqlite3_stmt * statement;
int return_code = sqlite3_prepare_v2(database, query.c_str(), -1, &statement, nullptr);
if (return_code != SQLITE_OK) {
throw SqliteException("SQL error when preparing statement '" + query + "' with return code: " +
std::to_string(return_code));
std::stringstream errmsg;
errmsg << "Error when preparing SQL statement '" << query << "'. SQLite error: (" <<
return_code << "): " << sqlite3_errstr(return_code);

throw SqliteException{errmsg.str()};
}

statement_ = statement;
Expand All @@ -52,8 +56,11 @@ std::shared_ptr<SqliteStatementWrapper> SqliteStatementWrapper::execute_and_rese
{
int return_code = sqlite3_step(statement_);
if (!is_query_ok(return_code)) {
throw SqliteException("Error processing SQLite statement. Return code: " +
std::to_string(return_code));
std::stringstream errmsg;
errmsg << "Error when processing SQL statement. SQLite error (" <<
return_code << "): " << sqlite3_errstr(return_code);

throw SqliteException{errmsg.str()};
}
return reset();
}
Expand Down Expand Up @@ -121,7 +128,11 @@ bool SqliteStatementWrapper::step()
} else if (return_code == SQLITE_DONE) {
return false;
} else {
throw SqliteException("Error reading query result: " + std::to_string(return_code));
std::stringstream errmsg;
errmsg << "Error reading SQL query. SQLite error (" <<
return_code << "): " << sqlite3_errstr(return_code);

throw SqliteException{errmsg.str()};
}
}

Expand Down Expand Up @@ -157,9 +168,12 @@ void SqliteStatementWrapper::obtain_column_value(
void SqliteStatementWrapper::check_and_report_bind_error(int return_code)
{
if (return_code != SQLITE_OK) {
throw SqliteException("SQLite error when binding parameter " +
std::to_string(last_bound_parameter_index_) + ". Return code: " +
std::to_string(return_code));
std::stringstream errmsg;
errmsg << "Error when binding SQL parameter " <<
last_bound_parameter_index_ << ". SQLite error (" <<
return_code << "): " << sqlite3_errstr(return_code);

throw SqliteException{errmsg.str()};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

Expand All @@ -37,20 +38,27 @@ SqliteWrapper::SqliteWrapper(
int rc = sqlite3_open_v2(uri.c_str(), &db_ptr,
SQLITE_OPEN_READONLY | SQLITE_OPEN_NOMUTEX, nullptr);
if (rc != SQLITE_OK) {
throw SqliteException("Could not read-only open database. Error code: " + std::to_string(rc));
std::stringstream errmsg;
errmsg << "Could not read-only open database. SQLite error (" <<
rc << "): " << sqlite3_errstr(rc);
throw SqliteException{errmsg.str()};
}
// throws an exception if the database is not valid.
prepare_statement("PRAGMA schema_version;")->execute_and_reset();
} else {
int rc = sqlite3_open_v2(uri.c_str(), &db_ptr,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX, nullptr);
if (rc != SQLITE_OK) {
throw SqliteException(
"Could not read-write open database. Error code: " + std::to_string(rc));
std::stringstream errmsg;
errmsg << "Could not read-write open database. SQLite error (" <<
rc << "): " << sqlite3_errstr(rc);
throw SqliteException{errmsg.str()};
}
prepare_statement("PRAGMA journal_mode = WAL;")->execute_and_reset();
prepare_statement("PRAGMA synchronous = NORMAL;")->execute_and_reset();
}

sqlite3_extended_result_codes(db_ptr, 1);
}

SqliteWrapper::SqliteWrapper()
Expand Down

0 comments on commit c349100

Please sign in to comment.