1 #include "../../include/db/sqlite3db/sqlite3statement.h"
\r
10 std::map<sqlite3_stmt *, long> Statement::m_statementcount;
\r
12 Statement::Statement()
\r
16 m_resultcolumncount=0;
\r
17 m_rowreturned=false;
\r
20 Statement::Statement(sqlite3_stmt *statement)
\r
22 m_statement=statement;
\r
23 m_parametercount=sqlite3_bind_parameter_count(m_statement);
\r
24 m_resultcolumncount=sqlite3_column_count(m_statement);
\r
25 m_rowreturned=false;
\r
29 m_statementcount[m_statement]++;
\r
33 Statement::Statement(Statement &rhs)
\r
37 m_resultcolumncount=0;
\r
38 m_rowreturned=false;
\r
42 Statement::~Statement()
\r
47 std::vector<char *>::iterator i;
\r
48 for(i=textptrs.begin(); i!=textptrs.end(); i++)
\r
57 const bool Statement::Bind(const int column)
\r
59 if(Valid() && column>=0 && column<m_parametercount)
\r
61 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
62 if(sqlite3_bind_null(m_statement,column+1)==SQLITE_OK)
\r
77 const bool Statement::Bind(const int column, const int value)
\r
79 if(Valid() && column>=0 && column<m_parametercount)
\r
81 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
82 if(sqlite3_bind_int(m_statement,column+1,value)==SQLITE_OK)
\r
97 const bool Statement::Bind(const int column, const double value)
\r
99 if(Valid() && column>=0 && column<m_parametercount)
\r
101 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
102 if(sqlite3_bind_double(m_statement,column+1,value)==SQLITE_OK)
\r
117 const bool Statement::Bind(const int column, const std::string &value)
\r
119 if(Valid() && column>=0 && column<m_parametercount)
\r
121 char *text=new char[value.size()+1];
\r
122 strncpy(text,value.c_str(),value.size());
\r
123 text[value.size()]=NULL;
\r
124 textptrs.push_back(text);
\r
125 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
126 if(sqlite3_bind_text(m_statement,column+1,text,value.size(),NULL)==SQLITE_OK)
\r
141 const bool Statement::Bind(const int column, const void *data, const int length)
\r
143 if(Valid() && column>=0 && column<m_parametercount)
\r
145 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
146 if(sqlite3_bind_blob(m_statement,column+1,data,length,NULL)==SQLITE_OK)
\r
161 void Statement::Finalize()
\r
165 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
166 m_statementcount[m_statement]--;
\r
167 if(m_statementcount[m_statement]<=0)
\r
169 m_statementcount.erase(m_statement);
\r
170 sqlite3_finalize(m_statement);
\r
176 Statement &Statement::operator=(const Statement &rhs)
\r
182 m_statement=rhs.m_statement;
\r
183 m_parametercount=rhs.m_parametercount;
\r
184 m_resultcolumncount=rhs.m_resultcolumncount;
\r
185 m_rowreturned=rhs.m_rowreturned;
\r
189 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
190 m_statementcount[m_statement]++;
\r
196 const bool Statement::Reset()
\r
200 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
201 if(sqlite3_reset(m_statement)==SQLITE_OK)
\r
216 const bool Statement::ResultBlob(const int column, void *data, int &length)
\r
218 if(Valid() && column>=0 && column<m_resultcolumncount)
\r
220 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
221 data=(void *)sqlite3_column_blob(m_statement,column);
\r
222 length=sqlite3_column_bytes(m_statement,column);
\r
231 const bool Statement::ResultDouble(const int column, double &result)
\r
233 if(Valid() && column>=0 && column<m_resultcolumncount)
\r
235 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
236 result=sqlite3_column_double(m_statement,column);
\r
245 const bool Statement::ResultInt(const int column, int &result)
\r
247 if(Valid() && column>=0 && column<m_resultcolumncount)
\r
249 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
250 result=sqlite3_column_int(m_statement,column);
\r
259 const bool Statement::ResultNull(const int column)
\r
261 if(Valid() && column>=0 && column<m_resultcolumncount)
\r
263 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
264 if(sqlite3_column_type(m_statement,column)==SQLITE_NULL)
\r
279 const bool Statement::ResultText(const int column, std::string &result)
\r
281 if(Valid() && column>=0 && column<m_resultcolumncount)
\r
283 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
284 const unsigned char *cresult=sqlite3_column_text(m_statement,column);
\r
287 result=(char *)cresult;
\r
301 const bool Statement::Step(const bool saveinsertrowid)
\r
303 m_rowreturned=false;
\r
306 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
307 int result=sqlite3_step(m_statement);
\r
308 if(result==SQLITE_OK || result==SQLITE_ROW || result==SQLITE_DONE)
\r
310 if(result==SQLITE_ROW)
\r
312 m_rowreturned=true;
\r
314 if(saveinsertrowid)
\r
316 m_lastinsertrowid=sqlite3_last_insert_rowid(DB::instance()->GetDB());
\r
331 const bool Statement::Valid()
\r
333 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);
\r
334 return m_statement ? true : false ;
\r