summaryrefslogtreecommitdiff
path: root/src/sqlite/SQLiteWrapper.cpp
blob: f5b484b71411c9e48c6e03eee1f4314ac9555389 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* 
   SQLiteWrapper.cpp

   Copyright (C) 2004 Ren� Nyffenegger

   This source code is provided 'as-is', without any express or implied
   warranty. In no event will the author be held liable for any damages
   arising from the use of this software.

   Permission is granted to anyone to use this software for any purpose,
   including commercial applications, and to alter it and redistribute it
   freely, subject to the following restrictions:

   1. The origin of this source code must not be misrepresented; you must not
      claim that you wrote the original source code. If you use this source code
      in a product, an acknowledgment in the product documentation would be
      appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and must not be
      misrepresented as being the original source code.

   3. This notice may not be removed or altered from any source distribution.

   Ren� Nyffenegger rene.nyffenegger@adp-gmbh.ch
   Modified by Mateusz Kaduk mateusz.kaduk@gmail.com

*/

#include "SQLiteWrapper.h"

SQLiteWrapper::SQLiteWrapper() : db_(0) {
}

bool SQLiteWrapper::Open(std::string const& db_file) {
  if (sqlite3_open(db_file.c_str(), &db_) != SQLITE_OK) {
    return false;
  }
  return true;
} 

bool SQLiteWrapper::Close() {
    if (sqlite3_close(db_) != SQLITE_OK) {
    return false;
  }
  return true;
}

bool SQLiteWrapper::SelectStmt(std::string const& stmt, ResultTable& res) {
  char *errmsg;
  int   ret;

  res.reset();

  ret = sqlite3_exec(db_, stmt.c_str(), SelectCallback, static_cast<void*> (&res), &errmsg);

  if (ret != SQLITE_OK) {
    return false;
  }
  return true;
//  if (ret != SQLITE_OK) {
//    std::cout << stmt << " [" << errmsg << "]" << std::endl;
//  }
}

// TODO parameter p_col_names
int SQLiteWrapper::SelectCallback(void *p_data, int num_fields, char **p_fields, char**  /*p_col_names*/) {
  ResultTable* res = reinterpret_cast<ResultTable*>(p_data);

  ResultRecord record;

  for(int i=0; i < num_fields; i++) {
    record.fields_.push_back(p_fields[i]);
  }

  res->records_.push_back(record);

  return 0;
}

SQLiteStatement* SQLiteWrapper::Statement(std::string const& statement) {
  SQLiteStatement* stmt;
  try {
    stmt = new SQLiteStatement(statement, db_);
    return stmt;
  }
  catch (const char* e) {
    return 0;
  }
}

SQLiteStatement::SQLiteStatement(std::string const& statement, sqlite3* db) {
  if ( sqlite3_prepare(
         db, 
         statement.c_str(),  // stmt
        -1,                  // If than zero, then stmt is read up to the first nul terminator
        &stmt_,
         0                   // Pointer to unused portion of stmt
       )
       != SQLITE_OK) {
    throw sqlite3_errmsg(db);
  }

  if (!stmt_) {
    throw "stmt_ is 0";
  }
}

SQLiteStatement::~SQLiteStatement() {
}

SQLiteStatement::SQLiteStatement() :
  stmt_       (0)
{
}

bool SQLiteStatement::Bind(int pos_zero_indexed, std::string const& value) {
  if (sqlite3_bind_text (
         stmt_,
         pos_zero_indexed+1,  // Index of wildcard
         value.c_str(),
         value.length(),      // length of text
         SQLITE_TRANSIENT     // SQLITE_TRANSIENT: SQLite makes its own copy
         )
       != SQLITE_OK) {
     return false;
  }
  return true;
}

bool SQLiteStatement::Bind(int pos_zero_indexed, double value) {
  if (sqlite3_bind_double(
          stmt_,
          pos_zero_indexed+1,  // Index of wildcard
          value
          )
        != SQLITE_OK) {
      return false;
  }
  return true;
}

bool SQLiteStatement::Bind(int pos_zero_indexed, int value) {
  if (sqlite3_bind_int(
          stmt_,
          pos_zero_indexed+1,  // Index of wildcard
           value 
          )
        != SQLITE_OK) {
      return false;
  }
  return true;
}

bool SQLiteStatement::BindNull(int pos_zero_indexed) {
  if (sqlite3_bind_null(
          stmt_,
          pos_zero_indexed+1  // Index of wildcard
          )
        != SQLITE_OK) {
      return false;
  }
  return true;
}

bool SQLiteStatement::Execute() {
  int rc = sqlite3_step(stmt_);
  if (rc == SQLITE_BUSY) {
    //::MessageBox(0, "SQLITE_BUSY", 0, 0); 
    return false;
  }
  if (rc == SQLITE_ERROR) {
    //::MessageBox(0, "SQLITE_ERROR", 0, 0); 
    return false;
  }
  if (rc == SQLITE_MISUSE) {
    //::MessageBox(0, "SQLITE_ERROR", 0, 0); 
    return false;
  }
  if (rc != SQLITE_DONE) {
    //sqlite3_reset(stmt_);
    return false;
  }
  sqlite3_reset(stmt_);
  return true;
}

SQLiteStatement::dataType SQLiteStatement::DataType(int pos_zero_indexed) {
  return dataType(sqlite3_column_type(stmt_, pos_zero_indexed));
}

int SQLiteStatement::ValueInt(int pos_zero_indexed) {
  return sqlite3_column_int(stmt_, pos_zero_indexed);
}

std::string SQLiteStatement::ValueString(int pos_zero_indexed) {
  return std::string(reinterpret_cast<const char*>(sqlite3_column_text(stmt_, pos_zero_indexed)));
}

bool SQLiteStatement::RestartSelect() {
  sqlite3_reset(stmt_);
  return true;
}

bool SQLiteStatement::Reset() {
  int rc = sqlite3_step(stmt_);

  sqlite3_reset(stmt_);

  if (rc == SQLITE_ROW) return true;
  return false;
}

bool SQLiteStatement::NextRow() {
  int rc = sqlite3_step(stmt_);

  if (rc == SQLITE_ROW   ) {
    return true;
  }
  if (rc == SQLITE_DONE  ) {
    sqlite3_reset(stmt_);
    return false;
  } 
  else if (rc == SQLITE_MISUSE) {
    //::MessageBox(0,  "SQLiteStatement::NextRow SQLITE_MISUSE", 0, 0);
  } 
  else if (rc == SQLITE_BUSY  ) {
    //::MessageBox(0, "SQLiteStatement::NextRow SQLITE_BUSY", 0, 0);
  } 
  else if (rc == SQLITE_ERROR ) {
    //::MessageBox(0, "SQLiteStatement::NextRow SQLITE_ERROR", 0, 0);
  }
  return false;
}

bool SQLiteWrapper::DirectStatement(std::string const& stmt) {
  char *errmsg;
  int   ret;

  ret = sqlite3_exec(db_, stmt.c_str(), 0, 0, &errmsg);

  if(ret != SQLITE_OK) {
    return false;
  }
  return true;

  //if(ret != SQLITE_OK) {
  //  std::cout << stmt << " [" << errmsg << "]" << std::endl;
  //}
}

std::string SQLiteWrapper::LastError() {
  return sqlite3_errmsg(db_);
}

bool SQLiteWrapper::Begin() {
  return DirectStatement("begin");
}

bool SQLiteWrapper::Commit() {
  return DirectStatement("commit");
}

bool SQLiteWrapper::Rollback() {
  return DirectStatement("rollback");
}