mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 01:58:19 +00:00
Add sqlite3 to the client
This commit is contained in:
parent
4301ffab58
commit
355c6fccd3
|
@ -1508,6 +1508,7 @@ set_src(ENGINE_INTERFACE GLOB src/engine
|
|||
server.h
|
||||
serverbrowser.h
|
||||
sound.h
|
||||
sqlite.h
|
||||
steam.h
|
||||
storage.h
|
||||
textrender.h
|
||||
|
@ -1646,6 +1647,7 @@ set(DEPS ${DEP_JSON} ${DEP_MD5} ${ZLIB_DEP})
|
|||
# Libraries
|
||||
set(LIBS
|
||||
${CRYPTO_LIBRARIES}
|
||||
${SQLite3_LIBRARIES}
|
||||
${WEBSOCKETS_LIBRARIES}
|
||||
${ZLIB_LIBRARIES}
|
||||
${PLATFORM_LIBS}
|
||||
|
@ -1734,6 +1736,7 @@ if(CLIENT)
|
|||
serverbrowser_http.h
|
||||
sound.cpp
|
||||
sound.h
|
||||
sqlite.cpp
|
||||
steam.cpp
|
||||
text.cpp
|
||||
updater.cpp
|
||||
|
@ -2080,7 +2083,6 @@ endif()
|
|||
set(LIBS_SERVER
|
||||
${LIBS}
|
||||
${MYSQL_LIBRARIES}
|
||||
${SQLite3_LIBRARIES}
|
||||
${TARGET_ANTIBOT}
|
||||
${MINIUPNPC_LIBRARIES}
|
||||
# Add pthreads (on non-Windows) at the end, so that other libraries can depend
|
||||
|
@ -2227,6 +2229,7 @@ if(GTEST_FOUND OR DOWNLOAD_GTEST)
|
|||
src/engine/client/serverbrowser.h
|
||||
src/engine/client/serverbrowser_http.cpp
|
||||
src/engine/client/serverbrowser_http.h
|
||||
src/engine/client/sqlite.cpp
|
||||
src/engine/server/name_ban.cpp
|
||||
src/engine/server/name_ban.h
|
||||
src/game/server/teehistorian.cpp
|
||||
|
|
60
src/engine/client/sqlite.cpp
Normal file
60
src/engine/client/sqlite.cpp
Normal file
|
@ -0,0 +1,60 @@
|
|||
#include <base/system.h>
|
||||
#include <engine/console.h>
|
||||
#include <engine/sqlite.h>
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
||||
void CSqliteDeleter::operator()(sqlite3 *pSqlite)
|
||||
{
|
||||
sqlite3_close(pSqlite);
|
||||
}
|
||||
|
||||
void CSqliteStmtDeleter::operator()(sqlite3_stmt *pStmt)
|
||||
{
|
||||
sqlite3_finalize(pStmt);
|
||||
}
|
||||
|
||||
int SqliteHandleError(IConsole *pConsole, int Error, sqlite3 *pSqlite, const char *pContext)
|
||||
{
|
||||
if(Error != SQLITE_OK && Error != SQLITE_DONE && Error != SQLITE_ROW)
|
||||
{
|
||||
char aBuf[512];
|
||||
str_format(aBuf, sizeof(aBuf), "%s at %s", sqlite3_errmsg(pSqlite), pContext);
|
||||
pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "sqlite3", aBuf);
|
||||
}
|
||||
return Error;
|
||||
}
|
||||
|
||||
CSqlite SqliteOpen(IConsole *pConsole, IStorage *pStorage, const char *pPath)
|
||||
{
|
||||
char aFullPath[MAX_PATH_LENGTH];
|
||||
pStorage->GetCompletePath(IStorage::TYPE_SAVE, pPath, aFullPath, sizeof(aFullPath));
|
||||
sqlite3 *pSqlite = nullptr;
|
||||
bool ErrorOpening = SQLITE_HANDLE_ERROR(sqlite3_open(aFullPath, &pSqlite)) != SQLITE_OK;
|
||||
// Even on error, the database is initialized and needs to be freed.
|
||||
// Except on allocation failure, but then it'll be nullptr which is
|
||||
// also fine.
|
||||
CSqlite pResult{pSqlite};
|
||||
if(ErrorOpening)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
bool Error = false;
|
||||
Error = Error || SQLITE_HANDLE_ERROR(sqlite3_exec(pSqlite, "PRAGMA journal_mode = WAL", nullptr, nullptr, nullptr));
|
||||
Error = Error || SQLITE_HANDLE_ERROR(sqlite3_exec(pSqlite, "PRAGMA synchronous = NORMAL", nullptr, nullptr, nullptr));
|
||||
if(Error)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return pResult;
|
||||
}
|
||||
|
||||
CSqliteStmt SqlitePrepare(IConsole *pConsole, sqlite3 *pSqlite, const char *pStatement)
|
||||
{
|
||||
sqlite3_stmt *pTemp;
|
||||
if(SQLITE_HANDLE_ERROR(sqlite3_prepare_v2(pSqlite, pStatement, -1, &pTemp, nullptr)))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return CSqliteStmt{pTemp};
|
||||
}
|
28
src/engine/sqlite.h
Normal file
28
src/engine/sqlite.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef ENGINE_SQLITE_H
|
||||
#define ENGINE_SQLITE_H
|
||||
#include <memory>
|
||||
|
||||
typedef struct sqlite3 sqlite3;
|
||||
typedef struct sqlite3_stmt sqlite3_stmt;
|
||||
class IConsole;
|
||||
class IStorage;
|
||||
|
||||
class CSqliteDeleter
|
||||
{
|
||||
public:
|
||||
void operator()(sqlite3 *pSqlite);
|
||||
};
|
||||
class CSqliteStmtDeleter
|
||||
{
|
||||
public:
|
||||
void operator()(sqlite3_stmt *pStmt);
|
||||
};
|
||||
typedef std::unique_ptr<sqlite3, CSqliteDeleter> CSqlite;
|
||||
typedef std::unique_ptr<sqlite3_stmt, CSqliteStmtDeleter> CSqliteStmt;
|
||||
|
||||
int SqliteHandleError(IConsole *pConsole, int Error, sqlite3 *pSqlite, const char *pContext);
|
||||
#define SQLITE_HANDLE_ERROR(x) SqliteHandleError(pConsole, x, &*pSqlite, #x)
|
||||
|
||||
CSqlite SqliteOpen(IConsole *pConsole, IStorage *pStorage, const char *pPath);
|
||||
CSqliteStmt SqlitePrepare(IConsole *pConsole, sqlite3 *pSqlite, const char *pStatement);
|
||||
#endif // ENGINE_SQLITE_H
|
Loading…
Reference in a new issue