aboutsummaryrefslogtreecommitdiff
path: root/src/sqlitepp.cpp
blob: 10315e529ff5df42315ea78a0fbadaead1975d36 (plain)
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
266
267
268
269
270
271
// Copyright (C) 2014--2015 Robin Krahl <robin.krahl@ireas.org>
// MIT license -- http://opensource.org/licenses/MIT

#include "sqlitepp/sqlitepp.h"
#include <exception>
#include <iostream>
#include <sstream>
#include <string>

namespace sqlitepp {

Openable::Openable(const bool open, const std::string& name)
    : m_open(open), m_name(name) {
}

bool Openable::isOpen() const {
  return m_open;
}

void Openable::requireOpen() const {
  if (!m_open) {
    throw std::logic_error(m_name + " is not open.");
  }
}

void Openable::setOpen(const bool open) {
    m_open = open;
}

std::string DatabaseError::getErrorMessage(const int errorCode,
    const std::string& errorMessage) {
  std::ostringstream stringStream;
  stringStream << "Caught SQLite3 error " << errorCode << " meaning: "
      << errorMessage;
  return stringStream.str();
}

DatabaseError::DatabaseError(const int errorCode)
    : DatabaseError(errorCode, sqlite3_errstr(errorCode)) {
}

DatabaseError::DatabaseError(const int errorCode,
                             const std::string& errorMessage)
    : std::runtime_error(getErrorMessage(errorCode, errorMessage)),
      m_errorCode(errorCode) {
}

int DatabaseError::errorCode() const {
  return m_errorCode;
}

Statement::Statement(sqlite3_stmt* handle)
    : Openable(true, "Statement"), m_handle(handle), m_canRead(false) {
}

Statement::~Statement() {
  if (isOpen()) {
    // errors that could occur during finalizing are ignored as they have
    // already been handled!
    sqlite3_finalize(m_handle);
    setOpen(false);
  }
}

void Statement::bind(const int index, const double value) {
  requireOpen();
  handleBindResult(index, sqlite3_bind_double(m_handle, index, value));
}

void Statement::bind(const std::string& name, const double value) {
  bind(getParameterIndex(name), value);
}

void Statement::bind(const int index, const int value) {
  requireOpen();
  handleBindResult(index, sqlite3_bind_int(m_handle, index, value));
}

void Statement::bind(const std::string& name, const int value) {
  bind(getParameterIndex(name), value);
}

void Statement::bind(const int index, const std::string& value) {
  requireOpen();
  handleBindResult(index, sqlite3_bind_text(m_handle, index, value.c_str(),
      value.size(), NULL));
}

void Statement::bind(const std::string& name, const std::string& value) {
  bind(getParameterIndex(name), value);
}

ResultSet Statement::execute() {
  step();
  return ResultSet(m_instancePointer.lock());
}

void Statement::requireCanRead() const {
  if (!m_canRead) {
    throw std::logic_error("Trying to read from statement without data");
  }
}

void Statement::setInstancePointer(
    const std::weak_ptr<Statement>& instancePointer) {
  m_instancePointer = instancePointer;
}

bool Statement::step() {
  requireOpen();
  int result = sqlite3_step(m_handle);
  if (result == SQLITE_ROW) {
    m_canRead = true;
  } else if (result == SQLITE_DONE) {
    m_canRead = false;
  } else {
    throw DatabaseError(result);
  }
  return m_canRead;
}

void Statement::close() {
  if (isOpen()) {
    // errors that could occur during finalizing are ignored as they have
    // already been handled!
    sqlite3_finalize(m_handle);
    setOpen(false);
  }
}

bool Statement::reset() {
  requireOpen();
  return sqlite3_reset(m_handle) == SQLITE_OK;
}

int Statement::getParameterIndex(const std::string& name) const {
  requireOpen();
  int index = sqlite3_bind_parameter_index(m_handle, name.c_str());
  if (index == 0) {
    throw std::invalid_argument("No such parameter: " + name);
  }
  return index;
}

void Statement::handleBindResult(const int index, const int result) const {
  std::ostringstream s;
  switch (result) {
    case SQLITE_OK:
      break;
    case SQLITE_RANGE:
      s << "Bind index out of range: " << index;
      throw std::out_of_range(s.str());
    case SQLITE_NOMEM:
      throw std::runtime_error("No memory to bind parameter");
    default:
      throw DatabaseError(result);
  }
}

Database::Database() : Openable(false, "Database") {
}

Database::Database(const std::string & file) : Database() {
    open(file);
}

Database::~Database() {
  if (isOpen()) {
    sqlite3_close(m_handle);
    setOpen(false);
  }
  // m_handle is deleted by sqlite3_close
}

void Database::close() {
  if (isOpen()) {
    int result = sqlite3_close(m_handle);
    if (result == SQLITE_OK) {
      setOpen(false);
    } else {
      throw sqlitepp::DatabaseError(result);
    }
  }
}

void Database::execute(const std::string& sql) {
  requireOpen();
  std::shared_ptr<Statement> statement = prepare(sql);
  statement->step();
}

int Database::lastInsertRowId() const {
  requireOpen();
  return sqlite3_last_insert_rowid(m_handle);
}

void Database::open(const std::string& file) {
  if (isOpen()) {
    throw std::logic_error("sqlitepp::Database::open(std::string&): "
                           "Database already open");
  }
  int result = sqlite3_open(file.c_str(), &m_handle);

  if (m_handle == NULL) {
    throw std::runtime_error("sqlitepp::Database::open(std::string&): "
                             "Can't allocate memory");
  }

  if (result == SQLITE_OK) {
    setOpen(true);
  } else {
    std::string errorMessage = sqlite3_errmsg(m_handle);
    sqlite3_close(m_handle);
    throw sqlitepp::DatabaseError(result, errorMessage);
  }
}

std::shared_ptr<Statement> Database::prepare(const std::string& sql) {
  requireOpen();
  sqlite3_stmt* statementHandle;
  int result = sqlite3_prepare_v2(m_handle, sql.c_str(), sql.size(),
                                  &statementHandle, NULL);
  if (result != SQLITE_OK) {
    throw DatabaseError(result, sqlite3_errmsg(m_handle));
  }
  if (statementHandle == NULL) {
    throw std::runtime_error("Statement handle is NULL");
  }
  auto statement = std::shared_ptr<Statement>(new Statement(statementHandle));
  statement->setInstancePointer(std::weak_ptr<Statement>(statement));
  return statement;
}

ResultSet::ResultSet(const std::shared_ptr<Statement> statement)
    : m_statement(statement) {
}

bool ResultSet::canRead() const {
  return m_statement->m_canRead;
}

int ResultSet::columnCount() const {
  m_statement->requireOpen();
  m_statement->requireCanRead();
  return sqlite3_data_count(m_statement->m_handle);
}

double ResultSet::readDouble(const int column) const {
  m_statement->requireOpen();
  m_statement->requireCanRead();
  return sqlite3_column_double(m_statement->m_handle, column);
}

int ResultSet::readInt(const int column) const {
  m_statement->requireOpen();
  m_statement->requireCanRead();
  return sqlite3_column_int(m_statement->m_handle, column);
}

std::string ResultSet::readString(const int column) const {
  m_statement->requireOpen();
  m_statement->requireCanRead();
  return std::string((const char*) sqlite3_column_text(m_statement->m_handle,
                                                       column));
}

bool ResultSet::next() {
  return m_statement->step();
}

}  // namespace sqlitepp