diff options
Diffstat (limited to 'include/odhtdb/sql/SqlQuery.hpp')
-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; + }; +} |