diff options
Diffstat (limited to 'lib/database.cpp')
| -rw-r--r-- | lib/database.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
| diff --git a/lib/database.cpp b/lib/database.cpp new file mode 100644 index 0000000..351b93d --- /dev/null +++ b/lib/database.cpp | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | #include "database.h" | ||
| 2 | #include <sqlite3.h> | ||
| 3 | #include <stdexcept> | ||
| 4 | #include "query.h" | ||
| 5 | |||
| 6 | namespace verbly { | ||
| 7 | |||
| 8 | database::database(std::string path) | ||
| 9 | { | ||
| 10 | if (sqlite3_open_v2(path.c_str(), &ppdb_, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) | ||
| 11 | { | ||
| 12 | // We still have to free the resources allocated. In the event that | ||
| 13 | // allocation failed, ppdb will be null and sqlite3_close_v2 will just | ||
| 14 | // ignore it. | ||
| 15 | std::string errmsg(sqlite3_errmsg(ppdb_)); | ||
| 16 | sqlite3_close_v2(ppdb_); | ||
| 17 | |||
| 18 | throw database_error("Could not open verbly datafile", errmsg); | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | database::database(database&& other) : database() | ||
| 23 | { | ||
| 24 | swap(*this, other); | ||
| 25 | } | ||
| 26 | |||
| 27 | database& database::operator=(database&& other) | ||
| 28 | { | ||
| 29 | swap(*this, other); | ||
| 30 | |||
| 31 | return *this; | ||
| 32 | } | ||
| 33 | |||
| 34 | void swap(database& first, database& second) | ||
| 35 | { | ||
| 36 | std::swap(first.ppdb_, second.ppdb_); | ||
| 37 | } | ||
| 38 | |||
| 39 | database::~database() | ||
| 40 | { | ||
| 41 | sqlite3_close_v2(ppdb_); | ||
| 42 | } | ||
| 43 | |||
| 44 | query<notion> database::notions(filter where, bool random, int limit) const | ||
| 45 | { | ||
| 46 | return query<notion>(*this, ppdb_, std::move(where), random, limit); | ||
| 47 | } | ||
| 48 | |||
| 49 | query<word> database::words(filter where, bool random, int limit) const | ||
| 50 | { | ||
| 51 | return query<word>(*this, ppdb_, std::move(where), random, limit); | ||
| 52 | } | ||
| 53 | |||
| 54 | query<group> database::groups(filter where, bool random, int limit) const | ||
| 55 | { | ||
| 56 | return query<group>(*this, ppdb_, std::move(where), random, limit); | ||
| 57 | } | ||
| 58 | |||
| 59 | query<frame> database::frames(filter where, bool random, int limit) const | ||
| 60 | { | ||
| 61 | return query<frame>(*this, ppdb_, std::move(where), random, limit); | ||
| 62 | } | ||
| 63 | |||
| 64 | query<lemma> database::lemmas(filter where, bool random, int limit) const | ||
| 65 | { | ||
| 66 | return query<lemma>(*this, ppdb_, std::move(where), random, limit); | ||
| 67 | } | ||
| 68 | |||
| 69 | query<form> database::forms(filter where, bool random, int limit) const | ||
| 70 | { | ||
| 71 | return query<form>(*this, ppdb_, std::move(where), random, limit); | ||
| 72 | } | ||
| 73 | |||
| 74 | query<pronunciation> database::pronunciations(filter where, bool random, int limit) const | ||
| 75 | { | ||
| 76 | return query<pronunciation>(*this, ppdb_, std::move(where), random, limit); | ||
| 77 | } | ||
| 78 | |||
| 79 | }; | ||
