Move git root from Client/ to src/ to track all source code: - Client: Game client source (moved to Client/Client/) - Server: Game server source - GameTools: Development tools - CryptoSource: Encryption utilities - database: Database scripts - Script: Game scripts - rylCoder_16.02.2008_src: Legacy coder tools - GMFont, Game: Additional resources 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
221 lines
3.9 KiB
C++
221 lines
3.9 KiB
C++
#include "stdafx.h"
|
|
#include "SQLite.h"
|
|
|
|
#include <cstdlib>
|
|
#include <sqlite-library/sqlite.h>
|
|
#include <Log/ServerLog.h>
|
|
|
|
#define SAFE_FREE_SQLITE_ERRMSG(p) if(p) { sqlite_freemem(p); (p) = 0; }
|
|
|
|
CSQLite::CSQLite()
|
|
: m_sqlDB(0), m_szErrorMsg(0), m_bLogError(true)
|
|
{
|
|
|
|
}
|
|
|
|
CSQLite::~CSQLite()
|
|
{
|
|
SAFE_FREE_SQLITE_ERRMSG(m_szErrorMsg);
|
|
Close();
|
|
}
|
|
|
|
|
|
bool CSQLite::Open(const char* szDBFileName)
|
|
{
|
|
char* szErrMsg = 0;
|
|
m_sqlDB = sqlite_open(szDBFileName, 0, &szErrMsg);
|
|
|
|
if(0 == m_sqlDB)
|
|
{
|
|
if(m_bLogError)
|
|
{
|
|
ERRLOG2(g_Log, "Can't open database : %s(%d)",
|
|
szDBFileName, GetLastError());
|
|
}
|
|
|
|
SAFE_FREE_SQLITE_ERRMSG(m_szErrorMsg);
|
|
m_szErrorMsg = szErrMsg;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void CSQLite::Close()
|
|
{
|
|
if(0 != m_sqlDB)
|
|
{
|
|
sqlite_close(m_sqlDB);
|
|
}
|
|
}
|
|
|
|
int CSQLite::GetLastInsertRowID()
|
|
{
|
|
return sqlite_last_insert_rowid(m_sqlDB);
|
|
}
|
|
|
|
|
|
bool CSQLite::ExecuteQuery(const char* szQuery, QueryCallBack fnCallBack, void* callBackArg)
|
|
{
|
|
char* szErrorMsg = 0;
|
|
int nResult = sqlite_exec(m_sqlDB, szQuery, fnCallBack, callBackArg, &szErrorMsg);
|
|
if(SQLITE_OK != nResult)
|
|
{
|
|
if(m_bLogError)
|
|
{
|
|
ERRLOG2(g_Log, "Execute Query failed : %s(Query:%s)",
|
|
szErrorMsg, szQuery);
|
|
}
|
|
|
|
SAFE_FREE_SQLITE_ERRMSG(m_szErrorMsg);
|
|
m_szErrorMsg = szErrorMsg;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
CSQLite::Dataset::Dataset(CSQLite& sqlite, const char* szQuery)
|
|
: m_SQLite(sqlite),
|
|
m_sqlite_vm(0),
|
|
m_szQuery(szQuery),
|
|
m_szErrorMsg(0),
|
|
m_nExecuteResult(SQLITE_ROW)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
CSQLite::Dataset::~Dataset()
|
|
{
|
|
Free();
|
|
SAFE_FREE_SQLITE_ERRMSG(m_szErrorMsg);
|
|
}
|
|
|
|
|
|
bool CSQLite::Dataset::Compile()
|
|
{
|
|
Free();
|
|
|
|
const char* szNextQuery = 0;
|
|
char* szErrorMsg = 0;
|
|
|
|
if('\0' == *m_szQuery)
|
|
{
|
|
// 쿼리가 끝까지 완료되었으면 빠져나감.
|
|
return false;
|
|
}
|
|
|
|
int nResult = sqlite_compile(m_SQLite.GetHandle(), m_szQuery,
|
|
&szNextQuery, &m_sqlite_vm, &szErrorMsg);
|
|
|
|
if(SQLITE_OK != nResult)
|
|
{
|
|
if(m_SQLite.IsErrorLogging())
|
|
{
|
|
ERRLOG2(g_Log, "SQLite error : compile query error - %s(%d)",
|
|
szErrorMsg, GetLastError());
|
|
}
|
|
|
|
SAFE_FREE_SQLITE_ERRMSG(m_szErrorMsg);
|
|
m_szErrorMsg = szErrorMsg;
|
|
return false;
|
|
}
|
|
|
|
m_szQuery = szNextQuery;
|
|
return true;
|
|
}
|
|
|
|
bool CSQLite::Dataset::Execute(int* nColNum_Out,
|
|
const char *** pazValue,
|
|
const char *** pazColName)
|
|
{
|
|
m_nExecuteResult = sqlite_step(m_sqlite_vm, nColNum_Out, pazValue, pazColName);
|
|
|
|
if(SQLITE_ROW == m_nExecuteResult ||
|
|
SQLITE_DONE == m_nExecuteResult)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
if(m_SQLite.IsErrorLogging())
|
|
{
|
|
ERRLOG1(g_Log, "SQLite error : Dataset - execute error :%s",
|
|
sqlite_error_string(m_nExecuteResult));
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
bool CSQLite::Dataset::IsExcuteDone()
|
|
{
|
|
return (SQLITE_ROW != m_nExecuteResult);
|
|
}
|
|
|
|
|
|
void CSQLite::Dataset::Free()
|
|
{
|
|
if(0 != m_sqlite_vm)
|
|
{
|
|
char* szErrorMsg = 0;
|
|
|
|
if(SQLITE_OK != sqlite_finalize(m_sqlite_vm, &szErrorMsg))
|
|
{
|
|
if(m_SQLite.IsErrorLogging())
|
|
{
|
|
ERRLOG2(g_Log, "SQLite error : finalize error - %s(%d)",
|
|
szErrorMsg, GetLastError());
|
|
}
|
|
|
|
SAFE_FREE_SQLITE_ERRMSG(m_szErrorMsg);
|
|
m_szErrorMsg = szErrorMsg;
|
|
}
|
|
|
|
m_sqlite_vm = 0;
|
|
}
|
|
}
|
|
|
|
|
|
CSQLite::Tableset::Tableset(CSQLite& sqlite, const char* szQuery)
|
|
: m_SQLite(sqlite), m_szQuery(szQuery), m_nRow(0), m_nCol(0),
|
|
m_szResult(0), m_szErrorMsg(0)
|
|
{
|
|
|
|
|
|
}
|
|
|
|
CSQLite::Tableset::~Tableset()
|
|
{
|
|
if(0 != m_szResult)
|
|
{
|
|
sqlite_free_table(m_szResult);
|
|
m_szResult = 0;
|
|
}
|
|
}
|
|
|
|
bool CSQLite::Tableset::Execute()
|
|
{
|
|
char* szErrorMsg = 0;
|
|
|
|
if(SQLITE_OK != sqlite_get_table(m_SQLite.GetHandle(), m_szQuery,
|
|
&m_szResult, &m_nRow, &m_nCol, &szErrorMsg))
|
|
{
|
|
if(m_SQLite.IsErrorLogging())
|
|
{
|
|
ERRLOG2(g_Log, "SQLite error : get_table error - %s(%d)",
|
|
szErrorMsg, GetLastError());
|
|
}
|
|
|
|
SAFE_FREE_SQLITE_ERRMSG(m_szErrorMsg);
|
|
m_szErrorMsg = szErrorMsg;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|