1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
#include "database.h"
#include <sqlite3.h>
#include <stdexcept>
#include "query.h"
namespace verbly {
database::database(std::string path)
{
if (sqlite3_open_v2(path.c_str(), &ppdb_, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK)
{
// We still have to free the resources allocated. In the event that
// allocation failed, ppdb will be null and sqlite3_close_v2 will just
// ignore it.
std::string errmsg(sqlite3_errmsg(ppdb_));
sqlite3_close_v2(ppdb_);
throw database_error("Could not open verbly datafile", errmsg);
}
}
database::database(database&& other) : database()
{
swap(*this, other);
}
database& database::operator=(database&& other)
{
swap(*this, other);
return *this;
}
void swap(database& first, database& second)
{
std::swap(first.ppdb_, second.ppdb_);
}
database::~database()
{
sqlite3_close_v2(ppdb_);
}
query<notion> database::notions(filter where, order sortOrder, int limit) const
{
return query<notion>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
}
query<word> database::words(filter where, order sortOrder, int limit) const
{
return query<word>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
}
query<frame> database::frames(filter where, order sortOrder, int limit) const
{
return query<frame>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
}
query<part> database::parts(filter where, order sortOrder, int limit) const
{
return query<part>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
}
query<lemma> database::lemmas(filter where, order sortOrder, int limit) const
{
return query<lemma>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
}
query<form> database::forms(filter where, order sortOrder, int limit) const
{
return query<form>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
}
query<pronunciation> database::pronunciations(filter where, order sortOrder, int limit) const
{
return query<pronunciation>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
}
std::set<std::string> database::synrestrs(int partId) const
{
std::string queryString = "SELECT synrestr FROM synrestrs WHERE part_id = ?";
sqlite3_stmt* ppstmt;
if (sqlite3_prepare_v2(ppdb_, queryString.c_str(), queryString.length(), &ppstmt, NULL) != SQLITE_OK)
{
std::string errorMsg = sqlite3_errmsg(ppdb_);
sqlite3_finalize(ppstmt);
throw database_error("Error preparing query", errorMsg);
}
if (sqlite3_bind_int(ppstmt, 1, partId) != SQLITE_OK)
{
std::string errorMsg = sqlite3_errmsg(ppdb_);
sqlite3_finalize(ppstmt);
throw database_error("Error binding value to query", errorMsg);
}
std::set<std::string> result;
while (sqlite3_step(ppstmt) == SQLITE_ROW)
{
result.insert(reinterpret_cast<const char*>(sqlite3_column_blob(ppstmt, 0)));
}
sqlite3_finalize(ppstmt);
return result;
}
};
|