51 lines
1.4 KiB
C++
51 lines
1.4 KiB
C++
#include <string>
|
|
#ifndef _WIN32
|
|
#include "Sqlite_utils.h"
|
|
#include "bts_assert.h"
|
|
#include <cstring>
|
|
namespace bts {
|
|
namespace sqlite {
|
|
|
|
void exec_nofail(sqlite3 *db, const char *sql) {
|
|
char *error_message = nullptr;
|
|
int result = sqlite3_exec(db, sql, nullptr, nullptr, &error_message);
|
|
if (result != SQLITE_OK) {
|
|
std::string log_msg =
|
|
"error_message: " + std::string(error_message) +
|
|
" query: " + (sql != nullptr ? std::string(sql) : "NULL");
|
|
bts_assert_msg(false, log_msg);
|
|
}
|
|
}
|
|
|
|
void exec_nofail(sqlite3_ptr &db, const char *sql) {
|
|
return exec_nofail(db.get(), sql);
|
|
}
|
|
|
|
sqlite3_ptr open_db(const char *path) {
|
|
sqlite3 *temp;
|
|
if (sqlite3_open(path, &temp) == SQLITE_OK) {
|
|
return sqlite3_ptr(temp, sqlite3_close);
|
|
} else {
|
|
LOG(Gen, Error, "Failed to open database {}", std::string(path));
|
|
return null_db();
|
|
}
|
|
}
|
|
|
|
sqlite3_stmt_ptr prepare_query(sqlite3 *db, const char *query) {
|
|
sqlite3_stmt *pquery;
|
|
const char *tail = nullptr;
|
|
sqlite3_prepare_v2(db, query, -1, &pquery, &tail);
|
|
bts_assert(
|
|
tail == nullptr ||
|
|
strlen(tail) == 0); // A tail indicates multiple statements in prepared
|
|
// statement string
|
|
return sqlite3_stmt_ptr(pquery, sqlite3_finalize);
|
|
}
|
|
sqlite3_stmt_ptr prepare_query(sqlite3_ptr &db, const char *query) {
|
|
return prepare_query(db.get(), query);
|
|
}
|
|
|
|
} // namespace sqlite
|
|
} // namespace bts
|
|
#endif
|