Dynamic configuration stored in a database
[archive/platform/core/system/libConfig.git] / src / config / sqlite3 / connection.cpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Jan Olszak <j.olszak@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19 /**
20  * @file
21  * @author Jan Olszak (j.olszak@samsung.com)
22  * @brief  Definition of the class managing a sqlite3 database connection
23  */
24
25
26 #include "config/sqlite3/connection.hpp"
27 #include "config/exception.hpp"
28
29 namespace config {
30 namespace sqlite3 {
31
32 Connection::Connection(const std::string& path)
33 {
34     if (::sqlite3_open_v2(path.c_str(),
35                           &mDbPtr,
36                           SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
37                           NULL) != SQLITE_OK) {
38         throw ConfigException("Error opening the database: " + getErrorMessage());
39     }
40
41     if (mDbPtr == NULL) {
42         throw ConfigException("Error opening the database: Unable to allocate memory.");
43     }
44 }
45
46 Connection::~Connection()
47 {
48     if (::sqlite3_close(mDbPtr) != SQLITE_OK) {
49         throw ConfigException("Error during closing the database. Error: " + getErrorMessage());
50     }
51 }
52
53 void Connection::exec(const std::string& query)
54 {
55     char* mess;
56     if (::sqlite3_exec(mDbPtr, query.c_str(), 0, 0, &mess) != SQLITE_OK) {
57         throw ConfigException("Error during executing statement " + std::string(mess));
58     }
59 }
60
61 ::sqlite3* Connection::get()
62 {
63     return mDbPtr;
64 }
65
66 std::string Connection::getErrorMessage()
67 {
68     return std::string(sqlite3_errmsg(mDbPtr));
69 }
70
71 } // namespace sqlite3
72 } // namespace config