Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / io / FIo_DbStatementImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FIo_DbStatementImpl.cpp
19  * @brief       This is the implementation file for _DbStatementImpl class.
20  */
21
22 #include <unique_ptr.h>
23 #include <sqlite3.h>
24
25 #include <FBaseString.h>
26 #include <FBaseByteBuffer.h>
27 #include <FBaseDateTime.h>
28 #include <FBaseSysLog.h>
29
30 #include <FBase_StringConverter.h>
31 #include <FBase_NativeError.h>
32 #include <FIo_DbStatementImpl.h>
33 #include <FApp_AppInfo.h>
34
35 using namespace Tizen::Base;
36 using namespace Tizen::App;
37
38 namespace Tizen { namespace Io
39 {
40
41 _DbStatementImpl::_DbStatementImpl(void)
42         : __shouldReleaseResource(true)
43         , __pStmt(null)
44         , __pDatabase(null)
45 {
46         __stmtType = DB_STATEMENT_TYPE_OTHER;
47 }
48
49 _DbStatementImpl::~_DbStatementImpl(void)
50 {
51         if (__shouldReleaseResource && __pStmt)
52         {
53                 sqlite3_finalize((sqlite3_stmt*) __pStmt);
54         }
55 }
56
57 result
58 _DbStatementImpl::BindInt(int columnIndex, int value)
59 {
60         SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
61                         "The Object is not constructed or the database is already been closed.");
62
63         int ret = 0;
64         result r = E_SUCCESS;
65
66         ret = sqlite3_bind_int((sqlite3_stmt*) __pStmt, columnIndex + 1, value);
67         if (ret != SQLITE_OK)
68         {
69                 if (!_AppInfo::IsOspCompat())
70                 {
71                         r = __ConvertNativeSqliteErrorToDetailResult(ret);
72                         switch (r)
73                         {
74                         case E_ILLEGAL_ACCESS:
75                         case E_OBJECT_LOCKED:
76                         case E_IO:
77                         case E_INVALID_FORMAT:
78                         case E_STORAGE_FULL:
79                                 r = E_SYSTEM;
80                                 break;
81                         }
82                 }
83                 else
84                 {
85                         r = __ConvertNativeSqliteErrorToResult(ret);
86                 }
87
88                 SysLog(NID_IO, "[%s] Failed to bind int value. (%d, %s).", GetErrorMessage(r),
89                                 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
90         }
91
92         return r;
93 }
94
95 result
96 _DbStatementImpl::BindInt64(int columnIndex, long long value)
97 {
98         SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
99                         "The Object is not constructed or the database is already been closed.");
100
101         int ret = 0;
102         result r = E_SUCCESS;
103
104         ret = sqlite3_bind_int64((sqlite3_stmt*) __pStmt, columnIndex + 1, value);
105         if (ret != SQLITE_OK)
106         {
107                 if (!_AppInfo::IsOspCompat())
108                 {
109                         r = __ConvertNativeSqliteErrorToDetailResult(ret);
110                         switch (r)
111                         {
112                         case E_ILLEGAL_ACCESS:
113                         case E_OBJECT_LOCKED:
114                         case E_IO:
115                         case E_INVALID_FORMAT:
116                         case E_STORAGE_FULL:
117                                 r = E_SYSTEM;
118                                 break;
119                         }
120                 }
121                 else
122                 {
123                         r = __ConvertNativeSqliteErrorToResult(ret);
124                 }
125
126                 SysLog(NID_IO, "[%s] Failed to bind int64 value. (%d, %s).", GetErrorMessage(r),
127                                 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
128         }
129
130         return r;
131 }
132
133 result
134 _DbStatementImpl::BindDouble(int columnIndex, double value)
135 {
136         SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
137                         "The Object is not constructed or the database is already been closed.");
138
139         int ret = 0;
140         result r = E_SUCCESS;
141
142         ret = sqlite3_bind_double((sqlite3_stmt*) __pStmt, columnIndex + 1, value);
143         if (ret != SQLITE_OK)
144         {
145                 if (!_AppInfo::IsOspCompat())
146                 {
147                         r = __ConvertNativeSqliteErrorToDetailResult(ret);
148                         switch (r)
149                         {
150                         case E_ILLEGAL_ACCESS:
151                         case E_OBJECT_LOCKED:
152                         case E_IO:
153                         case E_INVALID_FORMAT:
154                         case E_STORAGE_FULL:
155                                 r = E_SYSTEM;
156                                 break;
157                         }
158                 }
159                 else
160                 {
161                         r = __ConvertNativeSqliteErrorToResult(ret);
162                 }
163
164                 SysLog(NID_IO, "[%s] Failed to bind double value. (%d, %s).", GetErrorMessage(r),
165                                 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
166         }
167
168         return r;
169 }
170
171 result
172 _DbStatementImpl::BindString(int columnIndex, const String& value)
173 {
174         SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
175                         "The Object is not constructed or the database is already been closed.");
176
177         int ret = 0;
178         char* pStr = null;
179         result r = E_SUCCESS;
180
181         pStr = _StringConverter::CopyToCharArrayN(value);
182         if (pStr == null)
183         {
184                 return GetLastResult();
185         }
186
187         ret = sqlite3_bind_text((sqlite3_stmt*) __pStmt, columnIndex + 1, pStr, strlen(pStr), SQLITE_TRANSIENT);
188         if (ret != SQLITE_OK)
189         {
190                 if (!_AppInfo::IsOspCompat())
191                 {
192                         r = __ConvertNativeSqliteErrorToDetailResult(ret);
193                         switch (r)
194                         {
195                         case E_ILLEGAL_ACCESS:
196                         case E_OBJECT_LOCKED:
197                         case E_IO:
198                         case E_INVALID_FORMAT:
199                         case E_STORAGE_FULL:
200                                 r = E_SYSTEM;
201                                 break;
202                         }
203                 }
204                 else
205                 {
206                         r = __ConvertNativeSqliteErrorToResult(ret);
207                 }
208
209                 SysLog(NID_IO, "[%s] Failed to bind text value. (%d, %s).", GetErrorMessage(r),
210                                 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
211         }
212
213         delete[] pStr;
214
215         return r;
216 }
217
218 result
219 _DbStatementImpl::BindBlob(int columnIndex, const ByteBuffer& value)
220 {
221         SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
222                         "The Object is not constructed or the database is already been closed.");
223
224         int ret = 0;
225         int size = 0;
226         result r = E_SUCCESS;
227
228         size = value.GetLimit();
229         if (size <= 0)
230         {
231                 SysLog(NID_IO,
232                                         "[E_INVALID_ARG] ERROR Message: Wrong bind parameter. (Size of ByteBuffer should be greater than zero)");
233                 return E_INVALID_ARG;
234         }
235
236         ret = sqlite3_bind_blob((sqlite3_stmt*) __pStmt, columnIndex + 1, value.GetPointer(), size, SQLITE_TRANSIENT);
237         if (ret != SQLITE_OK)
238         {
239                 if (!_AppInfo::IsOspCompat())
240                 {
241                         r = __ConvertNativeSqliteErrorToDetailResult(ret);
242                         switch (r)
243                         {
244                         case E_ILLEGAL_ACCESS:
245                         case E_OBJECT_LOCKED:
246                         case E_IO:
247                         case E_INVALID_FORMAT:
248                         case E_STORAGE_FULL:
249                                 r = E_SYSTEM;
250                                 break;
251                         }
252                 }
253                 else
254                 {
255                         r = __ConvertNativeSqliteErrorToResult(ret);
256                 }
257
258                 SysLog(NID_IO, "[%s] Failed to bind blob data. (%d, %s).", GetErrorMessage(r),
259                                 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
260         }
261
262         return r;
263 }
264
265 result
266 _DbStatementImpl::BindBlob(int columnIndex, const void* buffer, int size)
267 {
268         SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
269                         "The Object is not constructed or the database is already been closed.");
270
271         int ret = 0;
272         result r = E_SUCCESS;
273
274         ret = sqlite3_bind_blob((sqlite3_stmt*) __pStmt, columnIndex + 1, buffer, size, SQLITE_TRANSIENT);
275         if (ret != SQLITE_OK)
276         {
277                 if (!_AppInfo::IsOspCompat())
278                 {
279                         r = __ConvertNativeSqliteErrorToDetailResult(ret);
280                         switch (r)
281                         {
282                         case E_ILLEGAL_ACCESS:
283                         case E_OBJECT_LOCKED:
284                         case E_IO:
285                         case E_INVALID_FORMAT:
286                         case E_STORAGE_FULL:
287                                 r = E_SYSTEM;
288                                 break;
289                         }
290                 }
291                 else
292                 {
293                         r = __ConvertNativeSqliteErrorToResult(ret);
294                 }
295
296                 SysLog(NID_IO, "[%s] Failed to bind blob data. (%d, %s).", GetErrorMessage(r),
297                                 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
298         }
299
300         return r;
301 }
302
303 result
304 _DbStatementImpl::BindDateTime(int columnIndex, const DateTime& value)
305 {
306         SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
307                         "The Object is not constructed or the database is already been closed.");
308
309         int ret = 0;
310         char* pStr = null;
311         result r = E_SUCCESS;
312
313         pStr = _StringConverter::CopyToCharArrayN(value.ToString());
314         if (pStr == null)
315         {
316                 return GetLastResult();
317         }
318
319         ret = sqlite3_bind_text((sqlite3_stmt*) __pStmt, columnIndex + 1, pStr, strlen(pStr), SQLITE_TRANSIENT);
320         if (ret != SQLITE_OK)
321         {
322                 if (!_AppInfo::IsOspCompat())
323                 {
324                         r = __ConvertNativeSqliteErrorToDetailResult(ret);
325                         switch (r)
326                         {
327                         case E_ILLEGAL_ACCESS:
328                         case E_OBJECT_LOCKED:
329                         case E_IO:
330                         case E_INVALID_FORMAT:
331                         case E_STORAGE_FULL:
332                                 r = E_SYSTEM;
333                                 break;
334                         }
335                 }
336                 else
337                 {
338                         r = __ConvertNativeSqliteErrorToResult(ret);
339                 }
340
341                 SysLog(NID_IO, "[%s] Failed to bind text value. (%d, %s).", GetErrorMessage(r),
342                                 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
343         }
344
345         delete[] pStr;
346
347         return r;
348 }
349
350 result
351 _DbStatementImpl::BindNull(int columnIndex)
352 {
353         SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
354                         "The Object is not constructed or the database is already been closed.");
355
356         int ret = 0;
357         result r = E_SUCCESS;
358
359         ret = sqlite3_bind_null((sqlite3_stmt*) __pStmt, columnIndex + 1);
360         if (ret != SQLITE_OK)
361         {
362                 if (!_AppInfo::IsOspCompat())
363                 {
364                         r = __ConvertNativeSqliteErrorToDetailResult(ret);
365                         switch (r)
366                         {
367                         case E_ILLEGAL_ACCESS:
368                         case E_OBJECT_LOCKED:
369                         case E_IO:
370                         case E_INVALID_FORMAT:
371                         case E_STORAGE_FULL:
372                                 r = E_SYSTEM;
373                                 break;
374                         }
375                 }
376                 else
377                 {
378                         r = __ConvertNativeSqliteErrorToResult(ret);
379                 }
380
381                 SysLog(NID_IO, "[%s] Failed to bind null. (%d, %s).", GetErrorMessage(r),
382                                 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
383         }
384
385         return r;
386 }
387
388 _DbStatementImpl*
389 _DbStatementImpl::GetInstance(DbStatement& dbStatement)
390 {
391         return dbStatement.__pDbStatementImpl;
392 }
393
394 const _DbStatementImpl*
395 _DbStatementImpl::GetInstance(const DbStatement& dbStatement)
396 {
397         return dbStatement.__pDbStatementImpl;
398 }
399
400 DbStatement*
401 _DbStatementImpl::CreateDbStatementInstanceN(void)
402 {
403         std::unique_ptr<DbStatement> pDbStatement(new (std::nothrow) DbStatement());
404         SysTryReturn(NID_IO, pDbStatement != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
405         return pDbStatement.release();
406 }
407
408 }} // Tizen::Io
409