Files
envi-code/SourceCode/Code2026/sqliteserver/TSQLite.cpp
T
gjm 164968b62e chore
把非utf8-bom编码的cpp/h文件改为 utf8 bom 编码, msvc识别utf8编码时,如果不是bom格式的,会使用当前cp_oem来解码.
2026-10-04 00:04:20 +08:00

232 lines
4.5 KiB
C++

#include "StdAfx.h"
#include "TSQLite.h"
#include "sqlite3.h"
#include "windows.h"
namespace
{
std::string unicode2ANSI(const wchar_t *psz, const DWORD &codePage)
{
if (!psz) return std::string("");
char *pBuffer;
int nLength;
int nLen;
nLen = (int)wcslen(psz);
nLength = ::WideCharToMultiByte(codePage, 0, psz, nLen, NULL, 0, 0, 0);
int rc = ::GetLastError();
pBuffer = new char[nLength + 1];
memset(pBuffer, 0, (nLength + 1)*sizeof(char));
::WideCharToMultiByte(codePage, 0, psz, nLen, pBuffer, nLength, 0, 0);
std::string str(pBuffer);
delete[] pBuffer;
return str;
}
std::wstring ANSI2unicode(const char *psz, const DWORD &codePage)
{
if (!psz) return std::wstring(L"");
int nLength, nLen;
wchar_t *pBuffer;
nLen = (int)strlen(psz);
nLength = ::MultiByteToWideChar(codePage, 0, psz, nLen, NULL, 0);
pBuffer = new wchar_t[nLength + 1];
memset(pBuffer, 0, (nLength + 1)*sizeof(wchar_t));
::MultiByteToWideChar(codePage, 0, psz, nLen, (LPWSTR)pBuffer, nLength);
std::wstring wstr(pBuffer);
delete[] pBuffer;
return wstr;
}
}
TSQLite::TSQLite() : m_pSQLite(NULL), m_pStmt(NULL) { }
TSQLite::~TSQLite()
{
if (m_pStmt) sqlite3_finalize(m_pStmt);
if (m_pSQLite) sqlite3_close(m_pSQLite);
}
std::string TSQLite::unicode2utf8(const wchar_t *psz) { return unicode2ANSI(psz, CP_UTF8); }
std::wstring TSQLite::utf82unicode(const char *psz) { return ANSI2unicode(psz, CP_UTF8); }
std::string TSQLite::utf82OEM(const char *psz)
{
std::wstring ws = utf82unicode(psz);
return unicode2ANSI(ws.c_str(), CP_OEMCP);
}
bool TSQLite::open(const TCHAR *filename)
{
if (!filename) return false;
std::string name = unicode2utf8(filename);
int rel = sqlite3_open(name.c_str(), &m_pSQLite);
m_sErr = sqlite3_errmsg(m_pSQLite);
if (rel == SQLITE_OK) return true;
return false;
}
bool TSQLite::close()
{
if (!m_pSQLite) return false;
int rel = sqlite3_close(m_pSQLite);
if (rel == SQLITE_OK) return true;
return false;
}
bool TSQLite::prepare(const TCHAR *pszSql)
{
if (!m_pSQLite) return false;
if (!pszSql) return false;
if (m_pStmt)
sqlite3_finalize(m_pStmt);
std::string sql = unicode2utf8(pszSql);
int rel = sqlite3_prepare_v2(m_pSQLite, sql.c_str(), (int)sql.size(), &m_pStmt, NULL);
m_sErr = sqlite3_errmsg(m_pSQLite);
if (rel == SQLITE_OK) return true;
return false;
}
bool TSQLite::exec(const TCHAR *SQL)
{
std::string sql = unicode2utf8(SQL);
int rel = sqlite3_exec(m_pSQLite, sql.c_str(), NULL, NULL, NULL);
m_sErr = sqlite3_errmsg(m_pSQLite);
if (rel == SQLITE_OK) return true;
return false;
}
TSQLite::TSQLITE TSQLite::oneStep()
{
if (!m_pSQLite) return eNONE;
int rel = sqlite3_step(m_pStmt);
m_sErr = sqlite3_errmsg(m_pSQLite);
if (rel == SQLITE_ROW) return eSQLITE_ROW;
return eNONE;
}
TSQLite::TValue TSQLite::query(int iCol)
{
sqlite3_value *pV = sqlite3_column_value(m_pStmt, iCol);
return TValue(pV);
}
int TSQLite::countOfCloumn()
{
return sqlite3_column_count(m_pStmt);
}
std::string TSQLite::getErrMsg()
{
return m_sErr;
}
int TSQLite::TValue::toInt()
{
if (m_pV) return sqlite3_value_int(m_pV);
return 0;
}
int TSQLite::TValue::toBlob(void *pBuffer, const int &len)
{
if (!m_pV) return -1;
// 得到的是 byte 个数,应该不用乘 sizeof(char) ,待测试
int memLen = sqlite3_value_bytes(m_pV);
if (!pBuffer) return memLen;
const void *p = sqlite3_value_blob(m_pV);
int cpyLen = (len > memLen) ? memLen : len;
::memcpy(pBuffer, p, cpyLen);
return cpyLen;
}
__int64 TSQLite::TValue::toInt64()
{
if (m_pV) return sqlite3_value_int64(m_pV);
return 0;
}
double TSQLite::TValue::toDouble()
{
if (m_pV) return sqlite3_value_double(m_pV);
return .0f;
}
std::string TSQLite::TValue::toTextA()
{
if (!m_pV) return "";
char *psz = (char*)sqlite3_value_text(m_pV);
if (!psz) return "";
std::string s = psz;
return utf82OEM(s.c_str());
}
std::wstring TSQLite::TValue::toTextW()
{
if (!m_pV) return L"";
char *psz = (char*)sqlite3_value_text(m_pV);
if (!psz) return L"";
std::string s = psz;
return utf82unicode(s.c_str());
}
TSQLite::TSQLITE TSQLite::TValue::getType()
{
if (!m_pV) return eNONE;
int type = sqlite3_value_type(m_pV);
switch (type)
{
case 1: return eINTEGER;
case 2: return eFLOAT;
case 3: return eTEXT;
case 4: return eBLOB;
case 5: return eNULL;
default: return eNONE;
}
return eNONE;
}
void TSQLite::beginTrans()
{
sqlite3_exec(m_pSQLite, "begin;", 0, 0, 0);
}
void TSQLite::commitTrans()
{
sqlite3_exec(m_pSQLite, "commit;", 0, 0, 0);
}
void TSQLite::rollbackTrans()
{
sqlite3_exec(m_pSQLite, "rollback;", 0, 0, 0);
}