WebFramework v3.0.12
Web framework for C++.
Loading...
Searching...
No Matches
SQLiteDatabaseModel.h
1#pragma once
2
3#include "Import/WebFrameworkCore.h"
4
5#include "SQLiteDatabase.h"
6#include "SQLiteResult.h"
7
8#define DECLARE_DATABASE_NAME(DatabaseName) public: static constexpr std::string_view databaseName = DatabaseName; inline std::string_view getDatabaseName() const override { return databaseName; }
9#define DECLARE_TABLE_NAME(TableName) public: static constexpr std::string_view tableName = TableName; inline std::string_view getTableName() const override { return tableName; }
10#define DECLARE_DATABASE_AND_TABLE_NAME(DatabaseName, TableName) DECLARE_DATABASE_NAME(DatabaseName); \
11 DECLARE_TABLE_NAME(TableName);
12
13namespace framework
14{
15 namespace sqlite
16 {
20 class WEB_FRAMEWORK_API SQLiteDatabaseModel
21 {
22 public:
23 static constexpr std::string_view databaseName = "";
24 static constexpr std::string_view tableName = "";
25
26 protected:
27 std::shared_ptr<SQLiteDatabase> database;
28
29 private:
30 std::shared_ptr<SQLiteDatabase> databaseConstructor;
31
32 protected:
38 static bool isNumber(std::string_view source);
39
45 static std::string convertToValue(std::string_view source);
46
47 protected:
53 utility::SQLiteResult execute(const std::string& query);
54
55 public:
57
59
60 SQLiteDatabaseModel& operator = (const SQLiteDatabaseModel&) = delete;
61
62 SQLiteDatabaseModel(SQLiteDatabaseModel&& other) noexcept = default;
63
64 SQLiteDatabaseModel& operator = (SQLiteDatabaseModel&& other) noexcept = default;
65
72 utility::SQLiteResult raw(const std::string& query);
73
79 virtual void createTable(const std::vector<std::pair<std::string, std::string>>& attributes = {});
80
85 virtual void dropTable();
86
92 virtual void recreateTable(const std::vector<std::pair<std::string, std::string>>& attributes = {});
93
99 virtual utility::SQLiteResult insert(const std::unordered_map<std::string, std::string>& attributes = {});
100
108 virtual void update(const std::unordered_map<std::string, std::string>& attributes, const std::string& fieldName, const std::string& fieldValue);
109
116 virtual void deleteQuery(const std::string& fieldName, const std::string& fieldValue);
117
121 virtual void deleteQuery(const std::unordered_map<std::string, std::string>& attributes = {});
122
128 utility::SQLiteResult selectAll();
129
137 virtual utility::SQLiteResult selectByField(const std::string& fieldName, const std::string& fieldValue);
138
145 virtual utility::SQLiteResult selectByField(const std::unordered_map<std::string, std::string>& attributes);
146
147 virtual std::string_view getDatabaseName() const = 0;
148
149 virtual std::string_view getTableName() const = 0;
150
151 virtual ~SQLiteDatabaseModel() = default;
152
153 friend class SQLiteManager;
154 };
155 }
156}
Providing SELECT, INSERT, UPDATE, DELETE or raw queries for SQLiteDatabase.
virtual utility::SQLiteResult selectByField(const std::unordered_map< std::string, std::string > &attributes)
SELECT with condition.
virtual void deleteQuery(const std::string &fieldName, const std::string &fieldValue)
Delete from table.
virtual utility::SQLiteResult selectByField(const std::string &fieldName, const std::string &fieldValue)
SELECT with condition.
virtual void deleteQuery(const std::unordered_map< std::string, std::string > &attributes={})
Delete from table.
Contains result of SQL request.