4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 #include <notification_error.h>
32 #include <notification_debug.h>
33 #include <notification_db.h>
35 sqlite3 * notification_db_open(const char *dbfile)
40 ret = db_util_open(dbfile, &db, 0);
41 if (ret != SQLITE_OK) {
42 if (ret == SQLITE_PERM) {
43 set_last_result(NOTIFICATION_ERROR_PERMISSION_DENIED);
46 set_last_result(NOTIFICATION_ERROR_FROM_DB);
54 int notification_db_close(sqlite3 ** db)
58 if (db == NULL || *db == NULL) {
59 return NOTIFICATION_ERROR_INVALID_PARAMETER;
62 ret = db_util_close(*db);
63 if (ret != SQLITE_OK) {
64 NOTIFICATION_ERR("DB close error(%d)", ret);
65 return NOTIFICATION_ERROR_FROM_DB;
70 return NOTIFICATION_ERROR_NONE;
73 int notification_db_exec(sqlite3 * db, const char *query, int *num_changes)
76 sqlite3_stmt *stmt = NULL;
79 return NOTIFICATION_ERROR_INVALID_PARAMETER;
82 return NOTIFICATION_ERROR_INVALID_PARAMETER;
85 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
86 if (ret != SQLITE_OK) {
87 NOTIFICATION_ERR("DB err(%d) : %s", ret,
89 return NOTIFICATION_ERROR_FROM_DB;
93 ret = sqlite3_step(stmt);
94 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
95 if (num_changes != NULL) {
96 *num_changes = sqlite3_changes(db);
98 sqlite3_finalize(stmt);
100 NOTIFICATION_ERR("DB err(%d) : %s", ret,
102 sqlite3_finalize(stmt);
103 return NOTIFICATION_ERROR_FROM_DB;
106 return NOTIFICATION_ERROR_FROM_DB;
109 return NOTIFICATION_ERROR_NONE;
112 char *notification_db_column_text(sqlite3_stmt * stmt, int col)
114 const unsigned char *col_text = NULL;
116 col_text = sqlite3_column_text(stmt, col);
117 if (col_text == NULL || col_text[0] == '\0') {
121 return strdup((char *)col_text);
124 bundle *notification_db_column_bundle(sqlite3_stmt * stmt, int col)
126 const unsigned char *col_bundle = NULL;
128 col_bundle = sqlite3_column_text(stmt, col);
129 if (col_bundle == NULL || col_bundle[0] == '\0') {
133 return bundle_decode(col_bundle, strlen((char *)col_bundle));