version 0.0.2
[fms.git] / src / db / sqlite3statement.cpp
1 #include "../../include/db/sqlite3db/sqlite3statement.h"\r
2 \r
3 #ifdef XMEM\r
4         #include <xmem.h>\r
5 #endif\r
6 \r
7 namespace SQLite3DB\r
8 {\r
9 \r
10 std::map<sqlite3_stmt *, long> Statement::m_statementcount;\r
11 \r
12 Statement::Statement()\r
13 {\r
14         m_statement=NULL;\r
15         m_parametercount=0;\r
16         m_resultcolumncount=0;\r
17         m_rowreturned=false;\r
18 }\r
19 \r
20 Statement::Statement(sqlite3_stmt *statement)\r
21 {\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
26         \r
27         if(m_statement)\r
28         {\r
29                 m_statementcount[m_statement]++;\r
30         }\r
31 }\r
32 \r
33 Statement::Statement(Statement &rhs)\r
34 {\r
35         m_statement=NULL;\r
36         m_parametercount=0;\r
37         m_resultcolumncount=0;\r
38         m_rowreturned=false;\r
39         *this=rhs;\r
40 }\r
41 \r
42 Statement::~Statement()\r
43 {\r
44         \r
45         Finalize();\r
46         \r
47         std::vector<char *>::iterator i;\r
48         for(i=textptrs.begin(); i!=textptrs.end(); i++)\r
49         {\r
50                 if((*i))\r
51                 {\r
52                         delete [] (*i);\r
53                 }\r
54         }\r
55 }\r
56 \r
57 const bool Statement::Bind(const int column)\r
58 {\r
59         if(Valid() && column>=0 && column<m_parametercount)\r
60         {\r
61                 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);\r
62                 if(sqlite3_bind_null(m_statement,column+1)==SQLITE_OK)\r
63                 {\r
64                         return true;\r
65                 }\r
66                 else\r
67                 {\r
68                         return false;\r
69                 }\r
70         }\r
71         else\r
72         {\r
73                 return false;\r
74         }\r
75 }\r
76 \r
77 const bool Statement::Bind(const int column, const int value)\r
78 {\r
79         if(Valid() && column>=0 && column<m_parametercount)\r
80         {\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
83                 {\r
84                         return true;\r
85                 }\r
86                 else\r
87                 {\r
88                         return false;\r
89                 }\r
90         }\r
91         else\r
92         {\r
93                 return false;\r
94         }\r
95 }\r
96 \r
97 const bool Statement::Bind(const int column, const double value)\r
98 {\r
99         if(Valid() && column>=0 && column<m_parametercount)\r
100         {\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
103                 {\r
104                         return true;\r
105                 }\r
106                 else\r
107                 {\r
108                         return false;\r
109                 }\r
110         }\r
111         else\r
112         {\r
113                 return false;\r
114         }       \r
115 }\r
116 \r
117 const bool Statement::Bind(const int column, const std::string &value)\r
118 {\r
119         if(Valid() && column>=0 && column<m_parametercount)\r
120         {\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
127                 {\r
128                         return true;\r
129                 }\r
130                 else\r
131                 {\r
132                         return false;\r
133                 }\r
134         }\r
135         else\r
136         {\r
137                 return false;\r
138         }       \r
139 }\r
140 \r
141 const bool Statement::Bind(const int column, const void *data, const int length)\r
142 {\r
143         if(Valid() && column>=0 && column<m_parametercount)\r
144         {\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
147                 {\r
148                         return true;\r
149                 }\r
150                 else\r
151                 {\r
152                         return false;\r
153                 }\r
154         }\r
155         else\r
156         {\r
157                 return false;\r
158         }\r
159 }\r
160 \r
161 void Statement::Finalize()\r
162 {\r
163         if(m_statement)\r
164         {\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
168                 {\r
169                         m_statementcount.erase(m_statement);\r
170                         sqlite3_finalize(m_statement);\r
171                 }\r
172                 m_statement=NULL;\r
173         }\r
174 }\r
175 \r
176 Statement &Statement::operator=(const Statement &rhs)\r
177 {\r
178         if(&rhs!=this)\r
179         {\r
180                 Finalize();\r
181 \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
186 \r
187                 if(m_statement)\r
188                 {\r
189                         ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);\r
190                         m_statementcount[m_statement]++;\r
191                 }\r
192         }\r
193         return *this;\r
194 }\r
195 \r
196 const bool Statement::Reset()\r
197 {\r
198         if(Valid())\r
199         {\r
200                 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);\r
201                 if(sqlite3_reset(m_statement)==SQLITE_OK)\r
202                 {\r
203                         return true;\r
204                 }\r
205                 else\r
206                 {\r
207                         return false;\r
208                 }\r
209         }\r
210         else\r
211         {\r
212                 return false;\r
213         }\r
214 }\r
215 \r
216 const bool Statement::ResultBlob(const int column, void *data, int &length)\r
217 {\r
218         if(Valid() && column>=0 && column<m_resultcolumncount)\r
219         {\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
223                 return true;\r
224         }\r
225         else\r
226         {\r
227                 return false;\r
228         }\r
229 }\r
230 \r
231 const bool Statement::ResultDouble(const int column, double &result)\r
232 {\r
233         if(Valid() && column>=0 && column<m_resultcolumncount)\r
234         {\r
235                 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);\r
236                 result=sqlite3_column_double(m_statement,column);\r
237                 return true;\r
238         }\r
239         else\r
240         {\r
241                 return false;\r
242         }\r
243 }\r
244 \r
245 const bool Statement::ResultInt(const int column, int &result)\r
246 {\r
247         if(Valid() && column>=0 && column<m_resultcolumncount)\r
248         {\r
249                 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);\r
250                 result=sqlite3_column_int(m_statement,column);\r
251                 return true;\r
252         }\r
253         else\r
254         {\r
255                 return false;\r
256         }\r
257 }\r
258 \r
259 const bool Statement::ResultNull(const int column)\r
260 {\r
261         if(Valid() && column>=0 && column<m_resultcolumncount)\r
262         {\r
263                 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);\r
264                 if(sqlite3_column_type(m_statement,column)==SQLITE_NULL)\r
265                 {\r
266                         return true;\r
267                 }\r
268                 else\r
269                 {\r
270                         return false;\r
271                 }\r
272         }\r
273         else\r
274         {\r
275                 return false;\r
276         }\r
277 }\r
278 \r
279 const bool Statement::ResultText(const int column, std::string &result)\r
280 {\r
281         if(Valid() && column>=0 && column<m_resultcolumncount)\r
282         {\r
283                 ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);\r
284                 const unsigned char *cresult=sqlite3_column_text(m_statement,column);\r
285                 if(cresult)\r
286                 {\r
287                         result=(char *)cresult;\r
288                 }\r
289                 else\r
290                 {\r
291                         result="";\r
292                 }\r
293                 return true;\r
294         }\r
295         else\r
296         {\r
297                 return false;\r
298         }\r
299 }\r
300 \r
301 const bool Statement::Step(const bool saveinsertrowid)\r
302 {\r
303         m_rowreturned=false;\r
304         if(Valid())\r
305         {\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
309                 {\r
310                         if(result==SQLITE_ROW)\r
311                         {\r
312                                 m_rowreturned=true;\r
313                         }\r
314                         if(saveinsertrowid)\r
315                         {\r
316                                 m_lastinsertrowid=sqlite3_last_insert_rowid(DB::instance()->GetDB());\r
317                         }\r
318                         return true;\r
319                 }\r
320                 else\r
321                 {\r
322                         return false;\r
323                 }\r
324         }\r
325         else\r
326         {\r
327                 return false;\r
328         }\r
329 }\r
330 \r
331 const bool Statement::Valid()\r
332 {\r
333         ZThread::Guard<ZThread::Mutex> g(DB::instance()->m_mutex);\r
334         return m_statement ? true : false ;\r
335 }\r
336 \r
337 }       // namespace\r