some sql classes refactorings like column and table structs to classes
This commit is contained in:
+23
-11
@@ -29,7 +29,7 @@ column::column(const char *name, const std::string& as)
|
||||
column::column(std::string name, std::string as)
|
||||
: table_(std::make_shared<sql::table>())
|
||||
, name(std::move(name))
|
||||
, alias(std::move(as)) {}
|
||||
, alias_(std::move(as)) {}
|
||||
|
||||
column::column(const sql_function_t func, std::string name)
|
||||
: table_(std::make_shared<sql::table>())
|
||||
@@ -39,25 +39,25 @@ column::column(const sql_function_t func, std::string name)
|
||||
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);
|
||||
, 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)) {
|
||||
, alias_(std::move(as)) {
|
||||
}
|
||||
|
||||
bool column::equals(const column &x) const {
|
||||
return *table_ == *x.table_ &&
|
||||
name == x.name &&
|
||||
alias == x.alias &&
|
||||
alias_ == x.alias_ &&
|
||||
function_ == x.function_;
|
||||
}
|
||||
|
||||
column &column::as(std::string a) {
|
||||
alias = std::move(a);
|
||||
alias_ = std::move(a);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -66,25 +66,37 @@ const std::string& column::column_name() const {
|
||||
}
|
||||
|
||||
std::string column::full_name() const {
|
||||
if (table_ && !table_->name.empty()) {
|
||||
return table_->name + "." + name;
|
||||
if (table_ && !table_->name().empty()) {
|
||||
return table_->name() + "." + name;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
const std::string& column::alias_name() const {
|
||||
return alias;
|
||||
return alias_;
|
||||
}
|
||||
|
||||
bool column::is_function() const {
|
||||
return function_ != sql_function_t::NONE;
|
||||
return function_ != sql_function_t::None;
|
||||
}
|
||||
|
||||
sql_function_t column::function() const {
|
||||
return function_;
|
||||
}
|
||||
|
||||
bool column::has_alias() const {
|
||||
return !alias.empty();
|
||||
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),
|
||||
|
||||
+217
-12
@@ -16,37 +16,47 @@ const std::string &dialect::data_type_at(const utils::basic_type type) const
|
||||
return data_types_.at(type);
|
||||
}
|
||||
|
||||
std::string dialect::prepare_identifier(const column &col) const
|
||||
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) + ".";
|
||||
if (!col.table()->name().empty()) {
|
||||
result = prepare_identifier_string(col.table()->has_alias() ? col.table()->alias() : col.table()->name()) + ".";
|
||||
}
|
||||
result += prepare_identifier_string(col.name);
|
||||
result += prepare_identifier_string(col.column_name());
|
||||
} else {
|
||||
result = sql_func_map_.at(col.function_) + "(" + col.name + ")";
|
||||
result = sql_func_map_.at(col.function()) + "(" + col.column_name() + ")";
|
||||
}
|
||||
if (!col.alias.empty()) {
|
||||
result += " AS " + col.alias;
|
||||
if (!col.alias().empty()) {
|
||||
result += " AS " + col.alias();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string dialect::prepare_condition(const column& col) const
|
||||
std::string dialect::table_name( const std::string& table, const std::string& schema_name ) const {
|
||||
if ( schema_name.empty() && default_schema_name_.empty()) {
|
||||
return prepare_identifier_string( table );
|
||||
}
|
||||
if ( schema_name.empty() ) {
|
||||
return prepare_identifier_string( default_schema_name_ ) + "." + prepare_identifier_string( table );
|
||||
}
|
||||
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) + ".";
|
||||
if (!col.table()->name().empty()) {
|
||||
result = prepare_identifier_string(col.table()->has_alias() ? col.table()->alias() : col.table()->name()) + ".";
|
||||
}
|
||||
result += prepare_identifier_string(col.name);
|
||||
result += prepare_identifier_string(col.column_name());
|
||||
// }
|
||||
} else {
|
||||
result = sql_func_map_.at(col.function_) + "(" + col.name + ")";
|
||||
result = sql_func_map_.at(col.function()) + "(" + col.column_name() + ")";
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -143,4 +153,199 @@ std::string dialect::default_schema_name() const
|
||||
return default_schema_name_;
|
||||
}
|
||||
|
||||
const std::string& dialect::add_constraint() const {
|
||||
return token_at(dialect_token::AddConstraint);
|
||||
}
|
||||
|
||||
const std::string& dialect::alter() const {
|
||||
return token_at(dialect_token::Alter);
|
||||
}
|
||||
|
||||
const std::string& dialect::and_() const {
|
||||
return token_at(dialect_token::And);
|
||||
}
|
||||
|
||||
const std::string& dialect::as() const {
|
||||
return token_at(dialect_token::As);
|
||||
}
|
||||
|
||||
const std::string& dialect::asc() const {
|
||||
return token_at(dialect_token::Asc);
|
||||
}
|
||||
|
||||
const std::string& dialect::begin() const {
|
||||
return token_at(dialect_token::Begin);
|
||||
}
|
||||
|
||||
const std::string& dialect::asterisk() const {
|
||||
return token_at(dialect_token::Asterisk);
|
||||
}
|
||||
|
||||
const std::string& dialect::begin_binary_data() const {
|
||||
return token_at(dialect_token::BeginBinaryData);
|
||||
}
|
||||
|
||||
const std::string& dialect::begin_string_data() const {
|
||||
return token_at(dialect_token::BeginStringData);
|
||||
}
|
||||
|
||||
const std::string& dialect::between() const {
|
||||
return token_at(dialect_token::Between);
|
||||
}
|
||||
|
||||
const std::string& dialect::column() const {
|
||||
return token_at(dialect_token::Column);
|
||||
}
|
||||
|
||||
const std::string& dialect::columns() const {
|
||||
return token_at(dialect_token::Columns);
|
||||
}
|
||||
|
||||
const std::string& dialect::commit() const {
|
||||
return token_at(dialect_token::Commit);
|
||||
}
|
||||
|
||||
const std::string& dialect::create() const {
|
||||
return token_at(dialect_token::Create);
|
||||
}
|
||||
|
||||
const std::string& dialect::drop() const {
|
||||
return token_at(dialect_token::Drop);
|
||||
}
|
||||
|
||||
const std::string& dialect::end_binary_data() const {
|
||||
return token_at(dialect_token::EndBinaryData);
|
||||
}
|
||||
|
||||
const std::string& dialect::end_string_data() const {
|
||||
return token_at(dialect_token::EndStringData);
|
||||
}
|
||||
|
||||
const std::string& dialect::from() const {
|
||||
return token_at(dialect_token::From);
|
||||
}
|
||||
|
||||
const std::string& dialect::in() const {
|
||||
return token_at(dialect_token::In);
|
||||
}
|
||||
|
||||
const std::string& dialect::desc() const {
|
||||
return token_at(dialect_token::Desc);
|
||||
}
|
||||
|
||||
const std::string& dialect::distinct() const {
|
||||
return token_at(dialect_token::Distinct);
|
||||
}
|
||||
|
||||
const std::string& dialect::drop_constraint() const {
|
||||
return token_at(dialect_token::DropConstraint);
|
||||
}
|
||||
|
||||
const std::string& dialect::end_quote() const {
|
||||
return token_at(dialect_token::EndQuote);
|
||||
}
|
||||
|
||||
const std::string& dialect::foreign_key() const {
|
||||
return token_at(dialect_token::ForeignKey);
|
||||
}
|
||||
|
||||
const std::string& dialect::group_by() const {
|
||||
return token_at(dialect_token::GroupBy);
|
||||
}
|
||||
|
||||
const std::string& dialect::insert() const {
|
||||
return token_at(dialect_token::Insert);
|
||||
}
|
||||
|
||||
const std::string& dialect::into() const {
|
||||
return token_at(dialect_token::Into);
|
||||
}
|
||||
|
||||
const std::string& dialect::join() const {
|
||||
return token_at(dialect_token::Join);
|
||||
}
|
||||
|
||||
const std::string& dialect::like() const {
|
||||
return token_at(dialect_token::Like);
|
||||
}
|
||||
|
||||
const std::string& dialect::limit() const {
|
||||
return token_at(dialect_token::Limit);
|
||||
}
|
||||
|
||||
const std::string& dialect::not_() const {
|
||||
return token_at(dialect_token::Not);
|
||||
}
|
||||
|
||||
const std::string& dialect::not_null() const {
|
||||
return token_at(dialect_token::NotNull);
|
||||
}
|
||||
|
||||
const std::string& dialect::offset() const {
|
||||
return token_at(dialect_token::Offset);
|
||||
}
|
||||
|
||||
const std::string& dialect::on() const {
|
||||
return token_at(dialect_token::On);
|
||||
}
|
||||
|
||||
const std::string& dialect::or_() const {
|
||||
return token_at(dialect_token::Or);
|
||||
}
|
||||
|
||||
const std::string& dialect::order_by() const {
|
||||
return token_at(dialect_token::OrderBy);
|
||||
}
|
||||
|
||||
const std::string& dialect::primary_key() const {
|
||||
return token_at(dialect_token::PrimaryKey);
|
||||
}
|
||||
|
||||
const std::string& dialect::references() const {
|
||||
return token_at(dialect_token::References);
|
||||
}
|
||||
|
||||
const std::string& dialect::remove() const {
|
||||
return token_at(dialect_token::Remove);
|
||||
}
|
||||
|
||||
const std::string& dialect::rollback() const {
|
||||
return token_at(dialect_token::Rollback);
|
||||
}
|
||||
|
||||
const std::string& dialect::schema() const {
|
||||
return token_at(dialect_token::Schema);
|
||||
}
|
||||
|
||||
const std::string& dialect::select() const {
|
||||
return token_at(dialect_token::Select);
|
||||
}
|
||||
|
||||
const std::string& dialect::set() const {
|
||||
return token_at(dialect_token::Set);
|
||||
}
|
||||
|
||||
const std::string& dialect::start_quote() const {
|
||||
return token_at(dialect_token::StartQuote);
|
||||
}
|
||||
|
||||
const std::string& dialect::string_quote() const {
|
||||
return token_at(dialect_token::StringQuote);
|
||||
}
|
||||
|
||||
const std::string& dialect::table() const {
|
||||
return token_at(dialect_token::Table);
|
||||
}
|
||||
|
||||
const std::string& dialect::update() const {
|
||||
return token_at(dialect_token::Update);
|
||||
}
|
||||
|
||||
const std::string& dialect::values() const {
|
||||
return token_at(dialect_token::Values);
|
||||
}
|
||||
|
||||
const std::string& dialect::where() const {
|
||||
return token_at(dialect_token::Where);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,14 +67,14 @@ const std::vector<record::field_ref> &record::columns() const
|
||||
// return columns_[pk_index_];
|
||||
//}
|
||||
|
||||
const field &record::at(const column &col) const
|
||||
const field &record::at(const sql::column &col) const
|
||||
{
|
||||
const auto &res = fields_by_name_.at(col.name);
|
||||
const auto &res = fields_by_name_.at(col.column_name());
|
||||
const auto &f = res.first;
|
||||
return f;
|
||||
}
|
||||
|
||||
const field &record::at(size_t index) const
|
||||
const field &record::at(const size_t index) const
|
||||
{
|
||||
return fields_.at(index);
|
||||
}
|
||||
|
||||
@@ -7,34 +7,51 @@ table::table(const char* name)
|
||||
{}
|
||||
|
||||
table::table(std::string name)
|
||||
: name(std::move(name))
|
||||
: name_(std::move(name))
|
||||
{}
|
||||
|
||||
table::table(const char *name, std::string as)
|
||||
: name(name)
|
||||
, alias(std::move(as))
|
||||
: name_(name)
|
||||
, alias_(std::move(as))
|
||||
{}
|
||||
|
||||
table::table(std::string name, std::string as)
|
||||
: name(std::move(name))
|
||||
, alias(std::move(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;
|
||||
return name_ == x.name_;
|
||||
}
|
||||
|
||||
table & table::as(const std::string &a) {
|
||||
alias = a;
|
||||
alias_ = a;
|
||||
return *this;
|
||||
}
|
||||
|
||||
table table::as(const std::string &a) const {
|
||||
return { name, a, columns };
|
||||
return { name_, a, columns_ };
|
||||
}
|
||||
|
||||
bool table::has_alias() const {
|
||||
return !alias.empty();
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user