LIPH's C++ Codes
sqlite.h
Go to the documentation of this file.
1#ifndef LIPH_SQLITE_H_
2#define LIPH_SQLITE_H_
3
4#ifndef NOSQLITE
5
6#include <iostream>
7#include <string>
8#include <string_view>
9#include <vector>
10
11#include "liph/format.h"
12#include "sqlite-3.43.1/sqlite3.h"
13
14namespace liph {
15
16class sqlite {
17public:
18 sqlite() {}
19
20 sqlite(std::string_view filename) : db_(nullptr) {
21 int ret = sqlite3_open(filename.data(), &db_);
22 if (ret != SQLITE_OK) {
23 std::string msg = sqlite3_errmsg(db_);
24 throw std::runtime_error(format("{}: {}", msg, filename));
25 }
26 }
27
28 bool open(std::string_view filename) {
29 if (db_) return false;
30 return sqlite3_open(filename.data(), &db_) == SQLITE_OK;
31 }
32
33 bool is_open() const { return db_ != nullptr; }
34
36 if (is_open()) {
37 sqlite3_close(db_);
38 db_ = nullptr;
39 }
40 }
41
42 int execute(std::string_view sql) { return sqlite3_exec(db_, sql.data(), nullptr, nullptr, nullptr); }
43
44 // the first column returns the field name if the result is not empty
45 std::vector<std::vector<std::string>> query(std::string_view sql) const {
46 std::vector<std::vector<std::string>> result;
47 int ret = sqlite3_exec(
48 db_, sql.data(),
49 [](void *data, int num, char **fields, char **names) {
50 auto *result = (std::vector<std::vector<std::string>> *)data;
51 if (result->empty()) {
52 std::vector<std::string> name;
53 for (int i = 0; i < num; i++) {
54 if (names[i] != nullptr) {
55 name.emplace_back(names[i]);
56 } else {
57 name.emplace_back("");
58 }
59 }
60 result->emplace_back(name);
61 }
62 std::vector<std::string> field;
63 for (int i = 0; i < num; i++) {
64 if (fields[i] != nullptr) {
65 field.emplace_back(fields[i]);
66 } else {
67 field.emplace_back("");
68 }
69 }
70 result->emplace_back(field);
71 return 0;
72 },
73 &result, nullptr);
74
75 if (ret != SQLITE_OK) {
76 std::cerr << sqlite3_errmsg(db_) << std::endl;
77 }
78 return result;
79 }
80
81 sqlite3 *db() const { return db_; }
82
83private:
84 sqlite3 *db_{nullptr};
85};
86
88public:
89 prepared_statement(sqlite& db, const char *sql) : st(nullptr) {
90 int ret = sqlite3_prepare_v2(db.db(), sql, -1, &st, nullptr);
91 if (ret != SQLITE_OK) {
92 throw std::runtime_error(sqlite3_errmsg(db.db()));
93 }
94 }
95
97 if (st) {
98 sqlite3_finalize(st);
99 st = nullptr;
100 }
101 }
102
103 int reset() { return sqlite3_reset(st); }
104 int step() { return sqlite3_step(st); }
105 int column_int(int index) { return sqlite3_column_int(st, index); }
106 double column_double(int index) { return sqlite3_column_double(st, index); }
107 std::string column_text(int index) { return (char *)sqlite3_column_text(st, index); }
108
109private:
110 sqlite3_stmt *st{nullptr};
111};
112
113} // namespace liph
114
115#endif
116
117#endif // LIPH_SQLITE_H_
Definition: sqlite.h:87
prepared_statement(sqlite &db, const char *sql)
Definition: sqlite.h:89
int step()
Definition: sqlite.h:104
~prepared_statement()
Definition: sqlite.h:96
std::string column_text(int index)
Definition: sqlite.h:107
double column_double(int index)
Definition: sqlite.h:106
int column_int(int index)
Definition: sqlite.h:105
int reset()
Definition: sqlite.h:103
Definition: sqlite.h:16
std::vector< std::vector< std::string > > query(std::string_view sql) const
Definition: sqlite.h:45
sqlite3 * db() const
Definition: sqlite.h:81
sqlite()
Definition: sqlite.h:18
bool open(std::string_view filename)
Definition: sqlite.h:28
int execute(std::string_view sql)
Definition: sqlite.h:42
bool is_open() const
Definition: sqlite.h:33
sqlite(std::string_view filename)
Definition: sqlite.h:20
~sqlite()
Definition: sqlite.h:35
Definition: algorithm.h:10