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