added query helper macro and join query parts
This commit is contained in:
parent
b11993d60b
commit
b255ae22b4
|
|
@ -11,6 +11,10 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
|||
|
||||
set(GCC_CLANG_COMMON_FLAGS "-Wall -Wconversion -Wextra -pedantic -ftemplate-backtrace-limit=0")
|
||||
|
||||
if (WIN32)
|
||||
add_compile_options(/Zc:preprocessor)
|
||||
endif()
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
|
||||
|
||||
find_package(ODBC REQUIRED)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
#ifndef QUERY_MACRO_MAP_HPP
|
||||
#define QUERY_MACRO_MAP_HPP
|
||||
|
||||
#define EVAL0(...) __VA_ARGS__
|
||||
#define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__)))
|
||||
#define EVAL2(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__)))
|
||||
#define EVAL3(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__)))
|
||||
#define EVAL4(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__)))
|
||||
#define EVAL(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__)))
|
||||
|
||||
#define MAP_END(...)
|
||||
#define MAP_OUT
|
||||
#define MAP_COMMA ,
|
||||
|
||||
#define MAP_GET_END2() 0, MAP_END
|
||||
#define MAP_GET_END1(...) MAP_GET_END2
|
||||
#define MAP_GET_END(...) MAP_GET_END1
|
||||
#define MAP_NEXT0(test, next, ...) next MAP_OUT
|
||||
#define MAP_NEXT1(test, next) MAP_NEXT0(test, next, 0)
|
||||
#define MAP_NEXT(test, next) MAP_NEXT1(MAP_GET_END test, next)
|
||||
|
||||
#define MAP0(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP1)(f, peek, __VA_ARGS__)
|
||||
#define MAP1(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP0)(f, peek, __VA_ARGS__)
|
||||
|
||||
#define MAP_LIST_NEXT1(test, next) MAP_NEXT0(test, MAP_COMMA next, 0)
|
||||
#define MAP_LIST_NEXT(test, next) MAP_LIST_NEXT1(MAP_GET_END test, next)
|
||||
|
||||
#define MAP_LIST0(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST1)(f, peek, __VA_ARGS__)
|
||||
#define MAP_LIST1(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST0)(f, peek, __VA_ARGS__)
|
||||
|
||||
/**
|
||||
* Applies the function macro `f` to each of the remaining parameters.
|
||||
*/
|
||||
#define MAP(f, ...) EVAL(MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
|
||||
|
||||
/**
|
||||
* Applies the function macro `f` to each of the remaining parameters and
|
||||
* inserts commas between the results.
|
||||
*/
|
||||
#define MAP_LIST(f, ...) EVAL(MAP_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
|
||||
|
||||
#endif //QUERY_MACRO_MAP_HPP
|
||||
|
|
@ -6,6 +6,9 @@
|
|||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
#include "query_helper.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
struct author
|
||||
|
|
@ -46,26 +49,36 @@ struct book
|
|||
}
|
||||
};
|
||||
|
||||
QUERY_HELPER(author, id, first_name, last_name, date_of_birth, year_of_birth, distinguished)
|
||||
QUERY_HELPER(book, id, book_author, title, published_in)
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace matador::sql;
|
||||
|
||||
const std::string env_var{"MATADOR_BACKENDS_PATH"};
|
||||
|
||||
std::string dns{"sqlite://demo.db"};
|
||||
// std::string dns{"memory://test"};
|
||||
auto s = std::make_shared<schema>();
|
||||
s->attach<author>("authors");
|
||||
s->attach<book>("books");
|
||||
|
||||
connection c(dns, s);
|
||||
|
||||
auto books = c.query()
|
||||
.select<book>({"author.name"})
|
||||
.from("book")
|
||||
.join_left("author")
|
||||
.select<book>({qh::book.id.name})
|
||||
.from({"book"})
|
||||
.join_left({"authors"})
|
||||
.on("book.author_id"_col == "author.id"_col)
|
||||
.where("book.published_in"_col < 2008 && "author.name"_col == "Michael Crichton")
|
||||
.order_by("book.title").asc()
|
||||
.offset(2)
|
||||
.limit(5)
|
||||
.fetch_all<book>();
|
||||
.str();
|
||||
// .fetch_all<book>();
|
||||
|
||||
std::cout << "SQL: " << books << "\n";
|
||||
|
||||
// SELECT book.title, book.id, book.author_id, book.published_in, author.name
|
||||
// FROM book
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
#ifndef QUERY_QUERY_HELPER_HPP
|
||||
#define QUERY_QUERY_HELPER_HPP
|
||||
|
||||
#include "macro_map.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <ostream>
|
||||
|
||||
template<typename QueryClass>
|
||||
struct basic_query;
|
||||
|
||||
struct table {
|
||||
template<typename QueryClass>
|
||||
table(const basic_query<QueryClass> &qc);
|
||||
const std::string name;
|
||||
};
|
||||
|
||||
struct field {
|
||||
const std::string name;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream &out, const field &f) {
|
||||
out << f.name;
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename QueryClass>
|
||||
struct basic_query {
|
||||
const table& operator()() const {
|
||||
return table_;
|
||||
}
|
||||
|
||||
const table table_;
|
||||
};
|
||||
|
||||
template<typename QueryClass>
|
||||
table::table(const basic_query<QueryClass>&)
|
||||
: name(QueryClass::internal_table_name_) {
|
||||
}
|
||||
|
||||
#define FIELD(x) const field x{#x};
|
||||
|
||||
#define QUERY_HELPER(C, ...) \
|
||||
namespace qh { \
|
||||
namespace internal { \
|
||||
struct C##_query { \
|
||||
MAP(FIELD, __VA_ARGS__) \
|
||||
}; } \
|
||||
static const internal:: C##_query C; \
|
||||
}
|
||||
|
||||
#endif //QUERY_QUERY_HELPER_HPP
|
||||
|
|
@ -24,6 +24,8 @@ public:
|
|||
private:
|
||||
void visit(query_select_part &select_part) override;
|
||||
void visit(query_from_part &from_part) override;
|
||||
void visit(query_join_part &join_part) override;
|
||||
void visit(query_on_part &on_part) override;
|
||||
void visit(query_where_part &where_part) override;
|
||||
void visit(query_group_by_part &group_by_part) override;
|
||||
void visit(query_order_by_part &order_by_part) override;
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ public:
|
|||
|
||||
statement prepare();
|
||||
|
||||
std::string str() const;
|
||||
[[nodiscard]] std::string str() const;
|
||||
|
||||
private:
|
||||
std::unique_ptr<query_result_impl> fetch();
|
||||
|
|
@ -148,7 +148,12 @@ class query_join_intermediate : public query_intermediate
|
|||
public:
|
||||
using query_intermediate::query_intermediate;
|
||||
|
||||
query_on_intermediate on(const basic_condition &cond);
|
||||
template<class Condition>
|
||||
query_on_intermediate on(const Condition &cond)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_on_part>(cond));
|
||||
return {connection_, data_};
|
||||
}
|
||||
};
|
||||
|
||||
class query_from_intermediate : public query_select_finish
|
||||
|
|
@ -156,7 +161,7 @@ class query_from_intermediate : public query_select_finish
|
|||
public:
|
||||
using query_select_finish::query_select_finish;
|
||||
|
||||
query_join_intermediate join_left(const std::string &join_table_name, const std::string &as = "");
|
||||
query_join_intermediate join_left(const table &t);
|
||||
query_where_intermediate where(const basic_condition &cond);
|
||||
query_group_by_intermediate group_by(const std::string &name);
|
||||
query_order_by_intermediate order_by(const std::string &name);
|
||||
|
|
@ -176,7 +181,7 @@ class query_select_intermediate : public query_start_intermediate
|
|||
public:
|
||||
query_select_intermediate(connection &s, const std::vector<column>& columns);
|
||||
|
||||
query_from_intermediate from(const std::string &table, const std::string &as = "");
|
||||
query_from_intermediate from(const table& t);
|
||||
};
|
||||
|
||||
template < class Type >
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ namespace matador::sql {
|
|||
|
||||
class query_select_part;
|
||||
class query_from_part;
|
||||
class query_join_part;
|
||||
class query_on_part;
|
||||
class query_where_part;
|
||||
class query_group_by_part;
|
||||
class query_order_by_part;
|
||||
|
|
@ -24,6 +26,8 @@ public:
|
|||
|
||||
virtual void visit(query_select_part &select_part) = 0;
|
||||
virtual void visit(query_from_part &from_part) = 0;
|
||||
virtual void visit(query_join_part &join_part) = 0;
|
||||
virtual void visit(query_on_part &on_part) = 0;
|
||||
virtual void visit(query_where_part &where_part) = 0;
|
||||
virtual void visit(query_group_by_part &group_by_part) = 0;
|
||||
virtual void visit(query_order_by_part &order_by_part) = 0;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
#ifndef QUERY_QUERY_PARTS_HPP
|
||||
#define QUERY_QUERY_PARTS_HPP
|
||||
|
||||
#include "matador/sql/basic_condition.hpp"
|
||||
#include "matador/sql/query_part_visitor.hpp"
|
||||
#include "matador/sql/column.hpp"
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
#include "matador/sql/dialect.hpp"
|
||||
#include "matador/sql/table.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
|
@ -46,15 +48,44 @@ private:
|
|||
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);
|
||||
explicit query_from_part(sql::table t);
|
||||
|
||||
[[nodiscard]] const sql::table& table() const;
|
||||
|
||||
private:
|
||||
void accept(query_part_visitor &visitor) override;
|
||||
|
||||
private:
|
||||
std::string table_;
|
||||
std::string alias_;
|
||||
sql::table table_;
|
||||
};
|
||||
|
||||
class query_join_part : public query_part
|
||||
{
|
||||
public:
|
||||
explicit query_join_part(sql::table t);
|
||||
|
||||
[[nodiscard]] const sql::table& table() const;
|
||||
|
||||
private:
|
||||
void accept(query_part_visitor &visitor) override;
|
||||
|
||||
private:
|
||||
sql::table table_;
|
||||
};
|
||||
|
||||
class query_on_part : public query_part
|
||||
{
|
||||
public:
|
||||
template < class Condition >
|
||||
explicit query_on_part(const Condition &cond)
|
||||
: query_part(dialect::token_t::ON)
|
||||
, condition_(new Condition(cond)) {}
|
||||
|
||||
private:
|
||||
void accept(query_part_visitor &visitor) override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<basic_condition> condition_;
|
||||
};
|
||||
|
||||
class query_where_part : public query_part
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ public:
|
|||
using iterator = repository::iterator;
|
||||
using const_iterator = repository::const_iterator;
|
||||
|
||||
std::string name() const;
|
||||
[[nodiscard]] std::string name() const;
|
||||
|
||||
template<typename Type>
|
||||
const table_info& attach(const std::string &table_name)
|
||||
|
|
|
|||
|
|
@ -8,8 +8,18 @@ namespace matador::sql {
|
|||
|
||||
struct table
|
||||
{
|
||||
std::type_index index;
|
||||
// std::type_index index;
|
||||
std::string name;
|
||||
std::string alias;
|
||||
|
||||
table(std::string name, std::string as = "") // NOLINT(*-explicit-constructor)
|
||||
: name(std::move(name)), alias(std::move(as)) {}
|
||||
|
||||
table& as(const std::string &a) {
|
||||
alias = a;
|
||||
return *this;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
#include "matador/sql/dialect.hpp"
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
|
||||
#include "matador/utils/string.hpp"
|
||||
|
||||
|
|
|
|||
|
|
@ -6,12 +6,12 @@
|
|||
namespace matador::sql {
|
||||
|
||||
query_compiler::query_compiler(const sql::dialect &d)
|
||||
: dialect_(d)
|
||||
: dialect_(d)
|
||||
{}
|
||||
|
||||
query_context query_compiler::compile(const query_data *data)
|
||||
{
|
||||
for (const auto &part : data->parts) {
|
||||
for (const auto &part: data->parts) {
|
||||
part->accept(*this);
|
||||
}
|
||||
|
||||
|
|
@ -20,6 +20,10 @@ query_context query_compiler::compile(const query_data *data)
|
|||
|
||||
void query_compiler::visit(query_select_part &select_part)
|
||||
{
|
||||
query_.sql = dialect_.token_at(dialect::token_t::SELECT) + " ";
|
||||
|
||||
query_.prototype.clear();
|
||||
|
||||
std::string result;
|
||||
const auto &columns = select_part.columns();
|
||||
if (columns.size() < 2) {
|
||||
|
|
@ -40,9 +44,30 @@ void query_compiler::visit(query_select_part &select_part)
|
|||
query_.prototype.append(column_definition{(*it).name});
|
||||
}
|
||||
}
|
||||
|
||||
query_.sql += result;
|
||||
}
|
||||
|
||||
void query_compiler::visit(query_from_part &from_part)
|
||||
{
|
||||
if (dialect_.default_schema_name().empty()) {
|
||||
query_.sql += " " + dialect_.token_at(dialect::token_t::FROM) +
|
||||
" " + dialect_.prepare_identifier(from_part.table().name) +
|
||||
(from_part.table().alias.empty() ? "" : " AS " + dialect_.prepare_identifier(from_part.table().alias));
|
||||
} else {
|
||||
query_.sql += " " + dialect_.token_at(dialect::token_t::FROM) +
|
||||
" " + dialect_.prepare_identifier(dialect_.default_schema_name()) +
|
||||
"." + dialect_.prepare_identifier(from_part.table().name) +
|
||||
(from_part.table().alias.empty() ? "" : " AS " + dialect_.prepare_identifier(from_part.table().alias));
|
||||
}
|
||||
}
|
||||
|
||||
void query_compiler::visit(query_join_part &join_part)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void query_compiler::visit(query_on_part &on_part)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "matador/sql/query_intermediates.hpp"
|
||||
#include "matador/sql/session.hpp"
|
||||
#include "matador/sql/query_compiler.hpp"
|
||||
#include "matador/sql/condition.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
basic_query_intermediate::basic_query_intermediate(connection &db)
|
||||
|
|
@ -125,16 +126,10 @@ query_order_by_intermediate query_on_intermediate::order_by(const std::string &n
|
|||
// return {connection_, builder_.order_by(name)};
|
||||
}
|
||||
|
||||
query_on_intermediate query_join_intermediate::on(const basic_condition &cond)
|
||||
query_join_intermediate query_from_intermediate::join_left(const table &t)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_join_part>(t));
|
||||
return {connection_, data_};
|
||||
// return {connection_, builder_.on(column, join_column)};
|
||||
}
|
||||
|
||||
query_join_intermediate query_from_intermediate::join_left(const std::string &join_table_name, const std::string &as)
|
||||
{
|
||||
return {connection_, data_};
|
||||
// return {connection_, builder_.join(join_table_name, join_type_t::INNER, as)};
|
||||
}
|
||||
|
||||
query_where_intermediate query_from_intermediate::where(const basic_condition &cond)
|
||||
|
|
@ -149,8 +144,9 @@ query_select_intermediate::query_select_intermediate(connection &db, const std::
|
|||
data_.parts.push_back(std::make_unique<query_select_part>(columns));
|
||||
}
|
||||
|
||||
query_from_intermediate query_select_intermediate::from(const std::string &table, const std::string &as)
|
||||
query_from_intermediate query_select_intermediate::from(const table& t)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_from_part>(t));
|
||||
return {connection_, data_};
|
||||
// return {connection_, builder_.from(table, as)};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,20 +20,39 @@ const std::vector<column>& query_select_part::columns() const
|
|||
return columns_;
|
||||
}
|
||||
|
||||
query_from_part::query_from_part(std::string table_name)
|
||||
query_from_part::query_from_part(sql::table t)
|
||||
: query_part(sql::dialect::token_t::FROM)
|
||||
, table_(std::move(table_name)) {}
|
||||
, table_(std::move(t)) {}
|
||||
|
||||
query_from_part::query_from_part(std::string table_name, std::string as)
|
||||
: query_part(sql::dialect::token_t::FROM)
|
||||
, table_(std::move(table_name))
|
||||
, alias_(std::move(as)) {}
|
||||
const sql::table &query_from_part::table() const
|
||||
{
|
||||
return table_;
|
||||
}
|
||||
|
||||
void query_from_part::accept(query_part_visitor &visitor)
|
||||
{
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
query_join_part::query_join_part(sql::table t)
|
||||
: query_part(sql::dialect::token_t::JOIN)
|
||||
, table_(std::move(t)) {}
|
||||
|
||||
const sql::table &query_join_part::table() const
|
||||
{
|
||||
return table_;
|
||||
}
|
||||
|
||||
void query_join_part::accept(query_part_visitor &visitor)
|
||||
{
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
void query_on_part::accept(query_part_visitor &visitor)
|
||||
{
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
void query_where_part::accept(query_part_visitor &visitor)
|
||||
{
|
||||
visitor.visit(*this);
|
||||
|
|
|
|||
Loading…
Reference in New Issue