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.
18 * @file common_dao_types.h
19 * @author Michal Ciepielski (m.ciepielski@samsung.com)
21 * @brief This file contains the implementation of common data types for wrtdb
25 #include <dpl/wrt-dao-ro/common_dao_types.h>
27 #include <dpl/log/wrt_log.h>
28 #include <dpl/assert.h>
32 SQLiteDAOBase::SQLiteDAOBase() : m_databaseInterface(NULL)
37 SQLiteDAOBase::~SQLiteDAOBase()
39 #if ENABLE(WIDGET_INTERFACE_DAO_PERFORMANCE)
42 if (NULL != m_databaseInterface) {
43 if (DB_UTIL_OK != db_util_close(m_databaseInterface)) {
44 WrtLogE("Fail to close database");
49 void SQLiteDAOBase::sqlBind(sqlite3_stmt* stmt, int index, const char* parameter) const
51 Assert(m_databaseInterface);
54 // In case of parameter is NULL pointer,
55 // then the fourth parameter is ignored and the end result is the same as
56 // sqlite3_bind_null().
57 if (SQLITE_OK != sqlite3_bind_text(stmt, index, parameter, -1, SQLITE_TRANSIENT))
59 WrtLogE("error msg : %s", sqlite3_errmsg(m_databaseInterface));
60 ThrowMsg(SQLiteDAOBase::Exception::DatabaseError, "Fail to bind text");
64 void SQLiteDAOBase::sqlBind(sqlite3_stmt* stmt, int index, const std::string& value) const
66 sqlBind(stmt, index, value.c_str());
69 void SQLiteDAOBase::sqlBind(sqlite3_stmt *stmt, int index, const DPL::String &value) const
71 sqlBind(stmt, index, DPL::ToUTF8String(value).c_str());
74 void SQLiteDAOBase::sqlBind(sqlite3_stmt* stmt, int index, int parameter) const
76 Assert(m_databaseInterface);
79 if (SQLITE_OK != sqlite3_bind_int(stmt, index, parameter))
81 WrtLogE("error msg : %s", sqlite3_errmsg(m_databaseInterface));
82 ThrowMsg(SQLiteDAOBase::Exception::DatabaseError, "Fail to bind int");
86 #if ENABLE(WIDGET_INTERFACE_DAO_PERFORMANCE)
87 sqlite3_stmt* SQLiteDAOBase::sqlPrepare(const char* const query)
90 Assert(m_databaseInterface);
92 auto it = m_statements.find(query);
93 if (m_statements.end() != it)
95 // statement already precompiled, just needs resetting
96 auto stmt = it->second;
97 if (SQLITE_OK != sqlite3_reset(stmt))
99 WrtLogE("error msg : %s", sqlite3_errmsg(m_databaseInterface));
100 ThrowMsg(SQLiteDAOBase::Exception::DatabaseError, "Fail to reset query");
104 // statement not ready, needs precompiling
105 sqlite3_stmt *stmt = NULL;
106 if (SQLITE_OK != sqlite3_prepare_v2(m_databaseInterface, query, -1, &stmt, NULL))
108 WrtLogE("error msg : %s", sqlite3_errmsg(m_databaseInterface));
109 ThrowMsg(SQLiteDAOBase::Exception::DatabaseError, "Fail to prepare query");
111 // inserting precompiled statement into map
112 m_statements[query] = stmt;
117 void SQLiteDAOBase::clearStatementsMap()
119 for (auto it = m_statements.begin(); it != m_statements.end(); ++it)
121 sqlite3_finalize(it->second);
123 m_statements.clear();
126 sqlite3_stmt* SQLiteDAOBase::sqlPrepare(const char* const query)
129 Assert(m_databaseInterface);
133 sqlite3_stmt* stmt = NULL;
134 if (SQLITE_OK != sqlite3_prepare_v2(m_databaseInterface, query, -1, &stmt, NULL))
136 WrtLogE("error msg : %s", sqlite3_errmsg(m_databaseInterface));
137 ThrowMsg(SQLiteDAOBase::Exception::DatabaseError, "Fail to prepare query");