The license change version 1.0 to version 1.1
[platform/framework/web/web-provider.git] / src / API / SqliteDB.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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  * @file    SqliteDB.cpp 
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  */
20 #include <string>
21 #include <cstring>
22 #include <cstdarg>
23 #include <sqlite3.h>
24 #include <db-util.h>
25
26 #include "SqliteDB.h"
27
28 SqliteDB::SqliteDB(std::string dbPath)
29     : m_path(dbPath)
30     , m_handle(NULL)
31     , m_stmt(NULL)
32 {
33 }
34
35 SqliteDB::~SqliteDB()
36 {
37 }
38
39 bool SqliteDB::openDB()
40 {
41     closeDB();
42     int ret;
43     ret = db_util_open(m_path.c_str(), &m_handle, DB_UTIL_REGISTER_HOOK_METHOD);
44     if (ret != SQLITE_OK) {
45         return false;
46     }
47
48     return true;
49 }
50
51 void SqliteDB::closeDB()
52 {
53     if (!m_handle) {
54         return;
55     }
56
57     if (!m_stmt) {
58         db_util_close(m_handle);
59         m_handle = NULL;
60         return;
61     }
62
63     sqlite3_finalize(m_stmt);
64     db_util_close(m_handle);
65     m_stmt = NULL;
66     m_handle = NULL;
67 }
68
69 bool SqliteDB::setCommand(std::string& query, const char* fmt, ...)
70 {
71     if (!m_handle || !fmt) {
72         return false;
73     }
74
75     int ret = 
76         sqlite3_prepare_v2(m_handle, query.c_str(), -1, &m_stmt, NULL);
77
78     if (ret != SQLITE_OK) {
79         return false;
80     }
81
82     // bind values to query
83     int intValue;
84     char* stringValue;
85         
86     va_list ap;
87     va_start(ap, fmt);
88     for (int i = 0; i < strlen(fmt); i++) {
89         switch (fmt[i]) {
90         case 'i':
91             intValue = va_arg(ap, int);
92             ret = sqlite3_bind_int(m_stmt, i + 1, intValue);
93             if (ret != SQLITE_OK) {
94                 va_end(ap);
95                 return false;
96             }
97             break;
98         case 's':
99             stringValue = va_arg(ap, char*);
100             ret = sqlite3_bind_text(m_stmt, i + 1, stringValue, -1, NULL);
101             if (ret != SQLITE_OK) {
102                 va_end(ap);
103                 return false;
104             }
105             break;
106         default:
107             break;
108         }
109     }
110     va_end(ap);
111
112     return true;
113 }
114
115 bool SqliteDB::executeCommand()
116 {
117     int ret =
118         sqlite3_step(m_stmt);
119     
120     if (ret != SQLITE_ROW) {
121         return false;
122     }
123
124     return true;
125 }
126
127 const char* SqliteDB::getText(int col)
128 {
129     const char* ret = 
130         reinterpret_cast<const char*>(sqlite3_column_text(m_stmt, col));
131
132     return ret; 
133 }
134
135 int SqliteDB::getInt(int col)
136 {
137     return sqlite3_column_int(m_stmt, col);
138 }