query parts progress
This commit is contained in:
parent
16d7fd7e76
commit
0822332669
|
|
@ -44,17 +44,18 @@ struct book
|
||||||
namespace field = matador::utils::access;
|
namespace field = matador::utils::access;
|
||||||
field::primary_key(op, "id", id);
|
field::primary_key(op, "id", id);
|
||||||
field::attribute(op, "title", title, 511);
|
field::attribute(op, "title", title, 511);
|
||||||
field::has_one(op, "authors", book_author, matador::utils::default_foreign_attributes);
|
field::has_one(op, "author_id", book_author, matador::utils::default_foreign_attributes);
|
||||||
field::attribute(op, "published_in", published_in);
|
field::attribute(op, "published_in", published_in);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
QUERY_HELPER(authors, id, first_name, last_name, date_of_birth, year_of_birth, distinguished)
|
QUERY_HELPER(authors, id, first_name, last_name, date_of_birth, year_of_birth, distinguished)
|
||||||
QUERY_HELPER(books, id, book_author, title, published_in)
|
QUERY_HELPER(books, id, author_id, title, published_in)
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
using namespace matador::sql;
|
using namespace matador::sql;
|
||||||
|
using namespace matador;
|
||||||
|
|
||||||
const std::string env_var{"MATADOR_BACKENDS_PATH"};
|
const std::string env_var{"MATADOR_BACKENDS_PATH"};
|
||||||
|
|
||||||
|
|
@ -67,12 +68,12 @@ int main()
|
||||||
connection c(dns, s);
|
connection c(dns, s);
|
||||||
|
|
||||||
auto books = c.query()
|
auto books = c.query()
|
||||||
.select<book>({matador::qh::books.id.name})
|
.select<book>({qh::authors.first_name})
|
||||||
.from({"book"})
|
.from(qh::books)
|
||||||
.join_left({"authors"})
|
.join_left(qh::authors)
|
||||||
.on("book.author_id"_col == "author.id"_col)
|
.on(qh::books.author_id == qh::authors.id)
|
||||||
.where("book.published_in"_col < 2008 && "author.name"_col == "Michael Crichton")
|
.where(qh::books.published_in < 2008 && qh::authors.first_name == "Michael Crichton")
|
||||||
.order_by("book.title").asc()
|
.order_by(qh::books.title).asc()
|
||||||
.offset(2)
|
.offset(2)
|
||||||
.limit(5)
|
.limit(5)
|
||||||
.str();
|
.str();
|
||||||
|
|
|
||||||
|
|
@ -1,49 +1,21 @@
|
||||||
#ifndef QUERY_QUERY_HELPER_HPP
|
#ifndef QUERY_QUERY_HELPER_HPP
|
||||||
#define QUERY_QUERY_HELPER_HPP
|
#define QUERY_QUERY_HELPER_HPP
|
||||||
|
|
||||||
#include "macro_map.hpp"
|
#include "matador/utils/macro_map.hpp"
|
||||||
|
|
||||||
|
#include "matador/sql/table.hpp"
|
||||||
|
#include "matador/sql/column.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
template<typename QueryClass>
|
#define FIELD(x) const sql::column x{this->name, #x, ""};
|
||||||
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, ...) \
|
#define QUERY_HELPER(C, ...) \
|
||||||
namespace matador::qh { \
|
namespace matador::qh { \
|
||||||
namespace internal { \
|
namespace internal { \
|
||||||
struct C##_query { \
|
struct C##_query : sql::table { \
|
||||||
|
C##_query() : table(#C) {} \
|
||||||
MAP(FIELD, __VA_ARGS__) \
|
MAP(FIELD, __VA_ARGS__) \
|
||||||
}; } \
|
}; } \
|
||||||
static const internal:: C##_query C; \
|
static const internal:: C##_query C; \
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,9 @@ query_select_intermediate query::select()
|
||||||
template<class Type>
|
template<class Type>
|
||||||
query_select_intermediate query::select(std::initializer_list<column> columns)
|
query_select_intermediate query::select(std::initializer_list<column> columns)
|
||||||
{
|
{
|
||||||
return query_select_intermediate{connection_, column_name_generator::generate<Type>(this->schema())};
|
auto cols = column_name_generator::generate<Type>(this->schema());
|
||||||
|
cols.insert(cols.end(), columns);
|
||||||
|
return query_select_intermediate{connection_, cols};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -127,7 +127,7 @@ public:
|
||||||
using query_select_finish::query_select_finish;
|
using query_select_finish::query_select_finish;
|
||||||
|
|
||||||
query_group_by_intermediate group_by(const std::string &name);
|
query_group_by_intermediate group_by(const std::string &name);
|
||||||
query_order_by_intermediate order_by(const std::string &name);
|
query_order_by_intermediate order_by(const column &col);
|
||||||
};
|
};
|
||||||
|
|
||||||
class query_join_intermediate;
|
class query_join_intermediate;
|
||||||
|
|
@ -145,7 +145,7 @@ public:
|
||||||
return {connection_, data_};
|
return {connection_, data_};
|
||||||
}
|
}
|
||||||
query_group_by_intermediate group_by(const std::string &name);
|
query_group_by_intermediate group_by(const std::string &name);
|
||||||
query_order_by_intermediate order_by(const std::string &name);
|
query_order_by_intermediate order_by(const column &col);
|
||||||
};
|
};
|
||||||
|
|
||||||
class query_join_intermediate : public query_intermediate
|
class query_join_intermediate : public query_intermediate
|
||||||
|
|
|
||||||
|
|
@ -125,13 +125,18 @@ private:
|
||||||
void accept(query_part_visitor &visitor) override;
|
void accept(query_part_visitor &visitor) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class query_order_by_part : public query_table_name_part
|
class query_order_by_part : public query_part
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit query_order_by_part(const std::string &table_name);
|
explicit query_order_by_part(sql::column col);
|
||||||
|
|
||||||
|
const sql::column& column() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void accept(query_part_visitor &visitor) override;
|
void accept(query_part_visitor &visitor) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
sql::column column_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class query_order_by_asc_part : public query_part
|
class query_order_by_asc_part : public query_part
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ namespace matador::sql {
|
||||||
|
|
||||||
struct table
|
struct table
|
||||||
{
|
{
|
||||||
// std::type_index index;
|
|
||||||
std::string name;
|
std::string name;
|
||||||
std::string alias;
|
std::string alias;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,13 +18,16 @@ std::string dialect::prepare_identifier(const column &col) const
|
||||||
{
|
{
|
||||||
std::string result;
|
std::string result;
|
||||||
if (!col.is_function()) {
|
if (!col.is_function()) {
|
||||||
result = prepare_identifier_string(col.name);
|
if (!col.table.empty()) {
|
||||||
|
result = prepare_identifier_string(col.table) + ".";
|
||||||
|
}
|
||||||
|
result += prepare_identifier_string(col.name);
|
||||||
} else {
|
} else {
|
||||||
result = sql_func_map_.at(col.function_) + "(" + col.name + ")";
|
result = sql_func_map_.at(col.function_) + "(" + col.name + ")";
|
||||||
}
|
}
|
||||||
if (!col.alias.empty()) {
|
// if (!col.alias.empty()) {
|
||||||
result += " AS " + col.alias;
|
// result += " AS " + col.alias;
|
||||||
}
|
// }
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,8 @@ void query_compiler::visit(query_group_by_part &group_by_part)
|
||||||
|
|
||||||
void query_compiler::visit(query_order_by_part &order_by_part)
|
void query_compiler::visit(query_order_by_part &order_by_part)
|
||||||
{
|
{
|
||||||
|
query_.sql += " " + dialect_.token_at(dialect::token_t::ORDER_BY) +
|
||||||
|
" " + dialect_.prepare_identifier(order_by_part.column());
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_compiler::visit(query_order_by_asc_part &order_by_asc_part)
|
void query_compiler::visit(query_order_by_asc_part &order_by_asc_part)
|
||||||
|
|
|
||||||
|
|
@ -96,8 +96,9 @@ query_group_by_intermediate query_where_intermediate::group_by(const std::string
|
||||||
// return {connection_, builder_.group_by(name)};
|
// return {connection_, builder_.group_by(name)};
|
||||||
}
|
}
|
||||||
|
|
||||||
query_order_by_intermediate query_where_intermediate::order_by(const std::string &name)
|
query_order_by_intermediate query_where_intermediate::order_by(const column &col)
|
||||||
{
|
{
|
||||||
|
data_.parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||||
return {connection_, data_};
|
return {connection_, data_};
|
||||||
// return {connection_, builder_.order_by(name)};
|
// return {connection_, builder_.order_by(name)};
|
||||||
}
|
}
|
||||||
|
|
@ -114,8 +115,9 @@ query_group_by_intermediate query_on_intermediate::group_by(const std::string &n
|
||||||
// return {connection_, builder_.group_by(name)};
|
// return {connection_, builder_.group_by(name)};
|
||||||
}
|
}
|
||||||
|
|
||||||
query_order_by_intermediate query_on_intermediate::order_by(const std::string &name)
|
query_order_by_intermediate query_on_intermediate::order_by(const column &col)
|
||||||
{
|
{
|
||||||
|
data_.parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||||
return {connection_, data_};
|
return {connection_, data_};
|
||||||
// return {connection_, builder_.order_by(name)};
|
// return {connection_, builder_.order_by(name)};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#include "matador/sql/query_parts.hpp"
|
#include "matador/sql/query_parts.hpp"
|
||||||
#include "matador/sql/basic_condition.hpp"
|
#include "matador/sql/basic_condition.hpp"
|
||||||
|
|
||||||
|
|
@ -81,10 +83,16 @@ void query_group_by_part::accept(query_part_visitor &visitor)
|
||||||
visitor.visit(*this);
|
visitor.visit(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
query_order_by_part::query_order_by_part(const std::string &table_name)
|
query_order_by_part::query_order_by_part(sql::column col)
|
||||||
: query_table_name_part(dialect::token_t::ORDER_BY, table_name)
|
: query_part(dialect::token_t::ORDER_BY)
|
||||||
|
, column_(std::move(col))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
const sql::column &query_order_by_part::column() const
|
||||||
|
{
|
||||||
|
return column_;
|
||||||
|
}
|
||||||
|
|
||||||
void query_order_by_part::accept(query_part_visitor &visitor)
|
void query_order_by_part::accept(query_part_visitor &visitor)
|
||||||
{
|
{
|
||||||
visitor.visit(*this);
|
visitor.visit(*this);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue