Refactoring rua api
[platform/core/appfw/librua.git] / src / rua_stat.c
1 /*
2  * Copyright (c) 2000 - 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 /*
18  * @file    rua_stat.c
19  * @author  Hyunho Kang (hhstark.kang@samsung.com)
20  * @version 0.1
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <db-util.h>
28
29 #include "rua_stat_internal.h"
30 #include "rua_stat.h"
31
32 int rua_stat_get_stat_tags(char *caller,
33                 int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data),
34                 void *data)
35 {
36         int r;
37         sqlite3_stmt *stmt;
38         char query[QUERY_MAXLEN];
39         const unsigned char *ct;
40         sqlite3 *db = NULL;
41
42         r = _rua_stat_init(db, SQLITE_OPEN_READONLY);
43         if (r == -1) {
44                 LOGE("__rua_stat_init fail");
45                 return -1;
46         }
47
48         sqlite3_snprintf(QUERY_MAXLEN, query,
49                 "SELECT rua_stat_tag FROM rua_panel_stat WHERE caller_panel = ? ORDER BY score DESC");
50
51         r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
52         if (r != SQLITE_OK) {
53                 LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(db), sqlite3_errmsg(db));
54                 goto out;
55         }
56
57         r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
58         if (r != SQLITE_OK) {
59                 LOGE("caller bind error(%d) \n", r);
60                 goto out;
61         }
62
63         while (sqlite3_step(stmt) == SQLITE_ROW) {
64                 ct = sqlite3_column_text(stmt, 0);
65                 if (ct == NULL || ct[0] == '\0')
66                         LOGW("sqlite3_column_text null");
67
68                 rua_stat_tag_iter_fn((const char *)ct, data);
69         }
70
71 out:
72         if (stmt)
73                 sqlite3_finalize(stmt);
74
75         _rua_stat_fini(db);
76
77         return r;
78 }