summary refs log tree commit diff stats
path: root/lib/database.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/database.cpp')
-rw-r--r--lib/database.cpp123
1 files changed, 21 insertions, 102 deletions
diff --git a/lib/database.cpp b/lib/database.cpp index fe64763..96eed45 100644 --- a/lib/database.cpp +++ b/lib/database.cpp
@@ -1,48 +1,19 @@
1#include "database.h" 1#include "database.h"
2#include <sqlite3.h>
3#include <stdexcept>
4#include <sstream> 2#include <sstream>
5#include "query.h" 3#include "query.h"
6#include "version.h" 4#include "version.h"
7 5
8namespace verbly { 6namespace verbly {
9 7
10 database::database(std::string path) 8 database::database(
9 std::string path) :
10 ppdb_(std::move(path), hatkirby::dbmode::read)
11 { 11 {
12 if (sqlite3_open_v2(path.c_str(), &ppdb_, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) 12 hatkirby::row version =
13 { 13 ppdb_.queryFirst("SELECT major, minor FROM version");
14 // We still have to free the resources allocated. In the event that
15 // allocation failed, ppdb will be null and sqlite3_close_v2 will just
16 // ignore it.
17 std::string errmsg(sqlite3_errmsg(ppdb_));
18 sqlite3_close_v2(ppdb_);
19
20 throw database_error("Could not open verbly datafile", errmsg);
21 }
22
23 std::string queryString = "SELECT major, minor FROM version";
24
25 sqlite3_stmt* ppstmt;
26 if (sqlite3_prepare_v2(ppdb_, queryString.c_str(), queryString.length(), &ppstmt, NULL) != SQLITE_OK)
27 {
28 std::string errorMsg = sqlite3_errmsg(ppdb_);
29 sqlite3_finalize(ppstmt);
30
31 throw database_error("Error reading database version", errorMsg);
32 }
33
34 if (sqlite3_step(ppstmt) != SQLITE_ROW)
35 {
36 std::string errorMsg = sqlite3_errmsg(ppdb_);
37 sqlite3_finalize(ppstmt);
38
39 throw database_error("Error reading database version", errorMsg);
40 }
41 14
42 major_ = sqlite3_column_int(ppstmt, 0); 15 major_ = mpark::get<int>(version[0]);
43 minor_ = sqlite3_column_int(ppstmt, 1); 16 minor_ = mpark::get<int>(version[1]);
44
45 sqlite3_finalize(ppstmt);
46 17
47 if (major_ != DATABASE_MAJOR_VERSION) 18 if (major_ != DATABASE_MAJOR_VERSION)
48 { 19 {
@@ -50,28 +21,6 @@ namespace verbly {
50 } 21 }
51 } 22 }
52 23
53 database::database(database&& other) : database()
54 {
55 swap(*this, other);
56 }
57
58 database& database::operator=(database&& other)
59 {
60 swap(*this, other);
61
62 return *this;
63 }
64
65 void swap(database& first, database& second)
66 {
67 std::swap(first.ppdb_, second.ppdb_);
68 }
69
70 database::~database()
71 {
72 sqlite3_close_v2(ppdb_);
73 }
74
75 query<notion> database::notions(filter where, order sortOrder, int limit) const 24 query<notion> database::notions(filter where, order sortOrder, int limit) const
76 { 25 {
77 return query<notion>(*this, ppdb_, std::move(where), std::move(sortOrder), limit); 26 return query<notion>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
@@ -104,65 +53,35 @@ namespace verbly {
104 53
105 std::set<std::string> database::selrestrs(int partId) const 54 std::set<std::string> database::selrestrs(int partId) const
106 { 55 {
107 std::string queryString = "SELECT selrestr FROM selrestrs WHERE part_id = ?"; 56 std::vector<hatkirby::row> rows =
108 57 ppdb_.queryAll(
109 sqlite3_stmt* ppstmt; 58 "SELECT selrestr FROM selrestrs WHERE part_id = ?",
110 if (sqlite3_prepare_v2(ppdb_, queryString.c_str(), queryString.length(), &ppstmt, NULL) != SQLITE_OK) 59 { partId });
111 {
112 std::string errorMsg = sqlite3_errmsg(ppdb_);
113 sqlite3_finalize(ppstmt);
114
115 throw database_error("Error preparing query", errorMsg);
116 }
117
118 if (sqlite3_bind_int(ppstmt, 1, partId) != SQLITE_OK)
119 {
120 std::string errorMsg = sqlite3_errmsg(ppdb_);
121 sqlite3_finalize(ppstmt);
122
123 throw database_error("Error binding value to query", errorMsg);
124 }
125 60
126 std::set<std::string> result; 61 std::set<std::string> result;
127 while (sqlite3_step(ppstmt) == SQLITE_ROW) 62
63 for (hatkirby::row& r : rows)
128 { 64 {
129 result.insert(reinterpret_cast<const char*>(sqlite3_column_blob(ppstmt, 0))); 65 result.emplace(std::move(mpark::get<std::string>(r[0])));
130 } 66 }
131 67
132 sqlite3_finalize(ppstmt);
133
134 return result; 68 return result;
135 } 69 }
136 70
137 std::set<std::string> database::synrestrs(int partId) const 71 std::set<std::string> database::synrestrs(int partId) const
138 { 72 {
139 std::string queryString = "SELECT synrestr FROM synrestrs WHERE part_id = ?"; 73 std::vector<hatkirby::row> rows =
140 74 ppdb_.queryAll(
141 sqlite3_stmt* ppstmt; 75 "SELECT synrestr FROM synrestrs WHERE part_id = ?",
142 if (sqlite3_prepare_v2(ppdb_, queryString.c_str(), queryString.length(), &ppstmt, NULL) != SQLITE_OK) 76 { partId });
143 {
144 std::string errorMsg = sqlite3_errmsg(ppdb_);
145 sqlite3_finalize(ppstmt);
146
147 throw database_error("Error preparing query", errorMsg);
148 }
149
150 if (sqlite3_bind_int(ppstmt, 1, partId) != SQLITE_OK)
151 {
152 std::string errorMsg = sqlite3_errmsg(ppdb_);
153 sqlite3_finalize(ppstmt);
154
155 throw database_error("Error binding value to query", errorMsg);
156 }
157 77
158 std::set<std::string> result; 78 std::set<std::string> result;
159 while (sqlite3_step(ppstmt) == SQLITE_ROW) 79
80 for (hatkirby::row& r : rows)
160 { 81 {
161 result.insert(reinterpret_cast<const char*>(sqlite3_column_blob(ppstmt, 0))); 82 result.emplace(std::move(mpark::get<std::string>(r[0])));
162 } 83 }
163 84
164 sqlite3_finalize(ppstmt);
165
166 return result; 85 return result;
167 } 86 }
168 87