60 lines
1.5 KiB
C++
60 lines
1.5 KiB
C++
#pragma once
|
|
|
|
// sqlite3头文件
|
|
#include "XNCore_global.h"
|
|
#include "XNString.h"
|
|
#include "XNType.h"
|
|
#include <sqlite3.h>
|
|
|
|
namespace XNSim {
|
|
using XN_DB = sqlite3;
|
|
using XN_DB_PTR = XN_DB *;
|
|
using XN_DB_STMT = sqlite3_stmt;
|
|
using XN_DB_STMT_PTR = XN_DB_STMT *;
|
|
|
|
FORCEINLINE XN_DB_PTR openDatabase(const XN_STRING &dbPath) {
|
|
sqlite3 *db;
|
|
if (sqlite3_open(dbPath.c_str(), &db) != SQLITE_OK) {
|
|
return nullptr;
|
|
}
|
|
return db;
|
|
}
|
|
|
|
FORCEINLINE XN_INT32 closeDatabase(XN_DB_PTR db) { return sqlite3_close(db); }
|
|
|
|
FORCEINLINE XN_DB_STMT_PTR prepareSql(XN_DB_PTR db, const XN_STRING &sql) {
|
|
sqlite3_stmt *stmt;
|
|
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
|
return nullptr;
|
|
}
|
|
return stmt;
|
|
}
|
|
|
|
FORCEINLINE void finalizeSql(XN_DB_STMT_PTR stmt) { sqlite3_finalize(stmt); }
|
|
|
|
FORCEINLINE XN_BOOL bindText(XN_DB_STMT_PTR stmt, XN_INT32 index,
|
|
const XN_STRING &text) {
|
|
if (sqlite3_bind_text(stmt, index, text.c_str(), text.length(), nullptr) !=
|
|
SQLITE_OK) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
FORCEINLINE XN_BOOL stepSql(XN_DB_STMT_PTR stmt) {
|
|
if (sqlite3_step(stmt) != SQLITE_ROW) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
FORCEINLINE XN_STRING getStringFromSqlite3(XN_DB_STMT_PTR stmt,
|
|
XN_INT32 column) {
|
|
const char *text =
|
|
reinterpret_cast<const char *>(sqlite3_column_text(stmt, column));
|
|
if (text == nullptr) {
|
|
return "";
|
|
}
|
|
return XN_STRING(text);
|
|
}
|
|
} // namespace XNSim
|