diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-02-06 20:58:37 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-02-06 20:58:37 -0500 |
commit | f1f67e62cebb4144f0599196263cd93b41fa972e (patch) | |
tree | 5d0f8fceeec63f47fb85846f5568bcb36f4a975f /lib/field.h | |
parent | 6cc23ba387d0813b801ba094709673a61bac889c (diff) | |
download | verbly-f1f67e62cebb4144f0599196263cd93b41fa972e.tar.gz verbly-f1f67e62cebb4144f0599196263cd93b41fa972e.tar.bz2 verbly-f1f67e62cebb4144f0599196263cd93b41fa972e.zip |
Made pronunciation::rhymes join dynamic
This involved adding a new type of filter; one that compares (currently only equality and inequality) a field with another field located in an enclosing join context. In the process, it was discovered that simplifying the lemma::forms join field earlier actually made some queries return inaccurate results because the inflection of the form was being ignored and anything in the lemma would be used because of the inner join. Because the existing condition join did not allow for the condition field to be on the from side of the join, two things were done: a condition version of joinThrough was made, and lemma was finally eliminated as a top-level object, replaced instead with a condition join between word and form through lemmas_forms. Queries are also now grouped by the first select field (assumed to be the primary ID) of the top table, in order to eliminate duplicates created by inner joins, so that there is a uniform distribution between results for random queries. Created a database index on pronunciations(rhyme) which decreases query time for rhyming filters. The new database version is backwards-compatible because no data or structure changed.
Diffstat (limited to 'lib/field.h')
-rw-r--r-- | lib/field.h | 61 |
1 files changed, 33 insertions, 28 deletions
diff --git a/lib/field.h b/lib/field.h index bec0618..f799900 100644 --- a/lib/field.h +++ b/lib/field.h | |||
@@ -19,6 +19,7 @@ namespace verbly { | |||
19 | join, | 19 | join, |
20 | join_where, | 20 | join_where, |
21 | join_through, | 21 | join_through, |
22 | join_through_where, | ||
22 | hierarchal_join | 23 | hierarchal_join |
23 | }; | 24 | }; |
24 | 25 | ||
@@ -100,11 +101,11 @@ namespace verbly { | |||
100 | object obj, | 101 | object obj, |
101 | const char* name, | 102 | const char* name, |
102 | object joinWith, | 103 | object joinWith, |
103 | const field& conditionField, | 104 | const char* conditionColumn, |
104 | int conditionValue, | 105 | int conditionValue, |
105 | bool nullable = false) | 106 | bool nullable = false) |
106 | { | 107 | { |
107 | return field(obj, type::join_where, name, nullable, 0, joinWith, 0, 0, 0, &conditionField, conditionValue); | 108 | return field(obj, type::join_where, name, nullable, 0, joinWith, 0, 0, 0, conditionColumn, conditionValue); |
108 | } | 109 | } |
109 | 110 | ||
110 | static field joinThrough( | 111 | static field joinThrough( |
@@ -129,6 +130,19 @@ namespace verbly { | |||
129 | return field(obj, type::join_through, name, true, joinTable, joinWith, foreignColumn, joinColumn, foreignJoinColumn); | 130 | return field(obj, type::join_through, name, true, joinTable, joinWith, foreignColumn, joinColumn, foreignJoinColumn); |
130 | } | 131 | } |
131 | 132 | ||
133 | static field joinThroughWhere( | ||
134 | object obj, | ||
135 | const char* name, | ||
136 | object joinWith, | ||
137 | const char* joinTable, | ||
138 | const char* foreignColumn, | ||
139 | const char* conditionColumn, | ||
140 | int conditionValue, | ||
141 | bool nullable = false) | ||
142 | { | ||
143 | return field(obj, type::join_through_where, name, nullable, joinTable, joinWith, foreignColumn, name, foreignColumn, conditionColumn, conditionValue); | ||
144 | } | ||
145 | |||
132 | static field selfJoin( | 146 | static field selfJoin( |
133 | object obj, | 147 | object obj, |
134 | const char* name, | 148 | const char* name, |
@@ -166,6 +180,7 @@ namespace verbly { | |||
166 | return ((type_ == type::join) | 180 | return ((type_ == type::join) |
167 | || (type_ == type::join_where) | 181 | || (type_ == type::join_where) |
168 | || (type_ == type::join_through) | 182 | || (type_ == type::join_through) |
183 | || (type_ == type::join_through_where) | ||
169 | || (type_ == type::hierarchal_join)); | 184 | || (type_ == type::hierarchal_join)); |
170 | } | 185 | } |
171 | 186 | ||
@@ -195,7 +210,7 @@ namespace verbly { | |||
195 | { | 210 | { |
196 | return (type_ == type::hierarchal_join) | 211 | return (type_ == type::hierarchal_join) |
197 | ? object_ | 212 | ? object_ |
198 | : ((type_ == type::join) || (type_ == type::join_where) || (type_ == type::join_through)) | 213 | : ((type_ == type::join) || (type_ == type::join_where) || (type_ == type::join_through) || (type_ == type::join_through_where)) |
199 | ? joinObject_ | 214 | ? joinObject_ |
200 | : throw std::domain_error("Non-join fields don't have join objects"); | 215 | : throw std::domain_error("Non-join fields don't have join objects"); |
201 | } | 216 | } |
@@ -205,40 +220,40 @@ namespace verbly { | |||
205 | const char* getForeignColumn() const | 220 | const char* getForeignColumn() const |
206 | { | 221 | { |
207 | // We ignore hierarchal joins because they are always self joins. | 222 | // We ignore hierarchal joins because they are always self joins. |
208 | return (type_ == type::join_through) | 223 | return ((type_ == type::join_through) || (type_ == type::join_through_where)) |
209 | ? foreignColumn_ | 224 | ? foreignColumn_ |
210 | : throw std::domain_error("Only many-to-many join fields have a foreign column"); | 225 | : throw std::domain_error("Only many-to-many join fields have a foreign column"); |
211 | } | 226 | } |
212 | 227 | ||
213 | const char* getJoinColumn() const | 228 | const char* getJoinColumn() const |
214 | { | 229 | { |
215 | return ((type_ == type::join_through) || (type_ == type::hierarchal_join)) | 230 | return ((type_ == type::join_through) || (type_ == type::join_through_where) || (type_ == type::hierarchal_join)) |
216 | ? joinColumn_ | 231 | ? joinColumn_ |
217 | : throw std::domain_error("Only many-to-many join fields have a join column"); | 232 | : throw std::domain_error("Only many-to-many join fields have a join column"); |
218 | } | 233 | } |
219 | 234 | ||
220 | const char* getForeignJoinColumn() const | 235 | const char* getForeignJoinColumn() const |
221 | { | 236 | { |
222 | return ((type_ == type::join_through) || (type_ == type::hierarchal_join)) | 237 | return ((type_ == type::join_through) || (type_ == type::join_through_where) || (type_ == type::hierarchal_join)) |
223 | ? foreignJoinColumn_ | 238 | ? foreignJoinColumn_ |
224 | : throw std::domain_error("Only many-to-many join fields have a foreign join column"); | 239 | : throw std::domain_error("Only many-to-many join fields have a foreign join column"); |
225 | } | 240 | } |
226 | 241 | ||
227 | // Condition joins | 242 | // Condition joins |
228 | 243 | ||
229 | const field& getConditionField() const | 244 | const char* getConditionColumn() const |
230 | { | 245 | { |
231 | if (type_ == type::join_where) | 246 | if ((type_ == type::join_where) || (type_ == type::join_through_where)) |
232 | { | 247 | { |
233 | return *conditionField_; | 248 | return conditionColumn_; |
234 | } else { | 249 | } else { |
235 | throw std::domain_error("Only condition join fields have a condition field"); | 250 | throw std::domain_error("Only condition join fields have a condition column"); |
236 | } | 251 | } |
237 | } | 252 | } |
238 | 253 | ||
239 | int getConditionValue() const | 254 | int getConditionValue() const |
240 | { | 255 | { |
241 | return (type_ == type::join_where) | 256 | return ((type_ == type::join_where) || (type_ == type::join_through_where)) |
242 | ? conditionValue_ | 257 | ? conditionValue_ |
243 | : throw std::domain_error("Only condition join fields have a condition value"); | 258 | : throw std::domain_error("Only condition join fields have a condition value"); |
244 | } | 259 | } |
@@ -254,13 +269,8 @@ namespace verbly { | |||
254 | // table (hypernymy); however, they have different join columns. For | 269 | // table (hypernymy); however, they have different join columns. For |
255 | // condition joins, the condition field and condition value are also | 270 | // condition joins, the condition field and condition value are also |
256 | // significant. | 271 | // significant. |
257 | if (conditionField_) | 272 | return std::tie(object_, column_, table_, joinColumn_, conditionColumn_, conditionValue_) |
258 | { | 273 | < std::tie(other.object_, other.column_, other.table_, other.joinColumn_, other.conditionColumn_, other.conditionValue_); |
259 | return std::tie(object_, column_, table_, joinColumn_, *conditionField_, conditionValue_) | ||
260 | < std::tie(other.object_, other.column_, other.table_, other.joinColumn_, *other.conditionField_, other.conditionValue_); | ||
261 | } else { | ||
262 | return std::tie(object_, column_, table_, joinColumn_) < std::tie(other.object_, other.column_, other.table_, other.joinColumn_); | ||
263 | } | ||
264 | } | 274 | } |
265 | 275 | ||
266 | // Equality | 276 | // Equality |
@@ -268,13 +278,8 @@ namespace verbly { | |||
268 | bool operator==(const field& other) const | 278 | bool operator==(const field& other) const |
269 | { | 279 | { |
270 | // See operator<() for documentation. | 280 | // See operator<() for documentation. |
271 | if (conditionField_) | 281 | return std::tie(object_, column_, table_, joinColumn_, conditionColumn_, conditionValue_) |
272 | { | 282 | == std::tie(other.object_, other.column_, other.table_, other.joinColumn_, other.conditionColumn_, other.conditionValue_); |
273 | return std::tie(object_, column_, table_, joinColumn_, *conditionField_, conditionValue_) | ||
274 | == std::tie(other.object_, other.column_, other.table_, other.joinColumn_, *other.conditionField_, other.conditionValue_); | ||
275 | } else { | ||
276 | return std::tie(object_, column_, table_, joinColumn_) == std::tie(other.object_, other.column_, other.table_, other.joinColumn_); | ||
277 | } | ||
278 | } | 283 | } |
279 | 284 | ||
280 | // Filter construction | 285 | // Filter construction |
@@ -325,7 +330,7 @@ namespace verbly { | |||
325 | const char* foreignColumn = 0, | 330 | const char* foreignColumn = 0, |
326 | const char* joinColumn = 0, | 331 | const char* joinColumn = 0, |
327 | const char* foreignJoinColumn = 0, | 332 | const char* foreignJoinColumn = 0, |
328 | const field* conditionField = 0, | 333 | const char* conditionColumn = 0, |
329 | int conditionValue = 0) : | 334 | int conditionValue = 0) : |
330 | object_(obj), | 335 | object_(obj), |
331 | type_(datatype), | 336 | type_(datatype), |
@@ -336,7 +341,7 @@ namespace verbly { | |||
336 | foreignColumn_(foreignColumn), | 341 | foreignColumn_(foreignColumn), |
337 | joinColumn_(joinColumn), | 342 | joinColumn_(joinColumn), |
338 | foreignJoinColumn_(foreignJoinColumn), | 343 | foreignJoinColumn_(foreignJoinColumn), |
339 | conditionField_(conditionField), | 344 | conditionColumn_(conditionColumn), |
340 | conditionValue_(conditionValue) | 345 | conditionValue_(conditionValue) |
341 | { | 346 | { |
342 | } | 347 | } |
@@ -359,7 +364,7 @@ namespace verbly { | |||
359 | const char* foreignJoinColumn_ = 0; | 364 | const char* foreignJoinColumn_ = 0; |
360 | 365 | ||
361 | // Condition joins | 366 | // Condition joins |
362 | const field* conditionField_ = 0; | 367 | const char* conditionColumn_ = 0; |
363 | int conditionValue_ = 0; | 368 | int conditionValue_ = 0; |
364 | 369 | ||
365 | }; | 370 | }; |