From 3554df2e34e63364eea3a7998e0dfb0e6be65ca4 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 30 Mar 2018 09:59:48 -0400 Subject: Started migrating to hkutil (does not build) --- .gitignore | 5 ++ generator/database.cpp | 173 ------------------------------------------- generator/database.h | 73 ------------------ generator/field.cpp | 193 ------------------------------------------------ generator/field.h | 76 ------------------- generator/form.h | 1 - generator/generator.cpp | 161 ++++++++++++++++++++++------------------ generator/generator.h | 2 +- generator/notion.cpp | 17 +++-- generator/notion.h | 2 - lib/util.h | 61 --------------- 11 files changed, 105 insertions(+), 659 deletions(-) create mode 100644 .gitignore delete mode 100644 generator/database.cpp delete mode 100644 generator/database.h delete mode 100644 generator/field.cpp delete mode 100644 generator/field.h delete mode 100644 lib/util.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..73d4d61 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +CMakeFiles +CMakeCache.txt +cmake_install.cmake +Makefile +.DS_Store diff --git a/generator/database.cpp b/generator/database.cpp deleted file mode 100644 index 188fa2a..0000000 --- a/generator/database.cpp +++ /dev/null @@ -1,173 +0,0 @@ -#include "database.h" -#include -#include -#include -#include -#include -#include -#include "field.h" -#include "../lib/util.h" - -namespace verbly { - namespace generator { - - sqlite3_error::sqlite3_error( - const std::string& what, - const std::string& db_err) : - what_(what + " (" + db_err + ")"), - db_err_(db_err) - { - } - - const char* sqlite3_error::what() const noexcept - { - return what_.c_str(); - } - - const char* sqlite3_error::db_err() const noexcept - { - return db_err_.c_str(); - } - - database::database(std::string path) - { - // If there is already a file at this path, overwrite it. - if (std::ifstream(path)) - { - if (std::remove(path.c_str())) - { - throw std::logic_error("Could not overwrite file at path"); - } - } - - if (sqlite3_open_v2(path.c_str(), &ppdb_, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) != SQLITE_OK) - { - // We still have to free the resources allocated. In the event that - // allocation failed, ppdb will be null and sqlite3_close_v2 will just - // ignore it. - std::string errmsg(sqlite3_errmsg(ppdb_)); - sqlite3_close_v2(ppdb_); - - throw sqlite3_error("Could not create output datafile", errmsg); - } - } - - database::database(database&& other) : database() - { - swap(*this, other); - } - - database& database::operator=(database&& other) - { - swap(*this, other); - - return *this; - } - - void swap(database& first, database& second) - { - std::swap(first.ppdb_, second.ppdb_); - } - - database::~database() - { - sqlite3_close_v2(ppdb_); - } - - void database::runQuery(std::string query) - { - // This can only happen when doing bad things with move semantics. - assert(ppdb_ != nullptr); - - sqlite3_stmt* ppstmt; - - if (sqlite3_prepare_v2(ppdb_, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) - { - throw sqlite3_error("Error writing to database", sqlite3_errmsg(ppdb_)); - } - - int result = sqlite3_step(ppstmt); - sqlite3_finalize(ppstmt); - - if (result != SQLITE_DONE) - { - throw sqlite3_error("Error writing to database", sqlite3_errmsg(ppdb_)); - } - } - - void database::insertIntoTable(std::string table, std::list fields) - { - // This can only happen when doing bad things with move semantics. - assert(ppdb_ != nullptr); - - // This shouldn't happen. - assert(!fields.empty()); - - std::list fieldNames; - std::list qs; - for (field& f : fields) - { - fieldNames.push_back(f.getName()); - qs.push_back("?"); - } - - std::ostringstream query; - query << "INSERT INTO "; - query << table; - query << " ("; - query << implode(std::begin(fieldNames), std::end(fieldNames), ", "); - query << ") VALUES ("; - query << implode(std::begin(qs), std::end(qs), ", "); - query << ")"; - - std::string query_str = query.str(); - - sqlite3_stmt* ppstmt; - - if (sqlite3_prepare_v2(ppdb_, query_str.c_str(), query_str.length(), &ppstmt, NULL) != SQLITE_OK) - { - throw sqlite3_error("Error writing to database", sqlite3_errmsg(ppdb_)); - } - - int i = 1; - for (field& f : fields) - { - switch (f.getType()) - { - case field::type::integer: - { - sqlite3_bind_int(ppstmt, i, f.getInteger()); - - break; - } - - case field::type::string: - { - sqlite3_bind_text(ppstmt, i, f.getString().c_str(), f.getString().length(), SQLITE_TRANSIENT); - - break; - } - - case field::type::invalid: - { - // Fields can only be invalid when doing bad things with move semantics. - assert(false); - - break; - } - } - - i++; - } - - int result = sqlite3_step(ppstmt); - sqlite3_finalize(ppstmt); - - if (result != SQLITE_DONE) - { - throw sqlite3_error("Error writing to database", sqlite3_errmsg(ppdb_)); - } - } - - }; -}; diff --git a/generator/database.h b/generator/database.h deleted file mode 100644 index 7906304..0000000 --- a/generator/database.h +++ /dev/null @@ -1,73 +0,0 @@ -#ifndef DATABASE_H_0B0A47D2 -#define DATABASE_H_0B0A47D2 - -#include -#include -#include - -struct sqlite3; - -namespace verbly { - namespace generator { - - class field; - - class sqlite3_error : public std::exception { - public: - - sqlite3_error(const std::string& what, const std::string& db_err); - - const char* what() const noexcept override; - const char* db_err() const noexcept; - - private: - std::string what_; - std::string db_err_; - - }; - - class database { - public: - - // Constructor - - explicit database(std::string path); - - // Disable copying - - database(const database& other) = delete; - database& operator=(const database& other) = delete; - - // Move constructor and move assignment - - database(database&& other); - database& operator=(database&& other); - - // Swap - - friend void swap(database& first, database& second); - - // Destructor - - ~database(); - - // Actions - - void runQuery(std::string query); - - void insertIntoTable(std::string table, std::list fields); - - private: - - database() - { - } - - sqlite3* ppdb_ = nullptr; - - }; - - }; -}; - -#endif /* end of include guard: DATABASE_H_0B0A47D2 */ diff --git a/generator/field.cpp b/generator/field.cpp deleted file mode 100644 index 84b2f91..0000000 --- a/generator/field.cpp +++ /dev/null @@ -1,193 +0,0 @@ -#include "field.h" -#include -#include - -namespace verbly { - namespace generator { - - field::field(const field& other) - { - type_ = other.type_; - name_ = other.name_; - - switch (type_) - { - case type::integer: - { - integer_ = other.integer_; - - break; - } - - case type::string: - { - new(&string_) std::string(other.string_); - - break; - } - - case type::invalid: - { - break; - } - } - } - - field::field(field&& other) : field() - { - swap(*this, other); - } - - field& field::operator=(field other) - { - swap(*this, other); - - return *this; - } - - void swap(field& first, field& second) - { - using type = field::type; - - type tempType = first.type_; - std::string tempName = std::move(first.name_); - int tempInteger; - std::string tempString; - - switch (first.type_) - { - case type::integer: - { - tempInteger = first.integer_; - - break; - } - - case type::string: - { - tempString = std::move(tempString); - - break; - } - - case type::invalid: - { - break; - } - } - - first.~field(); - - first.type_ = second.type_; - first.name_ = std::move(second.name_); - - switch (second.type_) - { - case type::integer: - { - first.integer_ = second.integer_; - - break; - } - - case type::string: - { - new(&first.string_) std::string(std::move(second.string_)); - - break; - } - - case type::invalid: - { - break; - } - } - - second.~field(); - - second.type_ = tempType; - second.name_ = std::move(tempName); - - switch (tempType) - { - case type::integer: - { - second.integer_ = tempInteger; - - break; - } - - case type::string: - { - new(&second.string_) std::string(std::move(tempString)); - - break; - } - - case type::invalid: - { - break; - } - } - } - - field::~field() - { - switch (type_) - { - case type::string: - { - using string_type = std::string; - string_.~string_type(); - - break; - } - - case type::integer: - case type::invalid: - { - break; - } - } - } - - field::field( - std::string name, - int arg) : - type_(type::integer), - name_(name), - integer_(arg) - { - } - - int field::getInteger() const - { - if (type_ != type::integer) - { - throw std::domain_error("field::getInteger called on non-integer field"); - } - - return integer_; - } - - field::field( - std::string name, - std::string arg) : - type_(type::string), - name_(name) - { - new(&string_) std::string(arg); - } - - std::string field::getString() const - { - if (type_ != type::string) - { - throw std::domain_error("field::getString called on non-string field"); - } - - return string_; - } - - }; -}; diff --git a/generator/field.h b/generator/field.h deleted file mode 100644 index aaca3fa..0000000 --- a/generator/field.h +++ /dev/null @@ -1,76 +0,0 @@ -#ifndef BINDING_H_CAE0B18E -#define BINDING_H_CAE0B18E - -#include - -namespace verbly { - namespace generator { - - class field { - public: - enum class type { - invalid, - integer, - string - }; - - // Copy and move constructors - - field(const field& other); - field(field&& other); - - // Assignment - - field& operator=(field other); - - // Swap - - friend void swap(field& first, field& second); - - // Destructor - - ~field(); - - // Generic accessors - - type getType() const - { - return type_; - } - - std::string getName() const - { - return name_; - } - - // Integer - - field(std::string name, int arg); - - int getInteger() const; - - // String - - field(std::string name, std::string arg); - - std::string getString() const; - - private: - - field() - { - } - - union { - int integer_; - std::string string_; - }; - - type type_ = type::invalid; - std::string name_; - }; - - }; -}; - -#endif /* end of include guard: BINDING_H_CAE0B18E */ diff --git a/generator/form.h b/generator/form.h index 5a2de30..37fd3cc 100644 --- a/generator/form.h +++ b/generator/form.h @@ -8,7 +8,6 @@ namespace verbly { namespace generator { class pronunciation; - class database; class form { public: diff --git a/generator/generator.cpp b/generator/generator.cpp index d774bd9..e34ca69 100644 --- a/generator/generator.cpp +++ b/generator/generator.cpp @@ -594,7 +594,12 @@ namespace verbly { fields.emplace_back("major", DATABASE_MAJOR_VERSION); fields.emplace_back("minor", DATABASE_MINOR_VERSION); - db_.insertIntoTable("version", std::move(fields)); + db_.insertIntoTable( + "version", + { + { "major", DATABASE_MAJOR_VERSION }, + { "minor", DATABASE_MINOR_VERSION } + }); } void generator::dumpObjects() @@ -689,11 +694,12 @@ namespace verbly { word& word1 = *wordByWnidAndWnum_.at(lookup1); word& word2 = *wordByWnidAndWnum_.at(lookup2); - std::list fields; - fields.emplace_back("antonym_1_id", word1.getId()); - fields.emplace_back("antonym_2_id", word2.getId()); - - db_.insertIntoTable("antonymy", std::move(fields)); + db_.insertIntoTable( + "antonymy", + { + { "antonym_1_id", word1.getId() }, + { "antonym_2_id", word2.getId() } + }); } } } @@ -721,11 +727,12 @@ namespace verbly { notion& notion1 = *notionByWnid_.at(lookup1); notion& notion2 = *notionByWnid_.at(lookup2); - std::list fields; - fields.emplace_back("noun_id", notion1.getId()); - fields.emplace_back("adjective_id", notion2.getId()); - - db_.insertIntoTable("variation", std::move(fields)); + db_.insertIntoTable( + "variation", + { + { "noun_id", notion1.getId() } + { "adjective_id", notion2.getId() } + }); } } } @@ -786,11 +793,12 @@ namespace verbly { { for (int word2 : rightJoin) { - std::list fields; - fields.emplace_back("term_id", word1); - fields.emplace_back("domain_id", word2); - - db_.insertIntoTable(table_name, std::move(fields)); + db_.insertIntoTable( + table_name, + { + { "term_id", word1 }, + { "domain_id", word2 } + }); } } } @@ -819,11 +827,12 @@ namespace verbly { notion& notion1 = *notionByWnid_.at(lookup1); notion& notion2 = *notionByWnid_.at(lookup2); - std::list fields; - fields.emplace_back("effect_id", notion1.getId()); - fields.emplace_back("cause_id", notion2.getId()); - - db_.insertIntoTable("causality", std::move(fields)); + db_.insertIntoTable( + "causality", + { + { "effect_id", notion1.getId() }, + { "cause_id", notion2.getId() } + }); } } } @@ -851,11 +860,12 @@ namespace verbly { notion& notion1 = *notionByWnid_.at(lookup1); notion& notion2 = *notionByWnid_.at(lookup2); - std::list fields; - fields.emplace_back("given_id", notion1.getId()); - fields.emplace_back("entailment_id", notion2.getId()); - - db_.insertIntoTable("entailment", std::move(fields)); + db_.insertIntoTable( + "entailment", + { + { "given_id", notion1.getId() }, + { "entailment_id", notion2.getId() } + }); } } } @@ -883,11 +893,12 @@ namespace verbly { notion& notion1 = *notionByWnid_.at(lookup1); notion& notion2 = *notionByWnid_.at(lookup2); - std::list fields; - fields.emplace_back("hyponym_id", notion1.getId()); - fields.emplace_back("hypernym_id", notion2.getId()); - - db_.insertIntoTable("hypernymy", std::move(fields)); + db_.insertIntoTable( + "hypernymy", + { + { "hyponym_id", notion1.getId() }, + { "hypernym_id", notion2.getId() } + }); } } } @@ -915,11 +926,12 @@ namespace verbly { notion& notion1 = *notionByWnid_.at(lookup1); notion& notion2 = *notionByWnid_.at(lookup2); - std::list fields; - fields.emplace_back("instance_id", notion1.getId()); - fields.emplace_back("class_id", notion2.getId()); - - db_.insertIntoTable("instantiation", std::move(fields)); + db_.insertIntoTable( + "instantiation", + { + { "instance_id", notion1.getId() }, + { "class_id", notion2.getId() } + }); } } } @@ -947,11 +959,12 @@ namespace verbly { notion& notion1 = *notionByWnid_.at(lookup1); notion& notion2 = *notionByWnid_.at(lookup2); - std::list fields; - fields.emplace_back("holonym_id", notion1.getId()); - fields.emplace_back("meronym_id", notion2.getId()); - - db_.insertIntoTable("member_meronymy", std::move(fields)); + db_.insertIntoTable( + "member_meronymy", + { + { "holonym_id", notion1.getId() }, + { "meronym_id", notion2.getId() } + }); } } } @@ -979,11 +992,12 @@ namespace verbly { notion& notion1 = *notionByWnid_.at(lookup1); notion& notion2 = *notionByWnid_.at(lookup2); - std::list fields; - fields.emplace_back("holonym_id", notion1.getId()); - fields.emplace_back("meronym_id", notion2.getId()); - - db_.insertIntoTable("part_meronymy", std::move(fields)); + db_.insertIntoTable( + "part_meronymy", + { + { "holonym_id", notion1.getId() }, + { "meronym_id", notion2.getId() } + }); } } } @@ -1011,11 +1025,12 @@ namespace verbly { notion& notion1 = *notionByWnid_.at(lookup1); notion& notion2 = *notionByWnid_.at(lookup2); - std::list fields; - fields.emplace_back("holonym_id", notion1.getId()); - fields.emplace_back("meronym_id", notion2.getId()); - - db_.insertIntoTable("substance_meronymy", std::move(fields)); + db_.insertIntoTable( + "substance_meronymy", + { + { "holonym_id", notion1.getId() }, + { "meronym_id", notion2.getId() } + }); } } } @@ -1045,18 +1060,20 @@ namespace verbly { if (word1.getNotion().getPartOfSpeech() == part_of_speech::adjective) { - std::list fields; - fields.emplace_back("pertainym_id", word1.getId()); - fields.emplace_back("noun_id", word2.getId()); - - db_.insertIntoTable("pertainymy", std::move(fields)); + db_.insertIntoTable( + "pertainymy", + { + { "pertainym_id", word1.getId() }, + { "noun_id", word2.getId() } + }); } else if (word1.getNotion().getPartOfSpeech() == part_of_speech::adverb) { - std::list fields; - fields.emplace_back("mannernym_id", word1.getId()); - fields.emplace_back("adjective_id", word2.getId()); - - db_.insertIntoTable("mannernymy", std::move(fields)); + db_.insertIntoTable( + "mannernymy", + { + { "mannernym_id", word1.getId() }, + { "adjective_id", word2.getId() } + }); } } } @@ -1085,11 +1102,12 @@ namespace verbly { word& word1 = *wordByWnidAndWnum_.at(lookup1); word& word2 = *wordByWnidAndWnum_.at(lookup2); - std::list fields; - fields.emplace_back("general_id", word1.getId()); - fields.emplace_back("specific_id", word2.getId()); - - db_.insertIntoTable("specification", std::move(fields)); + db_.insertIntoTable( + "specification", + { + { "general_id", word1.getId() }, + { "specific_id", word2.getId() } + }); } } } @@ -1117,11 +1135,12 @@ namespace verbly { notion& notion1 = *notionByWnid_.at(lookup1); notion& notion2 = *notionByWnid_.at(lookup2); - std::list fields; - fields.emplace_back("adjective_1_id", notion1.getId()); - fields.emplace_back("adjective_2_id", notion2.getId()); - - db_.insertIntoTable("similarity", std::move(fields)); + db_.insertIntoTable( + "similarity", + { + { "adjective_1_id", notion1.getId() }, + { "adjective_2_id", notion2.getId() } + }); } } } diff --git a/generator/generator.h b/generator/generator.h index 5458c97..52073bc 100644 --- a/generator/generator.h +++ b/generator/generator.h @@ -120,7 +120,7 @@ namespace verbly { // Output - database db_; + hatkirby::database db_; // Data diff --git a/generator/notion.cpp b/generator/notion.cpp index 1878ba9..35ba7b1 100644 --- a/generator/notion.cpp +++ b/generator/notion.cpp @@ -46,10 +46,11 @@ namespace verbly { { // First, serialize the notion { - std::list fields; + std::list fields; fields.emplace_back("notion_id", arg.getId()); - fields.emplace_back("part_of_speech", static_cast(arg.getPartOfSpeech())); + fields.emplace_back("part_of_speech", + static_cast(arg.getPartOfSpeech())); if (arg.hasWnid()) { @@ -69,12 +70,12 @@ namespace verbly { { for (std::string group : arg.getPrepositionGroups()) { - std::list fields; - - fields.emplace_back("notion_id", arg.getId()); - fields.emplace_back("groupname", group); - - db.insertIntoTable("is_a", std::move(fields)); + db.insertIntoTable( + "is_a", + { + { "notion_id", arg.getId() }, + { "groupname", group } + }); } } diff --git a/generator/notion.h b/generator/notion.h index 6e38497..817e66a 100644 --- a/generator/notion.h +++ b/generator/notion.h @@ -9,8 +9,6 @@ namespace verbly { namespace generator { - class database; - class notion { public: diff --git a/lib/util.h b/lib/util.h deleted file mode 100644 index 9db8678..0000000 --- a/lib/util.h +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef UTIL_H_15DDCA2D -#define UTIL_H_15DDCA2D - -#include -#include -#include - -namespace verbly { - - template - std::string implode(InputIterator first, InputIterator last, std::string delimiter) - { - std::stringstream result; - - for (InputIterator it = first; it != last; it++) - { - if (it != first) - { - result << delimiter; - } - - result << *it; - } - - return result.str(); - } - - template - void split(std::string input, std::string delimiter, OutputIterator out) - { - while (!input.empty()) - { - int divider = input.find(delimiter); - if (divider == std::string::npos) - { - *out = input; - out++; - - input = ""; - } else { - *out = input.substr(0, divider); - out++; - - input = input.substr(divider+delimiter.length()); - } - } - } - - template - Container split(std::string input, std::string delimiter) - { - Container result; - - split(input, delimiter, std::back_inserter(result)); - - return result; - } - -}; - -#endif /* end of include guard: UTIL_H_15DDCA2D */ -- cgit 1.4.1