remove unsed debug message
[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                         retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
58                 }
59         }
60
61         if(mode & DB_OPEN_RW) {
62                 if (!db_info.dbrw) {
63                         ret = db_util_open(APP_INFO_DB, &db_info.dbrw, DB_UTIL_REGISTER_HOOK_METHOD);
64                         retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
65                 }
66         }
67
68         return AIL_ERROR_OK;
69 }
70
71
72
73 ail_error_e db_prepare(const char *query, sqlite3_stmt **stmt)
74 {
75         int ret;
76
77         retv_if(!query, AIL_ERROR_INVALID_PARAMETER);
78         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
79         retv_if(!db_info.dbro, AIL_ERROR_DB_FAILED);
80
81         ret = sqlite3_prepare_v2(db_info.dbro, query, strlen(query), stmt, NULL);
82         if (ret != SQLITE_OK) {
83                 _E("%s\n", sqlite3_errmsg(db_info.dbro));
84                 return AIL_ERROR_DB_FAILED;
85         } else 
86                 return AIL_ERROR_OK;
87 }
88
89
90 ail_error_e db_bind_bool(sqlite3_stmt *stmt, int idx, bool value)
91 {
92         int ret;
93
94         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
95
96         ret = sqlite3_bind_int(stmt, idx, (int) value);
97         retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
98
99         return AIL_ERROR_OK;
100 }
101
102
103
104 ail_error_e db_bind_int(sqlite3_stmt *stmt, int idx, int value)
105 {
106         int ret;
107
108         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
109
110         ret = sqlite3_bind_int(stmt, idx, value);
111         retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
112
113         return AIL_ERROR_OK;
114 }
115
116
117
118 ail_error_e db_step(sqlite3_stmt *stmt)
119 {
120         int ret;
121
122         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
123
124         ret = sqlite3_step(stmt);
125         switch (ret) {
126                 case SQLITE_DONE:
127                         return AIL_ERROR_NO_DATA;
128                 case SQLITE_ROW:
129                         return AIL_ERROR_OK;
130         }
131
132         retv_with_dbmsg_if(1, AIL_ERROR_DB_FAILED);
133 }
134
135
136
137 ail_error_e db_column_bool(sqlite3_stmt *stmt, int index, bool *value)
138 {
139         int out_val;
140
141         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
142         retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
143
144         out_val = sqlite3_column_int(stmt, index);
145         *value = (out_val == 1)? true:false;
146
147         return AIL_ERROR_OK;
148 }
149
150
151
152 ail_error_e db_column_int(sqlite3_stmt *stmt, int index, int *value)
153 {
154         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
155         retv_if(!value, AIL_ERROR_INVALID_PARAMETER);
156
157         *value = sqlite3_column_int(stmt, index);
158
159         return AIL_ERROR_OK;
160 }
161
162
163
164 ail_error_e db_column_str(sqlite3_stmt *stmt, int index, char **str)
165 {
166         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
167         retv_if(!str, AIL_ERROR_INVALID_PARAMETER);
168
169         *str = (char *)sqlite3_column_text(stmt, index);
170
171         return AIL_ERROR_OK;
172 }
173
174
175
176 ail_error_e db_reset(sqlite3_stmt *stmt)
177 {
178         int ret;
179
180         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
181
182         sqlite3_clear_bindings(stmt);
183
184         ret = sqlite3_reset(stmt);
185         retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
186
187         return AIL_ERROR_OK;
188 }
189
190
191
192 ail_error_e db_finalize(sqlite3_stmt *stmt)
193 {
194         int ret;
195
196         retv_if(!stmt, AIL_ERROR_INVALID_PARAMETER);
197
198         ret = sqlite3_finalize(stmt);
199         retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
200
201         return AIL_ERROR_OK;
202 }
203
204
205
206 ail_error_e db_exec(const char *query)
207 {
208         int ret;
209         char *errmsg;
210
211         retv_if(!query, AIL_ERROR_INVALID_PARAMETER);
212         retv_if(!db_info.dbrw, AIL_ERROR_DB_FAILED);
213
214         ret = sqlite3_exec(db_info.dbrw, query, NULL, NULL, &errmsg);
215         if (ret != SQLITE_OK) {
216                 _E("Cannot execute this query - %s. because %s",
217                                 query, errmsg? errmsg:"uncatched error");
218                 sqlite3_free(errmsg);
219                 return AIL_ERROR_DB_FAILED;
220         }
221
222         return AIL_ERROR_OK;
223 }
224
225
226
227 ail_error_e db_close(void)
228 {
229         int ret;
230
231         if(db_info.dbro) {
232                 ret = sqlite3_close(db_info.dbro);
233                 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
234
235                 db_info.dbro = NULL;
236         }
237         if(db_info.dbrw) {
238                 ret = sqlite3_close(db_info.dbrw);
239                 retv_with_dbmsg_if(ret != SQLITE_OK, AIL_ERROR_DB_FAILED);
240
241                 db_info.dbrw = NULL;
242         }
243
244         return AIL_ERROR_OK;
245 }
246
247 EXPORT_API ail_error_e ail_db_close(void)
248 {
249         return db_close();
250 }
251
252 int db_exec_sqlite_query(char *query, sqlite_query_callback callback, void *data)
253 {
254         char *error_message = NULL;
255         if (SQLITE_OK !=
256             sqlite3_exec(db_info.dbro, query, callback, data, &error_message)) {
257                 _E("Don't execute query = %s error message = %s\n", query,
258                        error_message);
259                 sqlite3_free(error_message);
260                 return -1;
261         }
262         sqlite3_free(error_message);
263         return 0;
264 }
265
266 // End of file.