moved column and table class from namespace sql to query
This commit is contained in:
@@ -1,102 +0,0 @@
|
||||
#include "matador/sql/column.hpp"
|
||||
|
||||
#include "matador/sql/table.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
column operator ""_col(const char *name, size_t len) {
|
||||
const std::string str(name, len);
|
||||
const auto pos = str.find('.');
|
||||
if (pos == std::string::npos) {
|
||||
return column{str};
|
||||
}
|
||||
|
||||
if (str.find('.', pos + 1) != std::string::npos) {
|
||||
throw std::invalid_argument("Invalid column name: multiple dots found");
|
||||
}
|
||||
|
||||
return column{table{str.substr(0, pos)}, str.substr(pos + 1)};
|
||||
}
|
||||
|
||||
column::column(const char *name, const std::string& as)
|
||||
: column(std::string(name), as)
|
||||
{}
|
||||
|
||||
column::column(std::string name, std::string as)
|
||||
: table_(std::make_shared<sql::table>())
|
||||
, name(std::move(name))
|
||||
, alias_(std::move(as)) {}
|
||||
|
||||
column::column(const sql_function_t func, std::string name)
|
||||
: table_(std::make_shared<sql::table>())
|
||||
, name(std::move(name))
|
||||
, function_(func) {}
|
||||
|
||||
column::column(const sql::table& tab, std::string name, std::string as)
|
||||
: table_(std::make_shared<sql::table>(tab))
|
||||
, name(std::move(name))
|
||||
, alias_(std::move(as)) {
|
||||
table_->columns_.push_back(*this);
|
||||
}
|
||||
|
||||
column::column(const std::shared_ptr<sql::table>& t, std::string name, std::string as)
|
||||
: table_(t)
|
||||
, name(std::move(name))
|
||||
, alias_(std::move(as)) {
|
||||
}
|
||||
|
||||
bool column::equals(const column &x) const {
|
||||
return *table_ == *x.table_ &&
|
||||
name == x.name &&
|
||||
alias_ == x.alias_ &&
|
||||
function_ == x.function_;
|
||||
}
|
||||
|
||||
column &column::as(std::string a) {
|
||||
alias_ = std::move(a);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const std::string& column::column_name() const {
|
||||
return name;
|
||||
}
|
||||
|
||||
std::string column::full_name() const {
|
||||
if (table_ && !table_->name().empty()) {
|
||||
return table_->name() + "." + name;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
const std::string& column::alias_name() const {
|
||||
return alias_;
|
||||
}
|
||||
|
||||
bool column::is_function() const {
|
||||
return function_ != sql_function_t::None;
|
||||
}
|
||||
|
||||
sql_function_t column::function() const {
|
||||
return function_;
|
||||
}
|
||||
|
||||
bool column::has_alias() const {
|
||||
return !alias_.empty();
|
||||
}
|
||||
|
||||
std::string column::alias() const {
|
||||
return alias_;
|
||||
}
|
||||
|
||||
std::shared_ptr<table> column::table() const {
|
||||
return table_;
|
||||
}
|
||||
|
||||
void column::table( const std::shared_ptr<sql::table>& t ) {
|
||||
table_ = t;
|
||||
}
|
||||
}
|
||||
@@ -221,7 +221,7 @@ const class dialect &connection::dialect() const
|
||||
|
||||
utils::result<std::unique_ptr<statement_impl>, utils::error> connection::perform_prepare(const query_context& ctx) const {
|
||||
if (ctx.command != sql_command::SQL_CREATE_TABLE && (ctx.prototype.empty() || has_unknown_columns(ctx.prototype))) {
|
||||
if (const auto result = describe(ctx.table.name()); result.is_ok()) {
|
||||
if (const auto result = describe(ctx.table_name); result.is_ok()) {
|
||||
for (auto &col: ctx.prototype) {
|
||||
const auto rit = std::find_if(
|
||||
std::begin(*result),
|
||||
|
||||
@@ -6,31 +6,16 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
const std::string& dialect::token_at(const dialect_token token) const
|
||||
{
|
||||
const std::string& dialect::token_at(const dialect_token token) const {
|
||||
return tokens_.at(token);
|
||||
}
|
||||
|
||||
const std::string &dialect::data_type_at(const utils::basic_type type) const
|
||||
{
|
||||
const std::string &dialect::data_type_at(const utils::basic_type type) const {
|
||||
return data_types_.at(type);
|
||||
}
|
||||
|
||||
std::string dialect::prepare_identifier(const sql::column &col) const
|
||||
{
|
||||
std::string result;
|
||||
if (!col.is_function()) {
|
||||
if (!col.table()->name().empty()) {
|
||||
result = prepare_identifier_string(col.table()->has_alias() ? col.table()->alias() : col.table()->name()) + ".";
|
||||
}
|
||||
result += prepare_identifier_string(col.column_name());
|
||||
} else {
|
||||
result = sql_func_map_.at(col.function()) + "(" + col.column_name() + ")";
|
||||
}
|
||||
if (!col.alias().empty()) {
|
||||
result += " AS " + col.alias();
|
||||
}
|
||||
return result;
|
||||
const std::string& dialect::sql_function_at(const sql_function_t func) const {
|
||||
return sql_func_map_.at(func);
|
||||
}
|
||||
|
||||
std::string dialect::table_name( const std::string& table, const std::string& schema_name ) const {
|
||||
@@ -43,25 +28,6 @@ std::string dialect::table_name( const std::string& table, const std::string& sc
|
||||
return prepare_identifier_string( schema_name ) + "." + prepare_identifier_string( table );
|
||||
}
|
||||
|
||||
std::string dialect::prepare_condition(const sql::column& col) const
|
||||
{
|
||||
std::string result;
|
||||
if (!col.is_function()) {
|
||||
// if (!col.alias.empty()) {
|
||||
// result = col.alias;
|
||||
// } else {
|
||||
if (!col.table()->name().empty()) {
|
||||
result = prepare_identifier_string(col.table()->has_alias() ? col.table()->alias() : col.table()->name()) + ".";
|
||||
}
|
||||
result += prepare_identifier_string(col.column_name());
|
||||
// }
|
||||
} else {
|
||||
result = sql_func_map_.at(col.function()) + "(" + col.column_name() + ")";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const std::string& dialect::to_string(const bool val) const
|
||||
{
|
||||
return bool_strings_[static_cast<int>(val)];
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
#include "matador/sql/executor.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "matador/sql/record.hpp"
|
||||
#include "matador/sql/column.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
@@ -67,9 +66,8 @@ const std::vector<record::field_ref> &record::columns() const
|
||||
// return columns_[pk_index_];
|
||||
//}
|
||||
|
||||
const field &record::at(const sql::column &col) const
|
||||
{
|
||||
const auto &res = fields_by_name_.at(col.column_name());
|
||||
const field &record::at(const std::string &name) const {
|
||||
const auto &res = fields_by_name_.at(name);
|
||||
const auto &f = res.first;
|
||||
return f;
|
||||
}
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
#include "matador/sql/table.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
table::table(const char* name)
|
||||
: table(std::string(name))
|
||||
{}
|
||||
|
||||
table::table(std::string name)
|
||||
: name_(std::move(name))
|
||||
{}
|
||||
|
||||
table::table(const char *name, std::string as)
|
||||
: name_(name)
|
||||
, alias_(std::move(as))
|
||||
{}
|
||||
|
||||
table::table(std::string name, std::string as)
|
||||
: name_(std::move(name))
|
||||
, alias_(std::move(as))
|
||||
{}
|
||||
|
||||
table::table( std::string name, std::string as, const std::vector<column>& columns )
|
||||
: name_(std::move(name))
|
||||
, alias_(std::move(as))
|
||||
, columns_(columns) {}
|
||||
|
||||
bool table::operator==( const table& x ) const {
|
||||
return name_ == x.name_;
|
||||
}
|
||||
|
||||
table & table::as(const std::string &a) {
|
||||
alias_ = a;
|
||||
return *this;
|
||||
}
|
||||
|
||||
table table::as(const std::string &a) const {
|
||||
return { name_, a, columns_ };
|
||||
}
|
||||
|
||||
bool table::has_alias() const {
|
||||
return !alias_.empty();
|
||||
}
|
||||
|
||||
const std::string& table::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
const std::string& table::alias() const {
|
||||
return alias_;
|
||||
}
|
||||
|
||||
const std::vector<column>& table::columns() const {
|
||||
return columns_;
|
||||
}
|
||||
|
||||
table operator ""_tab(const char *name, size_t len) {
|
||||
return {{name, len}};
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user