148 lines
4.5 KiB
C++
148 lines
4.5 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
#include <modulus.hpp>
|
|
|
|
|
|
namespace noto {
|
|
|
|
class trusted_sql_fragment {
|
|
public:
|
|
trusted_sql_fragment() = default;
|
|
static auto unsafe(std::string sql) -> std::expected<noto::trusted_sql_fragment, modulus::error>;
|
|
auto value() const -> std::string const &;
|
|
auto empty() const -> std::bool_t;
|
|
private:
|
|
explicit trusted_sql_fragment(std::string sql);
|
|
std::string sql_{};
|
|
};
|
|
|
|
class column_type {
|
|
public:
|
|
column_type() = default;
|
|
static auto integer() -> noto::column_type;
|
|
static auto bigint() -> noto::column_type;
|
|
static auto varchar(std::uint32_t length) -> std::expected<noto::column_type, modulus::error>;
|
|
static auto text() -> noto::column_type;
|
|
static auto blob() -> noto::column_type;
|
|
static auto real() -> noto::column_type;
|
|
static auto boolean() -> noto::column_type;
|
|
static auto timestamp() -> noto::column_type;
|
|
static auto decimal(std::uint32_t precision, std::uint32_t scale) -> std::expected<noto::column_type, modulus::error>;
|
|
static auto trusted(noto::trusted_sql_fragment fragment) -> noto::column_type;
|
|
auto sql() const -> std::string const &;
|
|
auto empty() const -> std::bool_t;
|
|
private:
|
|
explicit column_type(std::string sql);
|
|
std::string sql_{};
|
|
};
|
|
|
|
class default_value {
|
|
public:
|
|
default_value() = default;
|
|
static auto null_value() -> noto::default_value;
|
|
static auto string(std::string_view value) -> std::expected<noto::default_value, modulus::error>;
|
|
static auto integer(std::int64_t value) -> noto::default_value;
|
|
static auto unsigned_integer(std::uint64_t value) -> noto::default_value;
|
|
static auto real(std::float64_t value) -> std::expected<noto::default_value, modulus::error>;
|
|
static auto boolean(std::bool_t value) -> noto::default_value;
|
|
static auto current_timestamp() -> noto::default_value;
|
|
static auto trusted_expression(noto::trusted_sql_fragment fragment) -> noto::default_value;
|
|
auto sql() const -> std::string const &;
|
|
auto empty() const -> std::bool_t;
|
|
private:
|
|
explicit default_value(std::string sql);
|
|
std::string sql_{};
|
|
};
|
|
|
|
struct table_name {
|
|
std::string database{};
|
|
std::string name{};
|
|
};
|
|
|
|
enum class generated_column_storage {
|
|
VIRTUAL,
|
|
STORED,
|
|
};
|
|
|
|
enum class reference_action {
|
|
RESTRICT,
|
|
CASCADE,
|
|
SET_NULL,
|
|
NO_ACTION,
|
|
SET_DEFAULT,
|
|
};
|
|
|
|
struct generated_column_definition {
|
|
noto::trusted_sql_fragment expression{};
|
|
noto::generated_column_storage storage{noto::generated_column_storage::VIRTUAL};
|
|
};
|
|
|
|
struct column_definition {
|
|
std::string name{};
|
|
noto::column_type type{};
|
|
std::bool_t nullable{true};
|
|
std::bool_t auto_increment{false};
|
|
std::bool_t primary_key{false};
|
|
std::optional<noto::default_value> default_value{};
|
|
std::optional<noto::generated_column_definition> generated{};
|
|
};
|
|
|
|
struct unique_constraint_definition {
|
|
std::string name{};
|
|
std::vector<std::string> columns{};
|
|
};
|
|
|
|
struct check_constraint_definition {
|
|
std::string name{};
|
|
noto::trusted_sql_fragment expression{};
|
|
std::bool_t enforced{true};
|
|
};
|
|
|
|
struct foreign_key_definition {
|
|
std::string name{};
|
|
std::vector<std::string> columns{};
|
|
noto::table_name referenced_table{};
|
|
std::vector<std::string> referenced_columns{};
|
|
std::optional<noto::reference_action> on_delete{};
|
|
std::optional<noto::reference_action> on_update{};
|
|
};
|
|
|
|
struct table_options {
|
|
std::bool_t strict{false};
|
|
std::bool_t without_rowid{false};
|
|
};
|
|
|
|
struct table_definition {
|
|
noto::table_name name{};
|
|
std::vector<noto::column_definition> columns{};
|
|
std::vector<std::string> primary_key{};
|
|
std::vector<noto::unique_constraint_definition> unique_constraints{};
|
|
std::vector<noto::check_constraint_definition> check_constraints{};
|
|
std::vector<noto::foreign_key_definition> foreign_keys{};
|
|
noto::table_options options{};
|
|
std::bool_t if_not_exists{true};
|
|
};
|
|
|
|
struct index_definition {
|
|
noto::table_name table{};
|
|
std::string name{};
|
|
std::vector<std::string> columns{};
|
|
std::bool_t unique{false};
|
|
};
|
|
|
|
auto quote_identifier(std::string_view identifier) -> std::expected<std::string, modulus::error>;
|
|
auto create_table_sql(noto::table_definition const &definition) -> std::expected<std::string, modulus::error>;
|
|
auto drop_table_sql(noto::table_name const &table, std::bool_t if_exists = true) -> std::expected<std::string, modulus::error>;
|
|
auto create_index_sql(noto::index_definition const &definition) -> std::expected<std::string, modulus::error>;
|
|
auto drop_index_sql(noto::index_definition const &definition) -> std::expected<std::string, modulus::error>;
|
|
auto table_exists_sql() -> std::string;
|
|
|
|
}
|