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>
45 #include "db-schema.h"
46 #include "perf-measure.h"
48 #define RUA_DB_NAME ".rua.db"
49 #define RUA_HISTORY "rua_history"
50 #define QUERY_MAXLEN 4096
52 "select pkg_name from rua_history " \
53 "order by launch_time desc limit 1 "
55 static sqlite3 *_db = NULL;
57 static int __exec(sqlite3 *db, char *query);
58 static int __create_table(sqlite3 *db);
59 static sqlite3 *__db_init(char *root);
61 int rua_clear_history(void)
64 char query[QUERY_MAXLEN];
69 snprintf(query, QUERY_MAXLEN, "delete from %s;", RUA_HISTORY);
71 r = __exec(_db, query);
76 int rua_delete_history_with_pkgname(char *pkg_name)
79 char query[QUERY_MAXLEN];
87 snprintf(query, QUERY_MAXLEN, "delete from %s where pkg_name = '%s';",
88 RUA_HISTORY, pkg_name);
90 r = __exec(_db, query);
95 int rua_delete_history_with_apppath(char *app_path)
98 char query[QUERY_MAXLEN];
103 if (app_path == NULL)
106 snprintf(query, QUERY_MAXLEN, "delete from %s where app_path = '%s';",
107 RUA_HISTORY, app_path);
109 r = __exec(_db, query);
114 int rua_add_history(struct rua_rec *rec)
118 char query[QUERY_MAXLEN];
121 unsigned int timestamp;
122 timestamp = PERF_MEASURE_START("RUA");
125 LOGE("Error db null");
130 LOGE("Error rec null");
134 snprintf(query, QUERY_MAXLEN,
135 "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY,
138 r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL);
139 if (r != SQLITE_OK) {
140 LOGE("Error sqlite3_prepare fail");
144 r = sqlite3_step(stmt);
145 if (r == SQLITE_ROW) {
146 cnt = sqlite3_column_int(stmt, 0);
148 sqlite3_finalize(stmt);
152 snprintf(query, QUERY_MAXLEN,
153 "insert into %s ( pkg_name, app_path, arg, launch_time ) "
154 " values ( \"%s\", \"%s\", \"%s\", %d ) ",
156 rec->pkg_name ? rec->pkg_name : "",
157 rec->app_path ? rec->app_path : "",
158 rec->arg ? rec->arg : "", time(NULL));
161 snprintf(query, QUERY_MAXLEN,
162 "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';",
164 rec->arg ? rec->arg : "", time(NULL), rec->pkg_name);
166 r = __exec(_db, query);
168 printf("[RUA ADD HISTORY ERROR] %s\n", query);
172 PERF_MEASURE_END("RUA", timestamp);
177 int rua_history_load_db(char ***table, int *nrows, int *ncols)
180 char query[QUERY_MAXLEN];
182 char **db_result = NULL;
185 char defname[FILENAME_MAX];
186 const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
187 snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
196 r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
202 snprintf(query, QUERY_MAXLEN,
203 "select * from %s order by launch_time desc;", RUA_HISTORY);
205 r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
210 sqlite3_free_table(db_result);
218 int rua_history_unload_db(char ***table)
221 sqlite3_free_table(*table);
228 int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
231 char **db_result = NULL;
241 db_result = table + ((row + 1) * ncols);
243 tmp = db_result[RUA_COL_ID];
248 tmp = db_result[RUA_COL_PKGNAME];
253 tmp = db_result[RUA_COL_APPPATH];
258 tmp = db_result[RUA_COL_ARG];
263 tmp = db_result[RUA_COL_LAUNCHTIME];
265 rec->launch_time = atoi(tmp);
271 int rua_is_latest_app(const char *pkg_name)
275 const unsigned char *ct;
278 char defname[FILENAME_MAX];
279 const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
280 snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
285 r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
291 r = sqlite3_prepare(db, Q_LATEST, sizeof(Q_LATEST), &stmt, NULL);
292 if (r != SQLITE_OK) {
296 r = sqlite3_step(stmt);
297 if (r == SQLITE_ROW) {
298 ct = sqlite3_column_text(stmt, 0);
299 if (ct == NULL || ct[0] == '\0') {
304 if (strncmp(pkg_name, ct, strlen(pkg_name)) == 0) {
312 sqlite3_finalize(stmt);
321 unsigned int timestamp;
322 timestamp = PERF_MEASURE_START("RUA");
328 char defname[FILENAME_MAX];
329 const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
330 snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
331 _db = __db_init(defname);
336 PERF_MEASURE_END("RUA", timestamp);
343 unsigned int timestamp;
344 timestamp = PERF_MEASURE_START("RUA");
351 PERF_MEASURE_END("RUA", timestamp);
355 static int __exec(sqlite3 *db, char *query)
363 r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
365 if (r != SQLITE_OK) {
366 sqlite3_free(errmsg);
373 static int __create_table(sqlite3 *db)
377 r = __exec(db, CREATE_RUA_HISTORY_TABLE);
384 static sqlite3 *__db_init(char *root)
389 r = db_util_open_with_options(root, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
395 r = __create_table(db);