diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-01-28 12:59:42 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-01-28 12:59:42 -0500 |
commit | a7645346293ed6a912c26d0c50b6f7943f1f3072 (patch) | |
tree | d4d144e03a5e2dfcebbad2692fa71e790719d8fd /lib/database.cpp | |
parent | 6ba8989bbbd497f949a3e8b17abed1d0bd048347 (diff) | |
download | verbly-a7645346293ed6a912c26d0c50b6f7943f1f3072.tar.gz verbly-a7645346293ed6a912c26d0c50b6f7943f1f3072.tar.bz2 verbly-a7645346293ed6a912c26d0c50b6f7943f1f3072.zip |
Restructured verb frame schema to be more queryable
Groups are much less significant now, and they no longer have a database table, nor are they considered a top level object anymore. Instead of containing their own role data, that data is folded into the frames so that it's easier to query; as a result, each group has its own copy of the frames that it contains. Additionally, parts are considered top level objects now, and you can query for frames based on attributes of their indexed parts. Synrestrs are also contained in their own table now, so that parts can be filtered against their synrestrs; they are however not considered top level objects. Created a new type of field, the "join where" or "condition join" field, which is a normal join field that has a built in condition on a specified field. This is used to allow creating multiple distinct join fields from one object to another. This is required for the lemma::form and frame::part joins, because filters for forms of separate inflections should not be coalesced; similarly, filters on differently indexed frame parts should not be coalesced. Queries can now be ordered, ascending or descending, by a field, in addition to randomly as before. This is necessary for accessing the parts of a verb frame in the correct order, but may be useful to an end user as well. Fixed a bug with statement generation in that condition groups were not being surrounded in parentheses, which made mixing OR groups and AND groups generate inaccurate statements. This has been fixed; additionally, parentheses are not placed around the top level condition, and nested condition groups with the same logic type are coalesced, to make query strings as easy to read as possible. Also simplified the form::lemma field; it no longer conditions on the inflection of the form like the lemma::form field does. Also added a debug flag to statement::getQueryString that makes it return a query string with all of the bindings filled in, for debug use only.
Diffstat (limited to 'lib/database.cpp')
-rw-r--r-- | lib/database.cpp | 60 |
1 files changed, 46 insertions, 14 deletions
diff --git a/lib/database.cpp b/lib/database.cpp index fb00ef3..563ec31 100644 --- a/lib/database.cpp +++ b/lib/database.cpp | |||
@@ -41,39 +41,71 @@ namespace verbly { | |||
41 | sqlite3_close_v2(ppdb_); | 41 | sqlite3_close_v2(ppdb_); |
42 | } | 42 | } |
43 | 43 | ||
44 | query<notion> database::notions(filter where, bool random, int limit) const | 44 | query<notion> database::notions(filter where, order sortOrder, int limit) const |
45 | { | 45 | { |
46 | return query<notion>(*this, ppdb_, std::move(where), random, limit); | 46 | return query<notion>(*this, ppdb_, std::move(where), std::move(sortOrder), limit); |
47 | } | 47 | } |
48 | 48 | ||
49 | query<word> database::words(filter where, bool random, int limit) const | 49 | query<word> database::words(filter where, order sortOrder, int limit) const |
50 | { | 50 | { |
51 | return query<word>(*this, ppdb_, std::move(where), random, limit); | 51 | return query<word>(*this, ppdb_, std::move(where), std::move(sortOrder), limit); |
52 | } | 52 | } |
53 | 53 | ||
54 | query<group> database::groups(filter where, bool random, int limit) const | 54 | query<frame> database::frames(filter where, order sortOrder, int limit) const |
55 | { | 55 | { |
56 | return query<group>(*this, ppdb_, std::move(where), random, limit); | 56 | return query<frame>(*this, ppdb_, std::move(where), std::move(sortOrder), limit); |
57 | } | 57 | } |
58 | 58 | ||
59 | query<frame> database::frames(filter where, bool random, int limit) const | 59 | query<part> database::parts(filter where, order sortOrder, int limit) const |
60 | { | 60 | { |
61 | return query<frame>(*this, ppdb_, std::move(where), random, limit); | 61 | return query<part>(*this, ppdb_, std::move(where), std::move(sortOrder), limit); |
62 | } | 62 | } |
63 | 63 | ||
64 | query<lemma> database::lemmas(filter where, bool random, int limit) const | 64 | query<lemma> database::lemmas(filter where, order sortOrder, int limit) const |
65 | { | 65 | { |
66 | return query<lemma>(*this, ppdb_, std::move(where), random, limit); | 66 | return query<lemma>(*this, ppdb_, std::move(where), std::move(sortOrder), limit); |
67 | } | 67 | } |
68 | 68 | ||
69 | query<form> database::forms(filter where, bool random, int limit) const | 69 | query<form> database::forms(filter where, order sortOrder, int limit) const |
70 | { | 70 | { |
71 | return query<form>(*this, ppdb_, std::move(where), random, limit); | 71 | return query<form>(*this, ppdb_, std::move(where), std::move(sortOrder), limit); |
72 | } | 72 | } |
73 | 73 | ||
74 | query<pronunciation> database::pronunciations(filter where, bool random, int limit) const | 74 | query<pronunciation> database::pronunciations(filter where, order sortOrder, int limit) const |
75 | { | 75 | { |
76 | return query<pronunciation>(*this, ppdb_, std::move(where), random, limit); | 76 | return query<pronunciation>(*this, ppdb_, std::move(where), std::move(sortOrder), limit); |
77 | } | ||
78 | |||
79 | std::set<std::string> database::synrestrs(int partId) const | ||
80 | { | ||
81 | std::string queryString = "SELECT synrestr FROM synrestrs WHERE part_id = ?"; | ||
82 | |||
83 | sqlite3_stmt* ppstmt; | ||
84 | if (sqlite3_prepare_v2(ppdb_, queryString.c_str(), queryString.length(), &ppstmt, NULL) != SQLITE_OK) | ||
85 | { | ||
86 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
87 | sqlite3_finalize(ppstmt); | ||
88 | |||
89 | throw database_error("Error preparing query", errorMsg); | ||
90 | } | ||
91 | |||
92 | if (sqlite3_bind_int(ppstmt, 1, partId) != SQLITE_OK) | ||
93 | { | ||
94 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
95 | sqlite3_finalize(ppstmt); | ||
96 | |||
97 | throw database_error("Error binding value to query", errorMsg); | ||
98 | } | ||
99 | |||
100 | std::set<std::string> result; | ||
101 | while (sqlite3_step(ppstmt) == SQLITE_ROW) | ||
102 | { | ||
103 | result.insert(reinterpret_cast<const char*>(sqlite3_column_blob(ppstmt, 0))); | ||
104 | } | ||
105 | |||
106 | sqlite3_finalize(ppstmt); | ||
107 | |||
108 | return result; | ||
77 | } | 109 | } |
78 | 110 | ||
79 | }; | 111 | }; |