Backout 128 limits in DataControl
[platform/framework/native/appfw.git] / src / io / FIoDatabase.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        FIoDatabase.cpp
20  * @brief       This is the implementation file for Database class.
21  */
22
23 #include <new>
24 #include <limits.h>
25 #include <unique_ptr.h>
26
27 #include <FBaseByteBuffer.h>
28 #include <FBaseResult.h>
29 #include <FBaseSysLog.h>
30 #ifdef _OSP_EMUL_
31 #include <FIoFile.h>
32 #else
33 #include <FSysSystemInfo.h>
34 #endif
35 #include <FIoDbTypes.h>
36 #include <FIoDatabase.h>
37 #include <FIoDbStatement.h>
38 #include <FIoDbEnumerator.h>
39
40 #include <FBase_NativeError.h>
41 #include <FIo_DatabaseImpl.h>
42
43 using namespace std;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Runtime;
46 using namespace Tizen::System;
47
48 namespace Tizen { namespace Io
49 {
50
51 Database::Database(void)
52         : __pDatabaseImpl(null)
53 {
54 }
55
56 Database::~Database(void)
57 {
58         delete __pDatabaseImpl;
59 }
60
61 result
62 Database::Construct(const String& dbPath, bool createIfNotExist)
63 {
64         SysAssertf(__pDatabaseImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
65
66         SysTryReturnResult(NID_IO, dbPath.GetLength() > 0 && dbPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
67                            "Wrong path length.");
68
69         unique_ptr<_DatabaseImpl> pDatabaseImpl(new (std::nothrow) _DatabaseImpl);
70         SysTryReturnResult(NID_IO, pDatabaseImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
71
72         result r = pDatabaseImpl->Construct(dbPath, createIfNotExist);
73         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
74
75         __pDatabaseImpl = pDatabaseImpl.release();
76
77         return E_SUCCESS;
78 }
79
80 result
81 Database::Construct(const String& dbPath, long openMode, long option)
82 {
83         result r = E_SUCCESS;
84
85         SysTryReturnResult(NID_IO, dbPath.GetLength() > 0 && dbPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
86                            "The length of the specified dbPath is zero or exceeds system limitations.");
87
88         SysAssertf(__pDatabaseImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
89
90         unique_ptr<_DatabaseImpl> pDatabaseImpl(new (std::nothrow) _DatabaseImpl);
91         SysTryReturnResult(NID_IO, pDatabaseImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
92
93         r = pDatabaseImpl->Construct(dbPath, openMode, null);
94         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
95
96         __pDatabaseImpl = pDatabaseImpl.release();
97
98         return E_SUCCESS;
99 }
100
101 result
102 Database::Construct(const String& dbPath, const char* pOpenMode)
103 {
104         SysAssertf(__pDatabaseImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
105         SysTryReturnResult(NID_IO, dbPath.GetLength() > 0 && dbPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
106                            "The length of the specified dbPath is zero or exceeds system limitations.");
107
108         unique_ptr<_DatabaseImpl> pDatabaseImpl(new (std::nothrow) _DatabaseImpl);
109         SysTryReturnResult(NID_IO, pDatabaseImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
110
111         result r = pDatabaseImpl->Construct(dbPath, pOpenMode, null);
112         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
113
114         __pDatabaseImpl = pDatabaseImpl.release();
115
116         return E_SUCCESS;
117 }
118
119 result
120 Database::Construct(const String& dbPath, const char* pOpenMode, const ByteBuffer& secureKey)
121 {
122         SysAssertf(__pDatabaseImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
123         SysTryReturnResult(NID_IO, dbPath.GetLength() > 0 && dbPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
124                            "The length of the specified dbPath is zero or exceeds system limitations.");
125         SysTryReturnResult(NID_IO, secureKey.GetRemaining() > 0, E_INVALID_ARG, "The length of the specified secureKey is zero.");
126
127         unique_ptr<_DatabaseImpl> pDatabaseImpl(new (std::nothrow) _DatabaseImpl);
128         SysTryReturnResult(NID_IO, pDatabaseImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
129
130 #ifndef _OSP_EMUL_
131         result r = pDatabaseImpl->Construct(dbPath, pOpenMode, &secureKey);
132 #else
133         result r = pDatabaseImpl->Construct(dbPath, pOpenMode, null);
134 #endif
135         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
136
137         __pDatabaseImpl = pDatabaseImpl.release();
138
139         return E_SUCCESS;
140 }
141
142 DbStatement*
143 Database::CreateStatementN(const String& statement)
144 {
145         SysTryReturn(NID_IO, statement.GetLength() > 0, null, E_INVALID_ARG,
146                         "[E_INVALID_ARG] The length of SQL statement is zero.");
147
148         SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
149
150         return __pDatabaseImpl->CreateStatementN(statement);
151 }
152
153 DbEnumerator*
154 Database::ExecuteStatementN(const DbStatement& dbStatement)
155 {
156         SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
157
158         return __pDatabaseImpl->ExecuteStatementN(dbStatement);
159 }
160
161 result
162 Database::ExecuteSql(const String& statement, bool autoCommit)
163 {
164         SysTryReturnResult(NID_IO, statement.GetLength() > 0, E_INVALID_ARG,
165                         "The length of SQL statement is zero.");
166
167         SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
168
169         return __pDatabaseImpl->ExecuteSql(statement, autoCommit);
170 }
171
172 DbEnumerator*
173 Database::QueryN(const String& statement)
174 {
175         SysTryReturn(NID_IO, statement.GetLength() > 0, null, E_INVALID_ARG,
176                         "[E_INVALID_ARG] The length of SQL statement is zero.");
177
178         SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
179
180         return __pDatabaseImpl->QueryN(statement);
181 }
182
183 result
184 Database::BeginTransaction(void)
185 {
186         SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
187
188         return __pDatabaseImpl->BeginTransaction();
189 }
190
191 result
192 Database::CommitTransaction(void)
193 {
194         SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
195
196         return __pDatabaseImpl->CommitTransaction();
197 }
198
199 result
200 Database::RollbackTransaction(void)
201 {
202         SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
203
204         return __pDatabaseImpl->RollbackTransaction();
205 }
206
207 String
208 Database::GetName(void) const
209 {
210         SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
211
212         return __pDatabaseImpl->GetName();
213 }
214
215 result
216 Database::Delete(const String& dbPath)
217 {
218         SysTryReturnResult(NID_IO, dbPath.GetLength() > 0 && dbPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
219                            "The length of the specified dbPath is zero or exceeds system limitations.");
220
221         return _DatabaseImpl::Delete(dbPath);
222 }
223
224 bool
225 Database::Exists(const String& dbPath)
226 {
227         SysTryReturn(NID_IO, dbPath.GetLength() > 0 && dbPath.GetLength() <= PATH_MAX, false, E_INVALID_ARG,
228                           "[E_INVALID_ARG] The length of the specified dbPath is zero or exceeds system limitations.");
229
230         return _DatabaseImpl::Exists(dbPath);
231 }
232
233 result
234 Database::ConvertToSecureDatabase(const String& normalDbPath, const String& secureDbPath, const ByteBuffer& secretKey)
235 {
236 #ifndef _OSP_EMUL_
237         bool secure = false;
238         result r = SystemInfo::GetValue(L"http://tizen.org/feature/database.encryption", secure);
239         SysTryReturnResult(NID_IO, !IsFailed(r), E_SYSTEM, "The method cannot proceed due to a severe system error.");
240         SysTryReturnResult(NID_IO, secure == true, E_UNSUPPORTED_OPERATION, "This operation is not supported.");
241         return _DatabaseImpl::ConvertToSecureDatabase(normalDbPath, secureDbPath, &secretKey);
242 #else
243         return File::Copy(normalDbPath, secureDbPath, true);
244 #endif
245 }
246
247 long long
248 Database::GetLastInsertRowId(void) const
249 {
250         SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
251         return __pDatabaseImpl->GetLastInsertRowId();
252 }
253
254 }} // Tizen::Io
255