From f1f67e62cebb4144f0599196263cd93b41fa972e Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 6 Feb 2017 20:58:37 -0500 Subject: 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. --- lib/field.h | 61 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 28 deletions(-) (limited to 'lib/field.h') 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 { join, join_where, join_through, + join_through_where, hierarchal_join }; @@ -100,11 +101,11 @@ namespace verbly { object obj, const char* name, object joinWith, - const field& conditionField, + const char* conditionColumn, int conditionValue, bool nullable = false) { - return field(obj, type::join_where, name, nullable, 0, joinWith, 0, 0, 0, &conditionField, conditionValue); + return field(obj, type::join_where, name, nullable, 0, joinWith, 0, 0, 0, conditionColumn, conditionValue); } static field joinThrough( @@ -129,6 +130,19 @@ namespace verbly { return field(obj, type::join_through, name, true, joinTable, joinWith, foreignColumn, joinColumn, foreignJoinColumn); } + static field joinThroughWhere( + object obj, + const char* name, + object joinWith, + const char* joinTable, + const char* foreignColumn, + const char* conditionColumn, + int conditionValue, + bool nullable = false) + { + return field(obj, type::join_through_where, name, nullable, joinTable, joinWith, foreignColumn, name, foreignColumn, conditionColumn, conditionValue); + } + static field selfJoin( object obj, const char* name, @@ -166,6 +180,7 @@ namespace verbly { return ((type_ == type::join) || (type_ == type::join_where) || (type_ == type::join_through) + || (type_ == type::join_through_where) || (type_ == type::hierarchal_join)); } @@ -195,7 +210,7 @@ namespace verbly { { return (type_ == type::hierarchal_join) ? object_ - : ((type_ == type::join) || (type_ == type::join_where) || (type_ == type::join_through)) + : ((type_ == type::join) || (type_ == type::join_where) || (type_ == type::join_through) || (type_ == type::join_through_where)) ? joinObject_ : throw std::domain_error("Non-join fields don't have join objects"); } @@ -205,40 +220,40 @@ namespace verbly { const char* getForeignColumn() const { // We ignore hierarchal joins because they are always self joins. - return (type_ == type::join_through) + return ((type_ == type::join_through) || (type_ == type::join_through_where)) ? foreignColumn_ : throw std::domain_error("Only many-to-many join fields have a foreign column"); } const char* getJoinColumn() const { - return ((type_ == type::join_through) || (type_ == type::hierarchal_join)) + return ((type_ == type::join_through) || (type_ == type::join_through_where) || (type_ == type::hierarchal_join)) ? joinColumn_ : throw std::domain_error("Only many-to-many join fields have a join column"); } const char* getForeignJoinColumn() const { - return ((type_ == type::join_through) || (type_ == type::hierarchal_join)) + return ((type_ == type::join_through) || (type_ == type::join_through_where) || (type_ == type::hierarchal_join)) ? foreignJoinColumn_ : throw std::domain_error("Only many-to-many join fields have a foreign join column"); } // Condition joins - const field& getConditionField() const + const char* getConditionColumn() const { - if (type_ == type::join_where) + if ((type_ == type::join_where) || (type_ == type::join_through_where)) { - return *conditionField_; + return conditionColumn_; } else { - throw std::domain_error("Only condition join fields have a condition field"); + throw std::domain_error("Only condition join fields have a condition column"); } } int getConditionValue() const { - return (type_ == type::join_where) + return ((type_ == type::join_where) || (type_ == type::join_through_where)) ? conditionValue_ : throw std::domain_error("Only condition join fields have a condition value"); } @@ -254,13 +269,8 @@ namespace verbly { // table (hypernymy); however, they have different join columns. For // condition joins, the condition field and condition value are also // significant. - if (conditionField_) - { - return std::tie(object_, column_, table_, joinColumn_, *conditionField_, conditionValue_) - < std::tie(other.object_, other.column_, other.table_, other.joinColumn_, *other.conditionField_, other.conditionValue_); - } else { - return std::tie(object_, column_, table_, joinColumn_) < std::tie(other.object_, other.column_, other.table_, other.joinColumn_); - } + return std::tie(object_, column_, table_, joinColumn_, conditionColumn_, conditionValue_) + < std::tie(other.object_, other.column_, other.table_, other.joinColumn_, other.conditionColumn_, other.conditionValue_); } // Equality @@ -268,13 +278,8 @@ namespace verbly { bool operator==(const field& other) const { // See operator<() for documentation. - if (conditionField_) - { - return std::tie(object_, column_, table_, joinColumn_, *conditionField_, conditionValue_) - == std::tie(other.object_, other.column_, other.table_, other.joinColumn_, *other.conditionField_, other.conditionValue_); - } else { - return std::tie(object_, column_, table_, joinColumn_) == std::tie(other.object_, other.column_, other.table_, other.joinColumn_); - } + return std::tie(object_, column_, table_, joinColumn_, conditionColumn_, conditionValue_) + == std::tie(other.object_, other.column_, other.table_, other.joinColumn_, other.conditionColumn_, other.conditionValue_); } // Filter construction @@ -325,7 +330,7 @@ namespace verbly { const char* foreignColumn = 0, const char* joinColumn = 0, const char* foreignJoinColumn = 0, - const field* conditionField = 0, + const char* conditionColumn = 0, int conditionValue = 0) : object_(obj), type_(datatype), @@ -336,7 +341,7 @@ namespace verbly { foreignColumn_(foreignColumn), joinColumn_(joinColumn), foreignJoinColumn_(foreignJoinColumn), - conditionField_(conditionField), + conditionColumn_(conditionColumn), conditionValue_(conditionValue) { } @@ -359,7 +364,7 @@ namespace verbly { const char* foreignJoinColumn_ = 0; // Condition joins - const field* conditionField_ = 0; + const char* conditionColumn_ = 0; int conditionValue_ = 0; }; -- cgit 1.4.1