2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * @author Jan Olszak (j.olszak@samsung.com)
22 * @brief Declaration of a class for key-value storage in a sqlite3 database
25 #ifndef COMMON_CONFIG_KVSTORE_HPP
26 #define COMMON_CONFIG_KVSTORE_HPP
28 #include "config/sqlite3/connection.hpp"
29 #include "config/sqlite3/statement.hpp"
32 #include <initializer_list>
46 * @param path configuration database file path
48 KVStore(const std::string& path);
49 KVStore(const KVStore& store);
53 * Clears all the stored data
58 * @return Number of all stored values
63 * @param key string regexp of the stored values
65 * @return Number of values corresponding to the passed key
67 unsigned int count(const std::string& key);
70 * Removes values corresponding to the passed key.
71 * Many values may correspond to one key, so many values may
74 * @param key string regexp of the stored values
76 void remove(const std::string& key);
79 * Stores a single value corresponding to the passed key
81 * @param key string key of the value
82 * @param value value corresponding to the key
85 void set(const std::string& key, const T& value)
87 return setInternal(key, value);
91 * Gets the value corresponding to the key.
92 * Uses stringstreams to parse.
94 * @param key string key of the value
95 * @tparam T = std::string desired type of the return value
96 * @return value corresponding to the key
98 template<typename T = std::string>
99 T get(const std::string& key)
101 return getInternal(key, static_cast<T*>(nullptr));
106 typedef std::lock_guard<std::mutex> Lock;
108 void setInternal(const std::string& key, const std::string& value);
109 void setInternal(const std::string& key, const std::initializer_list<std::string>& values);
110 void setInternal(const std::string& key, const std::vector<std::string>& values);
112 void setInternal(const std::string& key, const T& value);
114 void setInternal(const std::string& key, const std::vector<T>& values);
116 std::string getInternal(const std::string& key, std::string*);
117 std::vector<std::string> getInternal(const std::string& key, std::vector<std::string>*);
119 T getInternal(const std::string& key, T*);
121 std::vector<T> getInternal(const std::string& key, std::vector<T>*);
126 sqlite3::Connection mConn;
127 std::unique_ptr<sqlite3::Statement> mGetValueStmt;
128 std::unique_ptr<sqlite3::Statement> mGetValueCountStmt;
129 std::unique_ptr<sqlite3::Statement> mGetSizeStmt;
130 std::unique_ptr<sqlite3::Statement> mGetValueListStmt;
131 std::unique_ptr<sqlite3::Statement> mSetValueStmt;
132 std::unique_ptr<sqlite3::Statement> mRemoveValuesStmt;
135 void prepareStatements();
137 void removeInternal(const std::string& key);
138 unsigned int countInternal(const std::string& key);
144 std::string toString(const T& value)
146 std::ostringstream oss;
152 T fromString(const std::string& strValue)
154 std::istringstream iss(strValue);
163 void KVStore::setInternal(const std::string& key, const T& value)
165 setInternal(key, toString(value));
169 void KVStore::setInternal(const std::string& key, const std::vector<T>& values)
171 std::vector<std::string> strValues(values.size());
173 std::transform(values.begin(),
178 setInternal(key, strValues);
182 T KVStore::getInternal(const std::string& key, T*)
184 return fromString<T>(getInternal(key, static_cast<std::string*>(nullptr)));
188 std::vector<T> KVStore::getInternal(const std::string& key, std::vector<T>*)
190 std::vector<std::string> strValues = getInternal(key, static_cast<std::vector<std::string>*>(nullptr));
191 std::vector<T> values(strValues.size());
193 std::transform(strValues.begin(),
202 * Concatenates all parameters into one std::string.
203 * Uses '.' to connect the terms.
204 * @param args components of the string
205 * @tparam delim optional delimiter
206 * @tparam typename ... Args any type implementing str
207 * @return string created from he args
209 template<char delim = '.', typename Arg1, typename ... Args>
210 std::string key(const Arg1& a1, const Args& ... args)
212 std::string ret = toString(a1);
213 std::initializer_list<std::string> strings {toString(args)...};
214 for (const std::string& s : strings) {
222 * Function added for key function completeness.
224 * @tparam delim = '.' parameter not used, added for consistency
225 * @return empty string
227 template<char delim = '.'>
230 return std::string();
233 } // namespace config
235 #endif // COMMON_CONFIG_KVSTORE_HPP