query progress
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
#include "matador/sql/any_type_to_string_visitor.hpp"
|
||||
|
||||
#include "matador/sql/dialect.hpp"
|
||||
#include "matador/sql/query_context.hpp"
|
||||
|
||||
#include "matador/utils/string.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
any_type_to_string_visitor::any_type_to_string_visitor(const dialect &d, query_context &query)
|
||||
: d(d), query(query)
|
||||
{}
|
||||
|
||||
void any_type_to_string_visitor::to_string(const char *val)
|
||||
{
|
||||
result = "'" + d.prepare_literal(val) + "'";
|
||||
}
|
||||
|
||||
void any_type_to_string_visitor::to_string(std::string &val)
|
||||
{
|
||||
result = "'" + d.prepare_literal(val) + "'";
|
||||
}
|
||||
|
||||
void any_type_to_string_visitor::to_string(utils::blob &val)
|
||||
{
|
||||
// "This is a binary Data string" as binary data:
|
||||
// MySQL: X'5468697320697320612062616E617279204461746120737472696E67'
|
||||
// Postgres: E'\\x5468697320697320612062616E617279204461746120737472696E67'
|
||||
// MSSQL: 0x5468697320697320612062616E617279204461746120737472696E67
|
||||
// Sqlite: X'5468697320697320612062616E617279204461746120737472696E67'
|
||||
result = d.token_at(dialect::token_t::BEGIN_BINARY_DATA) + utils::to_string(val) + d.token_at(dialect::token_t::END_BINARY_DATA);
|
||||
}
|
||||
|
||||
void any_type_to_string_visitor::to_string(placeholder &/*val*/)
|
||||
{
|
||||
query.bind_vars.emplace_back("unknown");
|
||||
result = d.next_placeholder(query.bind_vars);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -97,7 +97,7 @@ sql::query connection::query(const std::shared_ptr<sql::schema> &schema) const
|
||||
query_result<record> connection::fetch(const query_context &q) const
|
||||
{
|
||||
if (q.prototype.empty() || q.prototype.unknown()) {
|
||||
const auto table_prototype = describe(q.table_name);
|
||||
const auto table_prototype = describe(q.table.name);
|
||||
for (auto &col : q.prototype) {
|
||||
if (const auto rit = table_prototype.find(col.name()); col.type() == data_type_t::type_unknown && rit != table_prototype.end()) {
|
||||
const_cast<column_definition&>(col).type(rit->type());
|
||||
|
||||
@@ -173,7 +173,7 @@ query_builder &query_builder::update(const std::string &table)
|
||||
{
|
||||
initialize(command_t::UPDATE, state_t::QUERY_UPDATE);
|
||||
|
||||
query_.table_name = table;
|
||||
query_.table = {table};
|
||||
query_parts_.emplace_back(dialect::token_t::UPDATE, dialect_.token_at(dialect::token_t::UPDATE) + " " + dialect_.prepare_identifier(table));
|
||||
|
||||
return *this;
|
||||
@@ -213,7 +213,7 @@ query_builder &query_builder::table(const std::string &table, const std::vector<
|
||||
transition_to(state_t::QUERY_TABLE_CREATE);
|
||||
|
||||
query_parts_.emplace_back(dialect::token_t::TABLE, " " + dialect_.token_at(dialect::token_t::TABLE) + " " + dialect_.prepare_identifier(table) + " ");
|
||||
query_.table_name = table;
|
||||
query_.table = {table};
|
||||
|
||||
std::string result = "(";
|
||||
|
||||
@@ -252,7 +252,7 @@ query_builder &query_builder::table(const std::string &table)
|
||||
transition_to(state_t::QUERY_TABLE_DROP);
|
||||
|
||||
query_parts_.emplace_back(dialect::token_t::TABLE, " " + dialect_.token_at(dialect::token_t::TABLE) + " " + dialect_.prepare_identifier(table));
|
||||
query_.table_name = table;
|
||||
query_.table = {table};
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -267,7 +267,7 @@ query_builder &query_builder::into(const std::string &table, const std::vector<c
|
||||
transition_to(state_t::QUERY_INTO);
|
||||
|
||||
query_parts_.emplace_back(dialect::token_t::INTO, " " + dialect_.token_at(dialect::token_t::INTO) + " " + dialect_.prepare_identifier(table) + " ");
|
||||
query_.table_name = table;
|
||||
query_.table = {table};
|
||||
|
||||
std::string result{"("};
|
||||
if (column_names.size() < 2) {
|
||||
@@ -337,7 +337,7 @@ query_builder &query_builder::from(const std::string &table, const std::string &
|
||||
"." + dialect_.prepare_identifier(table) +
|
||||
(as.empty() ? "" : " AS " + dialect_.prepare_identifier(as)));
|
||||
}
|
||||
query_.table_name = table;
|
||||
query_.table = {table};
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "matador/sql/query_data.hpp"
|
||||
#include "matador/sql/column_definition.hpp"
|
||||
#include "matador/sql/dialect.hpp"
|
||||
#include "matador/sql/query_builder.hpp"
|
||||
#include "matador/sql/any_type_to_string_visitor.hpp"
|
||||
|
||||
#include "matador/utils/string.hpp"
|
||||
|
||||
@@ -53,7 +53,7 @@ void query_compiler::visit(query_select_part &select_part)
|
||||
|
||||
void query_compiler::visit(query_from_part &from_part)
|
||||
{
|
||||
query_.table_name = from_part.table().name;
|
||||
query_.table = from_part.table();
|
||||
if (dialect_.default_schema_name().empty()) {
|
||||
query_.sql += " " + dialect_.token_at(dialect::token_t::FROM) +
|
||||
" " + dialect_.prepare_identifier(from_part.table().name) +
|
||||
@@ -125,7 +125,7 @@ void query_compiler::visit(query_insert_part &insert_part)
|
||||
|
||||
void query_compiler::visit(query_into_part &into_part)
|
||||
{
|
||||
query_.table_name = into_part.table().name;
|
||||
query_.table = into_part.table();
|
||||
query_.sql += " " + dialect_.token_at(dialect::token_t::INTO) +
|
||||
" " + dialect_.prepare_identifier(into_part.table().name);
|
||||
|
||||
@@ -150,7 +150,7 @@ void query_compiler::visit(query_values_part &values_part)
|
||||
{
|
||||
query_.sql += " " + dialect_.token_at(dialect::token_t::VALUES);
|
||||
|
||||
detail::any_type_to_string_visitor value_to_string(dialect_, query_);
|
||||
any_type_to_string_visitor value_to_string(dialect_, query_);
|
||||
|
||||
std::string result{"("};
|
||||
if (values_part.values().size() < 2) {
|
||||
@@ -177,13 +177,20 @@ void query_compiler::visit(query_values_part &values_part)
|
||||
|
||||
void query_compiler::visit(query_update_part &update_part)
|
||||
{
|
||||
query_.table_name = update_part.table().name;
|
||||
query_.table = update_part.table();
|
||||
query_.sql = dialect_.token_at(dialect::token_t::UPDATE) + " " + dialect_.prepare_identifier(update_part.table().name);
|
||||
}
|
||||
|
||||
void query_compiler::visit(query_delete_part &delete_part)
|
||||
{
|
||||
query_.sql = dialect_.token_at(dialect::token_t::REMOVE);
|
||||
}
|
||||
|
||||
void query_compiler::visit(query_delete_from_part &delete_from_part)
|
||||
{
|
||||
query_.table = delete_from_part.table();
|
||||
query_.sql += " " + dialect_.token_at(delete_from_part.token()) +
|
||||
" " + dialect_.prepare_identifier(delete_from_part.table().name);
|
||||
}
|
||||
|
||||
void query_compiler::visit(query_create_part &create_part)
|
||||
@@ -209,7 +216,7 @@ std::string build_create_column(const column_definition &col, const dialect &d,
|
||||
void query_compiler::visit(query_create_table_part &create_table_part)
|
||||
{
|
||||
query_.sql += " " + dialect_.token_at(dialect::token_t::TABLE) + " " + dialect_.prepare_identifier(create_table_part.table().name) + " ";
|
||||
query_.table_name = create_table_part.table().name;
|
||||
query_.table = create_table_part.table();
|
||||
|
||||
std::string result = "(";
|
||||
|
||||
@@ -251,7 +258,7 @@ void query_compiler::visit(query_set_part &set_part)
|
||||
{
|
||||
query_.sql += " " + dialect_.token_at(dialect::token_t::SET) + " ";
|
||||
|
||||
detail::any_type_to_string_visitor value_to_string(dialect_, query_);
|
||||
any_type_to_string_visitor value_to_string(dialect_, query_);
|
||||
std::string result;
|
||||
if (set_part.key_values().size() < 2) {
|
||||
for (const auto &col: set_part.key_values()) {
|
||||
@@ -281,7 +288,7 @@ void query_compiler::visit(query_set_part &set_part)
|
||||
void query_compiler::visit(query_drop_table_part &drop_table_part)
|
||||
{
|
||||
query_.sql += " " + dialect_.token_at(dialect::token_t::TABLE) + " " + dialect_.prepare_identifier(drop_table_part.table().name);
|
||||
query_.table_name = drop_table_part.table().name;
|
||||
query_.table = drop_table_part.table();
|
||||
}
|
||||
|
||||
std::string build_create_column(const column_definition &col, const dialect &d, column_context &context)
|
||||
|
||||
@@ -20,10 +20,10 @@ record query_select_finish::fetch_one()
|
||||
return *connection_.fetch(compiler.compile(&data_)).begin().get();
|
||||
}
|
||||
|
||||
std::string query_select_finish::str() const
|
||||
query_context query_select_finish::build() const
|
||||
{
|
||||
query_compiler compiler(connection_.dialect());
|
||||
return compiler.compile(&data_).sql;
|
||||
return compiler.compile(&data_);
|
||||
}
|
||||
|
||||
std::unique_ptr<query_result_impl> query_select_finish::fetch()
|
||||
@@ -184,10 +184,10 @@ statement query_execute_finish::prepare()
|
||||
return connection_.prepare(compiler.compile(&data_));
|
||||
}
|
||||
|
||||
std::string query_execute_finish::str() const
|
||||
query_context query_execute_finish::build() const
|
||||
{
|
||||
query_compiler compiler(connection_.dialect());
|
||||
return compiler.compile(&data_).sql;
|
||||
return compiler.compile(&data_);
|
||||
}
|
||||
|
||||
query_execute_finish query_into_intermediate::values(std::initializer_list<any_type> values)
|
||||
@@ -229,10 +229,10 @@ query_execute_finish query_drop_intermediate::table(const sql::table &table)
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_execute_finish query_execute_where_intermediate::limit(int limit)
|
||||
query_order_by_intermediate query_execute_where_intermediate::order_by(const column &col)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||
return {connection_, schema_, data_};
|
||||
// return {connection_, builder_.limit(limit)};
|
||||
}
|
||||
|
||||
query_execute_where_intermediate query_set_intermediate::where_clause(std::unique_ptr<basic_condition> &&cond)
|
||||
@@ -273,6 +273,7 @@ query_delete_intermediate::query_delete_intermediate(connection &db, const std::
|
||||
|
||||
query_delete_from_intermediate query_delete_intermediate::from(const sql::table &table)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_delete_from_part>(table));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
|
||||
@@ -5,5 +5,9 @@ namespace matador::sql {
|
||||
query_part::query_part(sql::dialect::token_t token)
|
||||
: token_(token) {}
|
||||
|
||||
dialect::token_t query_part::token() const
|
||||
{
|
||||
return token_;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -233,6 +233,20 @@ void query_delete_part::accept(query_part_visitor &visitor)
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
query_delete_from_part::query_delete_from_part(sql::table table)
|
||||
: query_part(sql::dialect::token_t::FROM)
|
||||
, table_(std::move(table)) {}
|
||||
|
||||
const sql::table &query_delete_from_part::table() const
|
||||
{
|
||||
return table_;
|
||||
}
|
||||
|
||||
void query_delete_from_part::accept(query_part_visitor &visitor)
|
||||
{
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
query_create_part::query_create_part()
|
||||
: query_part(sql::dialect::token_t::CREATE) {}
|
||||
|
||||
|
||||
+2
-2
@@ -5,12 +5,12 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
void table_info::create(connection &conn) const
|
||||
void table_info::create(connection &conn, schema &scm) const
|
||||
{
|
||||
// conn.query().create().table(name, prototype.columns()).execute();
|
||||
}
|
||||
|
||||
void table_info::drop(connection &conn) const
|
||||
void table_info::drop(connection &conn, schema &scm) const
|
||||
{
|
||||
// conn.query().drop().table(name).execute();
|
||||
}
|
||||
|
||||
+3
-3
@@ -15,7 +15,7 @@ void session::create_schema()
|
||||
{
|
||||
auto c = pool_.acquire();
|
||||
for (const auto &t : *schema_) {
|
||||
t.second.create(*c);
|
||||
t.second.create(*c, *schema_);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,9 +35,9 @@ query_result<record> session::fetch(const query_context &q) const
|
||||
if (!c.valid()) {
|
||||
throw std::logic_error("no database connection available");
|
||||
}
|
||||
auto it = prototypes_.find(q.table_name);
|
||||
auto it = prototypes_.find(q.table.name);
|
||||
if (it == prototypes_.end()) {
|
||||
it = prototypes_.emplace(q.table_name, c->describe(q.table_name)).first;
|
||||
it = prototypes_.emplace(q.table.name, c->describe(q.table.name)).first;
|
||||
}
|
||||
// adjust columns from given query
|
||||
for (auto &col : q.prototype) {
|
||||
|
||||
Reference in New Issue
Block a user