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