query compiler progress

This commit is contained in:
2024-02-18 18:12:00 +01:00
parent b966a7a1a7
commit a3f467a5a8
62 changed files with 1934 additions and 641 deletions
+25
View File
@@ -0,0 +1,25 @@
#ifndef QUERY_COLUMN_HPP
#define QUERY_COLUMN_HPP
#include <string>
namespace matador::query {
class column {
public:
column(const char *name); // NOLINT(*-explicit-constructor)
column(std::string name); // NOLINT(*-explicit-constructor)
column(std::string table_name, std::string name, std::string as);
column& as(std::string a);
std::string table;
std::string name;
std::string alias;
};
column operator "" _col(const char *name, size_t len);
}
#endif //QUERY_COLUMN_HPP
+22
View File
@@ -0,0 +1,22 @@
#ifndef QUERY_QUERY_HPP
#define QUERY_QUERY_HPP
#include "matador/query/query_parts.hpp"
namespace matador::query {
class connection;
class query
{
public:
explicit query(connection &c);
query& select(const std::vector<column> &columns);
query& from(const std::string &table, const std::string &as = "");
private:
connection &connection_;
};
}
#endif //QUERY_QUERY_HPP
+24
View File
@@ -0,0 +1,24 @@
#ifndef QUERY_QUERY_BUILDER_HPP
#define QUERY_QUERY_BUILDER_HPP
#include "matador/query/column.hpp"
#include "matador/query/query_intermediates.hpp"
#include "matador/sql/schema.hpp"
#include <memory>
namespace matador::query {
class query_builder
{
public:
explicit query_builder(const std::shared_ptr<sql::schema> &scm);
query_select_intermediate select(std::initializer_list<column> columns);
private:
std::shared_ptr<sql::schema> schema_;
};
}
#endif //QUERY_QUERY_BUILDER_HPP
+37
View File
@@ -0,0 +1,37 @@
#ifndef QUERY_QUERY_COMPILER_HPP
#define QUERY_QUERY_COMPILER_HPP
#include "matador/query/query_part_visitor.hpp"
#include "matador/query/query_parts.hpp"
#include <typeindex>
#include <string>
namespace matador::sql {
class dialect;
}
namespace matador::query {
struct query_data;
class query_compiler : public query_part_visitor
{
public:
explicit query_compiler(sql::dialect& d);
std::string compile(const query_data *data);
private:
void visit(query_select_part &select_part) override;
void visit(query_from_part &from_part) override;
private:
sql::dialect &dialect_;
std::string sql_;
};
}
#endif //QUERY_QUERY_COMPILER_HPP
+24
View File
@@ -0,0 +1,24 @@
#ifndef QUERY_QUERY_DATA_HPP
#define QUERY_QUERY_DATA_HPP
#include "matador/query/query_parts.hpp"
#include "matador/query/sql_commands.hpp"
#include "matador/query/table.hpp"
#include <memory>
#include <vector>
namespace matador::query {
struct query_data
{
SqlCommands command;
std::vector<std::unique_ptr<query_part>> parts;
std::vector<column> columns;
std::unordered_map<std::type_index, table_data> table_map_by_index;
using table_data_ref = std::reference_wrapper<table_data>;
std::unordered_map<std::string, table_data_ref> table_map_by_name;
};
}
#endif //QUERY_QUERY_DATA_HPP
@@ -0,0 +1,132 @@
#ifndef QUERY_QUERY_INTERMEDIATES_HPP
#define QUERY_QUERY_INTERMEDIATES_HPP
#include "matador/sql/schema.hpp"
#include <string>
#include <memory>
namespace matador::sql {
class basic_condition;
}
namespace matador::query {
class query_from_intermediate;
class query_join_intermediate;
class query_on_intermediate;
class query_where_intermediate;
class query_group_by_intermediate;
class query_order_by_intermediate;
class query_order_direction_intermediate;
class query_offset_intermediate;
class query_limit_intermediate;
class query_intermediate
{
public:
explicit query_intermediate(const std::shared_ptr<sql::schema> &scm);
protected:
sql::schema& schema() const;
private:
std::shared_ptr<sql::schema> schema_;
};
class query_select_finish_intermediate : public query_intermediate
{
public:
using query_intermediate::query_intermediate;
void compile();
};
class query_select_intermediate : public query_intermediate
{
public:
using query_intermediate::query_intermediate;
query_from_intermediate from(const std::string &table, const std::string &as = "");
};
class query_from_intermediate : public query_select_finish_intermediate
{
public:
using query_select_finish_intermediate::query_intermediate;
query_join_intermediate join(const std::string &join_table_name, const std::string &as);
query_where_intermediate where(const sql::basic_condition &cond);
query_group_by_intermediate group_by(const std::string &name);
query_order_by_intermediate order_by(const std::string &name);
};
class query_join_intermediate : public query_intermediate
{
public:
using query_intermediate::query_intermediate;
query_on_intermediate on(const std::string &column, const std::string &join_column);
};
class query_on_intermediate : public query_select_finish_intermediate
{
public:
using query_select_finish_intermediate::query_intermediate;
query_join_intermediate join(const std::string &join_table_name, const std::string &as);
query_where_intermediate where(const sql::basic_condition &cond);
query_group_by_intermediate group_by(const std::string &name);
query_order_by_intermediate order_by(const std::string &name);
};
class query_where_intermediate : public query_select_finish_intermediate
{
public:
using query_select_finish_intermediate::query_intermediate;
query_group_by_intermediate group_by(const std::string &name);
query_order_by_intermediate order_by(const std::string &name);
};
class query_group_by_intermediate : public query_select_finish_intermediate
{
public:
using query_select_finish_intermediate::query_intermediate;
query_order_by_intermediate order_by(const std::string &name);
};
class query_order_by_intermediate : public query_intermediate
{
public:
using query_intermediate::query_intermediate;
query_order_direction_intermediate asc();
query_order_direction_intermediate desc();
};
class query_order_direction_intermediate : public query_select_finish_intermediate
{
public:
using query_select_finish_intermediate::query_intermediate;
query_offset_intermediate offset(size_t offset);
query_limit_intermediate limit(size_t limit);
};
class query_offset_intermediate : public query_intermediate
{
public:
using query_intermediate::query_intermediate;
query_limit_intermediate limit(size_t limit);
};
class query_limit_intermediate : public query_select_finish_intermediate
{
public:
using query_select_finish_intermediate::query_intermediate;
};
}
#endif //QUERY_QUERY_INTERMEDIATES_HPP
@@ -0,0 +1,19 @@
#ifndef QUERY_QUERY_PART_VISITOR_HPP
#define QUERY_QUERY_PART_VISITOR_HPP
namespace matador::query {
class query_select_part;
class query_from_part;
class query_part_visitor
{
public:
virtual ~query_part_visitor() = default;
virtual void visit(query_select_part &select_part) = 0;
virtual void visit(query_from_part &from_part) = 0;
};
}
#endif //QUERY_QUERY_PART_VISITOR_HPP
+55
View File
@@ -0,0 +1,55 @@
#ifndef QUERY_QUERY_PARTS_HPP
#define QUERY_QUERY_PARTS_HPP
#include "matador/query/query_part_visitor.hpp"
#include "matador/query/column.hpp"
#include "matador/sql/dialect.hpp"
namespace matador::query {
class query_part
{
protected:
explicit query_part(sql::dialect::token_t token);
public:
virtual ~query_part() = default;
virtual void accept(query_part_visitor &visitor) = 0;
protected:
sql::dialect::token_t token_;
};
/**
* Represents the SQL SELECT part
*/
class query_select_part : public query_part
{
public:
explicit query_select_part(std::vector<column> columns);
void accept(query_part_visitor &visitor) override;
private:
std::vector<column> columns_;
};
/**
* Represents the SQL FROM part
*/
class query_from_part : public query_part
{
public:
explicit query_from_part(std::string table_name);
query_from_part(std::string table_name, std::string as);
private:
void accept(query_part_visitor &visitor) override;
private:
std::string table_;
std::string alias_;
};
}
#endif //QUERY_QUERY_PARTS_HPP
+31
View File
@@ -0,0 +1,31 @@
#ifndef QUERY_SQL_COMMANDS_HPP
#define QUERY_SQL_COMMANDS_HPP
#include "matador/utils/enum_mapper.hpp"
namespace matador::query {
enum class SqlCommands
{
UNKNOWN, /**< Unknown query command */
CREATE, /**< Create query command */
DROP, /**< Drop query command */
SELECT, /**< Select query command */
INSERT, /**< Insert query command */
UPDATE, /**< Update query command */
REMOVE /**< Remove query command */
};
static const utils::enum_mapper<SqlCommands> sql_command_enum({
{SqlCommands::UNKNOWN, "unknown"},
{SqlCommands::CREATE, "create"},
{SqlCommands::DROP, "drop"},
{SqlCommands::SELECT, "select"},
{SqlCommands::INSERT, "insert"},
{SqlCommands::UPDATE, "update"},
{SqlCommands::REMOVE, "delete"}
});
}
#endif //QUERY_SQL_COMMANDS_HPP
+16
View File
@@ -0,0 +1,16 @@
#ifndef QUERY_TABLE_HPP
#define QUERY_TABLE_HPP
#include <typeindex>
#include <string>
namespace matador::query {
struct table_data
{
std::type_index index;
std::string name;
};
}
#endif //QUERY_TABLE_HPP