Merge from kiran SPIN notification
[platform/core/api/notification.git] / src / notification_db.c
1 /*
2  *  libnotification
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@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 #include <errno.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include <sqlite3.h>
28 #include <db-util.h>
29 #include <tizen.h>
30
31 #include <notification_error.h>
32 #include <notification_debug.h>
33 #include <notification_db.h>
34
35 sqlite3 * notification_db_open(const char *dbfile)
36 {
37         int ret = 0;
38         sqlite3 *db =0;
39
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);
44                 }
45                 else {
46                         set_last_result(NOTIFICATION_ERROR_FROM_DB);
47                 }
48                 return NULL;
49         }
50
51         return db;
52 }
53
54 int notification_db_close(sqlite3 ** db)
55 {
56         int ret = 0;
57
58         if (db == NULL || *db == NULL) {
59                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
60         }
61
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;
66         }
67
68         *db = NULL;
69
70         return NOTIFICATION_ERROR_NONE;
71 }
72
73 int notification_db_exec(sqlite3 * db, const char *query, int *num_changes)
74 {
75         int ret = 0;
76         sqlite3_stmt *stmt = NULL;
77
78         if (db == NULL) {
79                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
80         }
81         if (query == NULL) {
82                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
83         }
84
85         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
86         if (ret != SQLITE_OK) {
87                 NOTIFICATION_ERR("DB err(%d) : %s", ret,
88                                  sqlite3_errmsg(db));
89                 return NOTIFICATION_ERROR_FROM_DB;
90         }
91
92         if (stmt != NULL) {
93                 ret = sqlite3_step(stmt);
94                 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
95                         if (num_changes != NULL) {
96                                 *num_changes = sqlite3_changes(db);
97                         }
98                         sqlite3_finalize(stmt);
99                 } else {
100                         NOTIFICATION_ERR("DB err(%d) : %s", ret,
101                                          sqlite3_errmsg(db));
102                         sqlite3_finalize(stmt);
103                         return NOTIFICATION_ERROR_FROM_DB;
104                 }
105         } else {
106                         return NOTIFICATION_ERROR_FROM_DB;
107         }
108
109         return NOTIFICATION_ERROR_NONE;
110 }
111
112 char *notification_db_column_text(sqlite3_stmt * stmt, int col)
113 {
114         const unsigned char *col_text = NULL;
115
116         col_text = sqlite3_column_text(stmt, col);
117         if (col_text == NULL || col_text[0] == '\0') {
118                 return NULL;
119         }
120
121         return strdup((char *)col_text);
122 }
123
124 bundle *notification_db_column_bundle(sqlite3_stmt * stmt, int col)
125 {
126         const unsigned char *col_bundle = NULL;
127
128         col_bundle = sqlite3_column_text(stmt, col);
129         if (col_bundle == NULL || col_bundle[0] == '\0') {
130                 return NULL;
131         }
132
133         return bundle_decode(col_bundle, strlen((char *)col_bundle));
134 }