From 6746da6edd7d9d50efe374eabbb79a3cac882d81 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 16 Jan 2017 18:02:50 -0500 Subject: Started structural rewrite The new object structure was designed to build on the existing WordNet structure, while also adding in all of the data that we get from other sources. More information about this can be found on the project wiki. The generator has already been completely rewritten to generate a datafile that uses the new structure. In addition, a number of indexes are created, which does double the size of the datafile, but also allows for much faster lookups. Finally, the new generator is written modularly and is a lot more readable than the old one. The verbly interface to the new object structure has mostly been completed, but has not been tested fully. There is a completely new search API which utilizes a lot of operator overloading; documentation on how to use it should go up at some point. Token processing and verb frames are currently unimplemented. Source for these have been left in the repository for now. --- lib/statement.h | 272 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 272 insertions(+) create mode 100644 lib/statement.h (limited to 'lib/statement.h') diff --git a/lib/statement.h b/lib/statement.h new file mode 100644 index 0000000..a528d60 --- /dev/null +++ b/lib/statement.h @@ -0,0 +1,272 @@ +#ifndef STATEMENT_H_29F51659 +#define STATEMENT_H_29F51659 + +#include +#include +#include +#include +#include "binding.h" +#include "enums.h" +#include "field.h" +#include "filter.h" + +namespace verbly { + + class filter; + + class statement { + public: + + statement(object context, filter queryFilter); + + std::string getQueryString(std::list select, bool random, int limit) const; + + std::list getBindings() const; + + private: + + class join { + public: + + join( + bool outer, + std::string foreignTableName, + std::string joinTable, + std::string joinColumn, + std::string foreignTable, + std::string foreignColumn) : + outer_(outer), + foreignTableName_(std::move(foreignTableName)), + joinTable_(std::move(joinTable)), + joinColumn_(std::move(joinColumn)), + foreignTable_(std::move(foreignTable)), + foreignColumn_(std::move(foreignColumn)) + { + } + + bool isOuterJoin() const + { + return outer_; + } + + const std::string& getForeignTableName() const + { + return foreignTableName_; + } + + const std::string& getJoinTable() const + { + return joinTable_; + } + + const std::string& getJoinColumn() const + { + return joinColumn_; + } + + const std::string& getForeignTable() const + { + return foreignTable_; + } + + const std::string& getForeignColumn() const + { + return foreignColumn_; + } + + private: + bool outer_ = false; + std::string foreignTableName_; + std::string joinTable_; + std::string joinColumn_; + std::string foreignTable_; + std::string foreignColumn_; + + }; + + friend std::ostream& operator<<(std::ostream& oss, const join& j); + + class condition { + public: + enum class type { + empty, + singleton, + group + }; + + enum class comparison { + equals, + does_not_equal, + is_greater_than, + is_at_most, + is_less_than, + is_at_least, + is_like, + is_not_like, + is_not_null, + is_null + }; + + // Copy and move constructors + + condition(const condition& other); + condition(condition&& other); + + // Assignment + + condition& operator=(condition other); + + // Swap + + friend void swap(condition& first, condition& second); + + // Destructor + + ~condition(); + + // Accessors + + type getType() const + { + return type_; + } + + // Empty + + condition(); + + // Singleton + + condition(std::string table, std::string column, bool isNull); + + condition(std::string table, std::string column, comparison comp, binding value); + + // Group + + explicit condition(bool orlogic); + + condition& operator+=(condition n); + + condition& operator&=(condition n); + + const std::list& getChildren() const; + + // Utility + + std::string toSql() const; + + std::list flattenBindings() const; + + private: + union { + struct { + std::string table_; + std::string column_; + comparison comparison_; + binding value_; + } singleton_; + struct { + std::list children_; + bool orlogic_; + } group_; + }; + type type_; + }; + + friend void swap(condition& first, condition& second); + + class with { + public: + + with( + std::string identifier, + field f, + std::map tables, + std::string topTable, + condition where, + std::list joins) : + identifier_(std::move(identifier)), + field_(f), + tables_(std::move(tables)), + topTable_(std::move(topTable)), + topCondition_(std::move(where)), + joins_(std::move(joins)) + { + } + + const std::string& getIdentifier() const + { + return identifier_; + } + + field getField() const + { + return field_; + } + + std::string getTableForId(std::string identifier) const + { + return tables_.at(identifier); + } + + const std::string& getTopTable() const + { + return topTable_; + } + + const condition& getCondition() const + { + return topCondition_; + } + + const std::list& getJoins() const + { + return joins_; + } + + private: + std::string identifier_; + field field_; + std::map tables_; + std::string topTable_; + condition topCondition_; + std::list joins_; + + }; + + static constexpr const char* getTableForContext(object context) + { + return (context == object::notion) ? "notions" + : (context == object::word) ? "words" + : (context == object::group) ? "groups" + : (context == object::frame) ? "frames" + : (context == object::lemma) ? "lemmas_forms" + : (context == object::form) ? "forms" + : (context == object::pronunciation) ? "pronunciations" + : throw std::domain_error("Provided context has no associated table"); + } + + static const std::list getSelectForContext(object context); + + statement(std::string tableName, filter clause, int nextTableId = 0, int nextWithId = 0); + + condition parseFilter(filter queryFilter); + + std::string instantiateTable(std::string name); + + condition integrate(statement subStmt); + + int nextTableId_; + int nextWithId_; + + std::map tables_; + std::string topTable_; + std::list joins_; + std::list withs_; + condition topCondition_; + + }; + +}; + +#endif /* end of include guard: STATEMENT_H_29F51659 */ -- cgit 1.4.1