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_
33 * @def DB_SQLITE_READWRITE_FLAG
34 * @brief Sqlite3 flag set for opening database in RW mode
36 #define DB_SQLITE_READWRITE_FLAG SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_PRIVATECACHE \
37 | SQLITE_OPEN_READWRITE
40 * @def DB_SQLITE_READONLY_FLAG
41 * @brief Sqlite3 flag set for opening database in RO mode
43 #define DB_SQLITE_READONLY_FLAG SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_PRIVATECACHE \
44 | SQLITE_OPEN_READONLY
47 * @typedef Sqlite3HeaderName
48 * @brief Holds single column name of sqlite select querry response.
50 typedef std::string Sqlite3HeaderName;
53 * @typedef Sqlite3HeaderNameVector
54 * @brief Holds column names vector of sqlite select querry response.
56 typedef std::vector<Sqlite3HeaderName> Sqlite3HeaderNameVector;
59 * @typedef Sqlite3RowCell
60 * @brief Holds single cell of row of sqlite select querry response.
62 typedef std::string Sqlite3RowCell;
66 * @brief Holds single row of sqlite select querry response.
68 typedef std::vector<Sqlite3RowCell> Sqlite3Row;
71 * @typedef Sqlite3RowVector
72 * @brief Holds multiple rows of sqlite select querry response.
74 typedef std::vector<Sqlite3Row> Sqlite3RowVector;
77 * @class Sqlite3DBaseSelectResult
78 * @brief Sqlite3 select querry response.
80 * Fields are public as there is no complicated logic to operate on them
82 struct Sqlite3DBaseSelectResult
86 * @brief Sqlite select querry column names
88 Sqlite3HeaderNameVector header;
92 * @brief Sqlite select querry rows vector
94 Sqlite3RowVector rows;
100 * @brief Simple interface for executing select statements on sqlite3 database
102 * Fields are public as there is no complicated logic to operate on them
108 * @brief A constructor
110 * @param db_path path to database file
111 * @param flags sqlite3 flags defining database opening mode
112 * (default value DB_SQLITE_READONLY_FLAG)
114 Sqlite3DBase(const std::string& db_path, int flags = DB_SQLITE_READONLY_FLAG);
117 * @brief A destructor
122 * @brief Open database.
124 * If database is already opened do nothing.
126 * @param tracker make assertion comments if error more useful
128 * @throw DPL::Test::TestRunner::TestFailed when opening database fails
130 void open(const Tracker& tracker);
133 * @brief Close database.
135 * @param tracker make assertion comments if error more useful
137 * @throw DPL::Test::TestRunner::TestFailed when closing database fails
139 void close(const Tracker& tracker);
142 * @brief Get database connection status.
144 * @return true if database is open
145 * false if database is closed
147 bool is_open(void) const;
150 * @brief Execute SQL querry on database
152 * @param tracker make assertion comments if error more useful
153 * @param sql_querry SQL querry
154 * @param result returned result
156 * @throw DPL::Test::TestRunner::TestFailed when execution of querry fails
158 void execute(const Tracker& tracker, const std::string& sql_querry,
159 Sqlite3DBaseSelectResult& result);
164 * @brief Handle to sqlite3 database
166 * NULL when database not opened.
168 sqlite3* m_db_handle;
172 * @brief Path to database file
174 std::string m_db_path;
178 * @brief Sqlite3 flags defining database opening mode
183 * @brief Callback used to aquire results from SQL query
185 * It is run by sqlite for every row in query result.
187 * @param p_result pointer to private data (Sqlite3DBaseSelectResult)
188 * @param cols number of columns in SQL result
189 * @param data array of strings containing single row
190 * @param header array of strings containing column names
191 * @return SQLITE_OK as we always know what to do with data
193 static int callback(void* p_result, int cols, char** data, char** header);
196 #endif /* LIBPRIVILEGE_CONTROL_TEST_DB_SQLITE_H_ */