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.
34 #include "db-schema.h"
35 #include "perf-measure.h"
37 #define RUA_DB_PATH "/opt/dbspace"
38 #define RUA_DB_NAME ".rua.db"
39 #define RUA_HISTORY "rua_history"
40 #define QUERY_MAXLEN 4096
42 "select pkg_name from rua_history " \
43 "order by launch_time desc limit 1 "
45 static sqlite3 *_db = NULL;
47 static int __exec(sqlite3 *db, char *query);
48 static int __create_table(sqlite3 *db);
49 static sqlite3 *__db_init(char *root);
51 int rua_clear_history(void)
54 char query[QUERY_MAXLEN];
59 snprintf(query, QUERY_MAXLEN, "delete from %s;", RUA_HISTORY);
61 r = __exec(_db, query);
66 int rua_delete_history_with_pkgname(char *pkg_name)
69 char query[QUERY_MAXLEN];
77 snprintf(query, QUERY_MAXLEN, "delete from %s where pkg_name = '%s';",
78 RUA_HISTORY, pkg_name);
80 r = __exec(_db, query);
85 int rua_delete_history_with_apppath(char *app_path)
88 char query[QUERY_MAXLEN];
96 snprintf(query, QUERY_MAXLEN, "delete from %s where app_path = '%s';",
97 RUA_HISTORY, app_path);
99 r = __exec(_db, query);
104 int rua_add_history(struct rua_rec *rec)
108 char query[QUERY_MAXLEN];
111 unsigned int timestamp;
112 timestamp = PERF_MEASURE_START("RUA");
120 snprintf(query, QUERY_MAXLEN,
121 "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY,
124 r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL);
125 if (r != SQLITE_OK) {
129 r = sqlite3_step(stmt);
130 if (r == SQLITE_ROW) {
131 cnt = sqlite3_column_int(stmt, 0);
133 sqlite3_finalize(stmt);
137 snprintf(query, QUERY_MAXLEN,
138 "insert into %s ( pkg_name, app_path, arg, launch_time ) "
139 " values ( \"%s\", \"%s\", \"%s\", %d ) ",
141 rec->pkg_name ? rec->pkg_name : "",
142 rec->app_path ? rec->app_path : "",
143 rec->arg ? rec->arg : "", time(NULL));
146 snprintf(query, QUERY_MAXLEN,
147 "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';",
149 rec->arg ? rec->arg : "", time(NULL), rec->pkg_name);
151 r = __exec(_db, query);
153 printf("[RUA ADD HISTORY ERROR] %s\n", query);
157 PERF_MEASURE_END("RUA", timestamp);
162 int rua_history_load_db(char ***table, int *nrows, int *ncols)
165 char query[QUERY_MAXLEN];
167 char **db_result = NULL;
176 snprintf(query, QUERY_MAXLEN,
177 "select * from %s order by launch_time desc;", RUA_HISTORY);
179 r = sqlite3_get_table(_db, query, &db_result, nrows, ncols, &db_err);
184 sqlite3_free_table(db_result);
189 int rua_history_unload_db(char ***table)
192 sqlite3_free_table(*table);
199 int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
202 char **db_result = NULL;
212 db_result = table + ((row + 1) * ncols);
214 tmp = db_result[RUA_COL_ID];
219 tmp = db_result[RUA_COL_PKGNAME];
224 tmp = db_result[RUA_COL_APPPATH];
229 tmp = db_result[RUA_COL_ARG];
234 tmp = db_result[RUA_COL_LAUNCHTIME];
236 rec->launch_time = atoi(tmp);
242 int rua_is_latest_app(const char *pkg_name)
246 const unsigned char *ct;
248 if (!pkg_name || !_db)
251 r = sqlite3_prepare(_db, Q_LATEST, sizeof(Q_LATEST), &stmt, NULL);
252 if (r != SQLITE_OK) {
256 r = sqlite3_step(stmt);
257 if (r == SQLITE_ROW) {
258 ct = sqlite3_column_text(stmt, 0);
259 if (ct == NULL || ct[0] == '\0') {
260 sqlite3_finalize(stmt);
264 if (strncmp(pkg_name, ct, strlen(pkg_name)) == 0) {
265 sqlite3_finalize(stmt);
270 sqlite3_finalize(stmt);
276 unsigned int timestamp;
277 timestamp = PERF_MEASURE_START("RUA");
283 char defname[FILENAME_MAX];
284 snprintf(defname, sizeof(defname), "%s/%s", RUA_DB_PATH, RUA_DB_NAME);
285 _db = __db_init(defname);
290 PERF_MEASURE_END("RUA", timestamp);
297 unsigned int timestamp;
298 timestamp = PERF_MEASURE_START("RUA");
305 PERF_MEASURE_END("RUA", timestamp);
309 static int __exec(sqlite3 *db, char *query)
317 r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
319 if (r != SQLITE_OK) {
320 sqlite3_free(errmsg);
327 static int __create_table(sqlite3 *db)
331 r = __exec(db, CREATE_RUA_HISTORY_TABLE);
338 static sqlite3 *__db_init(char *root)
343 r = db_util_open(root, &db, 0);
349 r = __create_table(db);