Change db path
[platform/core/appfw/librua.git] / src / rua_internal.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <db-util.h>
18 #include <aul.h>
19
20 #include "rua_internal.h"
21 #include "db-schema.h"
22 #include "rua_util.h"
23
24 static int __exec(sqlite3 *db, char *query)
25 {
26         int r;
27         char *errmsg = NULL;
28
29         if (db == NULL)
30                 return -1;
31
32         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
33
34         if (r != SQLITE_OK) {
35                 sqlite3_free(errmsg);
36                 return -1;
37         }
38
39         return 0;
40 }
41
42 static int __create_table(sqlite3 *db)
43 {
44         int r;
45
46         r = __exec(db, CREATE_RUA_HISTORY_TABLE);
47         if (r == -1)
48                 return -1;
49
50         return 0;
51 }
52
53 static sqlite3 *__db_init()
54 {
55         int r;
56         sqlite3 *db = NULL;
57
58         r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, getuid(), RUA_DB_NAME);
59         if (r != SQLITE_OK)
60                 return NULL;
61
62         r = __create_table(db);
63         if (r) {
64                 db_util_close(db);
65                 return NULL;
66         }
67
68         return db;
69 }
70
71 int rua_db_delete_history(bundle *b)
72 {
73         int r;
74         sqlite3 *db = NULL;
75         char query[QUERY_MAXLEN];
76         char *pkg_name = NULL;
77         char *app_path = NULL;
78         char *errmsg = NULL;
79         int result = 0;
80
81         db = __db_init();
82         if (db == NULL) {
83                 LOGE("Error db null");
84                 return -1;
85         }
86
87         if (b != NULL) {
88                 bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name);
89                 bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path);
90         }
91
92         if (pkg_name != NULL)
93                 snprintf(query, QUERY_MAXLEN, "delete from rua_history where pkg_name = '%s';", pkg_name);
94         else if (app_path != NULL)
95                 snprintf(query, QUERY_MAXLEN, "delete from rua_history where app_path = '%s';", app_path);
96         else
97                 snprintf(query, QUERY_MAXLEN, "delete from rua_history;");
98
99         LOGI("rua_delete_history_from_db : %s", query);
100         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
101
102         if (r != SQLITE_OK) {
103                 LOGE("fail to exec delete query %s : %s", query, errmsg);
104                 sqlite3_free(errmsg);
105                 result = -1;
106         }
107
108         if (db != NULL)
109                 db_util_close(db);
110
111         return result;
112 }
113
114 int rua_db_add_history(struct rua_rec *rec)
115 {
116         int r;
117         char query[QUERY_MAXLEN];
118         sqlite3 *db = NULL;
119
120         db = __db_init();
121         if (db == NULL) {
122                 LOGE("Error db null");
123                 return -1;
124         }
125
126         if (rec == NULL) {
127                 LOGE("Error rec null");
128                 db_util_close(db);
129                 return -1;
130         }
131
132         snprintf(query, QUERY_MAXLEN,
133                 "insert or replace into %s ( pkg_name, app_path, arg, launch_time) "
134                 " values ( \"%s\", \"%s\", \"%s\", %d) ",
135                 RUA_HISTORY,
136                 rec->pkg_name ? rec->pkg_name : "",
137                 rec->app_path ? rec->app_path : "",
138                 rec->arg ? rec->arg : "", (int)rec->launch_time);
139
140         r = __exec(db, query);
141         if (r == -1) {
142                 LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
143                 db_util_close(db);
144                 return -1;
145         }
146
147         db_util_close(db);
148         return r;
149 }