2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIoDatabase.cpp
19 * @brief This is the implementation file for Database class.
24 #include <unique_ptr.h>
26 #include <FBaseByteBuffer.h>
27 #include <FBaseResult.h>
28 #include <FBaseSysLog.h>
32 #include <FSysSystemInfo.h>
34 #include <FIoDbTypes.h>
35 #include <FIoDatabase.h>
36 #include <FIoDbStatement.h>
37 #include <FIoDbEnumerator.h>
39 #include <FBase_NativeError.h>
40 #include <FIo_DatabaseImpl.h>
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Runtime;
45 using namespace Tizen::System;
47 namespace Tizen { namespace Io
50 Database::Database(void)
51 : __pDatabaseImpl(null)
55 Database::~Database(void)
57 delete __pDatabaseImpl;
61 Database::Construct(const String& dbPath, bool createIfNotExist)
63 SysAssertf(__pDatabaseImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
65 SysTryReturnResult(NID_IO, dbPath.GetLength() > 0 && dbPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
66 "Wrong path length.");
68 unique_ptr<_DatabaseImpl> pDatabaseImpl(new (std::nothrow) _DatabaseImpl);
69 SysTryReturnResult(NID_IO, pDatabaseImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
71 result r = pDatabaseImpl->Construct(dbPath, createIfNotExist);
72 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
74 __pDatabaseImpl = pDatabaseImpl.release();
80 Database::Construct(const String& dbPath, long openMode, long option)
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.");
87 SysAssertf(__pDatabaseImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
89 unique_ptr<_DatabaseImpl> pDatabaseImpl(new (std::nothrow) _DatabaseImpl);
90 SysTryReturnResult(NID_IO, pDatabaseImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
92 r = pDatabaseImpl->Construct(dbPath, openMode, null);
93 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
95 __pDatabaseImpl = pDatabaseImpl.release();
101 Database::Construct(const String& dbPath, const char* pOpenMode)
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.");
107 unique_ptr<_DatabaseImpl> pDatabaseImpl(new (std::nothrow) _DatabaseImpl);
108 SysTryReturnResult(NID_IO, pDatabaseImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
110 result r = pDatabaseImpl->Construct(dbPath, pOpenMode, null);
111 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
113 __pDatabaseImpl = pDatabaseImpl.release();
119 Database::Construct(const String& dbPath, const char* pOpenMode, const ByteBuffer& secureKey)
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.");
126 unique_ptr<_DatabaseImpl> pDatabaseImpl(new (std::nothrow) _DatabaseImpl);
127 SysTryReturnResult(NID_IO, pDatabaseImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
130 result r = pDatabaseImpl->Construct(dbPath, pOpenMode, &secureKey);
132 result r = pDatabaseImpl->Construct(dbPath, pOpenMode, null);
134 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
136 __pDatabaseImpl = pDatabaseImpl.release();
142 Database::CreateStatementN(const String& statement)
144 SysTryReturn(NID_IO, statement.GetLength() > 0, null, E_INVALID_ARG,
145 "[E_INVALID_ARG] The length of SQL statement is zero.");
147 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
149 return __pDatabaseImpl->CreateStatementN(statement);
153 Database::ExecuteStatementN(const DbStatement& dbStatement)
155 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
157 return __pDatabaseImpl->ExecuteStatementN(dbStatement);
161 Database::ExecuteSql(const String& statement, bool autoCommit)
163 SysTryReturnResult(NID_IO, statement.GetLength() > 0, E_INVALID_ARG,
164 "The length of SQL statement is zero.");
166 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
168 return __pDatabaseImpl->ExecuteSql(statement, autoCommit);
172 Database::QueryN(const String& statement)
174 SysTryReturn(NID_IO, statement.GetLength() > 0, null, E_INVALID_ARG,
175 "[E_INVALID_ARG] The length of SQL statement is zero.");
177 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
179 return __pDatabaseImpl->QueryN(statement);
183 Database::BeginTransaction(void)
185 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
187 return __pDatabaseImpl->BeginTransaction();
191 Database::CommitTransaction(void)
193 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
195 return __pDatabaseImpl->CommitTransaction();
199 Database::RollbackTransaction(void)
201 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
203 return __pDatabaseImpl->RollbackTransaction();
207 Database::GetName(void) const
209 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
211 return __pDatabaseImpl->GetName();
215 Database::Delete(const String& dbPath)
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.");
220 return _DatabaseImpl::Delete(dbPath);
224 Database::Exists(const String& dbPath)
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.");
229 return _DatabaseImpl::Exists(dbPath);
233 Database::ConvertToSecureDatabase(const String& normalDbPath, const String& secureDbPath, const ByteBuffer& secretKey)
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);
242 return File::Copy(normalDbPath, secureDbPath, true);
247 Database::GetLastInsertRowId(void) const
249 SysAssertf(__pDatabaseImpl != null, "Not yet constructed. Construct() should be called before use.\n");
250 return __pDatabaseImpl->GetLastInsertRowId();