2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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.
17 * @author Lukasz Marek (l.marek@samsung.com)
18 * @author Jihoon Chung (jihoon.chung@samsung.com)
19 * @author Janusz Majnert (j.majnert@samsung.com)
20 * @author Kamil Lysik (k.lysik@samsung.com)
25 #include <wrt-commons/widget-interface-dao/widget_interface_dao.h>
32 #include <dpl/assert.h>
33 #include <dpl/log/wrt_log.h>
34 #include <dpl/string.h>
35 #include <dpl/wrt-dao-ro/property_dao_read_only.h>
36 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
37 #include <dpl/wrt-dao-ro/WrtDatabase.h>
38 #include <dpl/wrt-dao-ro/widget_config.h>
41 const char* const DATABASE_NAME = ".widget_interface.db";
44 const char* const UPDATE_PROPERTIES = "update Properties set value=?, readonly=?, configxml=? where key=?";
45 const char* const INSERT_INTO_PROPERTIES = "insert into Properties (value, readonly, configxml, key) values (?, ?, ?, ?)";
46 const char* const DELETE_FROM_PROPERTIES_WHERE_KEY = "delete from Properties where key=?";
47 const char* const SELECT_VALUE = "select value from Properties where key=?";
48 const char* const DELETE_FROM_PROPERTIES = "delete from Properties";
49 const char* const DELETE_FROM_PROPERTIES_WHERE_READONLY = "delete from Properties where readonly=0";
50 const char* const SELECT_COUNT = "select count(*) from Properties";
51 const char* const SELECT_KEY = "select key from Properties order by key";
52 const char* const SELECT_READONLY = "select readonly from Properties where key=?";
54 } // anonymous namespace
56 #define CATCH_DATABASE_ERROR \
57 Catch(SQLiteDAOBase::Exception::DatabaseError) { \
58 ReThrow(WidgetInterfaceDAO::Exception::DatabaseError); \
61 namespace WidgetInterfaceDB {
63 WidgetInterfaceDAO::WidgetInterfaceDAO(WrtDB::DbWidgetHandle widgetHandle)
65 if (DB_UTIL_OK != db_util_open_with_options(
66 databaseFileName(widgetHandle).c_str(),
68 SQLITE_OPEN_READWRITE | SQLITE_OPEN_NOMUTEX,
70 WrtLogE("Cannot open database. %s", sqlite3_errmsg(m_databaseInterface));
71 ThrowMsg(WidgetInterfaceDAO::Exception::DatabaseError, "Cannot open database");
75 WidgetInterfaceDAO::~WidgetInterfaceDAO()
79 void WidgetInterfaceDAO::setItem(const std::string& key,
80 const std::string& value,
83 setItem(key, value, readOnly, false);
86 void WidgetInterfaceDAO::setItem(const std::string& key,
87 const std::string& value,
91 bool keyExists = true;
92 bool rowReadOnly = false;
94 rowReadOnly = isReadOnly(key);
95 } Catch(Exception::InvalidArgumentException){
99 ThrowMsg(Exception::LocalStorageValueNoModifableException, "setItem fail");
102 const char* const query = keyExists ? UPDATE_PROPERTIES : INSERT_INTO_PROPERTIES;
105 stmt = sqlPrepare(query);
107 sqlBind(stmt, 1, value.c_str());
108 sqlBind(stmt, 2, readOnly ? 1 : 0);
109 sqlBind(stmt, 3, fromConfigXml ? 1 : 0);
110 sqlBind(stmt, 4, key.c_str());
111 } CATCH_DATABASE_ERROR
113 int rv = sqlite3_step(stmt);
114 releaseStatement(stmt);
116 if (SQLITE_DONE != rv) {
117 WrtLogE("sqlite3_step error. %s", sqlite3_errmsg(m_databaseInterface));
118 ThrowMsg(WidgetInterfaceDAO::Exception::DatabaseError, "setItem fail");
122 void WidgetInterfaceDAO::removeItem(const std::string& key)
124 bool readOnly = false;
126 readOnly = isReadOnly(key);
127 } Catch(Exception::InvalidArgumentException){
133 ThrowMsg(Exception::LocalStorageValueNoModifableException, "removeItem fail");
138 stmt = sqlPrepare(DELETE_FROM_PROPERTIES_WHERE_KEY);
139 sqlBind(stmt, 1, key.c_str());
140 } CATCH_DATABASE_ERROR
142 int rv = sqlite3_step(stmt);
143 releaseStatement(stmt);
145 if (SQLITE_DONE != rv) {
146 WrtLogE("sqlite3_step error. %s", sqlite3_errmsg(m_databaseInterface));
147 ThrowMsg(WidgetInterfaceDAO::Exception::DatabaseError, "removeItem fail");
151 boost::optional<std::string> WidgetInterfaceDAO::getValue(
152 const std::string& key)
156 stmt = sqlPrepare(SELECT_VALUE);
157 sqlBind(stmt, 1, key.c_str());
158 } CATCH_DATABASE_ERROR
160 boost::optional<std::string> value;
161 if (SQLITE_ROW == sqlite3_step(stmt)) {
163 reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
168 releaseStatement(stmt);
173 void WidgetInterfaceDAO::clear(bool removeReadOnly)
175 sqlite3_stmt* stmt = NULL;
177 if (removeReadOnly) {
178 stmt = sqlPrepare(DELETE_FROM_PROPERTIES);
180 stmt = sqlPrepare(DELETE_FROM_PROPERTIES_WHERE_READONLY);
182 } CATCH_DATABASE_ERROR
184 int rv = sqlite3_step(stmt);
185 releaseStatement(stmt);
187 if (SQLITE_DONE != rv) {
188 WrtLogE("sqlite3_step error. %s", sqlite3_errmsg(m_databaseInterface));
189 ThrowMsg(WidgetInterfaceDAO::Exception::DatabaseError, "clear fail");
193 size_t WidgetInterfaceDAO::getStorageSize()
197 stmt = sqlPrepare(SELECT_COUNT);
198 } CATCH_DATABASE_ERROR
201 if (SQLITE_ROW == sqlite3_step(stmt)) {
202 size = sqlite3_column_int(stmt, 0);
205 releaseStatement(stmt);
210 std::string WidgetInterfaceDAO::getKeyByIndex(size_t index)
212 if (index >= getStorageSize()) {
213 Throw(Exception::InvalidArgumentException);
218 stmt = sqlPrepare(SELECT_KEY);
219 } CATCH_DATABASE_ERROR
220 for (unsigned int loop = 0; loop <= index; ++loop) {
221 if (sqlite3_step(stmt) != SQLITE_ROW) {
222 releaseStatement(stmt);
223 ThrowMsg(WidgetInterfaceDAO::Exception::DatabaseError,
224 "getKeyByIndex fail");
228 const char* key = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
231 releaseStatement(stmt);
232 ThrowMsg(WidgetInterfaceDAO::Exception::DatabaseError,
233 "Invalid key is returned");
235 std::string retval = key;
236 releaseStatement(stmt);
240 std::string WidgetInterfaceDAO::databaseFileName(WrtDB::DbWidgetHandle widgetHandle)
242 using namespace WrtDB::WidgetConfig;
244 WrtDB::WrtDatabase::attachToThreadRO();
245 std::string filename;
247 WrtDB::WidgetDAOReadOnly widgetDAO(widgetHandle);
248 WrtDB::TizenPkgId pkgid = widgetDAO.getTizenPkgId();
249 filename = GetWidgetPersistentStoragePath(pkgid) + "/" + DATABASE_NAME;
250 } Catch(WrtDB::WidgetDAOReadOnly::Exception::DatabaseError) {
251 ReThrowMsg(WidgetInterfaceDAO::Exception::DatabaseError,
254 WrtDB::WrtDatabase::detachFromThread();
258 bool WidgetInterfaceDAO::isReadOnly(const std::string& key)
262 stmt = sqlPrepare(SELECT_READONLY);
263 sqlBind(stmt, 1, key.c_str());
264 } CATCH_DATABASE_ERROR
267 if (SQLITE_ROW == sqlite3_step(stmt)) {
268 ret = sqlite3_column_int(stmt, 0) ? true : false;
270 // If key doesn't exist, throw
271 releaseStatement(stmt);
272 Throw(Exception::InvalidArgumentException);
274 releaseStatement(stmt);
278 } // namespace WidgetInterfaceDB