insert has one progress
This commit is contained in:
parent
1cdd83cb31
commit
22f6f71412
|
|
@ -33,7 +33,7 @@ public:
|
|||
|
||||
utils::result<sql::execute_result, utils::error> execute(const sql::query_context &context) override;
|
||||
utils::result<std::unique_ptr<sql::statement_impl>, utils::error> prepare(const sql::query_context &context) override;
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::query_context &context) override;
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::query_context &ctx) override;
|
||||
|
||||
utils::result<std::vector<object::attribute>, utils::error> describe(const std::string& table) override;
|
||||
utils::result<bool, utils::error> exists(const std::string &schema_name, const std::string &table_name) override;
|
||||
|
|
@ -43,16 +43,16 @@ public:
|
|||
[[nodiscard]] std::string to_escaped_string( const utils::blob_type_t& value ) const override;
|
||||
|
||||
private:
|
||||
[[nodiscard]] static std::string generate_statement_name(const sql::query_context &query) ;
|
||||
[[nodiscard]] static std::string generate_statement_name(const sql::query_context &ctx) ;
|
||||
|
||||
utils::result<sql::execute_result, utils::error> execute(const std::string &stmt) const;
|
||||
[[nodiscard]] utils::result<sql::execute_result, utils::error> execute(const std::string &stmt) const;
|
||||
|
||||
private:
|
||||
PGconn *conn_{nullptr};
|
||||
|
||||
using string_to_int_map = std::unordered_map<std::string, unsigned long>;
|
||||
using hash_to_string_map = std::unordered_map<size_t, std::string>;
|
||||
|
||||
static string_to_int_map statement_name_map_;
|
||||
static hash_to_string_map statement_name_map_;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,15 @@
|
|||
|
||||
#include "matador/sql/error_code.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
|
||||
#include "matador/sql/internal/query_result_impl.hpp"
|
||||
|
||||
#include "matador/utils/string.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
postgres_connection::string_to_int_map postgres_connection::statement_name_map_{};
|
||||
postgres_connection::hash_to_string_map postgres_connection::statement_name_map_{};
|
||||
|
||||
postgres_connection::postgres_connection(const sql::connection_info &info)
|
||||
: connection_impl(info) {
|
||||
|
|
@ -82,20 +83,20 @@ utils::result<utils::version, utils::error> postgres_connection::server_version(
|
|||
|
||||
utils::basic_type oid2type(Oid oid);
|
||||
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_connection::fetch(const sql::query_context &context) {
|
||||
PGresult *res = PQexec(conn_, context.sql.c_str());
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_connection::fetch(const sql::query_context &ctx) {
|
||||
PGresult *res = PQexec(conn_, ctx.sql.c_str());
|
||||
|
||||
if (is_result_error(res)) {
|
||||
const auto err = make_error(sql::error_code::FetchFailed, res, conn_, "Failed to fetch", context.sql);
|
||||
const auto err = make_error(sql::error_code::FetchFailed, res, conn_, "Failed to fetch", ctx.sql);
|
||||
PQclear(res);
|
||||
return utils::failure(err);
|
||||
}
|
||||
|
||||
std::vector<object::attribute> prototype = context.prototype;
|
||||
std::vector<object::attribute> prototype = ctx.prototype;
|
||||
|
||||
const int num_col = PQnfields(res);
|
||||
if (prototype.size() != static_cast<size_t>(num_col)) {
|
||||
const auto err = make_error(sql::error_code::FetchFailed, res, conn_, "Number of received columns doesn't match expected columns.", context.sql);
|
||||
const auto err = make_error(sql::error_code::FetchFailed, res, conn_, "Number of received columns doesn't match expected columns.", ctx.sql);
|
||||
PQclear(res);
|
||||
return utils::failure(err);
|
||||
}
|
||||
|
|
@ -111,22 +112,18 @@ utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_co
|
|||
|
||||
return utils::ok(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res),
|
||||
std::move(prototype),
|
||||
context.resolver,
|
||||
context.result_type));
|
||||
ctx.resolver,
|
||||
ctx.result_type));
|
||||
}
|
||||
|
||||
std::string postgres_connection::generate_statement_name(const sql::query_context &query) {
|
||||
std::stringstream name;
|
||||
name << query.table_name << "_" << query.command_name;
|
||||
auto result = statement_name_map_.find(name.str());
|
||||
std::string postgres_connection::generate_statement_name(const sql::query_context &ctx) {
|
||||
auto it = statement_name_map_.find(ctx.sql_hash);
|
||||
|
||||
if (result == statement_name_map_.end()) {
|
||||
result = statement_name_map_.insert(std::make_pair(name.str(), 0)).first;
|
||||
if (it == statement_name_map_.end()) {
|
||||
it = statement_name_map_.insert(std::make_pair(ctx.sql_hash, utils::to_hex_string(ctx.sql_hash))).first;
|
||||
}
|
||||
|
||||
name << "_" << ++result->second;
|
||||
|
||||
return name.str();
|
||||
return it->second;
|
||||
}
|
||||
|
||||
utils::result<sql::execute_result, utils::error> postgres_connection::execute(const std::string& stmt) const {
|
||||
|
|
|
|||
|
|
@ -19,6 +19,28 @@ class executor;
|
|||
}
|
||||
|
||||
namespace matador::query {
|
||||
template<typename Type>
|
||||
class query_object_resolver_producer : public sql::object_resolver_producer {
|
||||
public:
|
||||
query_object_resolver_producer() = default;
|
||||
query_object_resolver_producer(basic_schema& repo, const table& tab, std::string pk_name)
|
||||
: object_resolver_producer(typeid(Type))
|
||||
, repo_(repo)
|
||||
, table_(tab)
|
||||
, pk_name_(std::move(pk_name)) {}
|
||||
|
||||
std::shared_ptr<object::abstract_type_resolver> produce(sql::statement&& stmt) override {
|
||||
return std::make_shared<query_object_resolver<Type>>(std::move(stmt));
|
||||
}
|
||||
|
||||
utils::result<sql::query_context, utils::error> build_query(const sql::dialect& d) override;
|
||||
|
||||
private:
|
||||
basic_schema& repo_;
|
||||
const table& table_;
|
||||
std::string pk_name_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
class query_collection_resolver_producer : public sql::collection_resolver_producer {
|
||||
public:
|
||||
|
|
@ -77,9 +99,7 @@ public:
|
|||
return utils::ok(stmt);
|
||||
}
|
||||
|
||||
std::shared_ptr<object::abstract_collection_resolver> produce(sql::statement&& stmt, const sql::resolver_service& rs) override {
|
||||
// const auto object_resolver = rs.object_resolver<typename Type::value_type>();
|
||||
|
||||
std::shared_ptr<object::abstract_collection_resolver> produce(sql::statement&& stmt, const sql::resolver_service& /*rs*/) override {
|
||||
return std::make_shared<query_collection_primitive_resolver<Type>>(std::move(stmt), root_type(), collection_name()/*, object_resolver*/);
|
||||
}
|
||||
|
||||
|
|
@ -106,7 +126,18 @@ public:
|
|||
template<class Pointer>
|
||||
static void on_belongs_to(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class Pointer>
|
||||
static void on_has_one(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
void on_has_one(const char * /*id*/, Pointer & /*x*/, const utils::foreign_attributes &/*attr*/) {
|
||||
const auto it = schema_.find(typeid(typename Pointer::value_type));
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
}
|
||||
if (!it->second.node().info().has_primary_key()) {
|
||||
throw query_builder_exception{error_code::MissingPrimaryKey, "Missing primary key"};
|
||||
}
|
||||
|
||||
auto producer = std::make_unique<query_object_resolver_producer<typename Pointer::value_type>>(schema_, it->second.table(), it->second.node().info().primary_key_attribute()->name());
|
||||
schema_.resolver_producers_[typeid(typename Pointer::value_type)] = std::move(producer);
|
||||
}
|
||||
|
||||
template<class CollectionType>
|
||||
void on_has_many(const char * /*id*/, CollectionType &/*cont*/, const char *join_column, const utils::foreign_attributes &/*attr*/, std::enable_if_t<object::is_object_ptr<typename CollectionType::value_type>::value> * = nullptr) {
|
||||
|
|
@ -187,42 +218,6 @@ private:
|
|||
const std::type_index root_type_;
|
||||
};
|
||||
|
||||
|
||||
template<typename Type>
|
||||
class query_object_resolver_producer : public sql::object_resolver_producer {
|
||||
public:
|
||||
query_object_resolver_producer() = default;
|
||||
query_object_resolver_producer(basic_schema& repo, const table& tab, std::string pk_name)
|
||||
: object_resolver_producer(typeid(Type))
|
||||
, repo_(repo)
|
||||
, table_(tab)
|
||||
, pk_name_(std::move(pk_name)) {}
|
||||
|
||||
std::shared_ptr<object::abstract_type_resolver> produce(sql::statement&& stmt) override {
|
||||
return std::make_shared<query_object_resolver<Type>>(std::move(stmt));
|
||||
}
|
||||
|
||||
utils::result<sql::query_context, utils::error> build_query(const sql::dialect& d) override {
|
||||
producer_creator pc(repo_, typeid(Type));
|
||||
Type obj;
|
||||
access::process(pc, obj);
|
||||
|
||||
select_query_builder qb(repo_);
|
||||
const auto *pk_column = table_[pk_name_];
|
||||
const auto result = qb.build<Type>(*pk_column == utils::_);
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok(result->compile(d));
|
||||
}
|
||||
|
||||
private:
|
||||
basic_schema& repo_;
|
||||
const table& table_;
|
||||
std::string pk_name_;
|
||||
};
|
||||
|
||||
class schema;
|
||||
|
||||
using schema_ref = std::reference_wrapper<schema>;
|
||||
|
|
@ -311,6 +306,23 @@ utils::result<void, utils::error> schema::drop_table(const sql::connection &conn
|
|||
return utils::failure(info.err());
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
utils::result<sql::query_context, utils::error> query_object_resolver_producer<Type>::
|
||||
build_query(const sql::dialect &d) {
|
||||
producer_creator pc(repo_, typeid(Type));
|
||||
Type obj;
|
||||
access::process(pc, obj);
|
||||
|
||||
select_query_builder qb(repo_);
|
||||
const auto *pk_column = table_[pk_name_];
|
||||
const auto result = qb.build<Type>(*pk_column == utils::_);
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok(result->compile(d));
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
void schema_observer<Type>::on_attach(const object::repository_node &node, const Type &/*prototype*/) const {
|
||||
primary_key_generator_finder finder;
|
||||
|
|
|
|||
|
|
@ -113,12 +113,12 @@ public:
|
|||
|
||||
template<class Pointer>
|
||||
void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
||||
on_foreign_object(id, obj, attr);
|
||||
on_foreign_object(id, obj, attr, true);
|
||||
}
|
||||
|
||||
template<class Pointer>
|
||||
void on_has_one(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
||||
on_foreign_object(id, obj, attr);
|
||||
on_foreign_object(id, obj, attr, false);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -258,7 +258,7 @@ public:
|
|||
|
||||
private:
|
||||
template<class Pointer>
|
||||
void on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr);
|
||||
void on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr, bool add_column_on_lazy);
|
||||
void push(const std::string &column_name);
|
||||
static std::string build_alias(char prefix, unsigned int count);
|
||||
[[nodiscard]] bool is_root_entity() const;
|
||||
|
|
@ -280,7 +280,7 @@ private:
|
|||
};
|
||||
|
||||
template<class Pointer>
|
||||
void select_query_builder::on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr) {
|
||||
void select_query_builder::on_foreign_object(const char *id, Pointer &, const utils::foreign_attributes &attr, const bool add_column_on_lazy) {
|
||||
const auto it = schema_.find(typeid(typename Pointer::value_type));
|
||||
if (it == schema_.end()) {
|
||||
throw query_builder_exception{error_code::UnknownType, "Unknown type"};
|
||||
|
|
@ -307,7 +307,7 @@ void select_query_builder::on_foreign_object(const char *id, Pointer &, const ut
|
|||
table_column{&table_info_stack_.top().table, id},
|
||||
table_column{&next->second, info.primary_key_attribute()->name()}
|
||||
);
|
||||
} else {
|
||||
} else if (add_column_on_lazy) {
|
||||
push(id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,9 +38,9 @@ public:
|
|||
pk_binder_.bind(*x, index_++, *binder_);
|
||||
}
|
||||
template<class Type, template < class ... > class Pointer>
|
||||
void on_has_one(const char * /*id*/, Pointer<Type> &x, const utils::foreign_attributes &/*attr*/) {
|
||||
pk_binder_.bind(*x, index_++, *binder_);
|
||||
}
|
||||
static void on_has_one(const char * /*id*/,
|
||||
Pointer<Type> &/*x*/,
|
||||
const utils::foreign_attributes &/*attr*/) {}
|
||||
template<class ContainerType>
|
||||
static void on_has_many(const char * /*id*/,
|
||||
ContainerType &/*c*/,
|
||||
|
|
|
|||
|
|
@ -5,8 +5,6 @@
|
|||
|
||||
#include "matador/sql/resolver_service.hpp"
|
||||
|
||||
#include "matador/utils/types.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
enum class sql_command {
|
||||
Unknown,
|
||||
|
|
@ -33,12 +31,10 @@ struct query_context {
|
|||
std::string sql;
|
||||
size_t sql_hash{};
|
||||
sql_command command{};
|
||||
std::string command_name{};
|
||||
std::string schema_name{};
|
||||
std::string table_name{};
|
||||
std::vector<object::attribute> prototype{};
|
||||
std::vector<std::string> bind_vars{};
|
||||
// std::vector<utils::database_type> bind_types{};
|
||||
// Data for resolving query result
|
||||
std::shared_ptr<resolver_service> resolver{};
|
||||
std::type_index result_type = typeid(void);
|
||||
|
|
|
|||
|
|
@ -22,6 +22,16 @@ MATADOR_UTILS_API std::string to_string(const blob_type_t &data);
|
|||
MATADOR_UTILS_API std::string to_string(const date_type_t &data);
|
||||
MATADOR_UTILS_API std::string to_string(const time_type_t &data);
|
||||
|
||||
template <typename IntegerType>
|
||||
std::string to_hex_string(IntegerType data, const size_t width = sizeof(IntegerType)<<1) {
|
||||
static auto digits = "0123456789ABCDEF";
|
||||
std::string result(width,'0');
|
||||
for (size_t i=0, j=(width-1)*4 ; i<width; ++i,j-=4) {
|
||||
result[i] = digits[(data>>j) & 0x0f];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a string by a delimiter and
|
||||
* add the string tokens to a vector. The
|
||||
|
|
|
|||
|
|
@ -12,8 +12,9 @@
|
|||
#include "matador/query/internal/string_builder_utils.hpp"
|
||||
#include "matador/query/internal/query_parts.hpp"
|
||||
|
||||
#include "matador/sql/interface/connection_impl.hpp"
|
||||
|
||||
#include "matador/sql/query_context.hpp"
|
||||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/dialect.hpp"
|
||||
|
||||
namespace matador::query {
|
||||
|
|
|
|||
|
|
@ -13,10 +13,21 @@ using namespace matador::query;
|
|||
using namespace matador::object;
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Test insert object with has_one relation", "[session][insert][has_one]") {
|
||||
session sess{conn};
|
||||
user_pk_generator u;
|
||||
u.name = "John Doe";
|
||||
sess.persist(u);
|
||||
const auto result = schema.attach<user_identity>("users")
|
||||
.and_then( [this] { return schema.attach<user_session_identity>("user_sessions"); } )
|
||||
.and_then([this] { return schema.create(db); } );
|
||||
REQUIRE(result.is_ok());
|
||||
|
||||
REQUIRE(u.id > 0);
|
||||
session ses({bus, connection::dns, 4}, schema);
|
||||
|
||||
const auto u = make_object<user_identity>("user1", "password");
|
||||
const auto us = make_object<user_session_identity>("session1", u);
|
||||
|
||||
REQUIRE(u.is_transient());
|
||||
REQUIRE(us.is_transient());
|
||||
REQUIRE(ses.insert(us).is_ok());
|
||||
REQUIRE(us.is_persistent());
|
||||
REQUIRE(u.is_persistent());
|
||||
|
||||
REQUIRE(u->session->session_token == "session1");
|
||||
}
|
||||
|
|
@ -17,5 +17,7 @@ META_TABLE(authors, AUTHOR, id, first_name, last_name, date_of_birth, year_of_bi
|
|||
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)
|
||||
META_TABLE(users, USER, id, name, password, session_id)
|
||||
META_TABLE(user_sessions, USER_SESSION, id, session_token, user_id)
|
||||
|
||||
#endif //MATADOR_MODEL_METAS_HPP
|
||||
|
|
@ -15,14 +15,22 @@ template<const utils::primary_key_attribute &PkAttribute>
|
|||
struct user_pk_generator {
|
||||
unsigned int id{};
|
||||
std::string name;
|
||||
std::string password;
|
||||
object::object_ptr<user_session_pk_generator<PkAttribute>> session;
|
||||
|
||||
user_pk_generator() = default;
|
||||
user_pk_generator(std::string name, std::string password)
|
||||
: name(std::move(name)), password(std::move(password)) {}
|
||||
user_pk_generator(const unsigned int id, std::string name, std::string password)
|
||||
: id(id), name(std::move(name)), password(std::move(password)) {}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::primary_key(op, "id", id, PkAttribute);
|
||||
field::attribute(op, "name", name, UniqueVarChar255);
|
||||
field::attribute(op, "password", password, UniqueVarChar255);
|
||||
field::has_one(op, "session", session, CascadeAllFetchLazy);
|
||||
}
|
||||
};
|
||||
|
|
@ -33,11 +41,17 @@ struct user_session_pk_generator {
|
|||
std::string session_token;
|
||||
object::object_ptr<user_pk_generator<PkAttribute>> user_;
|
||||
|
||||
user_session_pk_generator() = default;
|
||||
user_session_pk_generator(std::string session_token, object::object_ptr<user_pk_generator<PkAttribute>> user)
|
||||
: session_token(std::move(session_token)), user_(std::move(user)) {}
|
||||
user_session_pk_generator(const unsigned int id, std::string session_token, object::object_ptr<user_pk_generator<PkAttribute>> user)
|
||||
: id(id), session_token(std::move(session_token)), user_(std::move(user)) {}
|
||||
|
||||
template<class Operator>
|
||||
void process(Operator &op) {
|
||||
namespace field = matador::access;
|
||||
using namespace matador::utils;
|
||||
field::primary_key(op, "id", id);
|
||||
field::primary_key(op, "id", id, PkAttribute);
|
||||
field::attribute(op, "session_token", session_token, VarChar255);
|
||||
field::belongs_to(op, "user_id", user_, CascadeAllFetchLazy);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@
|
|||
#include "../../models/recipe.hpp"
|
||||
#include "../../models/order.hpp"
|
||||
#include "../../models/student.hpp"
|
||||
#include "../../models/user.hpp"
|
||||
#include "../../models/model_metas.hpp"
|
||||
|
||||
using namespace matador::object;
|
||||
|
|
@ -384,4 +385,22 @@ TEST_CASE("Test eager relationship", "[query][entity][builder]") {
|
|||
// .str(db);
|
||||
//
|
||||
// std::cout << ctx << std::endl;
|
||||
}
|
||||
|
||||
TEST_CASE("Test has one relationship", "[query][entity][builder][has_one]") {
|
||||
using namespace matador::test;
|
||||
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
||||
connection db("noop://noop.db");
|
||||
|
||||
schema scm;
|
||||
auto result = scm.attach<user>("users")
|
||||
.and_then( [&scm] { return scm.attach<user_session>("user_sessions"); } );
|
||||
REQUIRE(result);
|
||||
|
||||
select_query_builder eqb(scm);
|
||||
|
||||
auto q = eqb.build<user>();
|
||||
REQUIRE(q.is_ok());
|
||||
const auto sql = q->str(db);
|
||||
|
||||
}
|
||||
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
#include "utils/RecordingObserver.hpp"
|
||||
|
||||
#include <thread>
|
||||
|
||||
using namespace matador::test;
|
||||
using namespace matador::sql;
|
||||
using namespace matador::utils;
|
||||
|
|
|
|||
Loading…
Reference in New Issue