some sql classes refactorings like column and table structs to classes
This commit is contained in:
@@ -7,22 +7,23 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
struct table;
|
||||
class table;
|
||||
|
||||
enum class sql_function_t {
|
||||
NONE,
|
||||
COUNT,
|
||||
AVG,
|
||||
SUM,
|
||||
MIN,
|
||||
MAX
|
||||
None,
|
||||
Count,
|
||||
Avg,
|
||||
Sum,
|
||||
Min,
|
||||
Max
|
||||
};
|
||||
|
||||
struct column {
|
||||
class column {
|
||||
public:
|
||||
column(const char *name, const std::string& as = ""); // NOLINT(*-explicit-constructor)
|
||||
explicit column(std::string name, std::string as = ""); // NOLINT(*-explicit-constructor)
|
||||
explicit column(std::string name, std::string as = "");
|
||||
column(sql_function_t func, std::string name); // NOLINT(*-explicit-constructor)
|
||||
column(const struct table &tab, std::string name, std::string as = "");
|
||||
column(const class table &tab, std::string name, std::string as = "");
|
||||
column(const std::shared_ptr<table> &t, std::string name, std::string as = "");
|
||||
|
||||
[[nodiscard]] bool equals(const column &x) const;
|
||||
@@ -33,13 +34,17 @@ struct column {
|
||||
[[nodiscard]] std::string full_name() const;
|
||||
[[nodiscard]] const std::string& alias_name() const;
|
||||
[[nodiscard]] bool is_function() const;
|
||||
[[nodiscard]] sql_function_t function() const;
|
||||
[[nodiscard]] bool has_alias() const;
|
||||
[[nodiscard]] std::string alias() const;
|
||||
[[nodiscard]] std::shared_ptr<sql::table> table() const;
|
||||
void table(const std::shared_ptr<sql::table>& t);
|
||||
|
||||
private:
|
||||
std::shared_ptr<sql::table> table_;
|
||||
std::string name;
|
||||
std::string alias;
|
||||
sql_function_t function_{sql_function_t::NONE};
|
||||
std::string alias_;
|
||||
sql_function_t function_{sql_function_t::None};
|
||||
};
|
||||
|
||||
column operator ""_col(const char *name, size_t len);
|
||||
|
||||
@@ -54,6 +54,7 @@ public:
|
||||
[[nodiscard]] std::string prepare_identifier(const column &col) const;
|
||||
[[nodiscard]] std::string prepare_identifier_string(const std::string &col) const;
|
||||
[[nodiscard]] std::string prepare_condition(const column &col) const;
|
||||
[[nodiscard]] std::string table_name(const std::string &table, const std::string &schema_name) const;
|
||||
|
||||
[[nodiscard]] const std::string& to_string(bool val) const;
|
||||
|
||||
@@ -124,6 +125,56 @@ public:
|
||||
*/
|
||||
[[nodiscard]] std::string default_schema_name() const;
|
||||
|
||||
[[nodiscard]] const std::string& add_constraint() const;
|
||||
[[nodiscard]] const std::string& alter() const;
|
||||
[[nodiscard]] const std::string& and_() const;
|
||||
[[nodiscard]] const std::string& as() const;
|
||||
[[nodiscard]] const std::string& asc() const;
|
||||
[[nodiscard]] const std::string& begin() const;
|
||||
[[nodiscard]] const std::string& asterisk() const;
|
||||
[[nodiscard]] const std::string& begin_binary_data() const;
|
||||
[[nodiscard]] const std::string& begin_string_data() const;
|
||||
[[nodiscard]] const std::string& between() const;
|
||||
[[nodiscard]] const std::string& column() const;
|
||||
[[nodiscard]] const std::string& columns() const;
|
||||
[[nodiscard]] const std::string& commit() const;
|
||||
[[nodiscard]] const std::string& create() const;
|
||||
[[nodiscard]] const std::string& desc() const;
|
||||
[[nodiscard]] const std::string& distinct() const;
|
||||
[[nodiscard]] const std::string& drop() const;
|
||||
[[nodiscard]] const std::string& drop_constraint() const;
|
||||
[[nodiscard]] const std::string& end_binary_data() const;
|
||||
[[nodiscard]] const std::string& end_quote() const;
|
||||
[[nodiscard]] const std::string& end_string_data() const;
|
||||
[[nodiscard]] const std::string& foreign_key() const;
|
||||
[[nodiscard]] const std::string& from() const;
|
||||
[[nodiscard]] const std::string& group_by() const;
|
||||
[[nodiscard]] const std::string& in() const;
|
||||
[[nodiscard]] const std::string& insert() const;
|
||||
[[nodiscard]] const std::string& into() const;
|
||||
[[nodiscard]] const std::string& join() const;
|
||||
[[nodiscard]] const std::string& like() const;
|
||||
[[nodiscard]] const std::string& limit() const;
|
||||
[[nodiscard]] const std::string& not_() const;
|
||||
[[nodiscard]] const std::string& not_null() const;
|
||||
[[nodiscard]] const std::string& offset() const;
|
||||
[[nodiscard]] const std::string& on() const;
|
||||
[[nodiscard]] const std::string& or_() const;
|
||||
[[nodiscard]] const std::string& order_by() const;
|
||||
[[nodiscard]] const std::string& primary_key() const;
|
||||
[[nodiscard]] const std::string& references() const;
|
||||
[[nodiscard]] const std::string& remove() const;
|
||||
[[nodiscard]] const std::string& rollback() const;
|
||||
[[nodiscard]] const std::string& schema() const;
|
||||
[[nodiscard]] const std::string& select() const;
|
||||
[[nodiscard]] const std::string& set() const;
|
||||
[[nodiscard]] const std::string& start_quote() const;
|
||||
[[nodiscard]] const std::string& string_quote() const;
|
||||
[[nodiscard]] const std::string& table() const;
|
||||
[[nodiscard]] const std::string& update() const;
|
||||
[[nodiscard]] const std::string& values() const;
|
||||
[[nodiscard]] const std::string& where() const;
|
||||
|
||||
private:
|
||||
dialect() = default;
|
||||
|
||||
@@ -170,15 +221,16 @@ private:
|
||||
{dialect_token::Limit, "LIMIT"},
|
||||
{dialect_token::None, ""},
|
||||
{dialect_token::Not, "NOT"},
|
||||
{dialect_token::NotNull, "NOT NULL"},
|
||||
{dialect_token::NotNull, "NOT NULL"},
|
||||
{dialect_token::Offset, "OFFSET"},
|
||||
{dialect_token::On, "ON"},
|
||||
{dialect_token::Or, "OR"},
|
||||
{dialect_token::OrderBy, "ORDER BY"},
|
||||
{dialect_token::PrimaryKey, "PRIMARY KEY"},
|
||||
{dialect_token::References, "REFERENCES"},
|
||||
{dialect_token::PrimaryKey, "PRIMARY KEY"},
|
||||
{dialect_token::References, "REFERENCES"},
|
||||
{dialect_token::Remove, "DELETE"},
|
||||
{dialect_token::Rollback, "ROLLBACK TRANSACTION"},
|
||||
{dialect_token::Schema, "SCHEMA"},
|
||||
{dialect_token::Select, "SELECT"},
|
||||
{dialect_token::Set, "SET"},
|
||||
{dialect_token::StartQuote, "\""},
|
||||
@@ -210,12 +262,12 @@ private:
|
||||
};
|
||||
|
||||
sql_func_to_string_map sql_func_map_ {
|
||||
{sql_function_t::NONE, "NONE" },
|
||||
{sql_function_t::COUNT, "COUNT" },
|
||||
{sql_function_t::AVG, "AVG" },
|
||||
{sql_function_t::SUM, "SUM" },
|
||||
{sql_function_t::MIN, "MIN" },
|
||||
{sql_function_t::MAX, "MAX" },
|
||||
{sql_function_t::None, "NONE" },
|
||||
{sql_function_t::Count, "COUNT" },
|
||||
{sql_function_t::Avg, "AVG" },
|
||||
{sql_function_t::Sum, "SUM" },
|
||||
{sql_function_t::Min, "MIN" },
|
||||
{sql_function_t::Max, "MAX" },
|
||||
};
|
||||
|
||||
std::array<std::string, 2> bool_strings_ {
|
||||
|
||||
@@ -34,6 +34,7 @@ struct query_context {
|
||||
std::string sql;
|
||||
sql_command command{};
|
||||
std::string command_name{};
|
||||
std::string schema_name{};
|
||||
sql::table table{""};
|
||||
std::vector<object::attribute_definition> prototype{};
|
||||
std::vector<std::string> result_vars{};
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
struct column;
|
||||
class column;
|
||||
|
||||
class record final {
|
||||
private:
|
||||
@@ -41,7 +41,7 @@ public:
|
||||
|
||||
[[nodiscard]] const std::vector<field_ref>& columns() const;
|
||||
|
||||
[[nodiscard]] const field& at(const column &col) const;
|
||||
[[nodiscard]] const field& at(const sql::column &col) const;
|
||||
[[nodiscard]] const field& at(size_t index) const;
|
||||
template<class Type>
|
||||
std::optional<Type> at(const column &col) const {
|
||||
|
||||
@@ -8,18 +8,16 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
struct column;
|
||||
class column;
|
||||
|
||||
struct table {
|
||||
class table {
|
||||
public:
|
||||
table() = default;
|
||||
table(const char *name); // NOLINT(*-explicit-constructor)
|
||||
table(std::string name); // NOLINT(*-explicit-constructor)
|
||||
table(const char *name, std::string as); // NOLINT(*-explicit-constructor)
|
||||
table(std::string name, std::string as); // NOLINT(*-explicit-constructor)
|
||||
table(std::string name, std::string as, const std::vector<column> &columns)
|
||||
: name(std::move(name))
|
||||
, alias(std::move(as))
|
||||
, columns(columns) {}
|
||||
table(std::string name, std::string as, const std::vector<column> &columns);
|
||||
|
||||
table& as(const std::string &a);
|
||||
|
||||
@@ -29,13 +27,20 @@ struct table {
|
||||
|
||||
[[nodiscard]] bool has_alias() const;
|
||||
|
||||
std::string name;
|
||||
std::string alias;
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] const std::string& alias() const;
|
||||
[[nodiscard]] const std::vector<column>& columns() const;
|
||||
private:
|
||||
friend class column;
|
||||
|
||||
std::vector<column> columns;
|
||||
std::string name_;
|
||||
std::string alias_;
|
||||
|
||||
std::string schema_name_;
|
||||
std::vector<column> columns_;
|
||||
};
|
||||
|
||||
table operator "" _tab(const char *name, size_t len);
|
||||
table operator ""_tab(const char *name, size_t len);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user