tizen 2.4 release
[framework/web/wrt-commons.git] / modules / widget_dao / dao / common_dao_types.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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    common_dao_types.h
19  * @author  Michal Ciepielski (m.ciepielski@samsung.com)
20  * @version 1.0
21  * @brief   This file contains the implementation of common data types for wrtdb
22  */
23 #include <stddef.h>
24
25 #include <dpl/wrt-dao-ro/common_dao_types.h>
26 #include <db-util.h>
27 #include <dpl/log/wrt_log.h>
28 #include <dpl/assert.h>
29
30 namespace WrtDB {
31
32 SQLiteDAOBase::SQLiteDAOBase() : m_databaseInterface(NULL)
33 {
34
35 }
36
37 SQLiteDAOBase::~SQLiteDAOBase()
38 {
39 #if ENABLE(WIDGET_INTERFACE_DAO_PERFORMANCE)
40     clearStatementsMap();
41 #endif
42     if (NULL != m_databaseInterface) {
43         if (DB_UTIL_OK != db_util_close(m_databaseInterface)) {
44             WrtLogE("Fail to close database");
45         }
46     }
47 }
48
49 void SQLiteDAOBase::sqlBind(sqlite3_stmt* stmt, int index, const char* parameter) const
50 {
51     Assert(m_databaseInterface);
52     Assert(stmt);
53
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))
58     {
59         WrtLogE("error msg : %s", sqlite3_errmsg(m_databaseInterface));
60         ThrowMsg(SQLiteDAOBase::Exception::DatabaseError, "Fail to bind text");
61     }
62 }
63
64 void SQLiteDAOBase::sqlBind(sqlite3_stmt* stmt, int index, const std::string& value) const
65 {
66     sqlBind(stmt, index, value.c_str());
67 }
68
69 void SQLiteDAOBase::sqlBind(sqlite3_stmt *stmt, int index, const DPL::String &value) const
70 {
71     sqlBind(stmt, index, DPL::ToUTF8String(value).c_str());
72 }
73
74 void SQLiteDAOBase::sqlBind(sqlite3_stmt* stmt, int index, int parameter) const
75 {
76     Assert(m_databaseInterface);
77     Assert(stmt);
78
79     if (SQLITE_OK != sqlite3_bind_int(stmt, index, parameter))
80     {
81         WrtLogE("error msg : %s", sqlite3_errmsg(m_databaseInterface));
82         ThrowMsg(SQLiteDAOBase::Exception::DatabaseError, "Fail to bind int");
83     }
84 }
85
86 #if ENABLE(WIDGET_INTERFACE_DAO_PERFORMANCE)
87 sqlite3_stmt* SQLiteDAOBase::sqlPrepare(const char* const query)
88 {
89     Assert(query);
90     Assert(m_databaseInterface);
91
92     auto it = m_statements.find(query);
93     if (m_statements.end() != it)
94     {
95         // statement already precompiled, just needs resetting
96         auto stmt = it->second;
97         if (SQLITE_OK != sqlite3_reset(stmt))
98         {
99             WrtLogE("error msg : %s", sqlite3_errmsg(m_databaseInterface));
100             ThrowMsg(SQLiteDAOBase::Exception::DatabaseError, "Fail to reset query");
101         }
102         return stmt;
103     }
104     // statement not ready, needs precompiling
105     sqlite3_stmt *stmt = NULL;
106     if (SQLITE_OK != sqlite3_prepare_v2(m_databaseInterface, query, -1, &stmt, NULL))
107     {
108         WrtLogE("error msg : %s", sqlite3_errmsg(m_databaseInterface));
109         ThrowMsg(SQLiteDAOBase::Exception::DatabaseError, "Fail to prepare query");
110     }
111     // inserting precompiled statement into map
112     m_statements[query] = stmt;
113     return stmt;
114 }
115
116
117 void SQLiteDAOBase::clearStatementsMap()
118 {
119     for (auto it = m_statements.begin(); it != m_statements.end(); ++it)
120     {
121       sqlite3_finalize(it->second);
122     }
123     m_statements.clear();
124 }
125 #else
126 sqlite3_stmt* SQLiteDAOBase::sqlPrepare(const char* const query)
127 {
128     Assert(query);
129     Assert(m_databaseInterface);
130
131     //Compile statement
132
133     sqlite3_stmt* stmt = NULL;
134     if (SQLITE_OK != sqlite3_prepare_v2(m_databaseInterface, query, -1, &stmt, NULL))
135     {
136         WrtLogE("error msg : %s", sqlite3_errmsg(m_databaseInterface));
137         ThrowMsg(SQLiteDAOBase::Exception::DatabaseError, "Fail to prepare query");
138     }
139
140     return stmt;
141 }
142 #endif
143
144 } // namespace WrtDB