Q4Wine  2.0
Q4Wine is a Qt GUI for Wine.
db.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2008-2016 by Alexey S. Malakhov <brezerk@gmail.com> *
3  * *
4  * This program is free software: you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation, either version 3 of the License, or *
7  * (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License *
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
16  * *
17  ***************************************************************************/
18 
19 #pragma once
20 
21 #include <stdint.h>
22 #include <sqlite3.h>
23 #include <string>
24 #include <memory>
25 #include <iostream>
26 #include <initializer_list>
27 #include <typeinfo>
28 #include <algorithm>
29 #include <map>
30 #include <vector>
31 #include <utility>
32 
33 #include "src/lib/defines.hpp"
34 
35 namespace q4wine {
36 namespace lib {
37 
43 typedef std::map<std::string, std::string> result;
44 typedef std::pair<std::string, std::string> result_p;
50 typedef std::vector<result> rows;
51 
59 class DBEngine {
60  public:
69  static DBEngine* getInstance();
70 
80  bool open(std::string name);
81 
87  bool init(void);
88 
96  bool exec(const std::string& sql_s) const;
97 
104  bool exec(const std::string& sql_s,
105  const std::initializer_list<std::string>& args) const;
106 
112  rows select(const std::string& sql_s) const;
113 
121  rows select(const std::string& sql_s,
122  const std::initializer_list<std::string>& args) const;
123 
131  result select_one(const std::string& sql_s) const;
132 
142  result select_one(const std::string& sql_s,
143  const std::initializer_list<std::string>& args) const;
144 
149  intptr_t get_id(void) const;
150 
155  bool is_open(void) const;
156 
161  void close(void) const;
162 
163  protected:
165  DBEngine(DBEngine const&) = delete;
167  DBEngine& operator=(DBEngine const&) = delete;
169  DBEngine();
171  ~DBEngine();
172 
175 
176  private:
184  bool bind_args(sqlite3_stmt *stmt,
185  const std::string& sql_s,
186  const std::initializer_list<std::string>& args) const;
187  std::string name_;
188  sqlite3* db_;
189  bool is_open_;
190 };
191 
192 } // namespace lib
193 } // namespace q4wine
static DBEngine * DBEngine_instance
Definition: db.hpp:174
void close(void) const
Close database. Call this on app shutdown.
Definition: db.cpp:278
DBEngine()
Definition: db.cpp:33
std::map< std::string, std::string > result
A map that represents SQL result. The key is row name, the value is row value.
Definition: db.hpp:43
std::pair< std::string, std::string > result_p
Definition: db.hpp:44
static DBEngine * getInstance()
Return instance of DBEngine object. Check current instance. If it is NULL then create a new one...
Definition: db.cpp:28
result select_one(const std::string &sql_s) const
select Execute raw SQL statement and return the single result
Definition: db.cpp:219
bool init(void)
Itialize database. Create tables and default bootsrap data on first startup.
Definition: db.cpp:48
intptr_t get_id(void) const
Get last insert rowid.
Definition: db.cpp:266
Definition: db.cpp:23
rows select(const std::string &sql_s) const
Execute raw SQL statement and return the result.
Definition: db.cpp:170
bool is_open(void) const
Check if database already open.
Definition: db.cpp:274
std::vector< result > rows
An a vector of resuls.
Definition: db.hpp:50
bool exec(const std::string &sql_s) const
Execute raw SQL statement. Use this fuction to call raw SQL statements. If you need to bind values...
Definition: db.cpp:125
q4wine&#39;s SQLite3 engine warper. This class provides basic common SQL workflow, like making SQL querie...
Definition: db.hpp:59
bool open(std::string name)
Open SQLite database.
Definition: db.cpp:36
~DBEngine()
Definition: db.cpp:270
DBEngine & operator=(DBEngine const &)=delete