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.cpp
19 * @author Lukasz Wojciechowski (l.wojciechow@partner.samsung.com)
21 * @brief libprivilege-control tests API for sqlite3 database access
24 #include <tests_common.h>
25 #include "db_sqlite.h"
27 Sqlite3DBase::Sqlite3DBase(const std::string& db_path, int flags)
28 : m_db_handle(NULL), m_db_path(db_path), m_flags(flags)
32 Sqlite3DBase::~Sqlite3DBase()
34 sqlite3_close(m_db_handle);
37 #define VFS_NOT_USED NULL
39 void Sqlite3DBase::open(void)
41 if (m_db_handle) //database already opened
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));
50 void Sqlite3DBase::close(void)
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));
59 bool Sqlite3DBase::is_open(void) const
64 void Sqlite3DBase::execute(const std::string& sql_query, Sqlite3DBaseSelectResult& result)
69 int ret = sqlite3_exec(m_db_handle, sql_query.c_str(), callback, &result, &tmp);
75 RUNNER_ASSERT_MSG(ret == SQLITE_OK || ret == SQLITE_ABORT, "Error executing statement <" <<
76 sql_query << "> : " << errmsg);
79 int Sqlite3DBase::callback(void* p_result, int cols, char** data, char** header)
82 Sqlite3DBaseSelectResult* result = static_cast<Sqlite3DBaseSelectResult*>(p_result);
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] : "");
90 result->rows.push_back(Sqlite3Row());
91 for (i = 0; i < cols; ++i) {
92 result->rows.back().push_back(data[i] ? data[i] : "");