b92bd284fa955a3c29c10ca1518a3030d3977bc7
[platform/core/appfw/badge.git] / src / badge_db.c
1 /*
2  *  libbadge
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngjoo Park <yjoo93.park@samsung.com>,
7  *      Seungtaek Chung <seungtaek.chung@samsung.com>, Youngsub Ko <ys4610.ko@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the License);
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an AS IS BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <sqlite3.h>
26
27 #include "badge.h"
28 #include "badge_log.h"
29 #include "badge_error.h"
30 #include "badge_internal.h"
31
32 EXPORT_API
33 int badge_db_insert(const char *pkgname, const char *writable_pkg, const char *caller)
34 {
35         int err = BADGE_ERROR_NONE;
36         badge_h *badge = NULL;
37         char *pkgs = NULL;
38
39         if (!pkgname) {
40                 WARN("package name is NULL");
41                 return BADGE_ERROR_INVALID_PARAMETER;
42         }
43
44         pkgs = _badge_pkgs_new(&err, writable_pkg, caller, NULL);
45         if (!pkgs) {
46                 ERR("fail to _badge_pkgs_new : %d", err);
47                 return err;
48         }
49
50         INFO("pkgs : %s", pkgs);
51
52         badge = _badge_new(pkgname, pkgs, &err);
53         if (!badge) {
54                 ERR("fail to _badge_new : %d", err);
55                 free(pkgs);
56                 return err;
57         }
58         free(pkgs);
59
60         err = _badge_insert(badge);
61         if (err != BADGE_ERROR_NONE) {
62                 ERR("fail to _badge_insert : %d", err);
63                 _badge_free(badge);
64                 return err;
65         }
66
67         _badge_free(badge);
68
69         return BADGE_ERROR_NONE;
70 }
71
72 EXPORT_API
73 int badge_db_delete(const char *pkgname, const char *caller)
74 {
75         int result = BADGE_ERROR_NONE;
76
77         result = _badge_remove(caller, pkgname);
78
79         return result;
80 }
81
82 EXPORT_API
83 int badge_db_set_count(const char *pkgname, const char *caller, int count)
84 {
85         int result = BADGE_ERROR_NONE;
86
87         result = _badget_set_count(caller, pkgname, count);
88
89         return result;
90 }
91
92 EXPORT_API
93 int badge_db_set_display_option(const char *pkgname, const char *caller, int is_display)
94 {
95         int result = BADGE_ERROR_NONE;
96
97         result = _badget_set_display(pkgname, is_display);
98
99         return result;
100 }
101
102 EXPORT_API
103 int badge_db_exec(sqlite3 * db, const char *query, int *num_changes)
104 {
105         int ret = 0;
106         sqlite3_stmt *stmt = NULL;
107
108         if (db == NULL) {
109                 return BADGE_ERROR_INVALID_PARAMETER;
110         }
111         if (query == NULL) {
112                 return BADGE_ERROR_INVALID_PARAMETER;
113         }
114
115         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
116         if (ret != SQLITE_OK) {
117                 ERR("DB err(%d) : %s", ret,
118                                  sqlite3_errmsg(db));
119                 return BADGE_ERROR_FROM_DB;
120         }
121
122         if (stmt != NULL) {
123                 ret = sqlite3_step(stmt);
124                 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
125                         if (num_changes != NULL) {
126                                 *num_changes = sqlite3_changes(db);
127                         }
128                         sqlite3_finalize(stmt);
129                 } else {
130                         ERR("DB err(%d) : %s", ret,
131                                          sqlite3_errmsg(db));
132                         sqlite3_finalize(stmt);
133                         return BADGE_ERROR_FROM_DB;
134                 }
135         } else {
136                         return BADGE_ERROR_FROM_DB;
137         }
138
139         return BADGE_ERROR_NONE;
140 }