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 <dpl/test/test_runner.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(const Tracker& tracker)
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,
46 "DB: Error testing \"" << tracker.str() <<
47 "\" Sqlite3DBase: Error opening the database: Unable to allocate memory.");
48 RUNNER_ASSERT_MSG(ret == SQLITE_OK,
49 "DB: Error testing \"" << tracker.str() <<
50 "\" Sqlite3DBase: Error opening the database: " <<
51 sqlite3_errmsg(m_db_handle));
54 void Sqlite3DBase::close(const Tracker& tracker)
56 int ret = sqlite3_close(m_db_handle);
57 RUNNER_ASSERT_MSG(ret == SQLITE_OK,
58 "DB: Error testing \"" << tracker.str() <<
59 "\" Sqlite3DBase: Error closing the database: " <<
60 sqlite3_errmsg(m_db_handle));
65 bool Sqlite3DBase::is_open(void) const
70 void Sqlite3DBase::execute(const Tracker& tracker, const std::string& sql_querry,
71 Sqlite3DBaseSelectResult& result)
76 int ret = sqlite3_exec(m_db_handle, sql_querry.c_str(), callback, &result, &tmp);
82 RUNNER_ASSERT_MSG(ret == SQLITE_OK || ret == SQLITE_ABORT,
83 "DB: Error testing \"" << tracker.str() <<
84 "\" Sqlite3DBase: Error executing statement <" << sql_querry << "> : " <<
88 int Sqlite3DBase::callback(void* p_result, int cols, char** data, char** header)
91 Sqlite3DBaseSelectResult* result = static_cast<Sqlite3DBaseSelectResult*>(p_result);
93 // if this is first record get column names
94 if(result->rows.empty()) {
95 for(i = 0; i < cols; ++i) {
96 result->header.push_back(header[i] ? header[i] : "");
99 result->rows.push_back(Sqlite3Row());
100 for(i = 0; i < cols; ++i) {
101 result->rows.back().push_back(data[i] ? data[i] : "");