X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fdb%2Fsqlite3db.cpp;h=a828a4a265c2e5cd0f6e78259ff99a304b0bb27c;hb=59a5414ec47a2932a7802fcd1d98c4d80166564f;hp=1b63715094084439c8510a272434d99f4ceab7b9;hpb=c7fcb4c4bc5012a584add81a9509fc1f84c3c688;p=fms.git diff --git a/src/db/sqlite3db.cpp b/src/db/sqlite3db.cpp index 1b63715..a828a4a 100644 --- a/src/db/sqlite3db.cpp +++ b/src/db/sqlite3db.cpp @@ -1,5 +1,12 @@ #include "../../include/db/sqlite3db.h" +#ifdef QUERY_LOG +#include +#include +#include +#include "../../include/stringfunctions.h" +#endif + #ifdef XMEM #include #endif @@ -12,7 +19,7 @@ DB::DB() Initialize(); } -DB::DB(std::string filename) +DB::DB(const std::string &filename) { Initialize(); Open(filename); @@ -22,7 +29,7 @@ DB::~DB() { if(IsOpen()) { - Close(); + Close(); } } @@ -30,8 +37,12 @@ const bool DB::Close() { if(IsOpen()) { - //ZThread::Guard g(m_mutex); - PThread::Guard g(m_mutex); + sqlite3_stmt *st=0; + while((st=sqlite3_next_stmt(m_db,0))!=0) + { + sqlite3_finalize(st); + } + m_lastresult=sqlite3_close(m_db); if(m_lastresult==SQLITE_OK) { @@ -40,12 +51,12 @@ const bool DB::Close() } else { - return false; + return false; } } else { - return false; + return false; } } @@ -53,16 +64,17 @@ const bool DB::Execute(const std::string &sql) { if(IsOpen()) { - //ZThread::Guard g(m_mutex); - PThread::Guard g(m_mutex); m_lastresult=sqlite3_exec(m_db,sql.c_str(),NULL,NULL,NULL); +#ifdef QUERY_LOG + Poco::Logger::get("querylog").information("Execute : "+sql); +#endif if(m_lastresult==SQLITE_OK) { return true; } else { - return false; + return false; } } else @@ -75,9 +87,10 @@ const bool DB::ExecuteInsert(const std::string &sql, long &insertid) { if(IsOpen()) { - //ZThread::Guard g(m_mutex); - PThread::Guard g(m_mutex); m_lastresult=sqlite3_exec(m_db,sql.c_str(),NULL,NULL,NULL); +#ifdef QUERY_LOG + Poco::Logger::get("querylog").information("Execute Insert : "+sql); +#endif if(m_lastresult==SQLITE_OK) { insertid=sqlite3_last_insert_rowid(m_db); @@ -85,7 +98,7 @@ const bool DB::ExecuteInsert(const std::string &sql, long &insertid) } else { - return false; + return false; } } else @@ -98,8 +111,6 @@ const int DB::GetLastError(std::string &errormessage) { if(IsOpen()) { - //ZThread::Guard g(m_mutex); - PThread::Guard g(m_mutex); int errcode=sqlite3_errcode(m_db); const char *errmsg=sqlite3_errmsg(m_db); if(errmsg) @@ -110,7 +121,7 @@ const int DB::GetLastError(std::string &errormessage) } else { - return SQLITE_OK; + return SQLITE_OK; } } @@ -122,21 +133,17 @@ void DB::Initialize() const bool DB::IsOpen() { - //ZThread::Guard g(m_mutex); - PThread::Guard g(m_mutex); - return m_db ? true : false; + return m_db ? true : false; } const bool DB::Open(const std::string &filename) { - if(IsOpen()==false) + if(IsOpen()==true) { Close(); } if(IsOpen()==false) { - //ZThread::Guard g(m_mutex); - PThread::Guard g(m_mutex); m_lastresult=sqlite3_open(filename.c_str(),&m_db); if(m_lastresult==SQLITE_OK) { @@ -144,12 +151,12 @@ const bool DB::Open(const std::string &filename) } else { - return false; + return false; } } else { - return false; + return false; } } @@ -157,10 +164,14 @@ Statement DB::Prepare(const std::string &sql) { if(IsOpen()) { - //ZThread::Guard g(m_mutex); - PThread::Guard g(m_mutex); sqlite3_stmt *statement=NULL; m_lastresult=sqlite3_prepare_v2(m_db,sql.c_str(),sql.size(),&statement,NULL); +#ifdef QUERY_LOG + size_t temp=reinterpret_cast(statement); + std::string tempstr(""); + StringFunctions::Convert(temp,tempstr); + Poco::Logger::get("querylog").information("Prepare : "+sql+" "+tempstr); +#endif if(m_lastresult==SQLITE_OK) { return Statement(statement); @@ -180,11 +191,12 @@ Recordset DB::Query(const std::string &sql) { if(IsOpen()) { - //ZThread::Guard g(m_mutex); - PThread::Guard g(m_mutex); char **rs=NULL; int rows,cols; m_lastresult=sqlite3_get_table(m_db,sql.c_str(),&rs,&rows,&cols,NULL); +#ifdef QUERY_LOG + Poco::Logger::get("querylog").information("Query : "+sql); +#endif if(m_lastresult==SQLITE_OK) { return Recordset(rs,rows,cols); @@ -205,8 +217,6 @@ const int DB::SetBusyTimeout(const int ms) { if(IsOpen()) { - //ZThread::Guard g(m_mutex); - PThread::Guard g(m_mutex); m_lastresult=sqlite3_busy_timeout(m_db,ms); return m_lastresult; }