License change Apach to Flora
[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.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.tizenopensource.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                 return false;
95             }
96             break;
97         case 's':
98             stringValue = va_arg(ap, char*);
99             ret = sqlite3_bind_text(m_stmt, i + 1, stringValue, -1, NULL);
100             if (ret != SQLITE_OK) {
101                 return false;
102             }
103             break;
104         default:
105             break;
106         }
107     }
108     va_end(ap);
109
110     return true;
111 }
112
113 bool SqliteDB::executeCommand()
114 {
115     int ret =
116         sqlite3_step(m_stmt);
117     
118     if (ret != SQLITE_ROW) {
119         return false;
120     }
121
122     return true;
123 }
124
125 const char* SqliteDB::getText(int col)
126 {
127     const char* ret = 
128         reinterpret_cast<const char*>(sqlite3_column_text(m_stmt, col));
129
130     return ret; 
131 }
132
133 int SqliteDB::getInt(int col)
134 {
135     return sqlite3_column_int(m_stmt, col);
136 }