Refactoring rua api
[platform/core/appfw/librua.git] / src / rua_internal.c
1
2 #include <tzplatform_config.h>
3 #include <db-util.h>
4 #include <aul.h>
5
6 #include "rua_internal.h"
7 #include "db-schema.h"
8
9 static int __exec(sqlite3 *db, char *query)
10 {
11         int r;
12         char *errmsg = NULL;
13
14         if (db == NULL)
15                 return -1;
16
17         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
18
19         if (r != SQLITE_OK) {
20                 sqlite3_free(errmsg);
21                 return -1;
22         }
23
24         return 0;
25 }
26
27 static int __create_table(sqlite3 *db)
28 {
29         int r;
30
31         r = __exec(db, CREATE_RUA_HISTORY_TABLE);
32         if (r == -1)
33                 return -1;
34
35         return 0;
36 }
37
38 static sqlite3 *__db_init()
39 {
40         int r;
41         sqlite3 *db = NULL;
42
43         char defname[FILENAME_MAX];
44         const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
45         if (rua_db_path == NULL) {
46                 LOGE("fail to get rua_db_path");
47                 return NULL;
48         }
49         snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
50
51         r = db_util_open_with_options(defname, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
52         if (r) {
53                 db_util_close(db);
54                 return NULL;
55         }
56
57         r = __create_table(db);
58         if (r) {
59                 db_util_close(db);
60                 return NULL;
61         }
62
63         return db;
64 }
65
66 int rua_db_delete_history(bundle *b)
67 {
68         int r;
69         sqlite3 *db = NULL;
70         char query[QUERY_MAXLEN];
71
72         char *pkg_name = NULL;
73         char *app_path = NULL;
74         char *errmsg = NULL;
75         int result = 0;
76
77         db = __db_init();
78         if (db == NULL) {
79                 LOGE("Error db null");
80                 return -1;
81         }
82
83         if (b != NULL) {
84                 bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name);
85                 bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path);
86         }
87
88         if (pkg_name != NULL)
89                 snprintf(query, QUERY_MAXLEN, "delete from rua_history where pkg_name = '%s';", pkg_name);
90         else if (app_path != NULL)
91                 snprintf(query, QUERY_MAXLEN, "delete from rua_history where app_path = '%s';", app_path);
92         else
93                 snprintf(query, QUERY_MAXLEN, "delete from rua_history;");
94
95         LOGI("rua_delete_history_from_db : %s", query);
96         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
97
98         if (r != SQLITE_OK) {
99                 LOGE("fail to exec delete query %s : %s", query, errmsg);
100                 sqlite3_free(errmsg);
101                 result = -1;
102         }
103
104         if (db != NULL)
105                 db_util_close(db);
106
107         return result;
108
109 }
110
111 int rua_db_add_history(struct rua_rec *rec)
112 {
113         int r;
114         int cnt = 0;
115         char query[QUERY_MAXLEN];
116         sqlite3_stmt *stmt;
117         sqlite3 *db = NULL;
118
119         db = __db_init();
120         if (db == NULL) {
121                 LOGE("Error db null");
122                 return -1;
123         }
124
125         if (rec == NULL) {
126                 LOGE("Error rec null");
127                 db_util_close(db);
128                 return -1;
129         }
130
131         snprintf(query, QUERY_MAXLEN,
132                 "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY,
133                 rec->pkg_name);
134
135         r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
136         if (r != SQLITE_OK) {
137                 LOGE("Error sqlite3_prepare fail");
138                 db_util_close(db);
139                 return -1;
140         }
141
142         r = sqlite3_step(stmt);
143         if (r == SQLITE_ROW)
144                 cnt = sqlite3_column_int(stmt, 0);
145
146         sqlite3_finalize(stmt);
147
148         if (cnt == 0)
149                 /* insert */
150                 snprintf(query, QUERY_MAXLEN,
151                         "insert into %s ( pkg_name, app_path, arg, launch_time ) "
152                         " values ( \"%s\", \"%s\", \"%s\", %d ) ",
153                         RUA_HISTORY,
154                         rec->pkg_name ? rec->pkg_name : "",
155                         rec->app_path ? rec->app_path : "",
156                         rec->arg ? rec->arg : "", (int)time(NULL));
157         else
158                 /* update */
159                 snprintf(query, QUERY_MAXLEN,
160                         "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';",
161                         RUA_HISTORY,
162                         rec->arg ? rec->arg : "", (int)time(NULL), rec->pkg_name);
163
164         r = __exec(db, query);
165         if (r == -1) {
166                 LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
167                 db_util_close(db);
168                 return -1;
169         }
170
171         db_util_close(db);
172         return r;
173 }