4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
33 /* For multi-user support */
34 #include <tzplatform_config.h>
44 #include "db-schema.h"
45 #include "perf-measure.h"
47 #define RUA_DB_NAME ".rua.db"
48 #define RUA_HISTORY "rua_history"
49 #define QUERY_MAXLEN 4096
51 "select pkg_name from rua_history " \
52 "order by launch_time desc limit 1 "
54 static int __exec(sqlite3 *db, char *query);
55 static int __create_table(sqlite3 *db);
56 static sqlite3 *__db_init();
58 int rua_clear_history(void)
61 char query[QUERY_MAXLEN];
66 LOGE("Error db null");
70 snprintf(query, QUERY_MAXLEN, "delete from %s;", RUA_HISTORY);
72 r = __exec(db, query);
77 int rua_delete_history_with_pkgname(char *pkg_name)
80 char query[QUERY_MAXLEN];
86 LOGE("Error db null");
90 if (pkg_name == NULL) {
95 snprintf(query, QUERY_MAXLEN, "delete from %s where pkg_name = '%s';",
96 RUA_HISTORY, pkg_name);
98 r = __exec(db, query);
103 int rua_delete_history_with_apppath(char *app_path)
106 char query[QUERY_MAXLEN];
111 LOGE("Error db null");
115 if (app_path == NULL) {
120 snprintf(query, QUERY_MAXLEN, "delete from %s where app_path = '%s';",
121 RUA_HISTORY, app_path);
123 r = __exec(db, query);
128 int rua_add_history(struct rua_rec *rec)
132 char query[QUERY_MAXLEN];
136 unsigned int timestamp;
137 timestamp = PERF_MEASURE_START("RUA");
141 LOGE("Error db null");
146 LOGE("Error rec null");
151 snprintf(query, QUERY_MAXLEN,
152 "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY,
155 r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
156 if (r != SQLITE_OK) {
157 LOGE("Error sqlite3_prepare fail");
162 r = sqlite3_step(stmt);
164 cnt = sqlite3_column_int(stmt, 0);
166 sqlite3_finalize(stmt);
170 snprintf(query, QUERY_MAXLEN,
171 "insert into %s ( pkg_name, app_path, arg, launch_time ) "
172 " values ( \"%s\", \"%s\", \"%s\", %d ) ",
174 rec->pkg_name ? rec->pkg_name : "",
175 rec->app_path ? rec->app_path : "",
176 rec->arg ? rec->arg : "", time(NULL));
179 snprintf(query, QUERY_MAXLEN,
180 "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';",
182 rec->arg ? rec->arg : "", time(NULL), rec->pkg_name);
184 r = __exec(db, query);
186 LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
191 PERF_MEASURE_END("RUA", timestamp);
196 int rua_history_load_db(char ***table, int *nrows, int *ncols)
199 char query[QUERY_MAXLEN];
201 char **db_result = NULL;
204 char defname[FILENAME_MAX];
205 const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
206 if (rua_db_path == NULL) {
207 LOGE("fail to get rua_db_path");
210 snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
219 r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
225 snprintf(query, QUERY_MAXLEN,
226 "select * from %s order by launch_time desc;", RUA_HISTORY);
228 r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
233 sqlite3_free_table(db_result);
240 int rua_history_unload_db(char ***table)
243 sqlite3_free_table(*table);
250 int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
253 char **db_result = NULL;
263 db_result = table + ((row + 1) * ncols);
265 tmp = db_result[RUA_COL_ID];
270 tmp = db_result[RUA_COL_PKGNAME];
275 tmp = db_result[RUA_COL_APPPATH];
280 tmp = db_result[RUA_COL_ARG];
285 tmp = db_result[RUA_COL_LAUNCHTIME];
287 rec->launch_time = atoi(tmp);
293 int rua_is_latest_app(const char *pkg_name)
297 const unsigned char *ct;
300 char defname[FILENAME_MAX];
301 const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
302 if (rua_db_path == NULL) {
303 LOGE("fail to get rua_db_path");
306 snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
311 r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
317 r = sqlite3_prepare(db, Q_LATEST, sizeof(Q_LATEST), &stmt, NULL);
318 if (r != SQLITE_OK) {
323 r = sqlite3_step(stmt);
324 if (r == SQLITE_ROW) {
325 ct = sqlite3_column_text(stmt, 0);
326 if (ct == NULL || ct[0] == '\0') {
331 if (strncmp(pkg_name, ct, strlen(pkg_name)) == 0) {
339 sqlite3_finalize(stmt);
356 static int __exec(sqlite3 *db, char *query)
364 r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
366 if (r != SQLITE_OK) {
367 sqlite3_free(errmsg);
374 static int __create_table(sqlite3 *db)
378 r = __exec(db, CREATE_RUA_HISTORY_TABLE);
385 static sqlite3 *__db_init()
390 char defname[FILENAME_MAX];
391 const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
392 if (rua_db_path == NULL) {
393 LOGE("fail to get rua_db_path");
396 snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
398 r = db_util_open_with_options(defname, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
404 r = __create_table(db);