2016-01-07 08:14:33 +00:00
|
|
|
#pragma once
|
2015-05-16 07:37:13 +00:00
|
|
|
|
|
|
|
/* SQLite3 C++ RAII wrapper for nall
|
|
|
|
*
|
|
|
|
* Note on code below: it is safe (no-op) to call sqlite3_* functions on null sqlite3 objects
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
|
|
|
#include <nall/stdint.hpp>
|
|
|
|
#include <nall/string.hpp>
|
|
|
|
|
2019-01-16 00:46:42 +00:00
|
|
|
namespace nall::Database {
|
2015-05-16 07:37:13 +00:00
|
|
|
|
|
|
|
struct SQLite3 {
|
|
|
|
struct Statement {
|
2015-07-14 09:32:43 +00:00
|
|
|
Statement(const Statement& source) = delete;
|
|
|
|
auto operator=(const Statement& source) -> Statement& = delete;
|
2015-05-16 07:37:13 +00:00
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
Statement(sqlite3_stmt* statement) : _statement(statement) {}
|
2015-05-16 07:37:13 +00:00
|
|
|
Statement(Statement&& source) { operator=(move(source)); }
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto operator=(Statement&& source) -> Statement& {
|
|
|
|
_statement = source._statement;
|
|
|
|
_response = source._response;
|
|
|
|
_output = source._output;
|
|
|
|
source._statement = nullptr;
|
|
|
|
source._response = SQLITE_OK;
|
|
|
|
source._output = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
2015-05-16 07:37:13 +00:00
|
|
|
|
2015-08-02 06:23:13 +00:00
|
|
|
explicit operator bool() {
|
|
|
|
return sqlite3_data_count(statement());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto columns() -> unsigned {
|
|
|
|
return sqlite3_column_count(statement());
|
|
|
|
}
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto integer(unsigned column) -> int64_t {
|
|
|
|
return sqlite3_column_int64(statement(), column);
|
|
|
|
}
|
2015-05-16 07:37:13 +00:00
|
|
|
|
2015-11-14 00:52:51 +00:00
|
|
|
auto natural(unsigned column) -> uint64_t {
|
2015-07-14 09:32:43 +00:00
|
|
|
return sqlite3_column_int64(statement(), column);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto real(unsigned column) -> double {
|
|
|
|
return sqlite3_column_double(statement(), column);
|
|
|
|
}
|
2015-05-16 07:37:13 +00:00
|
|
|
|
|
|
|
auto text(unsigned column) -> string {
|
|
|
|
string result;
|
2015-07-14 09:32:43 +00:00
|
|
|
if(auto text = sqlite3_column_text(statement(), column)) {
|
|
|
|
result.resize(sqlite3_column_bytes(statement(), column));
|
2015-08-02 06:23:13 +00:00
|
|
|
memory::copy(result.get(), text, result.size());
|
2015-05-16 07:37:13 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto data(unsigned column) -> vector<uint8_t> {
|
|
|
|
vector<uint8_t> result;
|
2015-07-14 09:32:43 +00:00
|
|
|
if(auto data = sqlite3_column_blob(statement(), column)) {
|
|
|
|
result.resize(sqlite3_column_bytes(statement(), column));
|
2015-05-16 07:37:13 +00:00
|
|
|
memory::copy(result.data(), data, result.size());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto integer() -> int64_t { return integer(_output++); }
|
2015-11-14 00:52:51 +00:00
|
|
|
auto natural() -> uint64_t { return natural(_output++); }
|
2015-07-14 09:32:43 +00:00
|
|
|
auto real() -> double { return real(_output++); }
|
|
|
|
auto text() -> string { return text(_output++); }
|
|
|
|
auto data() -> vector<uint8_t> { return data(_output++); }
|
2015-05-16 07:37:13 +00:00
|
|
|
|
|
|
|
protected:
|
2015-07-14 09:32:43 +00:00
|
|
|
virtual auto statement() -> sqlite3_stmt* { return _statement; }
|
2015-05-16 07:37:13 +00:00
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
sqlite3_stmt* _statement = nullptr;
|
|
|
|
signed _response = SQLITE_OK;
|
|
|
|
unsigned _output = 0;
|
2015-05-16 07:37:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Query : Statement {
|
2015-07-14 09:32:43 +00:00
|
|
|
Query(const Query& source) = delete;
|
|
|
|
auto operator=(const Query& source) -> Query& = delete;
|
|
|
|
|
|
|
|
Query(sqlite3_stmt* statement) : Statement(statement) {}
|
|
|
|
Query(Query&& source) : Statement(source._statement) { operator=(move(source)); }
|
2015-05-16 07:37:13 +00:00
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
~Query() {
|
|
|
|
sqlite3_finalize(statement());
|
|
|
|
_statement = nullptr;
|
|
|
|
}
|
2015-05-16 07:37:13 +00:00
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto operator=(Query&& source) -> Query& {
|
|
|
|
_statement = source._statement;
|
|
|
|
_input = source._input;
|
|
|
|
source._statement = nullptr;
|
|
|
|
source._input = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& bind(unsigned column, nullptr_t) { sqlite3_bind_null(_statement, 1 + column); return *this; }
|
|
|
|
auto& bind(unsigned column, int32_t value) { sqlite3_bind_int(_statement, 1 + column, value); return *this; }
|
|
|
|
auto& bind(unsigned column, uint32_t value) { sqlite3_bind_int(_statement, 1 + column, value); return *this; }
|
|
|
|
auto& bind(unsigned column, int64_t value) { sqlite3_bind_int64(_statement, 1 + column, value); return *this; }
|
|
|
|
auto& bind(unsigned column, uint64_t value) { sqlite3_bind_int64(_statement, 1 + column, value); return *this; }
|
|
|
|
auto& bind(unsigned column, double value) { sqlite3_bind_double(_statement, 1 + column, value); return *this; }
|
|
|
|
auto& bind(unsigned column, const string& value) { sqlite3_bind_text(_statement, 1 + column, value.data(), value.size(), SQLITE_TRANSIENT); return *this; }
|
|
|
|
auto& bind(unsigned column, const vector<uint8_t>& value) { sqlite3_bind_blob(_statement, 1 + column, value.data(), value.size(), SQLITE_TRANSIENT); return *this; }
|
|
|
|
|
|
|
|
auto& bind(nullptr_t) { return bind(_input++, nullptr); }
|
|
|
|
auto& bind(int32_t value) { return bind(_input++, value); }
|
|
|
|
auto& bind(uint32_t value) { return bind(_input++, value); }
|
|
|
|
auto& bind(int64_t value) { return bind(_input++, value); }
|
|
|
|
auto& bind(uint64_t value) { return bind(_input++, value); }
|
|
|
|
auto& bind(double value) { return bind(_input++, value); }
|
|
|
|
auto& bind(const string& value) { return bind(_input++, value); }
|
|
|
|
auto& bind(const vector<uint8_t>& value) { return bind(_input++, value); }
|
|
|
|
|
|
|
|
auto step() -> bool {
|
|
|
|
_stepped = true;
|
|
|
|
return sqlite3_step(_statement) == SQLITE_ROW;
|
|
|
|
}
|
2015-05-16 07:37:13 +00:00
|
|
|
|
|
|
|
struct Iterator {
|
2015-07-14 09:32:43 +00:00
|
|
|
Iterator(Query& query, bool finished) : query(query), finished(finished) {}
|
|
|
|
auto operator*() -> Statement { return query._statement; }
|
2015-05-16 07:37:13 +00:00
|
|
|
auto operator!=(const Iterator& source) const -> bool { return finished != source.finished; }
|
|
|
|
auto operator++() -> Iterator& { finished = !query.step(); return *this; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Query& query;
|
|
|
|
bool finished = false;
|
|
|
|
};
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
auto begin() -> Iterator { return Iterator(*this, !step()); }
|
2015-05-16 07:37:13 +00:00
|
|
|
auto end() -> Iterator { return Iterator(*this, true); }
|
2015-07-14 09:32:43 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
auto statement() -> sqlite3_stmt* override {
|
|
|
|
if(!_stepped) step();
|
|
|
|
return _statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned _input = 0;
|
|
|
|
bool _stepped = false;
|
2015-05-16 07:37:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SQLite3() = default;
|
|
|
|
SQLite3(const string& filename) { open(filename); }
|
|
|
|
~SQLite3() { close(); }
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
explicit operator bool() const { return _database; }
|
2015-05-16 07:37:13 +00:00
|
|
|
|
|
|
|
auto open(const string& filename) -> bool {
|
|
|
|
close();
|
2015-07-14 09:32:43 +00:00
|
|
|
sqlite3_open(filename, &_database);
|
|
|
|
return _database;
|
2015-05-16 07:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto close() -> void {
|
2015-07-14 09:32:43 +00:00
|
|
|
sqlite3_close(_database);
|
|
|
|
_database = nullptr;
|
2015-05-16 07:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... P> auto execute(const string& statement, P&&... p) -> Query {
|
2015-07-14 09:32:43 +00:00
|
|
|
if(!_database) return {nullptr};
|
2015-05-16 07:37:13 +00:00
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
sqlite3_stmt* _statement = nullptr;
|
|
|
|
sqlite3_prepare_v2(_database, statement.data(), statement.size(), &_statement, nullptr);
|
|
|
|
if(!_statement) {
|
|
|
|
if(_debug) print("[sqlite3_prepare_v2] ", sqlite3_errmsg(_database), "\n");
|
2015-05-16 07:37:13 +00:00
|
|
|
return {nullptr};
|
|
|
|
}
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
Query query{_statement};
|
|
|
|
bind(query, forward<P>(p)...);
|
2015-05-16 07:37:13 +00:00
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto lastInsertID() const -> uint64_t {
|
2015-07-14 09:32:43 +00:00
|
|
|
return _database ? sqlite3_last_insert_rowid(_database) : 0;
|
2015-05-16 07:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto setDebug(bool debug = true) -> void {
|
2015-07-14 09:32:43 +00:00
|
|
|
_debug = debug;
|
2015-05-16 07:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-07-14 09:32:43 +00:00
|
|
|
auto bind(Query&) -> void {}
|
|
|
|
template<typename T, typename... P> auto bind(Query& query, const T& value, P&&... p) -> void {
|
|
|
|
query.bind(value);
|
|
|
|
bind(query, forward<P>(p)...);
|
2015-05-16 07:37:13 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 09:32:43 +00:00
|
|
|
bool _debug = false;
|
|
|
|
sqlite3* _database = nullptr;
|
2015-05-16 07:37:13 +00:00
|
|
|
};
|
|
|
|
|
2019-01-16 00:46:42 +00:00
|
|
|
}
|