collection_resolver progress

This commit is contained in:
Sascha Kühl 2026-02-02 13:40:53 +01:00
parent f9333158e2
commit 621c3703fd
7 changed files with 38 additions and 54 deletions

View File

@ -188,10 +188,6 @@ int main() {
.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 " );
//
@ -316,6 +312,13 @@ int main() {
.where(JOB.id > _ && JOB.name == _)
.str(conn->dialect());
std::cout << stmt1 << std::endl;
std::cout << stmt2 << std::endl;
std::cout << stmt3 << std::endl;
std::cout << stmt4 << std::endl;
std::cout << stmt5 << std::endl;
return 0;
}

View File

@ -6,9 +6,6 @@
#include "matador/object/object_resolver.hpp"
#include "matador/query/table.hpp"
#include "matador/query/select_query_builder.hpp"
namespace matador::sql {
class executor;
}
@ -17,41 +14,20 @@ namespace matador::query {
template<typename Type>
class query_object_resolver : public object::object_resolver<Type> {
public:
explicit query_object_resolver(const basic_schema &repo, sql::executor& exec, const table &tab, std::string pk_name)
: executor_(exec)
, schema_(repo)
, table_(tab)
, pk_name_(std::move(pk_name)) {}
explicit query_object_resolver(sql::statement &&stmt)
: stmt_(std::move(stmt)) {}
std::shared_ptr<Type> resolve(const utils::identifier &id) override;
protected:
sql::executor& executor_;
const basic_schema &schema_;
const table &table_;
std::string pk_name_;
std::type_index index{typeid(Type)};
sql::statement stmt_;
};
utils::result<sql::statement, utils::error> prepare_statement(sql::executor& exec, entity_query_data &&data);
template<typename Type>
std::shared_ptr<Type> query_object_resolver<Type>::resolve(const utils::identifier &id) {
select_query_builder qb(schema_, executor_);
const auto *pk_column = table_[pk_name_];
auto builder_result = qb.build<Type>(*pk_column == utils::_);
if (!builder_result) {
return nullptr;
}
auto stmt = prepare_statement(executor_, builder_result.release());
if (!stmt) {
return nullptr;
}
sql::identifier_statement_binder binder(*stmt);
sql::identifier_statement_binder binder(stmt_);
binder.bind(id);
auto result = stmt->template fetch_one_raw<Type>();
auto result = stmt_.template fetch_one_raw<Type>();
if (!result) {
return nullptr;
}

View File

@ -9,6 +9,7 @@
#include "matador/query/query_collection_resolver.hpp"
#include "matador/query/query_object_resolver.hpp"
#include "matador/query/select_query_builder.hpp"
#include "matador/query/basic_schema.hpp"
namespace matador::sql {
@ -119,7 +120,28 @@ public:
Type obj;
access::process(pc, obj);
return std::make_shared<query_object_resolver<Type>>(repo_, exec, table_, std::move(pk_name_));
select_query_builder qb(repo_, exec);
const auto *pk_column = table_[pk_name_];
auto builder_result = qb.build<Type>(*pk_column == utils::_);
if (!builder_result) {
return nullptr;
}
auto data = builder_result.release();
auto stmt = query::query::select(data.columns)
.from(*data.root_table)
.join_left(data.joins)
.where(std::move(data.where_clause))
.order_by({data.root_table, data.pk_column_name})
.asc()
.prepare(exec);
if (!stmt) {
return nullptr;
}
return std::make_shared<query_object_resolver<Type>>(stmt.release());
}
private:

View File

@ -5,12 +5,12 @@
#include "matador/query/criteria.hpp"
#include "matador/query/query.hpp"
#include "matador/query/criteria/criteria_visitor.hpp"
#include "matador/sql/statement.hpp"
#include "matador/object/join_columns_collector.hpp"
#include "matador/object/repository.hpp"
#include "matador/query/criteria/criteria_visitor.hpp"
#include "matador/utils/primary_key_attribute.hpp"
#include "matador/utils/result.hpp"

View File

@ -151,7 +151,6 @@ add_library(matador-orm STATIC
query/query_builder.cpp
query/query_builder_exception.cpp
query/query_collection_resolver.cpp
query/query_object_resolver.cpp
query/query_part.cpp
query/query_utils.cpp
query/schema.cpp

View File

@ -1,16 +0,0 @@
#include "matador/query/query_object_resolver.hpp"
#include "matador/query/criteria.hpp"
#include "matador/query/query.hpp"
namespace matador::query {
utils::result<sql::statement, utils::error> prepare_statement(sql::executor &exec, entity_query_data &&data) {
return query::query::select(data.columns)
.from(*data.root_table)
.join_left(data.joins)
.where(std::move(data.where_clause))
.order_by({data.root_table, data.pk_column_name})
.asc()
.prepare(exec);
}
}

View File

@ -24,7 +24,8 @@ using namespace matador::query::meta;
class StatementTestFixture : public QueryFixture {
public:
StatementTestFixture() {
REQUIRE(repo.attach<airplane>("airplanes"));
REQUIRE(repo.attach<airplane>("airplanes")
.and_then([this] {return repo.create(db); }));
repo.initialize_executor(db);
}
@ -38,7 +39,6 @@ protected:
TEST_CASE_METHOD(StatementTestFixture, "Create prepared statement", "[statement]") {
using namespace matador::utils;
REQUIRE(repo.create(db));
SECTION("Insert with prepared statement and placeholder") {
auto stmt = query::insert()
.into(AIRPLANE, {AIRPLANE.id, AIRPLANE.brand, AIRPLANE.model})