1#include <base/system.h>
2
3#include <engine/console.h>
4#include <engine/sqlite.h>
5
6#include <sqlite3.h>
7
8void CSqliteDeleter::operator()(sqlite3 *pSqlite)
9{
10 sqlite3_close(pSqlite);
11}
12
13void CSqliteStmtDeleter::operator()(sqlite3_stmt *pStmt)
14{
15 sqlite3_finalize(pStmt);
16}
17
18int SqliteHandleError(IConsole *pConsole, int Error, sqlite3 *pSqlite, const char *pContext)
19{
20 if(Error != SQLITE_OK && Error != SQLITE_DONE && Error != SQLITE_ROW)
21 {
22 char aBuf[512];
23 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s at %s", sqlite3_errmsg(pSqlite), pContext);
24 pConsole->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "sqlite3", pStr: aBuf);
25 }
26 return Error;
27}
28
29CSqlite SqliteOpen(IConsole *pConsole, IStorage *pStorage, const char *pPath)
30{
31 char aFullPath[IO_MAX_PATH_LENGTH];
32 pStorage->GetCompletePath(Type: IStorage::TYPE_SAVE, pDir: pPath, pBuffer: aFullPath, BufferSize: sizeof(aFullPath));
33 sqlite3 *pSqlite = nullptr;
34 const bool ErrorOpening = SQLITE_HANDLE_ERROR(sqlite3_open(aFullPath, &pSqlite)) != SQLITE_OK;
35 // Even on error, the database is initialized and needs to be freed.
36 // Except on allocation failure, but then it'll be nullptr which is
37 // also fine.
38 CSqlite pResult{pSqlite};
39 if(ErrorOpening)
40 {
41 return nullptr;
42 }
43 bool Error = false;
44 Error = Error || SQLITE_HANDLE_ERROR(sqlite3_exec(pSqlite, "PRAGMA journal_mode = WAL", nullptr, nullptr, nullptr));
45 Error = Error || SQLITE_HANDLE_ERROR(sqlite3_exec(pSqlite, "PRAGMA synchronous = NORMAL", nullptr, nullptr, nullptr));
46 if(Error)
47 {
48 return nullptr;
49 }
50 return pResult;
51}
52
53CSqliteStmt SqlitePrepare(IConsole *pConsole, sqlite3 *pSqlite, const char *pStatement)
54{
55 sqlite3_stmt *pTemp;
56 if(SQLITE_HANDLE_ERROR(sqlite3_prepare_v2(pSqlite, pStatement, -1, &pTemp, nullptr)))
57 {
58 return nullptr;
59 }
60 return CSqliteStmt{pTemp};
61}
62