initial matador ng commit

This commit is contained in:
2025-02-02 20:37:12 +01:00
parent 19de5714f4
commit ded3daceb3
378 changed files with 14913 additions and 13431 deletions
+101 -119
View File
@@ -2,9 +2,12 @@
#define QUERY_DIALECT_HPP
#include "matador/sql/column.hpp"
#include "matador/sql/data_type_traits.hpp"
#include "matador/sql/dialect_token.hpp"
#include "matador/utils/basic_types.hpp"
#include "matador/utils/types.hpp"
#include "matador/utils/string.hpp"
#include <cstdint>
#include <functional>
#include <string>
#include <unordered_map>
@@ -12,54 +15,11 @@
namespace matador::sql {
class connection_impl;
class dialect final
{
public:
enum class token_t : uint8_t
{
CREATE = 0,
DROP,
REMOVE,
INSERT,
UPDATE,
SELECT,
TABLE,
VALUES,
INSERT_VALUES,
COLUMNS,
COLUMN,
FROM,
JOIN,
ON,
INTO,
WHERE,
WHERE_CLAUSE,
AND,
OR,
LIKE,
ORDER_BY,
GROUP_BY,
ASC,
DESC,
LIMIT,
AS,
OFFSET,
DISTINCT,
SET,
UPDATE_VALUES,
NOT_NULL,
PRIMARY_KEY,
BEGIN,
COMMIT,
ROLLBACK,
START_QUOTE,
END_QUOTE,
STRING_QUOTE,
BEGIN_BINARY_DATA,
END_BINARY_DATA,
NONE
};
/**
* Holding enums concerning escaping identifiers
*/
@@ -68,26 +28,32 @@ public:
ESCAPE_CLOSING_BRACKET /**< The escape quotes differ; escape the closing one */
};
using token_to_string_map = std::unordered_map<token_t, std::string>;
using data_type_to_string_map = std::unordered_map<data_type_t, std::string>;
using token_to_string_map = std::unordered_map<dialect_token, std::string>;
using data_type_to_string_map = std::unordered_map<utils::basic_type, std::string>;
using sql_func_to_string_map = std::unordered_map<sql_function_t, std::string>;
using next_placeholder_func = std::function<std::string(size_t)>;
using to_escaped_string_func = std::function<std::string(const utils::blob &)>;
public:
[[nodiscard]] const std::string& token_at(token_t token) const;
[[nodiscard]] const std::string& data_type_at(data_type_t type) const;
[[nodiscard]] const std::string& token_at(dialect_token token) const;
[[nodiscard]] const std::string& data_type_at(utils::basic_type type) const;
/**
* Prepare sql dialect identifier for execution
* and escape quotes and quote the identifier
* string
*
* @param str The identifier string to be prepared
* @return The prepared string
*/
* Prepare sql dialect identifier for execution
* and escape quotes and quote the identifier
* string
*
* @param col The identifier string to be prepared
* @return The prepared string
*/
[[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]] const std::string& to_string(bool val) const;
void bool_strings(const std::string &true_string, const std::string &false_string);
/**
* Prepare string literal
@@ -123,7 +89,17 @@ public:
*
* @return How the identifier quotes should be escaped
*/
[[nodiscard]] virtual escape_identifier_t identifier_escape_type() const;
[[nodiscard]] escape_identifier_t identifier_escape_type() const;
/**
* Sets the identifier escape type. Possibilities are
* opening and closing escape characters are the same
* (ESCAPE_BOTH_SAME) or using a special closing
* escape character (ESCAPE_CLOSING_BRACKET)
*
* @param escape_identifier Identifier escape type
*/
void identifier_escape_type(escape_identifier_t escape_identifier);
/**
* Generates a next placeholder string. default is
@@ -133,6 +109,8 @@ public:
*/
[[nodiscard]] std::string next_placeholder(const std::vector<std::string> &bind_vars) const;
[[nodiscard]] std::string to_escaped_string(const utils::blob &value, const connection_impl *conn = nullptr) const;
/**
* Returns the default schema name.
*
@@ -147,73 +125,73 @@ private:
friend class dialect_builder;
next_placeholder_func placeholder_func_ = [](size_t) { return "?"; };
// to_escaped_string_func to_escaped_string_func_ = [](const utils::blob &val) { return utils::to_string(val); };
escape_identifier_t identifier_escape_type_ = escape_identifier_t::ESCAPE_BOTH_SAME;
std::string default_schema_name_;
// std::unique_ptr<query_compiler> compiler_;
token_to_string_map tokens_ {
{token_t::CREATE, "CREATE"},
{token_t::DROP, "DROP"},
{token_t::REMOVE, "DELETE"},
{token_t::INSERT, "INSERT"},
{token_t::TABLE, "TABLE"},
{token_t::INTO, "INTO"},
{token_t::VALUES, "VALUES"},
{token_t::UPDATE, "UPDATE"},
{token_t::SELECT, "SELECT"},
{token_t::COLUMNS, "COLUMNS"},
{token_t::COLUMN, "COLUMN"},
{token_t::FROM, "FROM"},
{token_t::JOIN, "INNER JOIN"},
{token_t::ON, "ON"},
{token_t::WHERE, "WHERE"},
{token_t::AND, "AND"},
{token_t::OR, "OR"},
{token_t::LIKE, "LIKE"},
{token_t::ORDER_BY, "ORDER BY"},
{token_t::GROUP_BY, "GROUP BY"},
{token_t::ASC, "ASC"},
{token_t::DESC, "DESC"},
{token_t::OFFSET, "OFFSET"},
{token_t::LIMIT, "LIMIT"},
{token_t::AS, "AS"},
{token_t::OFFSET, "OFFSET"},
{token_t::DISTINCT, "DISTINCT"},
{token_t::SET, "SET"},
{token_t::NOT_NULL, "NOT NULL"},
{token_t::PRIMARY_KEY, "PRIMARY KEY"},
{token_t::BEGIN, "BEGIN TRANSACTION"},
{token_t::COMMIT, "COMMIT TRANSACTION"},
{token_t::ROLLBACK, "ROLLBACK TRANSACTION"},
{token_t::START_QUOTE, "\""},
{token_t::END_QUOTE, "\""},
{token_t::STRING_QUOTE, "'"},
{token_t::BEGIN_BINARY_DATA, "X'"},
{token_t::END_BINARY_DATA, "'"},
{token_t::NONE, ""}
{dialect_token::CREATE, "CREATE"},
{dialect_token::DROP, "DROP"},
{dialect_token::REMOVE, "DELETE"},
{dialect_token::INSERT, "INSERT"},
{dialect_token::TABLE, "TABLE"},
{dialect_token::INTO, "INTO"},
{dialect_token::VALUES, "VALUES"},
{dialect_token::UPDATE, "UPDATE"},
{dialect_token::SELECT, "SELECT"},
{dialect_token::COLUMNS, "COLUMNS"},
{dialect_token::COLUMN, "COLUMN"},
{dialect_token::FROM, "FROM"},
{dialect_token::JOIN, "LEFT JOIN"},
{dialect_token::ON, "ON"},
{dialect_token::WHERE, "WHERE"},
{dialect_token::AND, "AND"},
{dialect_token::OR, "OR"},
{dialect_token::LIKE, "LIKE"},
{dialect_token::ORDER_BY, "ORDER BY"},
{dialect_token::GROUP_BY, "GROUP BY"},
{dialect_token::ASC, "ASC"},
{dialect_token::DESC, "DESC"},
{dialect_token::OFFSET, "OFFSET"},
{dialect_token::LIMIT, "LIMIT"},
{dialect_token::AS, "AS"},
{dialect_token::OFFSET, "OFFSET"},
{dialect_token::DISTINCT, "DISTINCT"},
{dialect_token::SET, "SET"},
{dialect_token::NOT_NULL, "NOT NULL"},
{dialect_token::PRIMARY_KEY, "PRIMARY KEY"},
{dialect_token::BEGIN, "BEGIN TRANSACTION"},
{dialect_token::COMMIT, "COMMIT TRANSACTION"},
{dialect_token::ROLLBACK, "ROLLBACK TRANSACTION"},
{dialect_token::START_QUOTE, "\""},
{dialect_token::END_QUOTE, "\""},
{dialect_token::STRING_QUOTE, "'"},
{dialect_token::BEGIN_BINARY_DATA, "X'"},
{dialect_token::END_BINARY_DATA, "'"},
{dialect_token::NONE, ""}
};
data_type_to_string_map data_types_ {
{data_type_t::type_char, "TINYINT"},
{data_type_t::type_short, "SMALLINT"},
{data_type_t::type_int, "INTEGER"},
{data_type_t::type_long, "BIGINT"},
{data_type_t::type_long_long, "BIGINT"},
{data_type_t::type_unsigned_char, "TINYINT"},
{data_type_t::type_unsigned_short, "INTEGER"},
{data_type_t::type_unsigned_int, "BIGINT"},
{data_type_t::type_unsigned_long, "BIGINT"},
{data_type_t::type_unsigned_long_long, "BIGINT"},
{data_type_t::type_float, "FLOAT"},
{data_type_t::type_double, "DOUBLE"},
{data_type_t::type_bool, "BOOLEAN"},
{data_type_t::type_char_pointer, "VARCHAR"},
{data_type_t::type_varchar, "VARCHAR"},
{data_type_t::type_text, "TEXT"},
{data_type_t::type_date, "DATE"},
{data_type_t::type_time, "DATETIME"},
{data_type_t::type_blob, "BLOB"},
{data_type_t::type_null, "NULL"},
{data_type_t::type_unknown, "UNKNOWN"}
{utils::basic_type::type_int8, "TINYINT"},
{utils::basic_type::type_int16, "SMALLINT"},
{utils::basic_type::type_int32, "INTEGER"},
{utils::basic_type::type_int64, "BIGINT"},
{utils::basic_type::type_uint8, "TINYINT"},
{utils::basic_type::type_uint16, "INTEGER"},
{utils::basic_type::type_uint32, "BIGINT"},
{utils::basic_type::type_uint64, "BIGINT"},
{utils::basic_type::type_float, "FLOAT"},
{utils::basic_type::type_double, "DOUBLE"},
{utils::basic_type::type_bool, "BOOLEAN"},
{utils::basic_type::type_varchar, "VARCHAR"},
{utils::basic_type::type_text, "TEXT"},
{utils::basic_type::type_date, "DATE"},
{utils::basic_type::type_time, "DATETIME"},
{utils::basic_type::type_blob, "BLOB"},
{utils::basic_type::type_null, "NULL"}
};
sql_func_to_string_map sql_func_map_ {
@@ -224,6 +202,10 @@ private:
{sql_function_t::MIN, "MIN" },
{sql_function_t::MAX, "MAX" },
};
std::array<std::string, 2> bool_strings_ {
"0", "1"
};
};
}