2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FIo_DbContextImpl.cpp
20 * @brief This is the implementation file for _DbContextImpl class.
25 #include <FBaseResult.h>
26 #include <FBaseSysLog.h>
28 #include <FBase_StringConverter.h>
29 #include <FBase_NativeError.h>
30 #include <FIo_DbContextImpl.h>
32 using namespace Tizen::Base;
34 namespace Tizen { namespace Io
37 // Mapping table for DB error to SQL error codes
38 static int dbErrorCodeToSqlErrorTable[] =
41 SQLITE_ERROR, // DB_ERROR
42 SQLITE_INTERNAL, // DB_INTERNAL
43 SQLITE_PERM, // DB_PERM
44 SQLITE_ABORT, // DB_ABORT
45 SQLITE_BUSY, // DB_BUSY
46 SQLITE_LOCKED, // DB_LOCKED
47 SQLITE_NOMEM, // DB_NOMEM
48 SQLITE_READONLY, // DB_READONLY
49 SQLITE_INTERRUPT, // DB_INTERRUPT
50 SQLITE_IOERR, // DB_IOERR
51 SQLITE_CORRUPT, // DB_AORRUPT
52 SQLITE_NOTFOUND, // DB_NOTFOUND
53 SQLITE_FULL, // DB_FULL
54 SQLITE_CANTOPEN, // DB_CANTOPEN
55 SQLITE_PROTOCOL, // DB_PROTOCOL
56 SQLITE_EMPTY, // DB_EMPTY
57 SQLITE_SCHEMA, // DB_SCHEMA
58 SQLITE_TOOBIG, // DB_TOOBIG
59 SQLITE_CONSTRAINT, // DB_CONSTRAINT
60 SQLITE_MISMATCH, // DB_MISMATCH
61 SQLITE_MISUSE, // DB_MISUSE
62 SQLITE_NOLFS, // DB_NOLFS
63 SQLITE_AUTH, // DB_AUTH
64 SQLITE_FORMAT, // DB_FORMAT
65 SQLITE_RANGE, // DB_RANGE
66 SQLITE_NOTADB, // DB_NOTADB
68 SQLITE_DONE // DB_DONE
72 _DbContextImpl::ConvertDbErrorToNativeSqliteError(int errorCode)
74 int nativeSqlErrCode = -1;
76 if ((errorCode >= 0) && (errorCode < (int) (sizeof(dbErrorCodeToSqlErrorTable) / sizeof(int))))
78 nativeSqlErrCode = dbErrorCodeToSqlErrorTable[errorCode];
81 return nativeSqlErrCode;
84 _DbContextImpl::_DbContextImpl(void)
88 , __pUserFunctionListener(null)
89 , __pUserAggregateListener(null)
90 , __pUserCollationListener(null)
95 _DbContextImpl::~_DbContextImpl(void)
100 _DbContextImpl::GetStringAt(int argumentIndex, String& value)
102 SysTryReturnResult(NID_IO, __pContext != null, E_INVALID_STATE,
103 "The Object is not constructed or database context has already been closed.");
104 SysTryReturnResult(NID_IO, argumentIndex >= 0 && argumentIndex < __argumentCount, E_INVALID_ARG,
105 "Th argumentIndex parameter is wrong.");
107 value = String((const char*) sqlite3_value_text((sqlite3_value*) __pArgList[argumentIndex]));
113 _DbContextImpl::SetResultString(const String& value)
115 SysTryReturnResult(NID_IO, __pContext != null, E_INVALID_STATE,
116 "The Object is not constructed or database context has already been closed.");
117 SysTryReturnResult(NID_IO, value.GetLength() > 0, E_INVALID_ARG,
118 "value length is 0 or negative.");
120 char* pValue = _StringConverter::CopyToCharArrayN(value); //pValue will be released by _DbContextImpl::DestroyListener
123 return GetLastResult();
125 sqlite3_result_text((sqlite3_context*) __pContext, pValue, -1, _DbContextImpl::DestroyListener);
131 _DbContextImpl::SetResultInt(int value)
133 SysTryReturnResult(NID_IO, __pContext != null, E_INVALID_STATE,
134 "The Object is not constructed or database context has already been closed.");
136 //set the integer value
137 sqlite3_result_int((sqlite3_context*) __pContext, value);
143 _DbContextImpl::SetResultDouble(double value)
145 SysTryReturnResult(NID_IO, __pContext != null, E_INVALID_STATE,
146 "The Object is not constructed or database context has already been closed.");
148 //set the double value
149 sqlite3_result_double((sqlite3_context*) __pContext, value);
155 _DbContextImpl::SetResultErrorCode(_DbUserListenerErrorCode errorCode)
159 SysTryReturnResult(NID_IO, __pContext != null, E_INVALID_STATE,
160 "The Object is not constructed or database context has already been closed.");
161 SysTryReturnResult(NID_IO, errorCode >= DB_USER_LISTENER_OK && errorCode <= DB_USER_LISTENER_NOMEM, E_INVALID_ARG,
162 "errorCode not in the range.");
164 sqlErrcode = _DbContextImpl::ConvertDbErrorToNativeSqliteError((int) errorCode);
165 //set the error code.
166 sqlite3_result_error_code((sqlite3_context*) __pContext, sqlErrcode);
172 _DbContextImpl::GetUserData(void)
174 SysTryReturn(NID_IO, __pContext != null, null, E_INVALID_STATE,
175 "[E_INVALID_STATE] The Object is not constructed or database context has already been closed.");
183 _DbContextImpl::DestroyListener(void* pData)
187 delete[] static_cast< char* >(pData);