upload tizen1.0 source
[apps/home/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: Jeonghoon Park <jh1979.park@samsung.com>, Youngjoo Park <yjoo93.park@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
30 #include <notification_error.h>
31 #include <notification_debug.h>
32 #include <notification_db.h>
33
34 #define SDFTET "/opt/dbspace/.notification_noti.db"
35
36 sqlite3 *notification_db_open(const char *dbfile)
37 {
38         int ret = 0;
39         sqlite3 *db;
40
41         ret = db_util_open(dbfile, &db, 0);
42         if (ret != SQLITE_OK) {
43                 NOTIFICATION_ERR("DB open error(%d), %s", ret, dbfile);
44                 return NULL;
45         }
46
47         return db;
48 }
49
50 int notification_db_close(sqlite3 ** db)
51 {
52         int ret = 0;
53
54         if (db == NULL || *db == NULL) {
55                 return NOTIFICATION_ERROR_INVALID_DATA;
56         }
57
58         ret = db_util_close(*db);
59         if (ret != SQLITE_OK) {
60                 NOTIFICATION_ERR("DB close error(%d)", ret);
61                 return NOTIFICATION_ERROR_FROM_DB;
62         }
63
64         *db = NULL;
65
66         return NOTIFICATION_ERROR_NONE;
67 }
68
69 int notification_db_exec(sqlite3 * db, const char *query)
70 {
71         int ret = 0;
72         char *err_msg = NULL;
73
74         if (db == NULL) {
75                 return NOTIFICATION_ERROR_INVALID_DATA;
76         }
77
78         ret = sqlite3_exec(db, query, NULL, NULL, &err_msg);
79         if (ret != SQLITE_OK) {
80                 NOTIFICATION_ERR("SQL error(%d) : %s", ret, err_msg);
81                 sqlite3_free(err_msg);
82                 return NOTIFICATION_ERROR_FROM_DB;
83         }
84
85         return NOTIFICATION_ERROR_NONE;
86 }
87
88 char *notification_db_column_text(sqlite3_stmt * stmt, int col)
89 {
90         const unsigned char *col_text = NULL;
91
92         col_text = sqlite3_column_text(stmt, col);
93         if (col_text == NULL || col_text[0] == '\0') {
94                 return NULL;
95         }
96
97         return strdup((char *)col_text);
98 }
99
100 bundle *notification_db_column_bundle(sqlite3_stmt * stmt, int col)
101 {
102         const unsigned char *col_bundle = NULL;
103
104         col_bundle = sqlite3_column_text(stmt, col);
105         if (col_bundle == NULL || col_bundle[0] == '\0') {
106                 return NULL;
107         }
108
109         return bundle_decode(col_bundle, strlen((char *)col_bundle));
110 }