diff options
Diffstat (limited to 'lib/preposition.cpp')
| -rw-r--r-- | lib/preposition.cpp | 107 |
1 files changed, 0 insertions, 107 deletions
| diff --git a/lib/preposition.cpp b/lib/preposition.cpp deleted file mode 100644 index cea9165..0000000 --- a/lib/preposition.cpp +++ /dev/null | |||
| @@ -1,107 +0,0 @@ | |||
| 1 | #include "verbly.h" | ||
| 2 | |||
| 3 | namespace verbly { | ||
| 4 | |||
| 5 | std::string preposition::get_form() const | ||
| 6 | { | ||
| 7 | return form; | ||
| 8 | } | ||
| 9 | |||
| 10 | preposition_query::preposition_query(const data& _data) : _data(_data) | ||
| 11 | { | ||
| 12 | |||
| 13 | } | ||
| 14 | |||
| 15 | preposition_query& preposition_query::limit(int _limit) | ||
| 16 | { | ||
| 17 | this->_limit = _limit; | ||
| 18 | |||
| 19 | return *this; | ||
| 20 | } | ||
| 21 | |||
| 22 | preposition_query& preposition_query::random() | ||
| 23 | { | ||
| 24 | _random = true; | ||
| 25 | |||
| 26 | return *this; | ||
| 27 | } | ||
| 28 | |||
| 29 | preposition_query& preposition_query::in_group(std::string _arg) | ||
| 30 | { | ||
| 31 | _in_group.push_back(_arg); | ||
| 32 | |||
| 33 | return *this; | ||
| 34 | } | ||
| 35 | |||
| 36 | std::list<preposition> preposition_query::run() const | ||
| 37 | { | ||
| 38 | std::stringstream construct; | ||
| 39 | construct << "SELECT form FROM prepositions"; | ||
| 40 | std::list<binding> bindings; | ||
| 41 | |||
| 42 | if (!_in_group.empty()) | ||
| 43 | { | ||
| 44 | std::list<std::string> clauses(_in_group.size(), "groupname = ?"); | ||
| 45 | construct << " WHERE preposition_id IN (SELECT preposition_id FROM preposition_groups WHERE "; | ||
| 46 | construct << verbly::implode(std::begin(clauses), std::end(clauses), " OR "); | ||
| 47 | construct << ")"; | ||
| 48 | |||
| 49 | for (auto g : _in_group) | ||
| 50 | { | ||
| 51 | bindings.emplace_back(g); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | if (_random) | ||
| 56 | { | ||
| 57 | construct << " ORDER BY RANDOM()"; | ||
| 58 | } | ||
| 59 | |||
| 60 | if (_limit != unlimited) | ||
| 61 | { | ||
| 62 | construct << " LIMIT " << _limit; | ||
| 63 | } | ||
| 64 | |||
| 65 | sqlite3_stmt* ppstmt; | ||
| 66 | std::string query = construct.str(); | ||
| 67 | if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | ||
| 68 | { | ||
| 69 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); | ||
| 70 | } | ||
| 71 | |||
| 72 | int i = 1; | ||
| 73 | for (auto& binding : bindings) | ||
| 74 | { | ||
| 75 | switch (binding.get_type()) | ||
| 76 | { | ||
| 77 | case binding::type::integer: | ||
| 78 | { | ||
| 79 | sqlite3_bind_int(ppstmt, i, binding.get_integer()); | ||
| 80 | |||
| 81 | break; | ||
| 82 | } | ||
| 83 | |||
| 84 | case binding::type::string: | ||
| 85 | { | ||
| 86 | sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_TRANSIENT); | ||
| 87 | |||
| 88 | break; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | i++; | ||
| 93 | } | ||
| 94 | |||
| 95 | std::list<preposition> output; | ||
| 96 | while (sqlite3_step(ppstmt) == SQLITE_ROW) | ||
| 97 | { | ||
| 98 | preposition pp; | ||
| 99 | pp.form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 0))); | ||
| 100 | |||
| 101 | output.push_back(pp); | ||
| 102 | } | ||
| 103 | |||
| 104 | return output; | ||
| 105 | } | ||
| 106 | |||
| 107 | }; | ||
