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 "ClientInfo.h"
25 unsigned int ClientInfo::__refCnt = 0;
26 DatabaseManager *ClientInfo::__dbMgr = NULL;
28 ClientInfo::ClientInfo()
35 __dbMgr = new(std::nothrow) DatabaseManager();
36 IF_FAIL_VOID_TAG(__dbMgr, _E, "Memory allocation failed");
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 ")" \
47 IF_FAIL_VOID_TAG(ret, _E, "Table creation failed");
50 ClientInfo::~ClientInfo()
59 int ClientInfo::get(std::string subject, std::string pkgId, Json& option)
61 IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
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());
71 ret = __dbMgr->executeSync(query, &records);
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);
83 int ClientInfo::get(std::string subject, std::vector<Json>& options)
85 IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
89 std::vector<Json> records;
90 char *query = sqlite3_mprintf(
91 "SELECT " KEY_OPTION " FROM " CLIENT_INFO " WHERE " \
95 ret = __dbMgr->executeSync(query, &records);
98 IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
99 IF_FAIL_RETURN(!records.empty(), ERR_NO_DATA);
101 for (auto jObj : records) {
102 if (!jObj.get(NULL, KEY_OPTION, &optStr))
104 options.push_back(Json(optStr));
110 bool ClientInfo::exist(std::string subject)
112 IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
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",
121 ret = __dbMgr->executeSync(query, &records);
124 IF_FAIL_RETURN(ret, false);
125 IF_FAIL_RETURN(!records.empty(), false);
130 bool ClientInfo::set(std::string subject, std::string pkgId, Json option, int retentionPeriod)
132 IF_FAIL_RETURN_TAG(__dbMgr, false, _W, "DB not initialized");
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);
139 ret = __dbMgr->executeSync(query, NULL);
145 bool ClientInfo::remove(std::string subject, std::string pkgId)
147 IF_FAIL_RETURN_TAG(__dbMgr, false, _W, "DB not initialized");
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());
155 ret = __dbMgr->executeSync(query, NULL);