Tizen 2.1 base
[platform/core/appfw/ail.git] / src / ail_db.c
1 /*
2  * ail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <db-util.h>
28 #include <glib.h>
29 #include "ail_private.h"
30 #include "ail_db.h"
31
32 #define retv_with_dbmsg_if(expr, val) do { \
33         if (expr) { \
34                 _E("db_info.dbro: %s", sqlite3_errmsg(db_info.dbro)); \
35                 _E("db_info.dbrw: %s", sqlite3_errmsg(db_info.dbrw)); \
36                 return (val); \
37         } \
38 } while (0)
39
40
41 static __thread struct {
42         sqlite3         *dbro;
43         sqlite3         *dbrw;
44 } db_info = {
45         .dbro = NULL,
46         .dbrw = NULL
47 };
48
49 ail_error_e db_open(db_open_mode mode)
50 {
51         int ret;
52         int changed = 0;
53
54         if(mode & DB_OPEN_RO) {
55                 if (!db_info.dbro) {
56                         ret = db_util_open_with_options(APP_INFO_DB, &db_info.dbro, SQLITE_OPEN_READONLY, NULL);
57                         _E("db_open_ro ret=%d", ret);
58                         retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
59                 }
60         }
61
62         if(mode & DB_OPEN_RW) {
63                 if (!db_info.dbrw) {
64                         ret = db_util_open(APP_INFO_DB, &db_info.dbrw, DB_UTIL_REGISTER_HOOK_METHOD);
65                         _E("db_open_rw ret=%d", ret);
66                         retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
67                 }
68         }
69
70         return AIL_ERROR_OK;
71 }
72
73
74
75 ail_error_e db_prepare(const char *query, sqlite3_stmt **stmt)
76 {
77         int ret;
78
79         retv_if(!query, AIL_ERROR_INVALID_PARAMETER);
80         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
81         retv_if(!db_info.dbro, AIL_ERROR_DB_FAILED);
82
83         ret = sqlite3_prepare_v2(db_info.dbro, query, strlen(query), stmt, NULL);
84         if (ret != SQLITE_OK) {
85                 _E("%s\n", sqlite3_errmsg(db_info.dbro));
86                 return AIL_ERROR_DB_FAILED;
87         } else 
88                 return AIL_ERROR_OK;
89 }
90
91
92 ail_error_e db_bind_bool(sqlite3_stmt *stmt, int idx, bool value)
93 {
94         int ret;
95
96         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
97
98         ret = sqlite3_bind_int(stmt, idx, (int) value);
99         retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
100
101         return AIL_ERROR_OK;
102 }
103
104
105
106 ail_error_e db_bind_int(sqlite3_stmt *stmt, int idx, int value)
107 {
108         int ret;
109
110         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
111
112         ret = sqlite3_bind_int(stmt, idx, value);
113         retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
114
115         return AIL_ERROR_OK;
116 }
117
118
119
120 ail_error_e db_step(sqlite3_stmt *stmt)
121 {
122         int ret;
123
124         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
125
126         ret = sqlite3_step(stmt);
127         switch (ret) {
128                 case SQLITE_DONE:
129                         return AIL_ERROR_NO_DATA;
130                 case SQLITE_ROW:
131                         return AIL_ERROR_OK;
132         }
133
134         retv_with_dbmsg_if(1, AIL_ERROR_DB_FAILED);
135 }
136
137
138
139 ail_error_e db_column_bool(sqlite3_stmt *stmt, int index, bool *value)
140 {
141         int out_val;
142
143         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
144         retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
145
146         out_val = sqlite3_column_int(stmt, index);
147         *value = (out_val == 1)? true:false;
148
149         return AIL_ERROR_OK;
150 }
151
152
153
154 ail_error_e db_column_int(sqlite3_stmt *stmt, int index, int *value)
155 {
156         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
157         retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
158
159         *value = sqlite3_column_int(stmt, index);
160
161         return AIL_ERROR_OK;
162 }
163
164
165
166 ail_error_e db_column_str(sqlite3_stmt *stmt, int index, char **str)
167 {
168         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
169         retv_if(!str, AIL_ERROR_INVALID_PARAMETER);
170
171         *str = (char *)sqlite3_column_text(stmt, index);
172
173         return AIL_ERROR_OK;
174 }
175
176
177
178 ail_error_e db_reset(sqlite3_stmt *stmt)
179 {
180         int ret;
181
182         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
183
184         sqlite3_clear_bindings(stmt);
185
186         ret = sqlite3_reset(stmt);
187         retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
188
189         return AIL_ERROR_OK;
190 }
191
192
193
194 ail_error_e db_finalize(sqlite3_stmt *stmt)
195 {
196         int ret;
197
198         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
199
200         ret = sqlite3_finalize(stmt);
201         retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
202
203         return AIL_ERROR_OK;
204 }
205
206
207
208 ail_error_e db_exec(const char *query)
209 {
210         int ret;
211         char *errmsg;
212
213         retv_if(!query, AIL_ERROR_INVALID_PARAMETER);
214         retv_if(!db_info.dbrw, AIL_ERROR_DB_FAILED);
215
216         ret = sqlite3_exec(db_info.dbrw, query, NULL, NULL, &errmsg);
217         if (ret != SQLITE_OK) {
218                 _E("Cannot execute this query - %s. because %s",
219                                 query, errmsg? errmsg:"uncatched error");
220                 sqlite3_free(errmsg);
221                 return AIL_ERROR_DB_FAILED;
222         }
223
224         return AIL_ERROR_OK;
225 }
226
227
228
229 ail_error_e db_close(void)
230 {
231         int ret;
232
233         if(db_info.dbro) {
234                 ret = sqlite3_close(db_info.dbro);
235                 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
236
237                 db_info.dbro = NULL;
238         }
239         if(db_info.dbrw) {
240                 ret = sqlite3_close(db_info.dbrw);
241                 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
242
243                 db_info.dbrw = NULL;
244         }
245
246         return AIL_ERROR_OK;
247 }
248
249 EXPORT_API ail_error_e ail_db_close(void)
250 {
251         return db_close();
252 }
253
254 int db_exec_sqlite_query(char *query, sqlite_query_callback callback, void *data)
255 {
256         char *error_message = NULL;
257         if (SQLITE_OK !=
258             sqlite3_exec(db_info.dbro, query, callback, data, &error_message)) {
259                 _E("Don't execute query = %s error message = %s\n", query,
260                        error_message);
261                 sqlite3_free(error_message);
262                 return -1;
263         }
264         sqlite3_free(error_message);
265         return 0;
266 }
267
268 // End of file.