2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIo_DbStatementImpl.cpp
19 * @brief This is the implementation file for _DbStatementImpl class.
22 #include <unique_ptr.h>
25 #include <FBaseString.h>
26 #include <FBaseByteBuffer.h>
27 #include <FBaseDateTime.h>
28 #include <FBaseSysLog.h>
30 #include <FBase_StringConverter.h>
31 #include <FBase_NativeError.h>
32 #include <FIo_DbStatementImpl.h>
33 #include <FApp_AppInfo.h>
35 using namespace Tizen::Base;
36 using namespace Tizen::App;
38 namespace Tizen { namespace Io
41 _DbStatementImpl::_DbStatementImpl(void)
42 : __shouldReleaseResource(true)
46 __stmtType = DB_STATEMENT_TYPE_OTHER;
49 _DbStatementImpl::~_DbStatementImpl(void)
51 if (__shouldReleaseResource && __pStmt)
53 sqlite3_finalize((sqlite3_stmt*) __pStmt);
58 _DbStatementImpl::BindInt(int columnIndex, int value)
60 SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
61 "The Object is not constructed or the database is already been closed.");
66 ret = sqlite3_bind_int((sqlite3_stmt*) __pStmt, columnIndex + 1, value);
69 if (!_AppInfo::IsOspCompat())
71 r = __ConvertNativeSqliteErrorToDetailResult(ret);
74 case E_ILLEGAL_ACCESS:
77 case E_INVALID_FORMAT:
85 r = __ConvertNativeSqliteErrorToResult(ret);
88 SysLog(NID_IO, "[%s] Failed to bind int value. (%d, %s).", GetErrorMessage(r),
89 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
96 _DbStatementImpl::BindInt64(int columnIndex, long long value)
98 SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
99 "The Object is not constructed or the database is already been closed.");
102 result r = E_SUCCESS;
104 ret = sqlite3_bind_int64((sqlite3_stmt*) __pStmt, columnIndex + 1, value);
105 if (ret != SQLITE_OK)
107 if (!_AppInfo::IsOspCompat())
109 r = __ConvertNativeSqliteErrorToDetailResult(ret);
112 case E_ILLEGAL_ACCESS:
113 case E_OBJECT_LOCKED:
115 case E_INVALID_FORMAT:
123 r = __ConvertNativeSqliteErrorToResult(ret);
126 SysLog(NID_IO, "[%s] Failed to bind int64 value. (%d, %s).", GetErrorMessage(r),
127 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
134 _DbStatementImpl::BindDouble(int columnIndex, double value)
136 SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
137 "The Object is not constructed or the database is already been closed.");
140 result r = E_SUCCESS;
142 ret = sqlite3_bind_double((sqlite3_stmt*) __pStmt, columnIndex + 1, value);
143 if (ret != SQLITE_OK)
145 if (!_AppInfo::IsOspCompat())
147 r = __ConvertNativeSqliteErrorToDetailResult(ret);
150 case E_ILLEGAL_ACCESS:
151 case E_OBJECT_LOCKED:
153 case E_INVALID_FORMAT:
161 r = __ConvertNativeSqliteErrorToResult(ret);
164 SysLog(NID_IO, "[%s] Failed to bind double value. (%d, %s).", GetErrorMessage(r),
165 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
172 _DbStatementImpl::BindString(int columnIndex, const String& value)
174 SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
175 "The Object is not constructed or the database is already been closed.");
179 result r = E_SUCCESS;
181 pStr = _StringConverter::CopyToCharArrayN(value);
184 return GetLastResult();
187 ret = sqlite3_bind_text((sqlite3_stmt*) __pStmt, columnIndex + 1, pStr, strlen(pStr), SQLITE_TRANSIENT);
188 if (ret != SQLITE_OK)
190 if (!_AppInfo::IsOspCompat())
192 r = __ConvertNativeSqliteErrorToDetailResult(ret);
195 case E_ILLEGAL_ACCESS:
196 case E_OBJECT_LOCKED:
198 case E_INVALID_FORMAT:
206 r = __ConvertNativeSqliteErrorToResult(ret);
209 SysLog(NID_IO, "[%s] Failed to bind text value. (%d, %s).", GetErrorMessage(r),
210 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
219 _DbStatementImpl::BindBlob(int columnIndex, const ByteBuffer& value)
221 SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
222 "The Object is not constructed or the database is already been closed.");
226 result r = E_SUCCESS;
228 size = value.GetLimit();
232 "[E_INVALID_ARG] ERROR Message: Wrong bind parameter. (Size of ByteBuffer should be greater than zero)");
233 return E_INVALID_ARG;
236 ret = sqlite3_bind_blob((sqlite3_stmt*) __pStmt, columnIndex + 1, value.GetPointer(), size, SQLITE_TRANSIENT);
237 if (ret != SQLITE_OK)
239 if (!_AppInfo::IsOspCompat())
241 r = __ConvertNativeSqliteErrorToDetailResult(ret);
244 case E_ILLEGAL_ACCESS:
245 case E_OBJECT_LOCKED:
247 case E_INVALID_FORMAT:
255 r = __ConvertNativeSqliteErrorToResult(ret);
258 SysLog(NID_IO, "[%s] Failed to bind blob data. (%d, %s).", GetErrorMessage(r),
259 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
266 _DbStatementImpl::BindBlob(int columnIndex, const void* buffer, int size)
268 SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
269 "The Object is not constructed or the database is already been closed.");
272 result r = E_SUCCESS;
274 ret = sqlite3_bind_blob((sqlite3_stmt*) __pStmt, columnIndex + 1, buffer, size, SQLITE_TRANSIENT);
275 if (ret != SQLITE_OK)
277 if (!_AppInfo::IsOspCompat())
279 r = __ConvertNativeSqliteErrorToDetailResult(ret);
282 case E_ILLEGAL_ACCESS:
283 case E_OBJECT_LOCKED:
285 case E_INVALID_FORMAT:
293 r = __ConvertNativeSqliteErrorToResult(ret);
296 SysLog(NID_IO, "[%s] Failed to bind blob data. (%d, %s).", GetErrorMessage(r),
297 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
304 _DbStatementImpl::BindDateTime(int columnIndex, const DateTime& value)
306 SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
307 "The Object is not constructed or the database is already been closed.");
311 result r = E_SUCCESS;
313 pStr = _StringConverter::CopyToCharArrayN(value.ToString());
316 return GetLastResult();
319 ret = sqlite3_bind_text((sqlite3_stmt*) __pStmt, columnIndex + 1, pStr, strlen(pStr), SQLITE_TRANSIENT);
320 if (ret != SQLITE_OK)
322 if (!_AppInfo::IsOspCompat())
324 r = __ConvertNativeSqliteErrorToDetailResult(ret);
327 case E_ILLEGAL_ACCESS:
328 case E_OBJECT_LOCKED:
330 case E_INVALID_FORMAT:
338 r = __ConvertNativeSqliteErrorToResult(ret);
341 SysLog(NID_IO, "[%s] Failed to bind text value. (%d, %s).", GetErrorMessage(r),
342 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
351 _DbStatementImpl::BindNull(int columnIndex)
353 SysTryReturnResult(NID_IO, __pStmt != null, E_INVALID_STATE,
354 "The Object is not constructed or the database is already been closed.");
357 result r = E_SUCCESS;
359 ret = sqlite3_bind_null((sqlite3_stmt*) __pStmt, columnIndex + 1);
360 if (ret != SQLITE_OK)
362 if (!_AppInfo::IsOspCompat())
364 r = __ConvertNativeSqliteErrorToDetailResult(ret);
367 case E_ILLEGAL_ACCESS:
368 case E_OBJECT_LOCKED:
370 case E_INVALID_FORMAT:
378 r = __ConvertNativeSqliteErrorToResult(ret);
381 SysLog(NID_IO, "[%s] Failed to bind null. (%d, %s).", GetErrorMessage(r),
382 ret, sqlite3_errmsg(static_cast< sqlite3* >(__pDatabase)));
389 _DbStatementImpl::GetInstance(DbStatement& dbStatement)
391 return dbStatement.__pDbStatementImpl;
394 const _DbStatementImpl*
395 _DbStatementImpl::GetInstance(const DbStatement& dbStatement)
397 return dbStatement.__pDbStatementImpl;
401 _DbStatementImpl::CreateDbStatementInstanceN(void)
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();