query parts progress
This commit is contained in:
parent
b255ae22b4
commit
16d7fd7e76
|
|
@ -232,8 +232,8 @@ TEST_CASE_METHOD(QueryFixture, " Select statement with foreign key and join_left
|
||||||
REQUIRE(f.airplane->id == 1);
|
REQUIRE(f.airplane->id == 1);
|
||||||
|
|
||||||
auto result = db.query().select({"f.id", "ap.brand", "ap.model", "f.pilot_name"})
|
auto result = db.query().select({"f.id", "ap.brand", "ap.model", "f.pilot_name"})
|
||||||
.from("flight", "f")
|
.from({"flight", "f"})
|
||||||
.join_left("airplane", "ap")
|
.join_left({"airplane", "ap"})
|
||||||
.on("f.airplane_id"_col == "ap.id"_col)
|
.on("f.airplane_id"_col == "ap.id"_col)
|
||||||
.order_by("f.id").asc()
|
.order_by("f.id").asc()
|
||||||
.fetch_all();
|
.fetch_all();
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@ private:
|
||||||
|
|
||||||
TEST_CASE_METHOD(StatementTestFixture, " Create prepared statement", "[statement]")
|
TEST_CASE_METHOD(StatementTestFixture, " Create prepared statement", "[statement]")
|
||||||
{
|
{
|
||||||
|
table ap{"airplane"};
|
||||||
SECTION("Insert with prepared statement and placeholder") {
|
SECTION("Insert with prepared statement and placeholder") {
|
||||||
auto stmt = db.query().insert()
|
auto stmt = db.query().insert()
|
||||||
.into<airplane>("airplane")
|
.into<airplane>("airplane")
|
||||||
|
|
@ -57,7 +58,7 @@ TEST_CASE_METHOD(StatementTestFixture, " Create prepared statement", "[statement
|
||||||
stmt.reset();
|
stmt.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = db.query().select<airplane>().from("airplane").fetch_all<airplane>();
|
auto result = db.query().select<airplane>().from(ap).fetch_all<airplane>();
|
||||||
|
|
||||||
size_t index{0};
|
size_t index{0};
|
||||||
for (const auto &i: result) {
|
for (const auto &i: result) {
|
||||||
|
|
@ -73,7 +74,7 @@ TEST_CASE_METHOD(StatementTestFixture, " Create prepared statement", "[statement
|
||||||
REQUIRE(res == 1);
|
REQUIRE(res == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto stmt = db.query().select<airplane>().from("airplane").where("brand"_col == _).prepare();
|
auto stmt = db.query().select<airplane>().from(ap).where("brand"_col == _).prepare();
|
||||||
|
|
||||||
stmt.bind(0, "Airbus");
|
stmt.bind(0, "Airbus");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,8 +49,8 @@ struct book
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
QUERY_HELPER(author, 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(book, id, book_author, title, published_in)
|
QUERY_HELPER(books, id, book_author, title, published_in)
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
|
@ -67,7 +67,7 @@ int main()
|
||||||
connection c(dns, s);
|
connection c(dns, s);
|
||||||
|
|
||||||
auto books = c.query()
|
auto books = c.query()
|
||||||
.select<book>({qh::book.id.name})
|
.select<book>({matador::qh::books.id.name})
|
||||||
.from({"book"})
|
.from({"book"})
|
||||||
.join_left({"authors"})
|
.join_left({"authors"})
|
||||||
.on("book.author_id"_col == "author.id"_col)
|
.on("book.author_id"_col == "author.id"_col)
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ table::table(const basic_query<QueryClass>&)
|
||||||
#define FIELD(x) const field x{#x};
|
#define FIELD(x) const field x{#x};
|
||||||
|
|
||||||
#define QUERY_HELPER(C, ...) \
|
#define QUERY_HELPER(C, ...) \
|
||||||
namespace qh { \
|
namespace matador::qh { \
|
||||||
namespace internal { \
|
namespace internal { \
|
||||||
struct C##_query { \
|
struct C##_query { \
|
||||||
MAP(FIELD, __VA_ARGS__) \
|
MAP(FIELD, __VA_ARGS__) \
|
||||||
|
|
|
||||||
|
|
@ -138,7 +138,12 @@ public:
|
||||||
using query_select_finish::query_select_finish;
|
using query_select_finish::query_select_finish;
|
||||||
|
|
||||||
query_join_intermediate join(const std::string &join_table_name, const std::string &as);
|
query_join_intermediate join(const std::string &join_table_name, const std::string &as);
|
||||||
query_where_intermediate where(const basic_condition &cond);
|
template<class Condition>
|
||||||
|
query_where_intermediate where(const Condition &cond)
|
||||||
|
{
|
||||||
|
data_.parts.push_back(std::make_unique<query_where_part>(cond));
|
||||||
|
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 std::string &name);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -81,6 +81,8 @@ public:
|
||||||
: query_part(dialect::token_t::ON)
|
: query_part(dialect::token_t::ON)
|
||||||
, condition_(new Condition(cond)) {}
|
, condition_(new Condition(cond)) {}
|
||||||
|
|
||||||
|
const basic_condition& condition() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void accept(query_part_visitor &visitor) override;
|
void accept(query_part_visitor &visitor) override;
|
||||||
|
|
||||||
|
|
@ -96,6 +98,8 @@ public:
|
||||||
: query_part(dialect::token_t::WHERE)
|
: query_part(dialect::token_t::WHERE)
|
||||||
, condition_(new Condition(cond)) {}
|
, condition_(new Condition(cond)) {}
|
||||||
|
|
||||||
|
const basic_condition& condition() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void accept(query_part_visitor &visitor) override;
|
void accept(query_part_visitor &visitor) override;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ struct table
|
||||||
std::string name;
|
std::string name;
|
||||||
std::string alias;
|
std::string alias;
|
||||||
|
|
||||||
|
table(const char *name, std::string as = "") // NOLINT(*-explicit-constructor)
|
||||||
|
: name(name), alias(std::move(as)) {}
|
||||||
table(std::string name, std::string as = "") // NOLINT(*-explicit-constructor)
|
table(std::string name, std::string as = "") // NOLINT(*-explicit-constructor)
|
||||||
: name(std::move(name)), alias(std::move(as)) {}
|
: name(std::move(name)), alias(std::move(as)) {}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
||||||
query_compiler::query_compiler(const sql::dialect &d)
|
query_compiler::query_compiler(const sql::dialect &d)
|
||||||
: dialect_(d)
|
: dialect_(d)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
query_context query_compiler::compile(const query_data *data)
|
query_context query_compiler::compile(const query_data *data)
|
||||||
|
|
@ -53,28 +53,34 @@ void query_compiler::visit(query_from_part &from_part)
|
||||||
if (dialect_.default_schema_name().empty()) {
|
if (dialect_.default_schema_name().empty()) {
|
||||||
query_.sql += " " + dialect_.token_at(dialect::token_t::FROM) +
|
query_.sql += " " + dialect_.token_at(dialect::token_t::FROM) +
|
||||||
" " + dialect_.prepare_identifier(from_part.table().name) +
|
" " + dialect_.prepare_identifier(from_part.table().name) +
|
||||||
(from_part.table().alias.empty() ? "" : " AS " + dialect_.prepare_identifier(from_part.table().alias));
|
(from_part.table().alias.empty() ? "" : " AS " +
|
||||||
|
dialect_.prepare_identifier(from_part.table().alias));
|
||||||
} else {
|
} else {
|
||||||
query_.sql += " " + dialect_.token_at(dialect::token_t::FROM) +
|
query_.sql += " " + dialect_.token_at(dialect::token_t::FROM) +
|
||||||
" " + dialect_.prepare_identifier(dialect_.default_schema_name()) +
|
" " + dialect_.prepare_identifier(dialect_.default_schema_name()) +
|
||||||
"." + dialect_.prepare_identifier(from_part.table().name) +
|
"." + dialect_.prepare_identifier(from_part.table().name) +
|
||||||
(from_part.table().alias.empty() ? "" : " AS " + dialect_.prepare_identifier(from_part.table().alias));
|
(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_join_part &join_part)
|
||||||
{
|
{
|
||||||
|
query_.sql += " " + dialect_.token_at(dialect::token_t::JOIN) +
|
||||||
|
" " + dialect_.prepare_identifier(join_part.table().name) +
|
||||||
|
(join_part.table().alias.empty() ? "" : " AS " + dialect_.prepare_identifier(join_part.table().alias));
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_compiler::visit(query_on_part &on_part)
|
void query_compiler::visit(query_on_part &on_part)
|
||||||
{
|
{
|
||||||
|
query_.sql += " " + dialect_.token_at(dialect::token_t::ON) +
|
||||||
|
" " + on_part.condition().evaluate(const_cast<dialect &>(dialect_), query_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_compiler::visit(query_where_part &where_part)
|
void query_compiler::visit(query_where_part &where_part)
|
||||||
{
|
{
|
||||||
|
query_.sql += " " + dialect_.token_at(dialect::token_t::WHERE) +
|
||||||
|
" " + where_part.condition().evaluate(const_cast<dialect &>(dialect_), query_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_compiler::visit(query_group_by_part &group_by_part)
|
void query_compiler::visit(query_group_by_part &group_by_part)
|
||||||
|
|
|
||||||
|
|
@ -108,12 +108,6 @@ query_join_intermediate query_on_intermediate::join(const std::string &join_tabl
|
||||||
// return {connection_, builder_.join(join_table_name, join_type_t::INNER, as)};
|
// return {connection_, builder_.join(join_table_name, join_type_t::INNER, as)};
|
||||||
}
|
}
|
||||||
|
|
||||||
query_where_intermediate query_on_intermediate::where(const basic_condition &cond)
|
|
||||||
{
|
|
||||||
return {connection_, data_};
|
|
||||||
// return {connection_, builder_.where(cond)};
|
|
||||||
}
|
|
||||||
|
|
||||||
query_group_by_intermediate query_on_intermediate::group_by(const std::string &name)
|
query_group_by_intermediate query_on_intermediate::group_by(const std::string &name)
|
||||||
{
|
{
|
||||||
return {connection_, data_};
|
return {connection_, data_};
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,11 @@ void query_join_part::accept(query_part_visitor &visitor)
|
||||||
visitor.visit(*this);
|
visitor.visit(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const basic_condition &query_on_part::condition() const
|
||||||
|
{
|
||||||
|
return *condition_;
|
||||||
|
}
|
||||||
|
|
||||||
void query_on_part::accept(query_part_visitor &visitor)
|
void query_on_part::accept(query_part_visitor &visitor)
|
||||||
{
|
{
|
||||||
visitor.visit(*this);
|
visitor.visit(*this);
|
||||||
|
|
@ -58,6 +63,11 @@ void query_where_part::accept(query_part_visitor &visitor)
|
||||||
visitor.visit(*this);
|
visitor.visit(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const basic_condition &query_where_part::condition() const
|
||||||
|
{
|
||||||
|
return *condition_;
|
||||||
|
}
|
||||||
|
|
||||||
query_table_name_part::query_table_name_part(sql::dialect::token_t token, std::string table_name)
|
query_table_name_part::query_table_name_part(sql::dialect::token_t token, std::string table_name)
|
||||||
: query_part(token)
|
: query_part(token)
|
||||||
, table_name_(std::move(table_name)) {}
|
, table_name_(std::move(table_name)) {}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue