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/query.h | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 lib/query.h (limited to 'lib/query.h') diff --git a/lib/query.h b/lib/query.h new file mode 100644 index 0000000..e31be3d --- /dev/null +++ b/lib/query.h @@ -0,0 +1,123 @@ +#ifndef QUERY_H_7CC5284C +#define QUERY_H_7CC5284C + +#include +#include +#include +#include +#include +#include +#include "statement.h" +#include "binding.h" + +namespace verbly { + + class database_error : public std::logic_error { + public: + + database_error(std::string msg, std::string sqlMsg) : std::logic_error(msg + " (" + sqlMsg + ")") + { + } + }; + + template + class query { + public: + + query(const database& db, sqlite3* ppdb, filter queryFilter, bool random, int limit) : db_(&db) + { + statement stmt(Object::objectType, std::move(queryFilter)); + + std::string queryString = stmt.getQueryString(Object::select, random, limit); + std::list bindings = stmt.getBindings(); + + std::cout << queryString << std::endl; + + if (sqlite3_prepare_v2(ppdb, queryString.c_str(), queryString.length(), &ppstmt_, NULL) != SQLITE_OK) + { + std::string errorMsg = sqlite3_errmsg(ppdb); + sqlite3_finalize(ppstmt_); + + throw database_error("Error preparing query", errorMsg); + } + + int i = 1; + for (const binding& value : bindings) + { + switch (value.getType()) + { + case binding::type::integer: + { + if (sqlite3_bind_int(ppstmt_, i, value.getInteger()) != SQLITE_OK) + { + std::string errorMsg = sqlite3_errmsg(ppdb); + sqlite3_finalize(ppstmt_); + + throw database_error("Error binding value to query", errorMsg); + } + + break; + } + + case binding::type::string: + { + if (sqlite3_bind_text(ppstmt_, i, value.getString().c_str(), value.getString().length(), SQLITE_TRANSIENT) != SQLITE_OK) + { + std::string errorMsg = sqlite3_errmsg(ppdb); + sqlite3_finalize(ppstmt_); + + throw database_error("Error binding value to query", errorMsg); + } + + break; + } + + case binding::type::invalid: + { + throw std::logic_error("Cannot use invalid bindings"); + } + } + + i++; + } + } + + ~query() + { + sqlite3_finalize(ppstmt_); + } + + std::vector all() const + { + std::vector result; + + while (sqlite3_step(ppstmt_) == SQLITE_ROW) + { + result.emplace_back(*db_, ppstmt_); + } + + sqlite3_reset(ppstmt_); + + return result; + } + + Object first() const + { + std::vector results = all(); + if (!results.empty()) + { + return results.front(); + } else { + throw std::logic_error("query returned empty dataset"); + } + } + + private: + const database* db_; + sqlite3_stmt* ppstmt_; + + }; + +}; + +#endif /* end of include guard: QUERY_H_7CC5284C */ -- cgit 1.4.1