2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <SensorRecorderTypes.h>
20 #include "TypesInternal.h"
21 #include "SensorProvider.h"
22 #include "ClientInfo.h"
26 unsigned int ClientInfo::__refCnt = 0;
27 DatabaseManager *ClientInfo::__dbMgr = NULL;
28 UninstallMonitor *ClientInfo::__uninstallMonitor = NULL;
30 ClientInfo::ClientInfo()
35 __uninstallMonitor = new(std::nothrow) UninstallMonitor();
36 IF_FAIL_VOID_TAG(__uninstallMonitor, _E, "Memory allocation failed");
38 __dbMgr = new(std::nothrow) DatabaseManager();
39 IF_FAIL_VOID_TAG(__dbMgr, _E, "Memory allocation failed");
41 bool ret = __dbMgr->executeSync(
42 "CREATE TABLE IF NOT EXISTS " CLIENT_INFO " (" \
43 KEY_SUBJECT " TEXT NOT NULL," \
44 KEY_PKG_ID " TEXT NOT NULL," \
45 KEY_OPTION " TEXT NOT NULL," \
46 KEY_RETENTION " INTEGER NOT NULL," \
47 "PRIMARY KEY (" KEY_SUBJECT "," KEY_PKG_ID ")" \
50 IF_FAIL_VOID_TAG(ret, _E, "Table creation failed");
53 ClientInfo::~ClientInfo()
61 delete __uninstallMonitor;
62 __uninstallMonitor = NULL;
65 int ClientInfo::get(std::string subject, std::string pkgId, Json& option)
67 IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
71 std::vector<Json> records;
72 char *query = sqlite3_mprintf(
73 "SELECT " KEY_OPTION " FROM " CLIENT_INFO " WHERE " \
74 KEY_SUBJECT "='%q' AND " KEY_PKG_ID "='%q'",
75 subject.c_str(), pkgId.c_str());
77 ret = __dbMgr->executeSync(query, &records);
80 IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
81 IF_FAIL_RETURN(!records.empty(), ERR_NO_DATA);
82 IF_FAIL_RETURN(records[0].get(NULL, KEY_OPTION, &optStr), ERR_OPERATION_FAILED);
89 int ClientInfo::get(std::string subject, std::vector<Json>& options)
91 IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
95 std::vector<Json> records;
96 char *query = sqlite3_mprintf(
97 "SELECT " KEY_OPTION " FROM " CLIENT_INFO " WHERE " \
101 ret = __dbMgr->executeSync(query, &records);
104 IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
105 IF_FAIL_RETURN(!records.empty(), ERR_NO_DATA);
107 for (Json& jObj : records) {
108 if (!jObj.get(NULL, KEY_OPTION, &optStr))
110 options.push_back(Json(optStr));
116 bool ClientInfo::exist(std::string subject)
118 IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
121 std::vector<Json> records;
122 char *query = sqlite3_mprintf(
123 "SELECT " KEY_PKG_ID " FROM " CLIENT_INFO " WHERE " \
124 KEY_SUBJECT "='%q' LIMIT 1",
127 ret = __dbMgr->executeSync(query, &records);
130 IF_FAIL_RETURN(ret, false);
131 IF_FAIL_RETURN(!records.empty(), false);
136 bool ClientInfo::set(std::string subject, std::string pkgId, Json option, int retentionPeriod)
138 IF_FAIL_RETURN_TAG(__dbMgr, false, _W, "DB not initialized");
141 char *query = sqlite3_mprintf(
142 "INSERT INTO " CLIENT_INFO " VALUES ('%q', '%q', '%q', %d)",
143 subject.c_str(), pkgId.c_str(), option.str().c_str(), retentionPeriod);
145 ret = __dbMgr->executeSync(query, NULL);
151 bool ClientInfo::remove(std::string subject, std::string pkgId)
153 IF_FAIL_RETURN_TAG(__dbMgr, false, _W, "DB not initialized");
156 char *query = sqlite3_mprintf(
157 "DELETE FROM " CLIENT_INFO " WHERE " \
158 KEY_SUBJECT "='%q' AND " KEY_PKG_ID "='%q'",
159 subject.c_str(), pkgId.c_str());
161 ret = __dbMgr->executeSync(query, NULL);
167 void ClientInfo::purgeClient(std::string pkgId)
169 IF_FAIL_VOID_TAG(__dbMgr, _W, "DB not initialized");
173 std::vector<Json> records;
175 char *query = sqlite3_mprintf(
176 "SELECT " KEY_SUBJECT " FROM " CLIENT_INFO " WHERE " KEY_PKG_ID "='%q'",
179 ret = __dbMgr->executeSync(query, &records);
183 for (Json& jObj : records) {
184 if (!jObj.get(NULL, KEY_SUBJECT, &subject))
186 _I("Stop recording '%s' for '%s'", subject.c_str(), pkgId.c_str());
187 SensorProvider::removeClient(subject, pkgId);