Compare commits
No commits in common. "686b1ddb7a323f47b8cefe87c8763e0a27756a60" and "328d164c05f15a9e68863206a9be990b5c454a47" have entirely different histories.
686b1ddb7a
...
328d164c05
103
demo/work.cpp
103
demo/work.cpp
|
|
@ -164,106 +164,5 @@ int main() {
|
||||||
in( iPT.payload_id, query::query::select({ temp_id_col }).from( temporary ) )
|
in( iPT.payload_id, query::query::select({ temp_id_col }).from( temporary ) )
|
||||||
)
|
)
|
||||||
.str( conn->dialect() );
|
.str( conn->dialect() );
|
||||||
|
|
||||||
// const QString statement = QString( "SELECT %5 FROM %1 "
|
|
||||||
// "LEFT JOIN (SELECT %2, MAX(%3) AS lastSuccessfulLogin FROM %1 WHERE %4 = ? GROUP BY %2) ll "
|
|
||||||
// "ON %1.%2 = ll.%2 "
|
|
||||||
// "WHERE (lastSuccessfulLogin IS NULL OR %3 <= lastSuccessfulLogin) AND %3 < ?" )
|
|
||||||
// .arg( LoginHistory::staticSqlMetaInfo().tableName() )
|
|
||||||
// .arg( LoginHistory::staticSqlMetaInfo().columnName( LoginHistory::Attributes::userAttributeId() ) )
|
|
||||||
// .arg( LoginHistory::staticSqlMetaInfo().columnName( LoginHistory::Attributes::timestampAttributeId() ) )
|
|
||||||
// .arg( LoginHistory::staticSqlMetaInfo().columnName( LoginHistory::Attributes::failReasonSqlAttributeId() ) )
|
|
||||||
// .arg( LoginHistory::staticSqlMetaInfo().columnName( LoginHistory::Attributes::idAttributeId() ) );
|
|
||||||
|
|
||||||
const auto q = query::query::select({LOGIN_HISTORY.client, query::max(LOGIN_HISTORY.login_time).as( "lastSuccessfulLogin" ) } )
|
|
||||||
.from( LOGIN_HISTORY )
|
|
||||||
.where( LOGIN_HISTORY.fail_reason == _ )
|
|
||||||
.group_by( LOGIN_HISTORY.client );
|
|
||||||
// .as_table("ll");
|
|
||||||
|
|
||||||
const auto stmt3 = query::query::select({ LOGIN_HISTORY.client })
|
|
||||||
.from( LOGIN_HISTORY )
|
|
||||||
.join_left(q/*, "ll"*/ )
|
|
||||||
.on( LOGIN_HISTORY.client == "ll.client" )
|
|
||||||
.where( query::is_null("lastSuccessfulLogin") && LOGIN_HISTORY.login_time < "ll.lastSuccessfulLogin" )
|
|
||||||
.str( conn->dialect() );
|
|
||||||
|
|
||||||
std::cout << stmt1 << std::endl;
|
|
||||||
std::cout << stmt2 << std::endl;
|
|
||||||
std::cout << stmt3 << std::endl;
|
|
||||||
|
|
||||||
// const QString subQuery = QString( "SELECT MAX(%1) AS lastSuccessfulLogin, %3 FROM %2 "
|
|
||||||
// "WHERE(%4 = ? OR %4 = ? OR %4 = ?) AND %3 = ? GROUP BY %3 " );
|
|
||||||
//
|
|
||||||
// const QString statement = QString( "SELECT MAX(t1.%1) AS LAST_FAILED_LOGIN FROM %2 t1 LEFT JOIN"
|
|
||||||
// "(%0) t2 "
|
|
||||||
// "ON t1.%3 = t2.%3 "
|
|
||||||
// "WHERE(t2.lastSuccessfulLogin IS NULL OR t2.lastSuccessfulLogin < t1.%1) "
|
|
||||||
// "AND (t1.%4 = ? OR t1.%4 = ?) "
|
|
||||||
// "AND t1.%3 = ?" )
|
|
||||||
// .arg( subQuery )
|
|
||||||
// .arg( LoginHistory::staticSqlMetaInfo().columnName( LoginHistory::Attributes::timestampAttributeId() ) )
|
|
||||||
// .arg( LoginHistory::staticSqlMetaInfo().tableName() )
|
|
||||||
// .arg( columnFilterName )
|
|
||||||
// .arg( LoginHistory::staticSqlMetaInfo().columnName( LoginHistory::Attributes::failReasonSqlAttributeId() ) );
|
|
||||||
//
|
|
||||||
// QSqlQuery query( sqlDatabase() );
|
|
||||||
// if ( query.prepare( statement ) ) {
|
|
||||||
// query.addBindValue( LoginHistory::FailReason::NoFailReason );
|
|
||||||
// query.addBindValue( LoginHistory::FailReason::NoScenarioForUser );
|
|
||||||
// query.addBindValue( LoginHistory::FailReason::UserLockedByAdmin );
|
|
||||||
// query.addBindValue( columnFilterValue );
|
|
||||||
// query.addBindValue( LoginHistory::FailReason::WrongPassword );
|
|
||||||
// query.addBindValue( LoginHistory::FailReason::UnknownUserName );
|
|
||||||
// query.addBindValue( columnFilterValue );
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
SELECT
|
|
||||||
"tasks"."payload", "payloads"."id"
|
|
||||||
FROM
|
|
||||||
"public"."tasks", "public"."payloads"
|
|
||||||
WHERE
|
|
||||||
"payloads"."id" = "tasks"."payload"
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
"iPT"."payload_id" FROM "public"."id_payloads" "iPT"
|
|
||||||
LEFT JOIN
|
|
||||||
"public"."payloads" "pT"
|
|
||||||
ON
|
|
||||||
"iPT"."id" = "pT"."id"
|
|
||||||
WHERE ("pT"."id" IN (
|
|
||||||
SELECT
|
|
||||||
"jobs"."payload"
|
|
||||||
FROM
|
|
||||||
"public"."jobs"
|
|
||||||
WHERE
|
|
||||||
"jobs"."state" = $1
|
|
||||||
) AND "iPT"."payload_id" IN (
|
|
||||||
SELECT
|
|
||||||
"ID"
|
|
||||||
FROM "public"."TempTable"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
"login_histories"."client"
|
|
||||||
FROM
|
|
||||||
"public"."login_histories"
|
|
||||||
LEFT JOIN (
|
|
||||||
SELECT
|
|
||||||
"login_histories"."client", MAX(login_time) AS lastSuccessfulLogin
|
|
||||||
FROM
|
|
||||||
"public"."login_histories"
|
|
||||||
WHERE
|
|
||||||
"login_histories"."fail_reason" = $1
|
|
||||||
GROUP BY
|
|
||||||
"login_histories"."client"
|
|
||||||
)
|
|
||||||
ON
|
|
||||||
"login_histories"."client" = 'll.client'
|
|
||||||
WHERE
|
|
||||||
("lastSuccessfulLogin" IS NULL AND "login_histories"."login_time" < 'll.lastSuccessfulLogin')
|
|
||||||
*/
|
|
||||||
|
|
@ -9,8 +9,6 @@
|
||||||
|
|
||||||
#include "matador/object/object_ptr.hpp"
|
#include "matador/object/object_ptr.hpp"
|
||||||
|
|
||||||
#include "matador/query/meta_table_macro.hpp"
|
|
||||||
|
|
||||||
#include "matador/utils/base_class.hpp"
|
#include "matador/utils/base_class.hpp"
|
||||||
#include "matador/utils/enum_mapper.hpp"
|
#include "matador/utils/enum_mapper.hpp"
|
||||||
#include "matador/utils/foreign_attributes.hpp"
|
#include "matador/utils/foreign_attributes.hpp"
|
||||||
|
|
@ -61,9 +59,6 @@ struct LoginHistory : core::Model {
|
||||||
field::attribute( op, "login_time", login_time );
|
field::attribute( op, "login_time", login_time );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
META_TABLE( login_histories, LOGIN_HISTORY, id, version, client, scenario, collection_center, login_name, fail_reason, login_time )
|
|
||||||
|
|
||||||
#endif //LOGIN_HISTORY_HPP
|
#endif //LOGIN_HISTORY_HPP
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,6 @@
|
||||||
|
|
||||||
#include "matador/object/object_ptr.hpp"
|
#include "matador/object/object_ptr.hpp"
|
||||||
|
|
||||||
#include "matador/query/meta_table_macro.hpp"
|
|
||||||
|
|
||||||
#include "matador/utils/base_class.hpp"
|
#include "matador/utils/base_class.hpp"
|
||||||
#include "matador/utils/foreign_attributes.hpp"
|
#include "matador/utils/foreign_attributes.hpp"
|
||||||
#include "matador/utils/types.hpp"
|
#include "matador/utils/types.hpp"
|
||||||
|
|
@ -44,7 +42,4 @@ struct User : core::Model {
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
META_TABLE(users, USER, id, version, name, symbol, salt, password, lock_type, locked_at, lock_reason, role, user_directory)
|
|
||||||
|
|
||||||
#endif //USER_HPP
|
#endif //USER_HPP
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
#include "matador/query/criteria/between_criteria.hpp"
|
#include "matador/query/criteria/between_criteria.hpp"
|
||||||
#include "matador/query/criteria/binary_criteria.hpp"
|
#include "matador/query/criteria/binary_criteria.hpp"
|
||||||
#include "matador/query/criteria/check_null_criteria.hpp"
|
|
||||||
#include "matador/query/criteria/collection_criteria.hpp"
|
#include "matador/query/criteria/collection_criteria.hpp"
|
||||||
#include "matador/query/criteria/like_criteria.hpp"
|
#include "matador/query/criteria/like_criteria.hpp"
|
||||||
#include "matador/query/criteria/logical_criteria.hpp"
|
#include "matador/query/criteria/logical_criteria.hpp"
|
||||||
|
|
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
#ifndef MATADOR_IS_NULL_CRITERIA_HPP
|
|
||||||
#define MATADOR_IS_NULL_CRITERIA_HPP
|
|
||||||
|
|
||||||
#include "matador/query/criteria/abstract_column_criteria.hpp"
|
|
||||||
|
|
||||||
namespace matador::query {
|
|
||||||
enum class check_null_operator {
|
|
||||||
IsNull,
|
|
||||||
IsNotNull,
|
|
||||||
};
|
|
||||||
|
|
||||||
class check_null_criteria final : public abstract_column_criteria {
|
|
||||||
public:
|
|
||||||
check_null_criteria() = delete;
|
|
||||||
check_null_criteria(const table_column& col, check_null_operator op);
|
|
||||||
|
|
||||||
void accept(criteria_visitor& visitor) const override;
|
|
||||||
|
|
||||||
[[nodiscard]] check_null_operator operand() const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
check_null_operator operator_{};
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
#endif // MATADOR_IS_NULL_CRITERIA_HPP
|
|
||||||
|
|
@ -104,8 +104,5 @@ criteria_ptr between(const table_column &col, utils::placeholder min, utils::pla
|
||||||
|
|
||||||
criteria_ptr like(const table_column &col, const std::string &pattern);
|
criteria_ptr like(const table_column &col, const std::string &pattern);
|
||||||
|
|
||||||
criteria_ptr is_null(const table_column &col);
|
|
||||||
criteria_ptr is_not_null(const table_column &col);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif //CRITERIA_CRITERIA_OPERATORS_HPP
|
#endif //CRITERIA_CRITERIA_OPERATORS_HPP
|
||||||
|
|
@ -5,7 +5,6 @@ namespace matador::query {
|
||||||
class between_criteria;
|
class between_criteria;
|
||||||
class binary_criteria;
|
class binary_criteria;
|
||||||
class binary_column_criteria;
|
class binary_column_criteria;
|
||||||
class check_null_criteria;
|
|
||||||
class like_criteria;
|
class like_criteria;
|
||||||
class logical_criteria;
|
class logical_criteria;
|
||||||
class not_criteria;
|
class not_criteria;
|
||||||
|
|
@ -19,7 +18,6 @@ public:
|
||||||
virtual void visit(const between_criteria &node) = 0;
|
virtual void visit(const between_criteria &node) = 0;
|
||||||
virtual void visit(const binary_criteria &node) = 0;
|
virtual void visit(const binary_criteria &node) = 0;
|
||||||
virtual void visit(const binary_column_criteria &node) = 0;
|
virtual void visit(const binary_column_criteria &node) = 0;
|
||||||
virtual void visit(const check_null_criteria &node) = 0;
|
|
||||||
virtual void visit(const collection_criteria &node) = 0;
|
virtual void visit(const collection_criteria &node) = 0;
|
||||||
virtual void visit(const collection_query_criteria &node) = 0;
|
virtual void visit(const collection_query_criteria &node) = 0;
|
||||||
virtual void visit(const like_criteria &node) = 0;
|
virtual void visit(const like_criteria &node) = 0;
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ public:
|
||||||
void visit(const between_criteria &node) override;
|
void visit(const between_criteria &node) override;
|
||||||
void visit(const binary_criteria &node) override;
|
void visit(const binary_criteria &node) override;
|
||||||
void visit(const binary_column_criteria &node) override;
|
void visit(const binary_column_criteria &node) override;
|
||||||
void visit(const check_null_criteria &node) override;
|
|
||||||
void visit(const collection_criteria &node) override;
|
void visit(const collection_criteria &node) override;
|
||||||
void visit(const collection_query_criteria &node) override;
|
void visit(const collection_query_criteria &node) override;
|
||||||
void visit(const like_criteria &node) override;
|
void visit(const like_criteria &node) override;
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ public:
|
||||||
using fetchable_query::fetchable_query;
|
using fetchable_query::fetchable_query;
|
||||||
|
|
||||||
query_join_intermediate join_left(const table &t);
|
query_join_intermediate join_left(const table &t);
|
||||||
query_join_intermediate join_left(const fetchable_query &q);
|
|
||||||
query_from_intermediate join_left(join_data &data);
|
query_from_intermediate join_left(join_data &data);
|
||||||
query_from_intermediate join_left(std::vector<join_data> &data_vector);
|
query_from_intermediate join_left(std::vector<join_data> &data_vector);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -143,10 +143,10 @@ private:
|
||||||
std::vector<table> tables_;
|
std::vector<table> tables_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class query_join_table_part final : public query_part
|
class query_join_part final : public query_part
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit query_join_table_part(class table tab);
|
explicit query_join_part(class table tab);
|
||||||
|
|
||||||
[[nodiscard]] const class table& table() const;
|
[[nodiscard]] const class table& table() const;
|
||||||
|
|
||||||
|
|
@ -157,20 +157,6 @@ private:
|
||||||
class table table_;
|
class table table_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class query_join_query_part final : public query_part
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
explicit query_join_query_part(fetchable_query q);
|
|
||||||
|
|
||||||
[[nodiscard]] const fetchable_query& query() const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
void accept(query_part_visitor &visitor) override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
fetchable_query query_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class query_on_part final : public query_part
|
class query_on_part final : public query_part
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
#ifndef MATADOR_STRING_BUILDER_UTILS_HPP
|
|
||||||
#define MATADOR_STRING_BUILDER_UTILS_HPP
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace matador::sql {
|
|
||||||
class dialect;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace matador::query {
|
|
||||||
void prepare_identifier_string_append(std::string& out, std::string_view col, const sql::dialect &d);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // MATADOR_STRING_BUILDER_UTILS_HPP
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include "matador/utils/macro_map.hpp"
|
#include "matador/utils/macro_map.hpp"
|
||||||
|
|
||||||
|
#include "matador/query/table_column.hpp"
|
||||||
#include "matador/query/table.hpp"
|
#include "matador/query/table.hpp"
|
||||||
|
|
||||||
#define FIELD(FIELD_NAME) const matador::query::table_column& FIELD_NAME;
|
#define FIELD(FIELD_NAME) const matador::query::table_column& FIELD_NAME;
|
||||||
|
|
|
||||||
|
|
@ -10,12 +10,10 @@ class connection;
|
||||||
}
|
}
|
||||||
namespace matador::query {
|
namespace matador::query {
|
||||||
class schema;
|
class schema;
|
||||||
|
table_column alias(const std::string &column, const std::string &as);
|
||||||
|
table_column alias(table_column &&col, const std::string &as);
|
||||||
table_column count(const std::string &column);
|
table_column count(const std::string &column);
|
||||||
table_column count_all();
|
table_column count_all();
|
||||||
table_column sum(const std::string &column);
|
|
||||||
table_column avg(const std::string &column);
|
|
||||||
table_column maximum(const std::string &column);
|
|
||||||
table_column minimum(const std::string &column);
|
|
||||||
|
|
||||||
class query
|
class query
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,6 @@ public:
|
||||||
void visit( const binary_criteria& node ) override;
|
void visit( const binary_criteria& node ) override;
|
||||||
void visit( const binary_column_criteria& node ) override;
|
void visit( const binary_column_criteria& node ) override;
|
||||||
void visit( const collection_criteria& node ) override;
|
void visit( const collection_criteria& node ) override;
|
||||||
void visit( const check_null_criteria& node ) override;
|
|
||||||
void visit( const collection_query_criteria& node ) override;
|
void visit( const collection_query_criteria& node ) override;
|
||||||
void visit( const like_criteria& node ) override;
|
void visit( const like_criteria& node ) override;
|
||||||
void visit( const logical_criteria& node ) override;
|
void visit( const logical_criteria& node ) override;
|
||||||
|
|
|
||||||
|
|
@ -47,8 +47,7 @@ protected:
|
||||||
protected:
|
protected:
|
||||||
void visit(internal::query_select_part &part) override;
|
void visit(internal::query_select_part &part) override;
|
||||||
void visit(internal::query_from_part &part) override;
|
void visit(internal::query_from_part &part) override;
|
||||||
void visit(internal::query_join_table_part &part) override;
|
void visit(internal::query_join_part &part) override;
|
||||||
void visit(internal::query_join_query_part &part) override;
|
|
||||||
void visit(internal::query_on_part &part) override;
|
void visit(internal::query_on_part &part) override;
|
||||||
void visit(internal::query_where_part &part) override;
|
void visit(internal::query_where_part &part) override;
|
||||||
void visit(internal::query_group_by_part &part) override;
|
void visit(internal::query_group_by_part &part) override;
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,7 @@ class query_add_foreign_key_reference_part;
|
||||||
class query_add_primary_key_constraint_part;
|
class query_add_primary_key_constraint_part;
|
||||||
class query_select_part;
|
class query_select_part;
|
||||||
class query_from_part;
|
class query_from_part;
|
||||||
class query_join_table_part;
|
class query_join_part;
|
||||||
class query_join_query_part;
|
|
||||||
class query_on_part;
|
class query_on_part;
|
||||||
class query_where_part;
|
class query_where_part;
|
||||||
class query_group_by_part;
|
class query_group_by_part;
|
||||||
|
|
@ -58,8 +57,7 @@ public:
|
||||||
|
|
||||||
virtual void visit(internal::query_select_part &part) = 0;
|
virtual void visit(internal::query_select_part &part) = 0;
|
||||||
virtual void visit(internal::query_from_part &part) = 0;
|
virtual void visit(internal::query_from_part &part) = 0;
|
||||||
virtual void visit(internal::query_join_table_part &part) = 0;
|
virtual void visit(internal::query_join_part &part) = 0;
|
||||||
virtual void visit(internal::query_join_query_part &part) = 0;
|
|
||||||
virtual void visit(internal::query_on_part &part) = 0;
|
virtual void visit(internal::query_on_part &part) = 0;
|
||||||
virtual void visit(internal::query_where_part &part) = 0;
|
virtual void visit(internal::query_where_part &part) = 0;
|
||||||
virtual void visit(internal::query_group_by_part &part) = 0;
|
virtual void visit(internal::query_group_by_part &part) = 0;
|
||||||
|
|
|
||||||
|
|
@ -16,19 +16,13 @@ class table;
|
||||||
class table_column {
|
class table_column {
|
||||||
public:
|
public:
|
||||||
table_column(const char *name); // NOLINT(*-explicit-constructor)
|
table_column(const char *name); // NOLINT(*-explicit-constructor)
|
||||||
table_column(const std::string& name); // NOLINT(*-explicit-constructor)
|
table_column(std::string name); // NOLINT(*-explicit-constructor)
|
||||||
table_column(const std::string& name, const std::string& alias);
|
table_column(std::string name, std::string alias);
|
||||||
table_column(sql::sql_function_t func, const std::string& name);
|
table_column(sql::sql_function_t func, std::string name);
|
||||||
table_column(const class table* tab, const std::string& name);
|
table_column(const class table* tab, std::string name);
|
||||||
table_column(const class table* tab, const std::string& name,
|
table_column(const class table* tab, std::string name, std::string alias);
|
||||||
const std::string& alias);
|
table_column(const class table* tab, std::string name, utils::basic_type type, const utils::field_attributes& attributes);
|
||||||
table_column(const class table* tab, const std::string& name, utils::basic_type type, const utils::field_attributes& attributes);
|
table_column(const class table*, std::string name, std::string alias, utils::basic_type type, const utils::field_attributes& attributes, sql::sql_function_t func = sql::sql_function_t::None);
|
||||||
table_column(const class table*,
|
|
||||||
const std::string& name,
|
|
||||||
const std::string& alias,
|
|
||||||
utils::basic_type type,
|
|
||||||
const utils::field_attributes& attributes,
|
|
||||||
sql::sql_function_t func = sql::sql_function_t::None);
|
|
||||||
table_column& operator=(const table_column& other);
|
table_column& operator=(const table_column& other);
|
||||||
table_column(const table_column& other) = default;
|
table_column(const table_column& other) = default;
|
||||||
table_column(table_column&& other) noexcept = default;
|
table_column(table_column&& other) noexcept = default;
|
||||||
|
|
@ -36,11 +30,10 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] bool equals(const table_column &x) const;
|
[[nodiscard]] bool equals(const table_column &x) const;
|
||||||
|
|
||||||
[[nodiscard]] table_column as(const std::string& alias) const;
|
table_column as(std::string a);
|
||||||
|
|
||||||
[[nodiscard]] const std::string& name() const;
|
[[nodiscard]] const std::string& name() const;
|
||||||
[[nodiscard]] const std::string& column_name() const;
|
[[nodiscard]] std::string canonical_name() const;
|
||||||
[[nodiscard]] const std::string& canonical_name() const;
|
|
||||||
[[nodiscard]] const std::string& alias() const;
|
[[nodiscard]] const std::string& alias() const;
|
||||||
[[nodiscard]] utils::basic_type type() const;
|
[[nodiscard]] utils::basic_type type() const;
|
||||||
[[nodiscard]] utils::field_attributes attributes() const;
|
[[nodiscard]] utils::field_attributes attributes() const;
|
||||||
|
|
@ -56,15 +49,11 @@ public:
|
||||||
// ReSharper disable once CppNonExplicitConversionOperator
|
// ReSharper disable once CppNonExplicitConversionOperator
|
||||||
operator const std::string&() const; // NOLINT(*-explicit-constructor)
|
operator const std::string&() const; // NOLINT(*-explicit-constructor)
|
||||||
|
|
||||||
private:
|
|
||||||
static std::string build_column_name(const class table *tab, const std::string& name);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class table;
|
friend class table;
|
||||||
|
|
||||||
const class table* table_{nullptr};
|
const class table* table_{nullptr};
|
||||||
std::string name_;
|
std::string name_;
|
||||||
std::string column_name_;
|
|
||||||
std::string alias_;
|
std::string alias_;
|
||||||
utils::basic_type type_{utils::basic_type::Unknown};
|
utils::basic_type type_{utils::basic_type::Unknown};
|
||||||
utils::field_attributes attributes_{};
|
utils::field_attributes attributes_{};
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,7 @@ public:
|
||||||
* @return The prepared string
|
* @return The prepared string
|
||||||
*/
|
*/
|
||||||
[[nodiscard]] std::string prepare_identifier_string(const std::string &col) const;
|
[[nodiscard]] std::string prepare_identifier_string(const std::string &col) const;
|
||||||
|
[[nodiscard]] std::string table_name(const std::string &table, const std::string &schema_name) const;
|
||||||
|
|
||||||
[[nodiscard]] const std::string& to_string(bool val) const;
|
[[nodiscard]] const std::string& to_string(bool val) const;
|
||||||
|
|
||||||
|
|
@ -151,8 +152,6 @@ public:
|
||||||
[[nodiscard]] const std::string& in() const;
|
[[nodiscard]] const std::string& in() const;
|
||||||
[[nodiscard]] const std::string& insert() const;
|
[[nodiscard]] const std::string& insert() const;
|
||||||
[[nodiscard]] const std::string& into() const;
|
[[nodiscard]] const std::string& into() const;
|
||||||
[[nodiscard]] const std::string& is_null() const;
|
|
||||||
[[nodiscard]] const std::string& is_not_null() const;
|
|
||||||
[[nodiscard]] const std::string& join() const;
|
[[nodiscard]] const std::string& join() const;
|
||||||
[[nodiscard]] const std::string& like() const;
|
[[nodiscard]] const std::string& like() const;
|
||||||
[[nodiscard]] const std::string& limit() const;
|
[[nodiscard]] const std::string& limit() const;
|
||||||
|
|
@ -219,8 +218,6 @@ private:
|
||||||
{dialect_token::In, "IN"},
|
{dialect_token::In, "IN"},
|
||||||
{dialect_token::Insert, "INSERT"},
|
{dialect_token::Insert, "INSERT"},
|
||||||
{dialect_token::Into, "INTO"},
|
{dialect_token::Into, "INTO"},
|
||||||
{dialect_token::IsNull, "IS NULL"},
|
|
||||||
{dialect_token::IsNotNull, "IS NOT NULL"},
|
|
||||||
{dialect_token::Join, "LEFT JOIN"},
|
{dialect_token::Join, "LEFT JOIN"},
|
||||||
{dialect_token::Like, "LIKE"},
|
{dialect_token::Like, "LIKE"},
|
||||||
{dialect_token::Limit, "LIMIT"},
|
{dialect_token::Limit, "LIMIT"},
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,6 @@ enum class dialect_token : uint8_t {
|
||||||
In,
|
In,
|
||||||
Insert,
|
Insert,
|
||||||
Into,
|
Into,
|
||||||
IsNull,
|
|
||||||
IsNotNull,
|
|
||||||
Join,
|
Join,
|
||||||
Like,
|
Like,
|
||||||
Limit,
|
Limit,
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ add_library(matador-orm STATIC
|
||||||
../../include/matador/query/criteria/abstract_column_criteria.hpp
|
../../include/matador/query/criteria/abstract_column_criteria.hpp
|
||||||
../../include/matador/query/criteria/abstract_criteria.hpp
|
../../include/matador/query/criteria/abstract_criteria.hpp
|
||||||
../../include/matador/query/criteria/between_criteria.hpp
|
../../include/matador/query/criteria/between_criteria.hpp
|
||||||
../../include/matador/query/criteria/check_null_criteria.hpp
|
|
||||||
../../include/matador/query/criteria/collection_criteria.hpp
|
../../include/matador/query/criteria/collection_criteria.hpp
|
||||||
../../include/matador/query/criteria/criteria_operators.hpp
|
../../include/matador/query/criteria/criteria_operators.hpp
|
||||||
../../include/matador/query/criteria/criteria_utils.hpp
|
../../include/matador/query/criteria/criteria_utils.hpp
|
||||||
|
|
@ -48,7 +47,6 @@ add_library(matador-orm STATIC
|
||||||
../../include/matador/query/internal/basic_type_to_string_visitor.hpp
|
../../include/matador/query/internal/basic_type_to_string_visitor.hpp
|
||||||
../../include/matador/query/internal/column_value_pair.hpp
|
../../include/matador/query/internal/column_value_pair.hpp
|
||||||
../../include/matador/query/internal/query_parts.hpp
|
../../include/matador/query/internal/query_parts.hpp
|
||||||
../../include/matador/query/internal/string_builder_utils.hpp
|
|
||||||
../../include/matador/query/join_data.hpp
|
../../include/matador/query/join_data.hpp
|
||||||
../../include/matador/query/key_value_generator.hpp
|
../../include/matador/query/key_value_generator.hpp
|
||||||
../../include/matador/query/meta_table_macro.hpp
|
../../include/matador/query/meta_table_macro.hpp
|
||||||
|
|
@ -104,7 +102,6 @@ add_library(matador-orm STATIC
|
||||||
query/criteria/abstract_column_criteria.cpp
|
query/criteria/abstract_column_criteria.cpp
|
||||||
query/criteria/between_criteria.cpp
|
query/criteria/between_criteria.cpp
|
||||||
query/criteria/binary_criteria.cpp
|
query/criteria/binary_criteria.cpp
|
||||||
query/criteria/check_null_criteria.cpp
|
|
||||||
query/criteria/collection_criteria.cpp
|
query/criteria/collection_criteria.cpp
|
||||||
query/criteria/criteria_operators.cpp
|
query/criteria/criteria_operators.cpp
|
||||||
query/criteria/like_criteria.cpp
|
query/criteria/like_criteria.cpp
|
||||||
|
|
@ -143,7 +140,6 @@ add_library(matador-orm STATIC
|
||||||
query/internal/column_value_pair.cpp
|
query/internal/column_value_pair.cpp
|
||||||
query/internal/query_parts.cpp
|
query/internal/query_parts.cpp
|
||||||
query/internal/query_result_impl.cpp
|
query/internal/query_result_impl.cpp
|
||||||
query/internal/string_builder_utils.cpp
|
|
||||||
query/key_value_generator.cpp
|
query/key_value_generator.cpp
|
||||||
query/query.cpp
|
query/query.cpp
|
||||||
query/query_builder.cpp
|
query/query_builder.cpp
|
||||||
|
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
#include "matador/query/criteria/check_null_criteria.hpp"
|
|
||||||
|
|
||||||
#include "matador/query/criteria/criteria_visitor.hpp"
|
|
||||||
|
|
||||||
namespace matador::query {
|
|
||||||
check_null_criteria::check_null_criteria(const table_column& col, const check_null_operator op)
|
|
||||||
: abstract_column_criteria(col)
|
|
||||||
, operator_{op} {}
|
|
||||||
|
|
||||||
void check_null_criteria::accept(criteria_visitor& visitor) const {
|
|
||||||
visitor.visit(*this);
|
|
||||||
}
|
|
||||||
check_null_operator check_null_criteria::operand() const {
|
|
||||||
return operator_;
|
|
||||||
}
|
|
||||||
} // namespace matador::query
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
#include "matador/query/criteria/criteria_operators.hpp"
|
#include "matador/query/criteria/criteria_operators.hpp"
|
||||||
|
|
||||||
#include "matador/query/criteria/between_criteria.hpp"
|
#include "matador/query/criteria/between_criteria.hpp"
|
||||||
#include "matador/query/criteria/check_null_criteria.hpp"
|
|
||||||
#include "matador/query/criteria/like_criteria.hpp"
|
#include "matador/query/criteria/like_criteria.hpp"
|
||||||
#include "matador/query/criteria/logical_criteria.hpp"
|
#include "matador/query/criteria/logical_criteria.hpp"
|
||||||
#include "matador/query/criteria/not_criteria.hpp"
|
#include "matador/query/criteria/not_criteria.hpp"
|
||||||
|
|
@ -119,11 +118,4 @@ criteria_ptr between(const table_column &col, utils::placeholder min, utils::pla
|
||||||
criteria_ptr like(const table_column &col, const std::string &pattern) {
|
criteria_ptr like(const table_column &col, const std::string &pattern) {
|
||||||
return std::make_unique<like_criteria>(col, pattern);
|
return std::make_unique<like_criteria>(col, pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
criteria_ptr is_null(const table_column &col) {
|
|
||||||
return std::make_unique<check_null_criteria>(col, check_null_operator::IsNull);
|
|
||||||
}
|
}
|
||||||
criteria_ptr is_not_null(const table_column &col) {
|
|
||||||
return std::make_unique<check_null_criteria>(col, check_null_operator::IsNotNull);
|
|
||||||
}
|
|
||||||
} // namespace matador::query
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,18 @@
|
||||||
#include "matador/query/criteria_evaluator.hpp"
|
#include "matador/query/criteria_evaluator.hpp"
|
||||||
|
|
||||||
|
#include "matador/query/query_utils.hpp"
|
||||||
#include "matador/query/criteria/between_criteria.hpp"
|
#include "matador/query/criteria/between_criteria.hpp"
|
||||||
#include "matador/query/criteria/binary_criteria.hpp"
|
#include "matador/query/criteria/binary_criteria.hpp"
|
||||||
#include "matador/query/criteria/check_null_criteria.hpp"
|
|
||||||
#include "matador/query/criteria/collection_criteria.hpp"
|
#include "matador/query/criteria/collection_criteria.hpp"
|
||||||
#include "matador/query/criteria/like_criteria.hpp"
|
#include "matador/query/criteria/like_criteria.hpp"
|
||||||
#include "matador/query/criteria/logical_criteria.hpp"
|
#include "matador/query/criteria/logical_criteria.hpp"
|
||||||
#include "matador/query/criteria/not_criteria.hpp"
|
#include "matador/query/criteria/not_criteria.hpp"
|
||||||
#include "matador/query/query_utils.hpp"
|
|
||||||
#include "matador/sql/dialect.hpp"
|
#include "matador/sql/dialect.hpp"
|
||||||
#include "matador/sql/query_context.hpp"
|
#include "matador/sql/query_context.hpp"
|
||||||
#include "matador/utils/enum_mapper.hpp"
|
|
||||||
#include "matador/utils/value.hpp"
|
#include "matador/utils/value.hpp"
|
||||||
|
#include "matador/utils/enum_mapper.hpp"
|
||||||
|
|
||||||
namespace matador::query {
|
namespace matador::query {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
@ -40,7 +41,7 @@ std::string criteria_evaluator::evaluate(const abstract_criteria &node) {
|
||||||
void criteria_evaluator::visit(const between_criteria &node) {
|
void criteria_evaluator::visit(const between_criteria &node) {
|
||||||
query_.bind_vars.emplace_back(node.col().name());
|
query_.bind_vars.emplace_back(node.col().name());
|
||||||
query_.bind_vars.emplace_back(node.col().name());
|
query_.bind_vars.emplace_back(node.col().name());
|
||||||
clause_ += prepare_identifier(dialect_, node.col()) + " " + dialect_.between() + " ";
|
clause_ += prepare_identifier(dialect_, node.col()) + " " + dialect_.token_at(sql::dialect_token::Between) + " ";
|
||||||
evaluate_value(node.minimum());
|
evaluate_value(node.minimum());
|
||||||
clause_ += " " + dialect_.token_at(sql::dialect_token::And) + " ";
|
clause_ += " " + dialect_.token_at(sql::dialect_token::And) + " ";
|
||||||
evaluate_value(node.maximum());
|
evaluate_value(node.maximum());
|
||||||
|
|
@ -60,14 +61,6 @@ void criteria_evaluator::visit( const binary_column_criteria& node ) {
|
||||||
clause_ += prepare_criteria(dialect_, node.left_column()) + " " + detail::BinaryOperatorEnum.to_string(node.operand()) + " " + prepare_criteria(dialect_, node.right_column());
|
clause_ += prepare_criteria(dialect_, node.left_column()) + " " + detail::BinaryOperatorEnum.to_string(node.operand()) + " " + prepare_criteria(dialect_, node.right_column());
|
||||||
}
|
}
|
||||||
|
|
||||||
void criteria_evaluator::visit(const check_null_criteria &node) {
|
|
||||||
clause_ += prepare_identifier(dialect_, node.col()) + " ";
|
|
||||||
if (node.operand() == check_null_operator::IsNull)
|
|
||||||
clause_ += dialect_.is_null();
|
|
||||||
else
|
|
||||||
clause_ += dialect_.is_not_null();
|
|
||||||
}
|
|
||||||
|
|
||||||
void criteria_evaluator::visit(const collection_criteria &node) {
|
void criteria_evaluator::visit(const collection_criteria &node) {
|
||||||
const auto count = node.values().size();
|
const auto count = node.values().size();
|
||||||
for (size_t i = 0; i < count; ++i) {
|
for (size_t i = 0; i < count; ++i) {
|
||||||
|
|
@ -94,12 +87,12 @@ void criteria_evaluator::visit(const collection_criteria &node) {
|
||||||
|
|
||||||
void criteria_evaluator::visit(const collection_query_criteria &node) {
|
void criteria_evaluator::visit(const collection_query_criteria &node) {
|
||||||
clause_ += prepare_identifier(dialect_, node.col()) +
|
clause_ += prepare_identifier(dialect_, node.col()) +
|
||||||
(node.operand() == collection_operator::Out ? " " + dialect_.not_() + " " : " ") +
|
(node.operand() == collection_operator::Out ? " " + dialect_.token_at(sql::dialect_token::Not) + " " : " ") +
|
||||||
dialect_.in() + " (" + node.query().str( dialect_ ) + ")";
|
dialect_.token_at(sql::dialect_token::In) + " (" + node.query().str( dialect_ ) + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
void criteria_evaluator::visit(const like_criteria &node) {
|
void criteria_evaluator::visit(const like_criteria &node) {
|
||||||
clause_ += prepare_criteria(dialect_, node.col()) + " " + dialect_.like() +
|
clause_ += prepare_criteria(dialect_, node.col()) + " " + dialect_.token_at(sql::dialect_token::Like) +
|
||||||
" " + dialect_.token_at(sql::dialect_token::BeginStringData) + node.pattern() + dialect_.token_at(
|
" " + dialect_.token_at(sql::dialect_token::BeginStringData) + node.pattern() + dialect_.token_at(
|
||||||
sql::dialect_token::EndStringData);
|
sql::dialect_token::EndStringData);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,24 +10,19 @@
|
||||||
namespace matador::query {
|
namespace matador::query {
|
||||||
|
|
||||||
query_join_intermediate query_from_intermediate::join_left(const table &t) {
|
query_join_intermediate query_from_intermediate::join_left(const table &t) {
|
||||||
context_->parts.push_back(std::make_unique<internal::query_join_table_part>(t));
|
context_->parts.push_back(std::make_unique<internal::query_join_part>(t));
|
||||||
return {context_};
|
|
||||||
}
|
|
||||||
|
|
||||||
query_join_intermediate query_from_intermediate::join_left(const fetchable_query &q) {
|
|
||||||
context_->parts.push_back(std::make_unique<internal::query_join_query_part>(q));
|
|
||||||
return {context_};
|
return {context_};
|
||||||
}
|
}
|
||||||
|
|
||||||
query_from_intermediate query_from_intermediate::join_left(join_data &data) {
|
query_from_intermediate query_from_intermediate::join_left(join_data &data) {
|
||||||
context_->parts.push_back(std::make_unique<internal::query_join_table_part>(*data.join_table));
|
context_->parts.push_back(std::make_unique<internal::query_join_part>(*data.join_table));
|
||||||
context_->parts.push_back(std::make_unique<internal::query_on_part>(std::move(data.condition)));
|
context_->parts.push_back(std::make_unique<internal::query_on_part>(std::move(data.condition)));
|
||||||
return {context_};
|
return {context_};
|
||||||
}
|
}
|
||||||
|
|
||||||
query_from_intermediate query_from_intermediate::join_left(std::vector<join_data> &data_vector) {
|
query_from_intermediate query_from_intermediate::join_left(std::vector<join_data> &data_vector) {
|
||||||
for (auto &[join_table, condition] : data_vector) {
|
for (auto &[join_table, condition] : data_vector) {
|
||||||
context_->parts.push_back(std::make_unique<internal::query_join_table_part>(*join_table));
|
context_->parts.push_back(std::make_unique<internal::query_join_part>(*join_table));
|
||||||
context_->parts.push_back(std::make_unique<internal::query_on_part>(std::move(condition)));
|
context_->parts.push_back(std::make_unique<internal::query_on_part>(std::move(condition)));
|
||||||
}
|
}
|
||||||
return {context_};
|
return {context_};
|
||||||
|
|
|
||||||
|
|
@ -147,29 +147,16 @@ void query_from_part::accept(query_part_visitor &visitor) {
|
||||||
visitor.visit(*this);
|
visitor.visit(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
query_join_table_part::query_join_table_part(class table tab)
|
query_join_part::query_join_part(class table tab)
|
||||||
: query_part(sql::dialect_token::Join)
|
: query_part(sql::dialect_token::Join)
|
||||||
, table_(std::move(tab)) {
|
, table_(std::move(tab)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const table &query_join_table_part::table() const {
|
const table &query_join_part::table() const {
|
||||||
return table_;
|
return table_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_join_table_part::accept(query_part_visitor &visitor) {
|
void query_join_part::accept(query_part_visitor &visitor) {
|
||||||
visitor.visit(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
query_join_query_part::query_join_query_part(fetchable_query q)
|
|
||||||
: query_part(sql::dialect_token::Join)
|
|
||||||
, query_(std::move(q))
|
|
||||||
{}
|
|
||||||
|
|
||||||
const fetchable_query &query_join_query_part::query() const {
|
|
||||||
return query_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void query_join_query_part::accept(query_part_visitor &visitor) {
|
|
||||||
visitor.visit(*this);
|
visitor.visit(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,40 +0,0 @@
|
||||||
#include "matador/query/internal/string_builder_utils.hpp"
|
|
||||||
|
|
||||||
#include "matador/sql/dialect.hpp"
|
|
||||||
|
|
||||||
namespace matador::query {
|
|
||||||
void prepare_identifier_string_append(std::string& out, const std::string_view col, const sql::dialect &d) {
|
|
||||||
bool first_part = true;
|
|
||||||
|
|
||||||
const char sq = d.start_quote()[0];
|
|
||||||
const char eq = d.end_quote()[0];
|
|
||||||
|
|
||||||
std::size_t i = 0;
|
|
||||||
while (true) {
|
|
||||||
const std::size_t start = i;
|
|
||||||
|
|
||||||
// find end of part
|
|
||||||
while (i < col.size() && col[i] != '.') {
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
const std::size_t end = i; // [start, end) is the part
|
|
||||||
|
|
||||||
if (!first_part) {
|
|
||||||
out.push_back('.');
|
|
||||||
}
|
|
||||||
first_part = false;
|
|
||||||
|
|
||||||
// quote_identifier(part) + escape_quotes_in_identifier(part), but streaming:
|
|
||||||
out.push_back(sq);
|
|
||||||
for (std::size_t j = start; j < end; ++j) {
|
|
||||||
const char c = col[j];
|
|
||||||
if (c == eq) out.push_back(eq); // escape " as ""
|
|
||||||
out.push_back(c);
|
|
||||||
}
|
|
||||||
out.push_back(eq);
|
|
||||||
|
|
||||||
if (i >= col.size()) break; // done
|
|
||||||
++i; // skip '.'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,6 +2,15 @@
|
||||||
|
|
||||||
namespace matador::query {
|
namespace matador::query {
|
||||||
|
|
||||||
|
table_column alias(const std::string &column, const std::string &as) {
|
||||||
|
return {column, as};
|
||||||
|
}
|
||||||
|
|
||||||
|
table_column alias(table_column &&col, const std::string &as) {
|
||||||
|
col.as(as);
|
||||||
|
return col;
|
||||||
|
}
|
||||||
|
|
||||||
table_column count(const std::string &column) {
|
table_column count(const std::string &column) {
|
||||||
return {sql::sql_function_t::Count, column};
|
return {sql::sql_function_t::Count, column};
|
||||||
}
|
}
|
||||||
|
|
@ -9,18 +18,6 @@ table_column count(const std::string &column) {
|
||||||
table_column count_all() {
|
table_column count_all() {
|
||||||
return count("*");
|
return count("*");
|
||||||
}
|
}
|
||||||
table_column sum(const std::string& column) {
|
|
||||||
return {sql::sql_function_t::Sum, column};
|
|
||||||
}
|
|
||||||
table_column avg(const std::string& column) {
|
|
||||||
return {sql::sql_function_t::Avg, column};
|
|
||||||
}
|
|
||||||
table_column maximum(const std::string& column) {
|
|
||||||
return {sql::sql_function_t::Max, column};
|
|
||||||
}
|
|
||||||
table_column minimum(const std::string& column) {
|
|
||||||
return {sql::sql_function_t::Min, column};
|
|
||||||
}
|
|
||||||
|
|
||||||
query_create_intermediate query::create()
|
query_create_intermediate query::create()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -15,10 +15,6 @@ void criteria_transformer::visit( const binary_criteria& node ) {
|
||||||
|
|
||||||
void criteria_transformer::visit( const binary_column_criteria& /*node*/ ) {}
|
void criteria_transformer::visit( const binary_column_criteria& /*node*/ ) {}
|
||||||
|
|
||||||
void criteria_transformer::visit(const check_null_criteria& node) {
|
|
||||||
update_criteria_column(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
void criteria_transformer::visit( const collection_criteria& node ) {
|
void criteria_transformer::visit( const collection_criteria& node ) {
|
||||||
update_criteria_column(node);
|
update_criteria_column(node);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,14 @@
|
||||||
#include "matador/query/table_column.hpp"
|
#include "matador/query/table_column.hpp"
|
||||||
|
|
||||||
#include "matador/query/internal/basic_type_to_string_visitor.hpp"
|
#include "matador/query/internal/basic_type_to_string_visitor.hpp"
|
||||||
#include "matador/query/internal/string_builder_utils.hpp"
|
|
||||||
#include "matador/query/internal/query_parts.hpp"
|
#include "matador/query/internal/query_parts.hpp"
|
||||||
|
|
||||||
#include "matador/sql/query_context.hpp"
|
#include "matador/sql/query_context.hpp"
|
||||||
#include "matador/sql/connection.hpp"
|
#include "matador/sql/connection.hpp"
|
||||||
#include "matador/sql/dialect.hpp"
|
#include "matador/sql/dialect.hpp"
|
||||||
|
|
||||||
|
#include "matador/utils/string.hpp"
|
||||||
|
|
||||||
namespace matador::query {
|
namespace matador::query {
|
||||||
|
|
||||||
sql::query_context query_compiler::compile(const query_data &data,
|
sql::query_context query_compiler::compile(const query_data &data,
|
||||||
|
|
@ -38,10 +39,10 @@ sql::query_context query_compiler::compile(const query_data &data,
|
||||||
|
|
||||||
std::string handle_column(sql::query_context &ctx, const sql::dialect *d, const query_data &data, const table_column &col) {
|
std::string handle_column(sql::query_context &ctx, const sql::dialect *d, const query_data &data, const table_column &col) {
|
||||||
if (col.is_function()) {
|
if (col.is_function()) {
|
||||||
ctx.prototype.emplace_back(col.name());
|
ctx.prototype.emplace_back(col.has_alias() ? col.alias() : col.canonical_name());
|
||||||
ctx.prototype.back().change_type(utils::basic_type::Int32);
|
ctx.prototype.back().change_type(utils::basic_type::Int32);
|
||||||
} else {
|
} else {
|
||||||
ctx.prototype.emplace_back(col.name());
|
ctx.prototype.emplace_back(col.canonical_name());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -61,30 +62,61 @@ void query_compiler::visit(internal::query_alter_part& part) {
|
||||||
void query_compiler::visit(internal::query_alter_table_part& part) {
|
void query_compiler::visit(internal::query_alter_table_part& part) {
|
||||||
query_.command = sql::sql_command::SQL_ALTER_TABLE;
|
query_.command = sql::sql_command::SQL_ALTER_TABLE;
|
||||||
query_.sql += " " + dialect_->token_at(part.token()) + " " +
|
query_.sql += " " + dialect_->token_at(part.token()) + " " +
|
||||||
dialect_->prepare_identifier_string(part.table().name());
|
dialect_->prepare_identifier_string(part.table().name());
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_compiler::visit(internal::query_add_key_constraint_part& part) {
|
void query_compiler::visit(internal::query_add_key_constraint_part& part) {
|
||||||
query_.sql += " " + dialect_->add_constraint() + " " + part.name();
|
query_.sql += " " + dialect_->add_constraint() + " " + part.name();
|
||||||
}
|
}
|
||||||
|
|
||||||
void build_columns(std::string &out, const std::vector<table_column> &cols, const sql::dialect &d);
|
|
||||||
|
|
||||||
void query_compiler::visit(internal::query_add_foreign_key_constraint_part& part) {
|
void query_compiler::visit(internal::query_add_foreign_key_constraint_part& part) {
|
||||||
query_.sql += " " + dialect_->token_at(part.token()) + " (";
|
query_.sql += " " + dialect_->token_at(part.token()) + " (";
|
||||||
build_columns(query_.sql, part.columns(), *dialect_);
|
|
||||||
|
if (part.columns().size() < 2) {
|
||||||
|
for (const auto &col: part.columns()) {
|
||||||
|
query_.sql += dialect_->prepare_identifier_string(col.name());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auto it = part.columns().begin();
|
||||||
|
query_.sql += dialect_->prepare_identifier_string(it->name());
|
||||||
|
for (; it != part.columns().end(); ++it) {
|
||||||
|
query_.sql += ", " + dialect_->prepare_identifier_string(it->name());
|
||||||
|
}
|
||||||
|
}
|
||||||
query_.sql += ")";
|
query_.sql += ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_compiler::visit(internal::query_add_primary_key_constraint_part& part) {
|
void query_compiler::visit(internal::query_add_primary_key_constraint_part& part) {
|
||||||
query_.sql += " " + dialect_->primary_key() + " (";
|
query_.sql += " " + dialect_->primary_key() + " (";
|
||||||
build_columns(query_.sql, part.columns(), *dialect_);
|
|
||||||
query_.sql += ")";
|
if (part.columns().size() < 2) {
|
||||||
|
for (const auto &col: part.columns()) {
|
||||||
|
query_.sql += dialect_->prepare_identifier_string(col.name());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auto it = part.columns().begin();
|
||||||
|
query_.sql += dialect_->prepare_identifier_string(it->name());
|
||||||
|
for (; it != part.columns().end(); ++it) {
|
||||||
|
query_.sql += ", " + dialect_->prepare_identifier_string(it->name());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
query_.sql += ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_compiler::visit(internal::query_add_foreign_key_reference_part& part) {
|
void query_compiler::visit(internal::query_add_foreign_key_reference_part& part) {
|
||||||
query_.sql += " " + dialect_->token_at(part.token()) + " " + part.table().name() + " (";
|
query_.sql += " " + dialect_->token_at(part.token()) + " " + part.table().name() + " (";
|
||||||
build_columns(query_.sql, part.columns(), *dialect_);
|
|
||||||
|
if (part.columns().size() < 2) {
|
||||||
|
for (const auto &col: part.columns()) {
|
||||||
|
query_.sql += dialect_->prepare_identifier_string(col.name());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auto it = part.columns().begin();
|
||||||
|
query_.sql += dialect_->prepare_identifier_string(it->name());
|
||||||
|
for (; it != part.columns().end(); ++it) {
|
||||||
|
query_.sql += ", " + dialect_->prepare_identifier_string(it->name());
|
||||||
|
}
|
||||||
|
}
|
||||||
query_.sql += ")";
|
query_.sql += ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -142,14 +174,10 @@ void query_compiler::visit(internal::query_from_part &part) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_compiler::visit(internal::query_join_table_part &part) {
|
void query_compiler::visit(internal::query_join_part &part) {
|
||||||
query_.sql += " " + build_table_name(part.token(), *dialect_, part.table());
|
query_.sql += " " + build_table_name(part.token(), *dialect_, part.table());
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_compiler::visit(internal::query_join_query_part &part) {
|
|
||||||
query_.sql += " " + dialect_->join() + " (" + part.query().str(*dialect_) + ")";
|
|
||||||
}
|
|
||||||
|
|
||||||
void query_compiler::visit(internal::query_on_part &part) {
|
void query_compiler::visit(internal::query_on_part &part) {
|
||||||
criteria_evaluator evaluator(*dialect_, query_);
|
criteria_evaluator evaluator(*dialect_, query_);
|
||||||
query_.sql += " " + dialect_->on() +
|
query_.sql += " " + dialect_->on() +
|
||||||
|
|
@ -166,7 +194,7 @@ void query_compiler::visit(internal::query_group_by_part &part) {
|
||||||
query_.sql += " " + dialect_->group_by() + " ";
|
query_.sql += " " + dialect_->group_by() + " ";
|
||||||
if (part.columns().size() < 2) {
|
if (part.columns().size() < 2) {
|
||||||
for (const auto &col: part.columns()) {
|
for (const auto &col: part.columns()) {
|
||||||
query_.sql.append(dialect_->prepare_identifier_string(col.name()));
|
query_.sql.append(dialect_->prepare_identifier_string(col.canonical_name()));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
auto it = part.columns().begin();
|
auto it = part.columns().begin();
|
||||||
|
|
@ -218,10 +246,23 @@ void query_compiler::visit(internal::query_insert_part &/*insert_part*/) {
|
||||||
void query_compiler::visit(internal::query_into_part &part) {
|
void query_compiler::visit(internal::query_into_part &part) {
|
||||||
query_.table_name = part.table().name();
|
query_.table_name = part.table().name();
|
||||||
query_.sql += " " + dialect_->into() +
|
query_.sql += " " + dialect_->into() +
|
||||||
" " + dialect_->prepare_identifier_string(part.table().name()) + " (";
|
" " + dialect_->prepare_identifier_string(part.table().name());
|
||||||
|
|
||||||
build_columns(query_.sql, part.columns(), *dialect_);
|
std::string result{"("};
|
||||||
query_.sql += ")"/* + result*/;
|
if (part.columns().size() < 2) {
|
||||||
|
for (const auto &col: part.columns()) {
|
||||||
|
result.append(dialect_->prepare_identifier_string(col.name()));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auto it = part.columns().begin();
|
||||||
|
result.append(dialect_->prepare_identifier_string((it++)->name()));
|
||||||
|
for (; it != part.columns().end(); ++it) {
|
||||||
|
result.append(", ");
|
||||||
|
result.append(dialect_->prepare_identifier_string(it->name()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result += (")");
|
||||||
|
query_.sql += " " + result;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct value_visitor {
|
struct value_visitor {
|
||||||
|
|
@ -296,7 +337,7 @@ void query_compiler::visit(internal::query_create_part &/*create_part*/)
|
||||||
query_.sql = dialect_->create();
|
query_.sql = dialect_->create();
|
||||||
}
|
}
|
||||||
|
|
||||||
void build_create_column(std::string &out, const table_column &col, const sql::dialect &d);
|
std::string build_create_column(const table_column &col, const sql::dialect &d);
|
||||||
std::string build_constraint(const table_constraint &cons, const sql::dialect &d);
|
std::string build_constraint(const table_constraint &cons, const sql::dialect &d);
|
||||||
|
|
||||||
void query_compiler::visit(internal::query_create_table_part &part)
|
void query_compiler::visit(internal::query_create_table_part &part)
|
||||||
|
|
@ -308,14 +349,22 @@ void query_compiler::visit(internal::query_create_table_part &part)
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_compiler::visit(internal::query_create_table_columns_part& part) {
|
void query_compiler::visit(internal::query_create_table_columns_part& part) {
|
||||||
bool first = true;
|
std::string result;
|
||||||
for (const auto& col : part.columns()) {
|
|
||||||
if (!first) {
|
if (part.columns().size() < 2) {
|
||||||
query_.sql.append(", ");
|
for (const auto &col: part.columns()) {
|
||||||
|
result.append(build_create_column(col, *dialect_));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auto it = part.columns().begin();
|
||||||
|
result.append(build_create_column(*it++, *dialect_));
|
||||||
|
for (; it != part.columns().end(); ++it) {
|
||||||
|
result.append(", ");
|
||||||
|
result.append(build_create_column(*it, *dialect_));
|
||||||
}
|
}
|
||||||
build_create_column(query_.sql, col, *dialect_);
|
|
||||||
first = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
query_.sql += result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_compiler::visit(internal::query_create_table_constraints_part& part) {
|
void query_compiler::visit(internal::query_create_table_constraints_part& part) {
|
||||||
|
|
@ -372,31 +421,22 @@ void query_compiler::visit(internal::query_drop_table_part &part) {
|
||||||
query_.sql += " " + build_table_name(part.token(), *dialect_, query_.table_name);
|
query_.sql += " " + build_table_name(part.token(), *dialect_, query_.table_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void build_create_column(std::string &out, const table_column &col, const sql::dialect &d) {
|
std::string build_create_column(const table_column &col, const sql::dialect &d) {
|
||||||
prepare_identifier_string_append(out, col.canonical_name(), d);
|
std::string result = d.prepare_identifier_string(col.name()) + " " + d.data_type_at(col.type());
|
||||||
out += " " + d.data_type_at(col.type());
|
|
||||||
if (col.attributes().size() > 0) {
|
if (col.attributes().size() > 0) {
|
||||||
out.append("(" + std::to_string(col.attributes().size()) + ")");
|
result.append("(" + std::to_string(col.attributes().size()) + ")");
|
||||||
}
|
}
|
||||||
if (!col.is_nullable()) {
|
if (!col.is_nullable()) {
|
||||||
out.append(" ").append(d.not_null());
|
result.append(" ").append(d.not_null());
|
||||||
}
|
}
|
||||||
if (is_constraint_set(col.attributes().options(), utils::constraints::Unique)) {
|
if (is_constraint_set(col.attributes().options(), utils::constraints::Unique)) {
|
||||||
out.append(" ").append(d.unique());
|
result.append(" ").append(d.unique());
|
||||||
}
|
}
|
||||||
if (is_constraint_set(col.attributes().options(), utils::constraints::PrimaryKey)) {
|
if (is_constraint_set(col.attributes().options(), utils::constraints::PrimaryKey)) {
|
||||||
out.append(" ").append(d.primary_key());
|
result.append(" ").append(d.primary_key());
|
||||||
}
|
|
||||||
}
|
|
||||||
void build_columns(std::string &out, const std::vector<table_column> &cols, const sql::dialect &d) {
|
|
||||||
bool first = true;
|
|
||||||
for (const auto& col : cols) {
|
|
||||||
if (!first) {
|
|
||||||
out.append(", ");
|
|
||||||
}
|
|
||||||
prepare_identifier_string_append(out, col.name(), d);
|
|
||||||
first = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string build_constraint(const table_constraint& cons, const sql::dialect& d) {
|
std::string build_constraint(const table_constraint& cons, const sql::dialect& d) {
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,31 @@
|
||||||
#include "matador/query/query_utils.hpp"
|
#include "matador/query/query_utils.hpp"
|
||||||
|
|
||||||
#include "matador/query/table.hpp"
|
#include "matador/query/table.hpp"
|
||||||
#include "matador/query/internal/string_builder_utils.hpp"
|
|
||||||
|
|
||||||
namespace matador::query {
|
namespace matador::query {
|
||||||
std::string prepare_identifier(const sql::dialect& d, const table_column& col) {
|
std::string prepare_identifier(const sql::dialect& d, const table_column& col) {
|
||||||
std::string result;
|
std::string result;
|
||||||
if (!col.is_function()) {
|
if (!col.is_function()) {
|
||||||
prepare_identifier_string_append(result, col.name(), d);
|
if (col.table()) {
|
||||||
|
result = d.prepare_identifier_string(col.table()->name()) + ".";
|
||||||
|
}
|
||||||
|
result += d.prepare_identifier_string(col.name());
|
||||||
} else {
|
} else {
|
||||||
result = d.sql_function_at(col.function()) + "(" + col.name() + ")";
|
result = d.sql_function_at(col.function()) + "(" + col.name() + ")";
|
||||||
}
|
}
|
||||||
|
if (!col.alias().empty()) {
|
||||||
|
result += " AS " + col.alias();
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string prepare_criteria(const sql::dialect& d, const table_column& col) {
|
std::string prepare_criteria(const sql::dialect& d, const table_column& col) {
|
||||||
std::string result;
|
std::string result;
|
||||||
if (!col.is_function()) {
|
if (!col.is_function()) {
|
||||||
prepare_identifier_string_append(result, col.name(), d);
|
if (col.table()) {
|
||||||
|
result = d.prepare_identifier_string(col.table()->name()) + ".";
|
||||||
|
}
|
||||||
|
result += d.prepare_identifier_string(col.name());
|
||||||
} else {
|
} else {
|
||||||
result = d.sql_function_at(col.function()) + "(" + col.name() + ")";
|
result = d.sql_function_at(col.function()) + "(" + col.name() + ")";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ table::table(const std::string& name, const std::vector<table_column> &columns)
|
||||||
: table(name, name, columns) {
|
: table(name, name, columns) {
|
||||||
}
|
}
|
||||||
|
|
||||||
table::table(std::string name, std::string alias, const std::vector<table_column> &columns)
|
table::table(std::string name, std::string alias, const std::vector<table_column> &columns)
|
||||||
: name_(std::move(name))
|
: name_(std::move(name))
|
||||||
, alias_(std::move(alias))
|
, alias_(std::move(alias))
|
||||||
, columns_(columns) {
|
, columns_(columns) {
|
||||||
|
|
@ -91,7 +91,7 @@ const table_column* table::operator[](const std::string &column_name) const {
|
||||||
|
|
||||||
const table_column * table::column_by_name(const table &tab, const std::string &column_name) {
|
const table_column * table::column_by_name(const table &tab, const std::string &column_name) {
|
||||||
for (const auto &col : tab.columns_) {
|
for (const auto &col : tab.columns_) {
|
||||||
if (col.column_name() == column_name) {
|
if (col.name() == column_name) {
|
||||||
return &col;
|
return &col;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,39 +25,42 @@ table_column::table_column(const char *name)
|
||||||
: table_column(std::string(name))
|
: table_column(std::string(name))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
table_column::table_column(const std::string& name)
|
table_column::table_column(std::string name)
|
||||||
: table_column(name, name) {}
|
: name_(std::move(name)) {}
|
||||||
|
table_column::table_column(std::string name, std::string alias)
|
||||||
|
: name_(std::move(name))
|
||||||
|
, alias_(std::move(alias)) {}
|
||||||
|
|
||||||
table_column::table_column(const std::string& name, const std::string& alias)
|
table_column::table_column(const sql::sql_function_t func, std::string name)
|
||||||
: table_column(nullptr, name, alias, utils::basic_type::Unknown, {}, sql::sql_function_t::None)
|
: name_(std::move(name))
|
||||||
{}
|
, function_(func) {}
|
||||||
|
|
||||||
table_column::table_column(const sql::sql_function_t func, const std::string& name)
|
table_column::table_column(const class table* tab, std::string name)
|
||||||
: table_column(nullptr, name, name, utils::basic_type::Unknown, {}, func)
|
: table_(tab)
|
||||||
{}
|
, name_(std::move(name)) {}
|
||||||
|
|
||||||
table_column::table_column(const class table* tab, const std::string& name)
|
table_column::table_column(const class table* tab, std::string name, std::string alias)
|
||||||
: table_column(tab, name, name, utils::basic_type::Unknown, {}, sql::sql_function_t::None)
|
: table_(tab)
|
||||||
{}
|
, name_(std::move(name))
|
||||||
|
, alias_(std::move(alias)) {}
|
||||||
table_column::table_column(const class table* tab, const std::string& name, const std::string& alias)
|
table_column::table_column(const class table* tab,
|
||||||
: table_column(tab, name, alias, utils::basic_type::Unknown, {}, sql::sql_function_t::None)
|
std::string name,
|
||||||
{}
|
const utils::basic_type type,
|
||||||
|
const utils::field_attributes& attributes)
|
||||||
table_column::table_column(const class table* tab, const std::string& name, const utils::basic_type type, const utils::field_attributes& attributes)
|
: table_(tab)
|
||||||
: table_column(tab, name, name, type, attributes, sql::sql_function_t::None)
|
, name_(std::move(name))
|
||||||
{}
|
, type_(type)
|
||||||
|
, attributes_(attributes) {}
|
||||||
|
|
||||||
table_column::table_column(const class table* tab,
|
table_column::table_column(const class table* tab,
|
||||||
const std::string& name,
|
std::string name,
|
||||||
const std::string& alias,
|
std::string alias,
|
||||||
const utils::basic_type type,
|
utils::basic_type type,
|
||||||
const utils::field_attributes &attributes,
|
const utils::field_attributes &attributes,
|
||||||
const sql::sql_function_t func)
|
const sql::sql_function_t func)
|
||||||
: table_(tab)
|
: table_(tab)
|
||||||
, name_(build_column_name(table_, name))
|
, name_(std::move(name))
|
||||||
, column_name_(name)
|
, alias_(std::move(alias))
|
||||||
, alias_(alias)
|
|
||||||
, type_(type)
|
, type_(type)
|
||||||
, attributes_(attributes)
|
, attributes_(attributes)
|
||||||
, function_(func) {}
|
, function_(func) {}
|
||||||
|
|
@ -68,7 +71,6 @@ table_column & table_column::operator=(const table_column &other) {
|
||||||
}
|
}
|
||||||
table_ = other.table_;
|
table_ = other.table_;
|
||||||
name_ = other.name_;
|
name_ = other.name_;
|
||||||
column_name_ = other.column_name_;
|
|
||||||
alias_ = other.alias_;
|
alias_ = other.alias_;
|
||||||
type_ = other.type_;
|
type_ = other.type_;
|
||||||
attributes_ = other.attributes_;
|
attributes_ = other.attributes_;
|
||||||
|
|
@ -80,7 +82,6 @@ bool table_column::equals(const table_column &x) const {
|
||||||
if (table_ != nullptr && x.table_ != nullptr) {
|
if (table_ != nullptr && x.table_ != nullptr) {
|
||||||
return *table_ == *x.table_ &&
|
return *table_ == *x.table_ &&
|
||||||
name_ == x.name_ &&
|
name_ == x.name_ &&
|
||||||
column_name_ == x.column_name_ &&
|
|
||||||
alias_ == x.alias_ &&
|
alias_ == x.alias_ &&
|
||||||
function_ == x.function_;
|
function_ == x.function_;
|
||||||
}
|
}
|
||||||
|
|
@ -90,22 +91,19 @@ bool table_column::equals(const table_column &x) const {
|
||||||
function_ == x.function_;
|
function_ == x.function_;
|
||||||
}
|
}
|
||||||
|
|
||||||
table_column table_column::as(const std::string& alias) const {
|
table_column table_column::as(std::string a) {
|
||||||
return {table_, column_name_, alias, type_, attributes_, function_};
|
alias_ = std::move(a);
|
||||||
|
return {table_, name_, alias_, type_, attributes_, function_};
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& table_column::name() const {
|
const std::string& table_column::name() const {
|
||||||
return has_alias() ? alias_ : name_;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& table_column::column_name() const {
|
|
||||||
return column_name_;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& table_column::canonical_name() const {
|
|
||||||
return name_;
|
return name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string table_column::canonical_name() const {
|
||||||
|
return table_ ? table_->name() + "." + name_ : name_;
|
||||||
|
}
|
||||||
|
|
||||||
const std::string& table_column::alias() const {
|
const std::string& table_column::alias() const {
|
||||||
return alias_;
|
return alias_;
|
||||||
}
|
}
|
||||||
|
|
@ -131,7 +129,7 @@ sql::sql_function_t table_column::function() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool table_column::has_alias() const {
|
bool table_column::has_alias() const {
|
||||||
return alias_ != column_name_;
|
return !alias_.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
const class table* table_column::table() const {
|
const class table* table_column::table() const {
|
||||||
|
|
@ -140,14 +138,9 @@ const class table* table_column::table() const {
|
||||||
|
|
||||||
void table_column::table(const query::table* tab) {
|
void table_column::table(const query::table* tab) {
|
||||||
table_ = tab;
|
table_ = tab;
|
||||||
name_ = build_column_name(table_, column_name_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
table_column::operator const std::string&() const {
|
table_column::operator const std::string&() const {
|
||||||
return name();
|
return name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string table_column::build_column_name(const class query::table* tab, const std::string& name) {
|
|
||||||
return tab ? tab->name() + "." + name : name;
|
|
||||||
}
|
}
|
||||||
} // namespace matador::query
|
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,16 @@ const std::string &dialect::sql_function_at(const sql_function_t func) const {
|
||||||
return sql_func_map_.at(func);
|
return sql_func_map_.at(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
const std::string &dialect::to_string(const bool val) const {
|
const std::string &dialect::to_string(const bool val) const {
|
||||||
return bool_strings_[static_cast<int>(val)];
|
return bool_strings_[static_cast<int>(val)];
|
||||||
}
|
}
|
||||||
|
|
@ -79,7 +89,7 @@ dialect::escape_identifier_t dialect::identifier_escape_type() const {
|
||||||
return identifier_escape_type_;
|
return identifier_escape_type_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dialect::identifier_escape_type(const escape_identifier_t escape_identifier) {
|
void dialect::identifier_escape_type(escape_identifier_t escape_identifier) {
|
||||||
identifier_escape_type_ = escape_identifier;
|
identifier_escape_type_ = escape_identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -211,14 +221,6 @@ const std::string &dialect::into() const {
|
||||||
return token_at(dialect_token::Into);
|
return token_at(dialect_token::Into);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string &dialect::is_null() const {
|
|
||||||
return token_at(dialect_token::IsNull);
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string &dialect::is_not_null() const {
|
|
||||||
return token_at(dialect_token::IsNotNull);
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string &dialect::join() const {
|
const std::string &dialect::join() const {
|
||||||
return token_at(dialect_token::Join);
|
return token_at(dialect_token::Join);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,5 @@ META_TABLE(departments, DEPARTMENT, id, name)
|
||||||
META_TABLE(employees, EMPLOYEE, id, first_name, last_name, dep_id)
|
META_TABLE(employees, EMPLOYEE, id, first_name, last_name, dep_id)
|
||||||
META_TABLE(authors, AUTHOR, id, first_name, last_name, date_of_birth, year_of_birth, distinguished)
|
META_TABLE(authors, AUTHOR, id, first_name, last_name, date_of_birth, year_of_birth, distinguished)
|
||||||
META_TABLE(books, BOOK, id, title, author_id, published_in)
|
META_TABLE(books, BOOK, id, title, author_id, published_in)
|
||||||
META_TABLE(orders, ORDER, order_id, order_date, required_date, shipped_date, ship_via, freight, ship_name, ship_address, ship_city, ship_region, ship_postal_code, ship_country)
|
|
||||||
META_TABLE(courses, COURSE, id, title)
|
|
||||||
|
|
||||||
#endif //MATADOR_MODEL_METAS_HPP
|
#endif //MATADOR_MODEL_METAS_HPP
|
||||||
|
|
@ -24,14 +24,12 @@
|
||||||
#include "../../models/recipe.hpp"
|
#include "../../models/recipe.hpp"
|
||||||
#include "../../models/order.hpp"
|
#include "../../models/order.hpp"
|
||||||
#include "../../models/student.hpp"
|
#include "../../models/student.hpp"
|
||||||
#include "../../models/model_metas.hpp"
|
|
||||||
|
|
||||||
using namespace matador::object;
|
using namespace matador::object;
|
||||||
using namespace matador::query;
|
using namespace matador::query;
|
||||||
using namespace matador::utils;
|
using namespace matador::utils;
|
||||||
using namespace matador::sql;
|
using namespace matador::sql;
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
using namespace matador::query::meta;
|
|
||||||
|
|
||||||
TEST_CASE("Create sql query data for entity with eager has one", "[query][entity][builder]") {
|
TEST_CASE("Create sql query data for entity with eager has one", "[query][entity][builder]") {
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
|
|
@ -48,7 +46,7 @@ TEST_CASE("Create sql query data for entity with eager has one", "[query][entity
|
||||||
|
|
||||||
const auto it = scm.find(typeid(flight));
|
const auto it = scm.find(typeid(flight));
|
||||||
REQUIRE(it != scm.end());
|
REQUIRE(it != scm.end());
|
||||||
const auto* col = it->second.table()[FLIGHT.id];
|
const auto* col = it->second.table()["id"];
|
||||||
REQUIRE(col);
|
REQUIRE(col);
|
||||||
auto data = eqb.build<flight>(*col == _);
|
auto data = eqb.build<flight>(*col == _);
|
||||||
|
|
||||||
|
|
@ -102,7 +100,7 @@ TEST_CASE("Create sql query data for entity with eager belongs to", "[query][ent
|
||||||
|
|
||||||
const auto it = scm.find(typeid(book));
|
const auto it = scm.find(typeid(book));
|
||||||
REQUIRE(it != scm.end());
|
REQUIRE(it != scm.end());
|
||||||
const auto* col = it->second.table()[BOOK.id];
|
const auto* col = it->second.table()["id"];
|
||||||
REQUIRE(col);
|
REQUIRE(col);
|
||||||
auto data = eqb.build<book>(*col == _);
|
auto data = eqb.build<book>(*col == _);
|
||||||
|
|
||||||
|
|
@ -174,7 +172,7 @@ TEST_CASE("Create sql query data for entity with eager has many belongs to", "[q
|
||||||
|
|
||||||
const auto it = scm.find(typeid(order));
|
const auto it = scm.find(typeid(order));
|
||||||
REQUIRE(it != scm.end());
|
REQUIRE(it != scm.end());
|
||||||
const auto* col = it->second.table()[ORDER.order_id];
|
const auto* col = it->second.table()["order_id"];
|
||||||
REQUIRE(col);
|
REQUIRE(col);
|
||||||
auto data = eqb.build<order>(*col == _);
|
auto data = eqb.build<order>(*col == _);
|
||||||
|
|
||||||
|
|
@ -238,7 +236,7 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e
|
||||||
|
|
||||||
const auto it = scm.find(typeid(ingredient));
|
const auto it = scm.find(typeid(ingredient));
|
||||||
REQUIRE(it != scm.end());
|
REQUIRE(it != scm.end());
|
||||||
const auto* col = it->second.table()[INGREDIENT.id];
|
const auto* col = it->second.table()["id"];
|
||||||
REQUIRE(col);
|
REQUIRE(col);
|
||||||
auto data = eqb.build<ingredient>(*col == _);
|
auto data = eqb.build<ingredient>(*col == _);
|
||||||
|
|
||||||
|
|
@ -292,7 +290,7 @@ TEST_CASE("Create sql query data for entity with eager many to many (inverse par
|
||||||
|
|
||||||
const auto it = scm.find(typeid(course));
|
const auto it = scm.find(typeid(course));
|
||||||
REQUIRE(it != scm.end());
|
REQUIRE(it != scm.end());
|
||||||
const auto* col = it->second.table()[COURSE.id];
|
const auto* col = it->second.table()["id"];
|
||||||
REQUIRE(col);
|
REQUIRE(col);
|
||||||
auto data = eqb.build<course>(*col == _);
|
auto data = eqb.build<course>(*col == _);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ TEST_CASE("Generate columns from object", "[column][generator]") {
|
||||||
REQUIRE(columns.size() == expected_columns.size());
|
REQUIRE(columns.size() == expected_columns.size());
|
||||||
|
|
||||||
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
||||||
REQUIRE(expected_columns[i] == columns[i].column_name());
|
REQUIRE(expected_columns[i] == columns[i].name());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
#include "matador/query/criteria.hpp"
|
#include "matador/query/criteria.hpp"
|
||||||
#include "matador/query/criteria_evaluator.hpp"
|
#include "matador/query/criteria_evaluator.hpp"
|
||||||
#include "matador/query/query.hpp"
|
|
||||||
|
|
||||||
#include "matador/sql/dialect_builder.hpp"
|
#include "matador/sql/dialect_builder.hpp"
|
||||||
|
#include "matador/sql/query_context.hpp"
|
||||||
|
|
||||||
#include "matador/utils/placeholder.hpp"
|
#include "matador/utils/placeholder.hpp"
|
||||||
|
|
||||||
|
|
@ -79,9 +79,7 @@ TEST_CASE_METHOD(CriteriaFixture, "Test in query criteria", "[criteria][in query
|
||||||
query_context sub_ctx;
|
query_context sub_ctx;
|
||||||
sub_ctx.sql = R"(SELECT "name" FROM "test")";
|
sub_ctx.sql = R"(SELECT "name" FROM "test")";
|
||||||
|
|
||||||
auto q = query::select({name_col}).from("test");
|
auto clause = age_col != 7 && in(name_col, std::move(sub_ctx));
|
||||||
|
|
||||||
auto clause = age_col != 7 && in(name_col, std::move(q));
|
|
||||||
}
|
}
|
||||||
TEST_CASE_METHOD(CriteriaFixture, "Test out criteria", "[criteria][out]") {
|
TEST_CASE_METHOD(CriteriaFixture, "Test out criteria", "[criteria][out]") {
|
||||||
const auto age_col = "age"_col;
|
const auto age_col = "age"_col;
|
||||||
|
|
@ -90,12 +88,12 @@ TEST_CASE_METHOD(CriteriaFixture, "Test out criteria", "[criteria][out]") {
|
||||||
criteria_evaluator evaluator(dlc, ctx);
|
criteria_evaluator evaluator(dlc, ctx);
|
||||||
auto str = evaluator.evaluate(*clause);
|
auto str = evaluator.evaluate(*clause);
|
||||||
|
|
||||||
REQUIRE(str == R"(("age" <> 7 AND "age" NOT IN (7, 5, 5, 8)))");
|
REQUIRE(str == "(\"age\" <> 7 AND \"age\" NOT IN (7, 5, 5, 8))");
|
||||||
|
|
||||||
clause = age_col != 7 && out(age_col, {_, _, _});
|
clause = age_col != 7 && out(age_col, {_, _, _});
|
||||||
str = evaluator.evaluate(*clause);
|
str = evaluator.evaluate(*clause);
|
||||||
|
|
||||||
REQUIRE(str == R"(("age" <> 7 AND "age" NOT IN (?, ?, ?)))");
|
REQUIRE(str == "(\"age\" <> 7 AND \"age\" NOT IN (?, ?, ?))");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE_METHOD(CriteriaFixture, "Test between criteria", "[criteria][between]") {
|
TEST_CASE_METHOD(CriteriaFixture, "Test between criteria", "[criteria][between]") {
|
||||||
|
|
@ -105,12 +103,12 @@ TEST_CASE_METHOD(CriteriaFixture, "Test between criteria", "[criteria][between]"
|
||||||
criteria_evaluator evaluator(dlc, ctx);
|
criteria_evaluator evaluator(dlc, ctx);
|
||||||
auto str = evaluator.evaluate(*clause);
|
auto str = evaluator.evaluate(*clause);
|
||||||
|
|
||||||
REQUIRE(str == R"(("age" <> 7 OR "age" BETWEEN 21 AND 30))");
|
REQUIRE(str == "(\"age\" <> 7 OR \"age\" BETWEEN 21 AND 30)");
|
||||||
|
|
||||||
clause = age_col != 7 || between(age_col, _, _);
|
clause = age_col != 7 || between(age_col, _, _);
|
||||||
str = evaluator.evaluate(*clause);
|
str = evaluator.evaluate(*clause);
|
||||||
|
|
||||||
REQUIRE(str == R"(("age" <> 7 OR "age" BETWEEN ? AND ?))");
|
REQUIRE(str == "(\"age\" <> 7 OR \"age\" BETWEEN ? AND ?)");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE_METHOD(CriteriaFixture, "Test like criteria", "[criteria][like]") {
|
TEST_CASE_METHOD(CriteriaFixture, "Test like criteria", "[criteria][like]") {
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
using namespace matador::query;
|
using namespace matador::query;
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
|
|
||||||
TEST_CASE("Test schema with belongs-to to has-many relation" "[schema][relation][belongsto]") {
|
TEST_CASE("Test schema with belongs-to to has-many relation" "[schema][relation][belongs_to]") {
|
||||||
schema repo;
|
schema repo;
|
||||||
auto result = repo.attach<package>("packages")
|
auto result = repo.attach<package>("packages")
|
||||||
.and_then( [&repo] { return repo.attach<shipment>("shipments"); } );
|
.and_then( [&repo] { return repo.attach<shipment>("shipments"); } );
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue