Merge branch 'devel/tizen_3.0' into tizen
[platform/core/context/context-provider.git] / src / sensor / ClientInfo.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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 #include <sqlite3.h>
18 #include <Types.h>
19 #include <SensorRecorderTypes.h>
20 #include "TypesInternal.h"
21 #include "ClientInfo.h"
22
23 using namespace ctx;
24
25 unsigned int ClientInfo::__refCnt = 0;
26 DatabaseManager *ClientInfo::__dbMgr = NULL;
27
28 ClientInfo::ClientInfo()
29 {
30         ++__refCnt;
31
32         if (__dbMgr)
33                 return;
34
35         __dbMgr = new(std::nothrow) DatabaseManager();
36         IF_FAIL_VOID_TAG(__dbMgr, _E, "Memory allocation failed");
37
38         bool ret = __dbMgr->executeSync(
39                         "CREATE TABLE IF NOT EXISTS " CLIENT_INFO " (" \
40                                 KEY_SUBJECT " TEXT NOT NULL," \
41                                 KEY_PKG_ID " TEXT NOT NULL," \
42                                 KEY_OPTION " TEXT NOT NULL," \
43                                 KEY_RETENTION " INTEGER NOT NULL," \
44                                 "PRIMARY KEY (" KEY_SUBJECT "," KEY_PKG_ID ")" \
45                         ")", NULL);
46
47         IF_FAIL_VOID_TAG(ret, _E, "Table creation failed");
48 }
49
50 ClientInfo::~ClientInfo()
51 {
52         if (--__refCnt != 0)
53                 return;
54
55         delete __dbMgr;
56         __dbMgr = NULL;
57 }
58
59 int ClientInfo::get(std::string subject, std::string pkgId, Json& option)
60 {
61         IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
62
63         bool ret;
64         std::string optStr;
65         std::vector<Json> records;
66         char *query = sqlite3_mprintf(
67                         "SELECT " KEY_OPTION " FROM " CLIENT_INFO " WHERE " \
68                         KEY_SUBJECT "='%q' AND " KEY_PKG_ID "='%q'",
69                         subject.c_str(), pkgId.c_str());
70
71         ret = __dbMgr->executeSync(query, &records);
72         sqlite3_free(query);
73
74         IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
75         IF_FAIL_RETURN(!records.empty(), ERR_NO_DATA);
76         IF_FAIL_RETURN(records[0].get(NULL, KEY_OPTION, &optStr), ERR_OPERATION_FAILED);
77
78         option = optStr;
79
80         return ERR_NONE;
81 }
82
83 int ClientInfo::get(std::string subject, std::vector<Json>& options)
84 {
85         IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
86
87         bool ret;
88         std::string optStr;
89         std::vector<Json> records;
90         char *query = sqlite3_mprintf(
91                         "SELECT " KEY_OPTION " FROM " CLIENT_INFO " WHERE " \
92                         KEY_SUBJECT "='%q'",
93                         subject.c_str());
94
95         ret = __dbMgr->executeSync(query, &records);
96         sqlite3_free(query);
97
98         IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
99         IF_FAIL_RETURN(!records.empty(), ERR_NO_DATA);
100
101         for (auto jObj : records) {
102                 if (!jObj.get(NULL, KEY_OPTION, &optStr))
103                         continue;
104                 options.push_back(Json(optStr));
105         }
106
107         return ERR_NONE;
108 }
109
110 bool ClientInfo::exist(std::string subject)
111 {
112         IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
113
114         bool ret;
115         std::vector<Json> records;
116         char *query = sqlite3_mprintf(
117                         "SELECT " KEY_PKG_ID " FROM " CLIENT_INFO " WHERE " \
118                         KEY_SUBJECT "='%q' LIMIT 1",
119                         subject.c_str());
120
121         ret = __dbMgr->executeSync(query, &records);
122         sqlite3_free(query);
123
124         IF_FAIL_RETURN(ret, false);
125         IF_FAIL_RETURN(!records.empty(), false);
126
127         return true;
128 }
129
130 bool ClientInfo::set(std::string subject, std::string pkgId, Json option, int retentionPeriod)
131 {
132         IF_FAIL_RETURN_TAG(__dbMgr, false, _W, "DB not initialized");
133
134         bool ret;
135         char *query = sqlite3_mprintf(
136                         "INSERT INTO " CLIENT_INFO " VALUES ('%q', '%q', '%q', %d)",
137                         subject.c_str(), pkgId.c_str(), option.str().c_str(), retentionPeriod);
138
139         ret = __dbMgr->executeSync(query, NULL);
140         sqlite3_free(query);
141
142         return ret;
143 }
144
145 bool ClientInfo::remove(std::string subject, std::string pkgId)
146 {
147         IF_FAIL_RETURN_TAG(__dbMgr, false, _W, "DB not initialized");
148
149         bool ret;
150         char *query = sqlite3_mprintf(
151                         "DELETE FROM " CLIENT_INFO " WHERE " \
152                         KEY_SUBJECT "='%q' AND " KEY_PKG_ID "='%q'",
153                         subject.c_str(), pkgId.c_str());
154
155         ret = __dbMgr->executeSync(query, NULL);
156         sqlite3_free(query);
157
158         return ret;
159 }