diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-05-14 00:20:11 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 23:25:46 +0200 |
commit | 8b94c3bf3a06caa7b003fe61e8242bdb00004eb5 (patch) | |
tree | c307bf3a9c1bf38ea23608b0700755fc76e6980e /include/odhtdb/sql | |
parent | 0c8761b3d76912f034a0cb819d72f0349a25bf4f (diff) |
Replace files with sqlite
Using sqlite because sqlite has transactions, storing/loading from files
automatically, unloading data that is not accessed often.
Removed cosmetic data (node name, username). They can be added using
addData by the application that uses odhtdb instead.
Database callback functions can now be called with stored data using
database.loadNode function.
TODO: Add local user storage (with password) back, it has been temorary
disabled
Diffstat (limited to 'include/odhtdb/sql')
-rw-r--r-- | include/odhtdb/sql/SqlQuery.hpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/include/odhtdb/sql/SqlQuery.hpp b/include/odhtdb/sql/SqlQuery.hpp new file mode 100644 index 0000000..f26ee1a --- /dev/null +++ b/include/odhtdb/sql/SqlQuery.hpp @@ -0,0 +1,65 @@ +#pragma once + +#include "../DataView.hpp" +#include <utility> +#include <stdexcept> + +class sqlite3; +class sqlite3_stmt; + +namespace odhtdb +{ + class SqlQueryException : public std::runtime_error + { + public: + SqlQueryException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class SqlArg + { + public: + enum class Type : u8 + { + DATA_VIEW, + INT, + INT64 + }; + + SqlArg(const DataView &data) : dataView(data), type(Type::DATA_VIEW) {} + SqlArg(int data) : integer(data), type(Type::INT) {} + SqlArg(i64 data) : integer64(data), type(Type::INT64) {} + + int bind(sqlite3_stmt *stmt, int paramIndex) const; + private: + union + { + const DataView dataView; + const int integer; + const i64 integer64; + }; + const Type type; + }; + + class SqlQuery + { + public: + // Throws SqlQueryException on failure + SqlQuery(sqlite3 *db, const char *sql, std::initializer_list<SqlArg> args); + ~SqlQuery(); + + // Return true if we got result, false if there are not rows left. + // Throws SqlQueryException on failure + bool next(); + + int getInt(int index); + i64 getInt64(int index); + // The returned blob is NOT a copy, it will be invalid after next call to @next or when SqlQuery is deallocated + const DataView getBlob(int index); + private: + void checkColumnIndex(int index); + private: + sqlite3 *db; + sqlite3_stmt *stmt; + int numColumns; + }; +} |