summary refs log tree commit diff stats
path: root/data.h
blob: 2c23c1562bf497b4d11e73b06dee920ac3d7abcd (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef DATA_H_C4AEC3DD
#define DATA_H_C4AEC3DD

#include "verb.h"
#include <sqlite3.h>
#include <stdexcept>

namespace verbly {
  
  class data {
    private:
      sqlite3* ppdb;
      
    public:
      class verb_query {
        public:
          const static int unlimited = -1;
          
        private:
          const data& database;
          int m_limit = unlimited;
          bool m_random = false;
          
        public:
          verb_query(const data& database) : database(database)
          {
            
          }
          
          verb_query& limit(int m_limit)
          {
            if ((m_limit > 0) || (m_limit == unlimited))
            {
              this->m_limit = m_limit;
            }
            
            return *this;
          }
          
          verb_query& random(bool m_random)
          {
            this->m_random = m_random;
            
            return *this;
          }
          
          std::list<verb> run() const
          {
            std::stringstream construct;
            construct << "SELECT verb_id, infinitive, past_tense, past_participle, ing_form, s_form FROM verbs";
            
            if (m_random)
            {
              construct << " ORDER BY RANDOM()";
            }
            
            if (m_limit != unlimited)
            {
              construct << " LIMIT " << m_limit;
            }
            
            sqlite3_stmt* ppstmt;
            std::string query = construct.str();
            if (sqlite3_prepare_v2(database.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
            {
              throw std::runtime_error(sqlite3_errmsg(database.ppdb));
            }
            
            std::list<verb> output;
            while (sqlite3_step(ppstmt) == SQLITE_ROW)
            {
              verb tnc {sqlite3_column_int(ppstmt, 0)};
              tnc.infinitive = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1)));
              tnc.past_tense = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2)));
              tnc.past_participle = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 3)));
              tnc.ing_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 4)));
              tnc.s_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 5)));
              
              output.push_back(tnc);
            }
            
            sqlite3_finalize(ppstmt);
            
            return output;
          }
          
      };
      
      class adjective_query {
        public:
          const static int unlimited = -1;
          
        private:
          const data& database;
          int m_limit = unlimited;
          bool m_random = false;
          
        public:
          adjective_query(const data& database) : database(database)
          {
            
          }
          
          adjective_query& limit(int m_limit)
          {
            if ((m_limit > 0) || (m_limit == unlimited))
            {
              this->m_limit = m_limit;
            }
            
            return *this;
          }
          
          adjective_query& random(bool m_random)
          {
            this->m_random = m_random;
            
            return *this;
          }
          
          std::list<adjective> run() const
          {
            std::stringstream construct;
            construct << "SELECT adjective_id, adjective FROM adjectives";
            
            if (m_random)
            {
              construct << " ORDER BY RANDOM()";
            }
            
            if (m_limit != unlimited)
            {
              construct << " LIMIT " << m_limit;
            }
            
            sqlite3_stmt* ppstmt;
            std::string query = construct.str();
            if (sqlite3_prepare_v2(database.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
            {
              throw std::runtime_error(sqlite3_errmsg(database.ppdb));
            }
            
            std::list<adjective> output;
            while (sqlite3_step(ppstmt) == SQLITE_ROW)
            {
              adjective tnc {sqlite3_column_int(ppstmt, 0)};
              tnc.value = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1)));
              
              output.push_back(tnc);
            }
            
            sqlite3_finalize(ppstmt);
            
            return output;
          }
          
      };
      
      data(std::string datafile)
      {
        if (sqlite3_open_v2(datafile.c_str(), &ppdb, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK)
        {
          throw std::invalid_argument(sqlite3_errmsg(ppdb));
        }
      }
      
      data(const data& other) = delete;
      data& operator=(const data& other) = delete;
      
      data(data&& other)
      {
        ppdb = other.ppdb;
      }
      
      data& operator=(data&& other)
      {
        ppdb = other.ppdb;
        
        return *this;
      }
      
      ~data()
      {
        sqlite3_close_v2(ppdb);
      }
      
      verb_query verbs() const
      {
        return verb_query(*this);
      }
      
      adjective_query adjectives() const
      {
        return adjective_query(*this);
      }
      
  };
  
};

#endif /* end of include guard: DATA_H_C4AEC3DD */