2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FIoDatabase.cpp
20 * @brief This is the implementation file for Database class.
25 #include <unique_ptr.h>
27 #include <FBaseByteBuffer.h>
28 #include <FBaseResult.h>
29 #include <FBaseSysLog.h>
33 #include <FSysSystemInfo.h>
35 #include <FIoDbTypes.h>
36 #include <FIoDatabase.h>
37 #include <FIoDbStatement.h>
38 #include <FIoDbEnumerator.h>
40 #include <FBase_NativeError.h>
41 #include <FIo_DatabaseImpl.h>
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Runtime;
46 using namespace Tizen::System;
48 namespace Tizen { namespace Io
51 Database::Database(void)
52 : __pDatabaseImpl(null)
56 Database::~Database(void)
58 delete __pDatabaseImpl;
62 Database::Construct(const String& dbPath, bool createIfNotExist)
64 SysAssertf(__pDatabaseImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
66 SysTryReturnResult(NID_IO, dbPath.GetLength() > 0 && dbPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
67 "Wrong path length.");
69 unique_ptr<_DatabaseImpl> pDatabaseImpl(new (std::nothrow) _DatabaseImpl);
70 SysTryReturnResult(NID_IO, pDatabaseImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
72 result r = pDatabaseImpl->Construct(dbPath, createIfNotExist);
73 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
75 __pDatabaseImpl = pDatabaseImpl.release();
81 Database::Construct(const String& dbPath, long openMode, long option)
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.");
88 SysAssertf(__pDatabaseImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
90 unique_ptr<_DatabaseImpl> pDatabaseImpl(new (std::nothrow) _DatabaseImpl);
91 SysTryReturnResult(NID_IO, pDatabaseImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
93 r = pDatabaseImpl->Construct(dbPath, openMode, null);
94 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
96 __pDatabaseImpl = pDatabaseImpl.release();
102 Database::Construct(const String& dbPath, const char* pOpenMode)
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.");
108 unique_ptr<_DatabaseImpl> pDatabaseImpl(new (std::nothrow) _DatabaseImpl);
109 SysTryReturnResult(NID_IO, pDatabaseImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
111 result r = pDatabaseImpl->Construct(dbPath, pOpenMode, null);
112 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
114 __pDatabaseImpl = pDatabaseImpl.release();
120 Database::Construct(const String& dbPath, const char* pOpenMode, const ByteBuffer& secureKey)
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.");
127 unique_ptr<_DatabaseImpl> pDatabaseImpl(new (std::nothrow) _DatabaseImpl);
128 SysTryReturnResult(NID_IO, pDatabaseImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
131 result r = pDatabaseImpl->Construct(dbPath, pOpenMode, &secureKey);
133 result r = pDatabaseImpl->Construct(dbPath, pOpenMode, null);
135 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
137 __pDatabaseImpl = pDatabaseImpl.release();
143 Database::CreateStatementN(const String& statement)
145 SysTryReturn(NID_IO, statement.GetLength() > 0, null, E_INVALID_ARG,
146 "[E_INVALID_ARG] The length of SQL statement is zero.");
148 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
150 return __pDatabaseImpl->CreateStatementN(statement);
154 Database::ExecuteStatementN(const DbStatement& dbStatement)
156 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
158 return __pDatabaseImpl->ExecuteStatementN(dbStatement);
162 Database::ExecuteSql(const String& statement, bool autoCommit)
164 SysTryReturnResult(NID_IO, statement.GetLength() > 0, E_INVALID_ARG,
165 "The length of SQL statement is zero.");
167 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
169 return __pDatabaseImpl->ExecuteSql(statement, autoCommit);
173 Database::QueryN(const String& statement)
175 SysTryReturn(NID_IO, statement.GetLength() > 0, null, E_INVALID_ARG,
176 "[E_INVALID_ARG] The length of SQL statement is zero.");
178 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
180 return __pDatabaseImpl->QueryN(statement);
184 Database::BeginTransaction(void)
186 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
188 return __pDatabaseImpl->BeginTransaction();
192 Database::CommitTransaction(void)
194 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
196 return __pDatabaseImpl->CommitTransaction();
200 Database::RollbackTransaction(void)
202 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
204 return __pDatabaseImpl->RollbackTransaction();
208 Database::GetName(void) const
210 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
212 return __pDatabaseImpl->GetName();
216 Database::Delete(const String& dbPath)
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.");
221 return _DatabaseImpl::Delete(dbPath);
225 Database::Exists(const String& dbPath)
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.");
230 return _DatabaseImpl::Exists(dbPath);
234 Database::ConvertToSecureDatabase(const String& normalDbPath, const String& secureDbPath, const ByteBuffer& secretKey)
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);
243 return File::Copy(normalDbPath, secureDbPath, true);
248 Database::GetLastInsertRowId(void) const
250 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
251 return __pDatabaseImpl->GetLastInsertRowId();