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_DbContextImpl.cpp
19 * @brief This is the implementation file for _DbContextImpl class.
24 #include <FBaseResult.h>
25 #include <FBaseSysLog.h>
27 #include <FBase_StringConverter.h>
28 #include <FBase_NativeError.h>
29 #include <FIo_DbContextImpl.h>
31 using namespace Tizen::Base;
33 namespace Tizen { namespace Io
36 // Mapping table for DB error to SQL error codes
37 static int dbErrorCodeToSqlErrorTable[] =
40 SQLITE_ERROR, // DB_ERROR
41 SQLITE_INTERNAL, // DB_INTERNAL
42 SQLITE_PERM, // DB_PERM
43 SQLITE_ABORT, // DB_ABORT
44 SQLITE_BUSY, // DB_BUSY
45 SQLITE_LOCKED, // DB_LOCKED
46 SQLITE_NOMEM, // DB_NOMEM
47 SQLITE_READONLY, // DB_READONLY
48 SQLITE_INTERRUPT, // DB_INTERRUPT
49 SQLITE_IOERR, // DB_IOERR
50 SQLITE_CORRUPT, // DB_AORRUPT
51 SQLITE_NOTFOUND, // DB_NOTFOUND
52 SQLITE_FULL, // DB_FULL
53 SQLITE_CANTOPEN, // DB_CANTOPEN
54 SQLITE_PROTOCOL, // DB_PROTOCOL
55 SQLITE_EMPTY, // DB_EMPTY
56 SQLITE_SCHEMA, // DB_SCHEMA
57 SQLITE_TOOBIG, // DB_TOOBIG
58 SQLITE_CONSTRAINT, // DB_CONSTRAINT
59 SQLITE_MISMATCH, // DB_MISMATCH
60 SQLITE_MISUSE, // DB_MISUSE
61 SQLITE_NOLFS, // DB_NOLFS
62 SQLITE_AUTH, // DB_AUTH
63 SQLITE_FORMAT, // DB_FORMAT
64 SQLITE_RANGE, // DB_RANGE
65 SQLITE_NOTADB, // DB_NOTADB
67 SQLITE_DONE // DB_DONE
71 _DbContextImpl::ConvertDbErrorToNativeSqliteError(int errorCode)
73 int nativeSqlErrCode = -1;
75 if ((errorCode >= 0) && (errorCode < (int) (sizeof(dbErrorCodeToSqlErrorTable) / sizeof(int))))
77 nativeSqlErrCode = dbErrorCodeToSqlErrorTable[errorCode];
80 return nativeSqlErrCode;
83 _DbContextImpl::_DbContextImpl(void)
87 , __pUserFunctionListener(null)
88 , __pUserAggregateListener(null)
89 , __pUserCollationListener(null)
94 _DbContextImpl::~_DbContextImpl(void)
99 _DbContextImpl::GetStringAt(int argumentIndex, String& value)
101 SysTryReturnResult(NID_IO, __pContext != null, E_INVALID_STATE,
102 "The Object is not constructed or database context has already been closed.");
103 SysTryReturnResult(NID_IO, argumentIndex >= 0 && argumentIndex < __argumentCount, E_INVALID_ARG,
104 "Th argumentIndex parameter is wrong.");
106 value = String((const char*) sqlite3_value_text((sqlite3_value*) __pArgList[argumentIndex]));
112 _DbContextImpl::SetResultString(const String& value)
114 SysTryReturnResult(NID_IO, __pContext != null, E_INVALID_STATE,
115 "The Object is not constructed or database context has already been closed.");
116 SysTryReturnResult(NID_IO, value.GetLength() > 0, E_INVALID_ARG,
117 "value length is 0 or negative.");
119 char* pValue = _StringConverter::CopyToCharArrayN(value); //pValue will be released by _DbContextImpl::DestroyListener
122 return GetLastResult();
124 sqlite3_result_text((sqlite3_context*) __pContext, pValue, -1, _DbContextImpl::DestroyListener);
130 _DbContextImpl::SetResultInt(int value)
132 SysTryReturnResult(NID_IO, __pContext != null, E_INVALID_STATE,
133 "The Object is not constructed or database context has already been closed.");
135 //set the integer value
136 sqlite3_result_int((sqlite3_context*) __pContext, value);
142 _DbContextImpl::SetResultDouble(double value)
144 SysTryReturnResult(NID_IO, __pContext != null, E_INVALID_STATE,
145 "The Object is not constructed or database context has already been closed.");
147 //set the double value
148 sqlite3_result_double((sqlite3_context*) __pContext, value);
154 _DbContextImpl::SetResultErrorCode(_DbUserListenerErrorCode errorCode)
158 SysTryReturnResult(NID_IO, __pContext != null, E_INVALID_STATE,
159 "The Object is not constructed or database context has already been closed.");
160 SysTryReturnResult(NID_IO, errorCode >= DB_USER_LISTENER_OK && errorCode <= DB_USER_LISTENER_NOMEM, E_INVALID_ARG,
161 "errorCode not in the range.");
163 sqlErrcode = _DbContextImpl::ConvertDbErrorToNativeSqliteError((int) errorCode);
164 //set the error code.
165 sqlite3_result_error_code((sqlite3_context*) __pContext, sqlErrcode);
171 _DbContextImpl::GetUserData(void)
173 SysTryReturn(NID_IO, __pContext != null, null, E_INVALID_STATE,
174 "[E_INVALID_STATE] The Object is not constructed or database context has already been closed.");
182 _DbContextImpl::DestroyListener(void* pData)
186 delete[] static_cast< char* >(pData);