2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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.
18 * @file libprivilege-control_test_db_sqlite.h
19 * @author Lukasz Wojciechowski (l.wojciechow@partner.samsung.com)
21 * @brief libprivilege-control tests API for sqlite3 database access
24 #ifndef LIBPRIVILEGE_CONTROL_TEST_DB_SQLITE_H_
25 #define LIBPRIVILEGE_CONTROL_TEST_DB_SQLITE_H_
32 * @def DB_SQLITE_READONLY_FLAG
33 * @brief Sqlite3 flag set for opening database in RO mode
35 #define DB_SQLITE_READONLY_FLAG SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_PRIVATECACHE \
36 | SQLITE_OPEN_READONLY
39 * @typedef Sqlite3HeaderName
40 * @brief Holds single column name of sqlite select query response.
42 typedef std::string Sqlite3HeaderName;
45 * @typedef Sqlite3HeaderNameVector
46 * @brief Holds column names vector of sqlite select query response.
48 typedef std::vector<Sqlite3HeaderName> Sqlite3HeaderNameVector;
51 * @typedef Sqlite3RowCell
52 * @brief Holds single cell of row of sqlite select query response.
54 typedef std::string Sqlite3RowCell;
58 * @brief Holds single row of sqlite select query response.
60 typedef std::vector<Sqlite3RowCell> Sqlite3Row;
63 * @typedef Sqlite3RowVector
64 * @brief Holds multiple rows of sqlite select query response.
66 typedef std::vector<Sqlite3Row> Sqlite3RowVector;
69 * @class Sqlite3DBaseSelectResult
70 * @brief Sqlite3 select query response.
72 * Fields are public as there is no complicated logic to operate on them
74 struct Sqlite3DBaseSelectResult
78 * @brief Sqlite select query column names
80 Sqlite3HeaderNameVector header;
84 * @brief Sqlite select query rows vector
86 Sqlite3RowVector rows;
92 * @brief Simple interface for executing select statements on sqlite3 database
94 * Fields are public as there is no complicated logic to operate on them
100 * @brief A constructor
102 * @param db_path path to database file
103 * @param flags sqlite3 flags defining database opening mode
104 * (default value DB_SQLITE_READONLY_FLAG)
106 Sqlite3DBase(const std::string& db_path, int flags = DB_SQLITE_READONLY_FLAG);
109 * @brief A destructor
114 * @brief Open database.
116 * If database is already opened do nothing.
118 * @throw DPL::Test::TestRunner::TestFailed when opening database fails
123 * @brief Close database.
125 * @throw DPL::Test::TestRunner::TestFailed when closing database fails
130 * @brief Get database connection status.
132 * @return true if database is open
133 * false if database is closed
135 bool is_open(void) const;
138 * @brief Execute SQL query on database
140 * @param sql_query SQL query
141 * @param result returned result
143 * @throw DPL::Test::TestRunner::TestFailed when execution of query fails
145 void execute(const std::string& sql_query, Sqlite3DBaseSelectResult& result);
150 * @brief Handle to sqlite3 database
152 * nullptr when database not opened.
154 sqlite3* m_db_handle;
158 * @brief Path to database file
160 std::string m_db_path;
164 * @brief Sqlite3 flags defining database opening mode
169 * @brief Callback used to aquire results from SQL query
171 * It is run by sqlite for every row in query result.
173 * @param p_result pointer to private data (Sqlite3DBaseSelectResult)
174 * @param cols number of columns in SQL result
175 * @param data array of strings containing single row
176 * @param header array of strings containing column names
177 * @return SQLITE_OK as we always know what to do with data
179 static int callback(void* p_result, int cols, char** data, char** header);
182 #endif /* LIBPRIVILEGE_CONTROL_TEST_DB_SQLITE_H_ */