summary refs log tree commit diff stats
path: root/lib/preposition.cpp
blob: cea91655e7d548fb47f2194f9783189d39c9ba07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "verbly.h"

namespace verbly {
  
  std::string preposition::get_form() const
  {
    return form;
  }
  
  preposition_query::preposition_query(const data& _data) : _data(_data)
  {
    
  }
  
  preposition_query& preposition_query::limit(int _limit)
  {
    this->_limit = _limit;
    
    return *this;
  }
  
  preposition_query& preposition_query::random()
  {
    _random = true;
    
    return *this;
  }
  
  preposition_query& preposition_query::in_group(std::string _arg)
  {
    _in_group.push_back(_arg);
    
    return *this;
  }
  
  std::list<preposition> preposition_query::run() const
  {
    std::stringstream construct;
    construct << "SELECT form FROM prepositions";
    std::list<binding> bindings;
    
    if (!_in_group.empty())
    {
      std::list<std::string> clauses(_in_group.size(), "groupname = ?");
      construct << " WHERE preposition_id IN (SELECT preposition_id FROM preposition_groups WHERE ";
      construct << verbly::implode(std::begin(clauses), std::end(clauses), " OR ");
      construct << ")";
      
      for (auto g : _in_group)
      {
        bindings.emplace_back(g);
      }
    }
    
    if (_random)
    {
      construct << " ORDER BY RANDOM()";
    }
    
    if (_limit != unlimited)
    {
      construct << " LIMIT " << _limit;
    }
    
    sqlite3_stmt* ppstmt;
    std::string query = construct.str();
    if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
    {
      throw std::runtime_error(sqlite3_errmsg(_data.ppdb));
    }
    
    int i = 1;
    for (auto& binding : bindings)
    {
      switch (binding.get_type())
      {
        case binding::type::integer:
        {
          sqlite3_bind_int(ppstmt, i, binding.get_integer());
          
          break;
        }
        
        case binding::type::string:
        {
          sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_TRANSIENT);
          
          break;
        }
      }
      
      i++;
    }
    
    std::list<preposition> output;
    while (sqlite3_step(ppstmt) == SQLITE_ROW)
    {
      preposition pp;
      pp.form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 0)));
      
      output.push_back(pp);
    }
    
    return output;
  }

};