Merging tizen into ckm. Stage 1.
[platform/core/test/security-tests.git] / src / common / db_sqlite.h
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15 */
16
17 /*
18  * @file        libprivilege-control_test_db_sqlite.h
19  * @author      Lukasz Wojciechowski (l.wojciechow@partner.samsung.com)
20  * @version     1.0
21  * @brief       libprivilege-control tests API for sqlite3 database access
22  */
23
24 #ifndef LIBPRIVILEGE_CONTROL_TEST_DB_SQLITE_H_
25 #define LIBPRIVILEGE_CONTROL_TEST_DB_SQLITE_H_
26
27 #include <vector>
28 #include <string>
29 #include <sqlite3.h>
30
31 /**
32  * @def         DB_SQLITE_READONLY_FLAG
33  * @brief       Sqlite3 flag set for opening database in RO mode
34  */
35 #define DB_SQLITE_READONLY_FLAG       SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_PRIVATECACHE \
36                                       | SQLITE_OPEN_READONLY
37
38 /**
39  * @typedef     Sqlite3HeaderName
40  * @brief       Holds single column name of sqlite select query response.
41  */
42 typedef std::string Sqlite3HeaderName;
43
44 /**
45  * @typedef     Sqlite3HeaderNameVector
46  * @brief       Holds column names vector of sqlite select query response.
47  */
48 typedef std::vector<Sqlite3HeaderName> Sqlite3HeaderNameVector;
49
50 /**
51  * @typedef     Sqlite3RowCell
52  * @brief       Holds single cell of row of sqlite select query response.
53  */
54 typedef std::string Sqlite3RowCell;
55
56 /**
57  * @typedef     Sqlite3Row
58  * @brief       Holds single row of sqlite select query response.
59  */
60 typedef std::vector<Sqlite3RowCell> Sqlite3Row;
61
62 /**
63  * @typedef     Sqlite3RowVector
64  * @brief       Holds multiple rows of sqlite select query response.
65  */
66 typedef std::vector<Sqlite3Row> Sqlite3RowVector;
67
68 /**
69  * @class       Sqlite3DBaseSelectResult
70  * @brief       Sqlite3 select query response.
71  *
72  * Fields are public as there is no complicated logic to operate on them
73  */
74 struct Sqlite3DBaseSelectResult
75 {
76 /**
77  * @var         header
78  * @brief       Sqlite select query column names
79  */
80     Sqlite3HeaderNameVector header;
81
82 /**
83  * @var         rows
84  * @brief       Sqlite select query rows vector
85  */
86     Sqlite3RowVector rows;
87 };
88
89
90 /**
91  * @class       Sqlite3DBase
92  * @brief       Simple interface for executing select statements on sqlite3 database
93  *
94  * Fields are public as there is no complicated logic to operate on them
95  */
96 class Sqlite3DBase
97 {
98 public:
99 /**
100  * @brief A constructor
101  *
102  * @param db_path       path to database file
103  * @param flags         sqlite3 flags defining database opening mode
104  *                      (default value DB_SQLITE_READONLY_FLAG)
105  */
106     Sqlite3DBase(const std::string& db_path, int flags = DB_SQLITE_READONLY_FLAG);
107
108 /**
109  * @brief A destructor
110  */
111     ~Sqlite3DBase();
112
113 /**
114  * @brief Open database.
115  *
116  * If database is already opened do nothing.
117  *
118  * @throw DPL::Test::TestRunner::TestFailed    when opening database fails
119  */
120     void open(void);
121
122 /**
123  * @brief Close database.
124  *
125  * @throw DPL::Test::TestRunner::TestFailed    when closing database fails
126  */
127     void close(void);
128
129 /**
130  * @brief Get database connection status.
131  *
132  * @return      true     if database is open
133  *              false    if database is closed
134  */
135     bool is_open(void) const;
136
137 /**
138  * @brief Execute SQL query on database
139  *
140  * @param sql_query     SQL query
141  * @param result        returned result
142  *
143  * @throw DPL::Test::TestRunner::TestFailed    when execution of query fails
144  */
145     void execute(const std::string& sql_query, Sqlite3DBaseSelectResult& result);
146
147 private:
148 /**
149  * @var db_handle
150  * @brief Handle to sqlite3 database
151  *
152  * nullptr when database not opened.
153  */
154     sqlite3* m_db_handle;
155
156 /**
157  * @var db_path
158  * @brief Path to database file
159  */
160     std::string m_db_path;
161
162 /**
163  * @var flags
164  * @brief Sqlite3 flags defining database opening mode
165  */
166     int m_flags;
167
168 /**
169  * @brief Callback used to aquire results from SQL query
170  *
171  * It is run by sqlite for every row in query result.
172  *
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
178  */
179     static int callback(void* p_result, int cols, char** data, char** header);
180 };
181
182 #endif /* LIBPRIVILEGE_CONTROL_TEST_DB_SQLITE_H_ */