Merge "Update deprecated libprivilege-control API functions." into tizen
[platform/framework/native/appfw.git] / src / io / FIo_DbContextImpl.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_DbContextImpl.cpp
19  * @brief       This is the implementation file for _DbContextImpl class.
20  */
21
22 #include <sqlite3.h>
23
24 #include <FBaseResult.h>
25 #include <FBaseSysLog.h>
26
27 #include <FBase_StringConverter.h>
28 #include <FBase_NativeError.h>
29 #include <FIo_DbContextImpl.h>
30
31 using namespace Tizen::Base;
32
33 namespace Tizen { namespace Io
34 {
35
36 // Mapping table for DB error to SQL error codes
37 static int dbErrorCodeToSqlErrorTable[] =
38 {
39         SQLITE_OK,          // DB_OK
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
66         SQLITE_ROW,         // DB_ROW
67         SQLITE_DONE         // DB_DONE
68 };
69
70 int
71 _DbContextImpl::ConvertDbErrorToNativeSqliteError(int errorCode)
72 {
73         int nativeSqlErrCode = -1;
74
75         if ((errorCode >= 0) && (errorCode < (int) (sizeof(dbErrorCodeToSqlErrorTable) / sizeof(int))))
76         {
77                 nativeSqlErrCode = dbErrorCodeToSqlErrorTable[errorCode];
78         }
79
80         return nativeSqlErrCode;
81 }
82
83 _DbContextImpl::_DbContextImpl(void)
84         : __pContext(null)
85         , __pArgList(null)
86         , __pUserData(null)
87         , __pUserFunctionListener(null)
88         , __pUserAggregateListener(null)
89         , __pUserCollationListener(null)
90         , __argumentCount(0)
91 {
92 }
93
94 _DbContextImpl::~_DbContextImpl(void)
95 {
96 }
97
98 result
99 _DbContextImpl::GetStringAt(int argumentIndex, String& value)
100 {
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.");
105
106         value = String((const char*) sqlite3_value_text((sqlite3_value*) __pArgList[argumentIndex]));
107
108         return E_SUCCESS;
109 }
110
111 result
112 _DbContextImpl::SetResultString(const String& value)
113 {
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.");
118
119         char* pValue = _StringConverter::CopyToCharArrayN(value);  //pValue will be released by _DbContextImpl::DestroyListener
120         if (pValue == null)
121         {
122                 return GetLastResult();
123         }
124         sqlite3_result_text((sqlite3_context*) __pContext, pValue, -1, _DbContextImpl::DestroyListener);
125
126         return E_SUCCESS;
127 }
128
129 result
130 _DbContextImpl::SetResultInt(int value)
131 {
132         SysTryReturnResult(NID_IO, __pContext != null, E_INVALID_STATE,
133                                 "The Object is not constructed or database context has already been closed.");
134
135         //set the integer value
136         sqlite3_result_int((sqlite3_context*) __pContext, value);
137
138         return E_SUCCESS;
139 }
140
141 result
142 _DbContextImpl::SetResultDouble(double value)
143 {
144         SysTryReturnResult(NID_IO, __pContext != null, E_INVALID_STATE,
145                                 "The Object is not constructed or database context has already been closed.");
146
147         //set the double value
148         sqlite3_result_double((sqlite3_context*) __pContext, value);
149
150         return E_SUCCESS;
151 }
152
153 result
154 _DbContextImpl::SetResultErrorCode(_DbUserListenerErrorCode errorCode)
155 {
156         int sqlErrcode = -1;
157
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.");
162
163         sqlErrcode = _DbContextImpl::ConvertDbErrorToNativeSqliteError((int) errorCode);
164         //set the error code.
165         sqlite3_result_error_code((sqlite3_context*) __pContext, sqlErrcode);
166
167         return E_SUCCESS;
168 }
169
170 void*
171 _DbContextImpl::GetUserData(void)
172 {
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.");
175
176         ClearLastResult();
177
178         return __pUserData;
179 }
180
181 void
182 _DbContextImpl::DestroyListener(void* pData)
183 {
184         if (pData != null)
185         {
186                 delete[] static_cast< char* >(pData);
187                 pData = null;
188         }
189 }
190
191 }} // Tizen::Io
192