many to many query progress (multiple joins)
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
struct table;
|
||||
|
||||
enum class sql_function_t {
|
||||
NONE,
|
||||
COUNT,
|
||||
@@ -21,6 +23,7 @@ struct column
|
||||
column(sql_function_t func, std::string name); // NOLINT(*-explicit-constructor)
|
||||
column(std::string table_name, std::string name, std::string as);
|
||||
column(std::string table_name, const char* name, std::string as);
|
||||
column(table &t, const char* name, std::string as);
|
||||
|
||||
bool equals(const column &x) const;
|
||||
|
||||
|
||||
@@ -17,20 +17,23 @@ namespace matador::sql {
|
||||
class column_generator
|
||||
{
|
||||
private:
|
||||
column_generator(std::vector<column> &column_infos, const sql::schema &ts, const std::string &table_name);
|
||||
column_generator(std::vector<column> &column_infos,
|
||||
const sql::schema &ts,
|
||||
const std::string &table_name,
|
||||
bool force_lazy);
|
||||
|
||||
public:
|
||||
~column_generator() = default;
|
||||
|
||||
template < class Type >
|
||||
static std::vector<column> generate(const sql::schema &ts)
|
||||
static std::vector<column> generate(const sql::schema &ts, bool force_lazy = false)
|
||||
{
|
||||
const auto info = ts.info<Type>();
|
||||
if (!info) {
|
||||
return {};
|
||||
}
|
||||
std::vector<column> columns;
|
||||
column_generator gen(columns, ts, info.value().name);
|
||||
column_generator gen(columns, ts, info.value().name, force_lazy);
|
||||
Type obj;
|
||||
matador::utils::access::process(gen, obj);
|
||||
return std::move(columns);
|
||||
@@ -53,7 +56,7 @@ public:
|
||||
template<class Pointer>
|
||||
void on_belongs_to(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
||||
{
|
||||
if (attr.fetch() == utils::fetch_type::LAZY) {
|
||||
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
|
||||
push(id);
|
||||
} else {
|
||||
const auto info = table_schema_.info<typename Pointer::value_type>();
|
||||
@@ -69,7 +72,7 @@ public:
|
||||
template<class Pointer>
|
||||
void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
||||
{
|
||||
if (attr.fetch() == utils::fetch_type::LAZY) {
|
||||
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
|
||||
push(id);
|
||||
} else {
|
||||
const auto info = table_schema_.info<typename Pointer::value_type>();
|
||||
@@ -85,7 +88,7 @@ public:
|
||||
template<class ContainerType>
|
||||
void on_has_many(const char *, ContainerType &, const char *, const char *, const utils::foreign_attributes &attr)
|
||||
{
|
||||
if (attr.fetch() == utils::fetch_type::LAZY) {
|
||||
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
|
||||
return;
|
||||
}
|
||||
const auto info = table_schema_.info<typename ContainerType::value_type::value_type>();
|
||||
@@ -112,6 +115,7 @@ private:
|
||||
std::vector<column> &column_infos_;
|
||||
const sql::schema &table_schema_;
|
||||
int column_index{0};
|
||||
bool force_lazy_{false};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public:
|
||||
columns_.clear();
|
||||
table_name_ = info.value().name;
|
||||
query q(db, schema_);
|
||||
return q.select<EntityType>().from({table_name_}).build();
|
||||
return q.select(column_generator::generate<EntityType>(schema_)).from({table_name_}).build();
|
||||
// auto from_intermediate = q.select<EntityType>().from({"t"});
|
||||
// return {};
|
||||
}
|
||||
|
||||
@@ -16,12 +16,9 @@ public:
|
||||
|
||||
query_create_intermediate create();
|
||||
query_drop_intermediate drop();
|
||||
template < class Type >
|
||||
query_select_intermediate select();
|
||||
template < class Type >
|
||||
query_select_intermediate select(std::initializer_list<column> columns);
|
||||
query_select_intermediate select(std::initializer_list<column> columns);
|
||||
query_select_intermediate select(const std::vector<column>& columns);
|
||||
query_select_intermediate select(std::vector<column> columns, std::initializer_list<column> additional_columns);
|
||||
query_insert_intermediate insert();
|
||||
query_update_intermediate update(const sql::table &table);
|
||||
query_delete_intermediate remove();
|
||||
@@ -31,19 +28,5 @@ private:
|
||||
const sql::schema &schema_;
|
||||
};
|
||||
|
||||
template<class Type>
|
||||
query_select_intermediate query::select()
|
||||
{
|
||||
return select(column_generator::generate<Type>(schema_));
|
||||
}
|
||||
|
||||
template<class Type>
|
||||
query_select_intermediate query::select(std::initializer_list<column> columns)
|
||||
{
|
||||
auto cols = column_generator::generate<Type>(schema_);
|
||||
cols.insert(cols.end(), columns);
|
||||
return select(cols);
|
||||
}
|
||||
|
||||
}
|
||||
#endif //QUERY_QUERY_HPP
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include <string>
|
||||
#include <ostream>
|
||||
|
||||
#define FIELD(x) const sql::column x{this->name, #x, ""};
|
||||
#define FIELD(x) const sql::column x{*this, #x, ""};
|
||||
|
||||
#define QUERY_HELPER(C, ...) \
|
||||
namespace matador::qh { \
|
||||
|
||||
@@ -257,9 +257,6 @@ public:
|
||||
template<class Type>
|
||||
query_execute_finish table(const sql::table &table)
|
||||
{
|
||||
// if (!schema_.exists<Type>()) {
|
||||
// schema_.attach<Type>(table.name);
|
||||
// }
|
||||
return this->table(table, column_definition_generator::generate<Type>(schema_));
|
||||
}
|
||||
};
|
||||
@@ -279,11 +276,7 @@ public:
|
||||
|
||||
query_into_intermediate into(const sql::table &table, std::initializer_list<column> column_names);
|
||||
query_into_intermediate into(const sql::table &table, std::vector<column> &&column_names);
|
||||
template<class Type>
|
||||
query_into_intermediate into(const sql::table &table)
|
||||
{
|
||||
return into(table, column_generator::generate<Type>(schema_));
|
||||
}
|
||||
query_into_intermediate into(const sql::table &table);
|
||||
};
|
||||
|
||||
class query_execute_where_intermediate : public query_execute_finish
|
||||
|
||||
@@ -101,7 +101,11 @@ entity<Type> session::insert(Type *obj)
|
||||
if (!info) {
|
||||
return {};
|
||||
}
|
||||
c->query(*schema_).insert().into<Type>(info->name).values(*obj).execute();
|
||||
c->query(*schema_)
|
||||
.insert()
|
||||
.into(info->name, column_generator::generate<Type>(*schema_))
|
||||
.values(*obj)
|
||||
.execute();
|
||||
|
||||
return entity{obj};
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
#ifndef QUERY_TABLE_HPP
|
||||
#define QUERY_TABLE_HPP
|
||||
|
||||
#include "matador/sql/column.hpp"
|
||||
|
||||
#include <typeindex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
struct table
|
||||
{
|
||||
std::string name;
|
||||
std::string alias;
|
||||
|
||||
table(const char *name, std::string as = "") // NOLINT(*-explicit-constructor)
|
||||
: name(name), alias(std::move(as)) {}
|
||||
table(std::string name, std::string as = "") // NOLINT(*-explicit-constructor)
|
||||
@@ -21,6 +21,10 @@ struct table
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string name;
|
||||
std::string alias;
|
||||
|
||||
std::vector<column> columns;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user