implemented query_builder::join and query_builder::on and added a test

This commit is contained in:
2024-02-01 17:15:26 +01:00
parent c025fb282a
commit f13837e515
3 changed files with 36 additions and 9 deletions
+23 -8
View File
@@ -10,7 +10,8 @@ namespace matador::sql {
namespace detail {
any_type_to_string_visitor::any_type_to_string_visitor(const dialect &d, query_context &query)
: d(d), query(query) {}
: d(d), query(query)
{}
void any_type_to_string_visitor::to_string(const char *val)
{
@@ -52,8 +53,10 @@ query_builder::query_state_transition_map query_builder::transitions_{
{state_t::QUERY_DELETE, {state_t::QUERY_FROM}},
{state_t::QUERY_TABLE_CREATE, {state_t::QUERY_FINISH}},
{state_t::QUERY_TABLE_DROP, {state_t::QUERY_FINISH}},
{state_t::QUERY_FROM, {state_t::QUERY_OFFSET, state_t::QUERY_LIMIT, state_t::QUERY_WHERE, state_t::QUERY_ORDER_BY, state_t::QUERY_GROUP_BY, state_t::QUERY_FINISH}},
{state_t::QUERY_FROM, {state_t::QUERY_OFFSET, state_t::QUERY_LIMIT, state_t::QUERY_WHERE, state_t::QUERY_ORDER_BY, state_t::QUERY_GROUP_BY, state_t::QUERY_JOIN, state_t::QUERY_FINISH}},
{state_t::QUERY_SET, {state_t::QUERY_WHERE, state_t::QUERY_FINISH}},
{state_t::QUERY_JOIN, {state_t::QUERY_ON}},
{state_t::QUERY_ON, {state_t::QUERY_OFFSET, state_t::QUERY_LIMIT, state_t::QUERY_WHERE, state_t::QUERY_ORDER_BY, state_t::QUERY_GROUP_BY, state_t::QUERY_JOIN, state_t::QUERY_FINISH}},
{state_t::QUERY_INTO, {state_t::QUERY_VALUES}},
{state_t::QUERY_WHERE, {state_t::QUERY_ORDER_BY, state_t::QUERY_GROUP_BY, state_t::QUERY_OFFSET, state_t::QUERY_LIMIT, state_t::QUERY_FINISH}},
{state_t::QUERY_ORDER_BY, {state_t::QUERY_ORDER_DIRECTION}},
@@ -324,26 +327,38 @@ query_builder &query_builder::from(const std::string &table, const std::string &
if (dialect_.default_schema_name().empty()) {
query_parts_.emplace_back(dialect::token_t::FROM, " " + dialect_.token_at(dialect::token_t::FROM) +
" " + dialect_.prepare_identifier(table) +
(as.empty() ? "" : " " + as));
" " + dialect_.prepare_identifier(table) +
(as.empty() ? "" : " AS " + dialect_.prepare_identifier(as)));
} else {
query_parts_.emplace_back(dialect::token_t::FROM, " " + dialect_.token_at(dialect::token_t::FROM) +
" " + dialect_.prepare_identifier(dialect_.default_schema_name()) +
"." + dialect_.prepare_identifier(table) +
(as.empty() ? "" : " " + as));
" " + dialect_.prepare_identifier(dialect_.default_schema_name()) +
"." + dialect_.prepare_identifier(table) +
(as.empty() ? "" : " AS " + dialect_.prepare_identifier(as)));
}
query_.table_name = table;
return *this;
}
query_builder &query_builder::join(const std::string &table, join_type_t)
query_builder &query_builder::join(const std::string &table, join_type_t, const std::string &as)
{
transition_to(state_t::QUERY_JOIN);
query_parts_.emplace_back(dialect::token_t::JOIN, " " + dialect_.token_at(dialect::token_t::JOIN) +
" " + dialect_.prepare_identifier(table) +
(as.empty() ? "" : " AS " + dialect_.prepare_identifier(as)));
return *this;
}
query_builder &query_builder::on(const std::string &column, const std::string &join_column)
{
transition_to(state_t::QUERY_ON);
query_parts_.emplace_back(dialect::token_t::ON, " " + dialect_.token_at(dialect::token_t::ON) +
" " + dialect_.prepare_identifier(column) +
"=" + dialect_.prepare_identifier(join_column));
return *this;
}