Merging tizen into ckm. Stage 1.
[platform/core/test/security-tests.git] / src / common / db_sqlite.cpp
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.cpp
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 #include <tests_common.h>
25 #include "db_sqlite.h"
26
27 Sqlite3DBase::Sqlite3DBase(const std::string& db_path, int flags)
28     : m_db_handle(nullptr), m_db_path(db_path), m_flags(flags)
29 {
30 }
31
32 Sqlite3DBase::~Sqlite3DBase()
33 {
34     sqlite3_close(m_db_handle);
35 }
36
37 #define VFS_NOT_USED    nullptr
38
39 void Sqlite3DBase::open(void)
40 {
41     if (m_db_handle) //database already opened
42         return;
43
44     int ret = sqlite3_open_v2(m_db_path.c_str(), &m_db_handle, m_flags, VFS_NOT_USED);
45     RUNNER_ASSERT_MSG(m_db_handle, "Error opening the database: Unable to allocate memory.");
46     RUNNER_ASSERT_MSG(ret == SQLITE_OK, "Error opening the database: " <<
47                          sqlite3_errmsg(m_db_handle));
48 }
49
50 void Sqlite3DBase::close(void)
51 {
52     int ret = sqlite3_close(m_db_handle);
53     RUNNER_ASSERT_MSG(ret == SQLITE_OK, "Error closing the database: " <<
54                          sqlite3_errmsg(m_db_handle));
55
56     m_db_handle = nullptr;
57 }
58
59 bool Sqlite3DBase::is_open(void) const
60 {
61     return !!m_db_handle;
62 }
63
64 void Sqlite3DBase::execute(const std::string& sql_query, Sqlite3DBaseSelectResult& result)
65 {
66     char* tmp = nullptr;
67     std::string errmsg;
68
69     int ret = sqlite3_exec(m_db_handle, sql_query.c_str(), callback, &result, &tmp);
70     if (tmp) {
71         errmsg.assign(tmp);
72     }
73     sqlite3_free(tmp);
74
75     RUNNER_ASSERT_MSG(ret == SQLITE_OK || ret == SQLITE_ABORT, "Error executing statement <" <<
76                          sql_query << "> : " << errmsg);
77 }
78
79 int Sqlite3DBase::callback(void* p_result, int cols, char** data, char** header)
80 {
81     int i;
82     Sqlite3DBaseSelectResult* result = static_cast<Sqlite3DBaseSelectResult*>(p_result);
83
84     // if this is first record get column names
85     if (result->rows.empty()) {
86         for (i = 0; i < cols; ++i) {
87             result->header.push_back(header[i] ? header[i] : "");
88         }
89     }
90     result->rows.push_back(Sqlite3Row());
91     for (i = 0; i < cols; ++i) {
92         result->rows.back().push_back(data[i] ? data[i] : "");
93     }
94     return SQLITE_OK;
95 }