795a08f40cf2440c8049a80c49192cb5caf6171e
[platform/upstream/csr-framework.git] / src / framework / db / connection.h
1 /*
2  *  Copyright (c) 2016 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 #pragma once
17
18 #include <string>
19
20 #include <sqlite3.h>
21
22 namespace Csr {
23 namespace Db {
24
25 class Connection {
26 public:
27         enum Mode {
28                 Create = SQLITE_OPEN_CREATE,
29                 ReadWrite = SQLITE_OPEN_READWRITE,
30                 MultiThread = SQLITE_OPEN_NOMUTEX,
31                 Serialized = SQLITE_OPEN_FULLMUTEX
32         };
33
34         Connection(const std::string &name, const int flags);
35         virtual ~Connection();
36
37         int exec(const std::string &query);
38
39         inline long long getLastInsertRowId() const noexcept
40         {
41                 return sqlite3_last_insert_rowid(m_handle);
42         }
43
44         inline const std::string &getName() const noexcept
45         {
46                 return m_filename;
47         }
48
49         inline int getErrorCode() const
50         {
51                 return sqlite3_errcode(m_handle);
52         }
53
54         inline int getExtendedErrorCode() const
55         {
56                 return sqlite3_extended_errcode(m_handle);
57         }
58
59         inline std::string getErrorMessage() const
60         {
61                 return sqlite3_errmsg(m_handle);
62         }
63
64         inline sqlite3 *get() const noexcept
65         {
66                 return m_handle;
67         }
68
69 private:
70         sqlite3 *m_handle;
71         std::string m_filename;
72 };
73
74 } // namespace Db
75 } // namespace Csr