Fix implicit declaration of function warning.
[platform/core/appfw/librua.git] / src / rua_stat.c
1 /*
2  *  RUA
3  *
4  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 /*
21  * @file    rua_stat.c
22  * @author  Hyunho Kang (hhstark.kang@samsung.com)
23  * @version 0.1
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <db-util.h>
31
32 /* For multi-user support */
33 #include <tzplatform_config.h>
34
35 #include "rua_stat.h"
36 #include "db-schema.h"
37 #include "perf-measure.h"
38
39 #include <dlog.h>
40
41 #ifdef LOG_TAG
42 #undef LOG_TAG
43 #endif
44
45 #define LOG_TAG "RUA"
46
47 #define RUA_STAT_DB_NAME        ".rua_stat.db"
48 #define QUERY_MAXLEN    4096
49 #define WIN_SCORE 100
50 #define LOSE_SCORE_RATE 0.7f
51
52 static sqlite3 *_db = NULL;
53 static int __exec(sqlite3 *db, char *query);
54 static sqlite3 *__db_init(char *root, int flags);
55
56 int __rua_stat_init(int flags)
57 {
58         char defname[FILENAME_MAX];
59         const char *rua_stat_db_path = tzplatform_getenv(TZ_USER_DB);
60
61         if (_db)
62                 return 0;
63
64         snprintf(defname, sizeof(defname), "%s/%s", rua_stat_db_path, RUA_STAT_DB_NAME);
65         _db = __db_init(defname, flags);
66
67         if (_db == NULL) {
68                 LOGW("__rua_stat_init error");
69                 return -1;
70         }
71
72         return 0;
73 }
74
75 int __rua_stat_fini(void)
76 {
77         if (_db) {
78                 db_util_close(_db);
79                 _db = NULL;
80         }
81
82         return 0;
83 }
84
85 int __rua_stat_insert(char *caller, char *rua_stat_tag) {
86
87         int r;
88         char query[QUERY_MAXLEN];
89         sqlite3_stmt *stmt = NULL;
90         sqlite3_snprintf(QUERY_MAXLEN, query,
91                 "INSERT INTO rua_panel_stat (caller_panel, rua_stat_tag, score) VALUES (?,?,?)");
92
93         r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL);
94         if (r != SQLITE_OK) {
95                 LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db));
96                 goto out;
97         }
98
99         r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
100         if(r != SQLITE_OK) {
101                 LOGE("caller bind error(%d) \n", r);
102                 goto out;
103         }
104
105         r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC);
106         if(r != SQLITE_OK) {
107                 LOGE("rua_stat_tag bind error(%d) \n", r);
108                 goto out;
109         }
110
111         r = sqlite3_bind_int(stmt, 3, WIN_SCORE);
112         if(r != SQLITE_OK) {
113                 LOGE("arg bind error(%d) \n", r);
114                 goto out;
115         }
116
117         r = sqlite3_step(stmt);
118         if (r != SQLITE_DONE) {
119                 LOGE("step error(%d) \n", r);
120                 goto out;
121         }
122
123
124 out :
125         if(stmt)
126                 sqlite3_finalize(stmt);
127
128         return r;
129 }
130
131 int __rua_stat_lose_score_update(char *caller, char *rua_stat_tag) {
132
133         int r;
134         char query[QUERY_MAXLEN];
135         sqlite3_stmt *stmt = NULL;
136         sqlite3_snprintf(QUERY_MAXLEN, query,
137                 "UPDATE rua_panel_stat SET score = score * %f WHERE caller_panel = ? AND rua_stat_tag != ?",
138                 LOSE_SCORE_RATE);
139
140         LOGD("lose score update sql : %s", query);
141
142         r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL);
143         if (r != SQLITE_OK) {
144                 LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db));
145                 goto out;
146         }
147
148         r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
149         if(r != SQLITE_OK) {
150                 LOGE("caller bind error(%d) \n", r);
151                 goto out;
152         }
153
154         r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC);
155         if(r != SQLITE_OK) {
156                 LOGE("rua_stat_tag bind error(%d) \n", r);
157                 goto out;
158         }
159
160         r = sqlite3_step(stmt);
161         if (r != SQLITE_DONE) {
162                 LOGE("step error(%d) \n", r);
163                 goto out;
164         }
165
166
167 out :
168         if(stmt)
169                 sqlite3_finalize(stmt);
170
171         return r;
172
173 }
174
175 int __rua_stat_win_score_update(char *caller, char *rua_stat_tag) {
176
177         int r;
178         char query[QUERY_MAXLEN];
179         sqlite3_stmt *stmt = NULL;
180         sqlite3_snprintf(QUERY_MAXLEN, query,
181                 "UPDATE rua_panel_stat SET score = score + %d WHERE caller_panel = ? AND rua_stat_tag = ?",
182                 WIN_SCORE);
183
184         LOGD("win score update sql : %s", query);
185
186         r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL);
187         if (r != SQLITE_OK) {
188                 LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db));
189                 goto out;
190         }
191
192         r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
193         if(r != SQLITE_OK) {
194                 LOGE("caller bind error(%d) \n", r);
195                 goto out;
196         }
197
198         r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC);
199         if(r != SQLITE_OK) {
200                 LOGE("rua_stat_tag bind error(%d) \n", r);
201                 goto out;
202         }
203
204         r = sqlite3_step(stmt);
205         if (r != SQLITE_DONE) {
206                 LOGE("step error(%d) \n", r);
207                 goto out;
208         }
209
210
211 out :
212         if(stmt)
213                 sqlite3_finalize(stmt);
214
215         return r;
216
217 }
218
219 int rua_stat_update(char *caller, char *rua_stat_tag)
220 {
221         int r;
222         int affected_rows = 0;
223         sqlite3_stmt *stmt = NULL;
224
225         LOGD("rua_stat_update start");
226
227         r = __rua_stat_init(SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE);
228         if (r == -1) {
229                 LOGE("__rua_stat_init fail");
230                 return -1;
231         }
232
233         if (_db == NULL) {
234                 LOGE("rua_stat is not initialized");
235                 return -1;
236         }
237
238         if (caller == NULL) {
239                 LOGE("caller is null");
240                 return -1;
241         }
242
243         if (rua_stat_tag == NULL) {
244                 LOGE("rua_stat_tag is null");
245                 return -1;
246         }
247
248
249         r = __rua_stat_lose_score_update(caller, rua_stat_tag);
250         if (r != SQLITE_DONE) {
251                 LOGE("__rua_stat_lose_score_insert fail.");
252                 return -1;
253         }
254
255         r = __rua_stat_win_score_update(caller, rua_stat_tag);
256         affected_rows = sqlite3_changes(_db);
257         if ((r != SQLITE_DONE) || (affected_rows == 0)) {
258                 r = __rua_stat_insert(caller, rua_stat_tag);
259
260                 if (r != SQLITE_DONE) {
261                         LOGE("__rua_stat_insert fail.");
262                         return -1;
263                 }
264         }
265
266         __rua_stat_fini();
267         LOGD("rua_stat_update done");
268
269         return r;
270
271 }
272
273
274 int rua_stat_get_stat_tags(char *caller, int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data),
275                 void *data) {
276
277         int r;
278         sqlite3_stmt *stmt;
279         char query[QUERY_MAXLEN];
280         const unsigned char *ct;
281
282         r = __rua_stat_init(SQLITE_OPEN_READONLY);
283         if (r == -1) {
284                 LOGE("__rua_stat_init fail");
285                 return -1;
286         }
287
288         sqlite3_snprintf(QUERY_MAXLEN, query,
289                 "SELECT rua_stat_tag FROM rua_panel_stat WHERE caller_panel = ? ORDER BY score DESC");
290
291         if (!_db)
292                 return -1;
293
294         r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL);
295         if (r != SQLITE_OK) {
296                 LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db));
297                 goto out;
298         }
299
300         r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
301         if (r != SQLITE_OK) {
302                 LOGE("caller bind error(%d) \n", r);
303                 goto out;
304         }
305
306         while(sqlite3_step(stmt) == SQLITE_ROW) {
307
308                 ct = sqlite3_column_text(stmt, 0);
309                 if (ct == NULL || ct[0] == '\0') {
310                         LOGW("sqlite3_column_text null");
311                 }
312                 rua_stat_tag_iter_fn(ct, data);
313         }
314
315 out:
316         if (stmt)
317                 sqlite3_finalize(stmt);
318
319         __rua_stat_fini();
320
321         return r;
322 }
323
324 static int __exec(sqlite3 *db, char *query)
325 {
326         int r;
327         char *errmsg = NULL;
328
329         if (db == NULL)
330                 return -1;
331
332         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
333         if (r != SQLITE_OK) {
334                 SECURE_LOGE("query(%s) exec error(%s)", query, errmsg);
335                 sqlite3_free(errmsg);
336                 return -1;
337         }
338
339         return 0;
340 }
341
342 static int __create_table(sqlite3 *db)
343 {
344         int r;
345
346         r = __exec(db, CREATE_RUA_STAT_TABLE);
347         if (r == -1) {
348                 LOGE("create table error");
349                 return -1;
350         }
351
352         return 0;
353 }
354
355 static sqlite3 *__db_init(char *root, int flags)
356 {
357         int r;
358         sqlite3 *db = NULL;
359
360         r = db_util_open_with_options(root, &db, flags, NULL);
361         if (r) {
362                 LOGE("db util open error(%d/%d/%d/%s)", r,
363                         sqlite3_errcode(db),
364                         sqlite3_extended_errcode(db),
365                         sqlite3_errmsg(db));
366                 return NULL;
367
368         }
369         r = __create_table(db);
370         if (r) {
371                 db_util_close(db);
372                 return NULL;
373         }
374
375         return db;
376 }