session queries as statement progress
This commit is contained in:
parent
5490e67ecf
commit
4fd9a74ae8
|
|
@ -101,12 +101,12 @@ void foreign_node_completer::on_has_many( const char* /*id*/, CollectionType&, c
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class CollectionType>
|
template<class CollectionType>
|
||||||
void foreign_node_completer::on_has_many_to_many( const char* id, CollectionType& /*collection*/, const char* /*join_column*/, const char* /*inverse_join_column*/, const utils::foreign_attributes& /*attr*/ ) {
|
void foreign_node_completer::on_has_many_to_many( const char* /*id*/, CollectionType& /*collection*/, const char* /*join_column*/, const char* /*inverse_join_column*/, const utils::foreign_attributes& /*attr*/ ) {
|
||||||
attach_node<typename CollectionType::value_type::value_type>(/*id*/);
|
attach_node<typename CollectionType::value_type::value_type>(/*id*/);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class CollectionType>
|
template<class CollectionType>
|
||||||
void foreign_node_completer::on_has_many_to_many( const char* id, CollectionType& /*collection*/, const utils::foreign_attributes& /*attr*/ ) {
|
void foreign_node_completer::on_has_many_to_many( const char* /*id*/, CollectionType& /*collection*/, const utils::foreign_attributes& /*attr*/ ) {
|
||||||
attach_node<typename CollectionType::value_type::value_type>(/*id*/);
|
attach_node<typename CollectionType::value_type::value_type>(/*id*/);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -121,15 +121,17 @@ public:
|
||||||
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto obj = build_select_query(data.release()).template fetch_one<Type>(*this);
|
auto res = build_select_query(data.release()).prepare(*this);
|
||||||
|
// auto obj = build_select_query(data.release()).template fetch_one<Type>(*this);
|
||||||
|
|
||||||
if (!obj) {
|
if (!res) {
|
||||||
return utils::failure(obj.err());
|
return utils::failure(res.err());
|
||||||
}
|
}
|
||||||
if (!obj->get()) {
|
auto stmt_result = res->bind(0, const_cast<PrimaryKeyType&>(pk)).template fetch_one<Type>();
|
||||||
|
if (!stmt_result) {
|
||||||
return utils::failure(make_error(error_code::FailedToFindObject, "Failed to find object of type " + info->get().name() + " with primary key " + std::to_string(pk) + "."));
|
return utils::failure(make_error(error_code::FailedToFindObject, "Failed to find object of type " + info->get().name() + " with primary key " + std::to_string(pk) + "."));
|
||||||
}
|
}
|
||||||
return utils::ok(object::object_ptr<Type>{ obj->release() });
|
return utils::ok(object::object_ptr<Type>{ stmt_result->release() });
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
|
|
|
||||||
|
|
@ -88,9 +88,9 @@ public:
|
||||||
if (pk_.is_null()) {
|
if (pk_.is_null()) {
|
||||||
entity_query_data_.pk_column_name = id;
|
entity_query_data_.pk_column_name = id;
|
||||||
} else if (pk_.is_integer()) {
|
} else if (pk_.is_integer()) {
|
||||||
auto v = *pk_.as<V>();
|
// auto v = *pk_.as<V>();
|
||||||
auto c = sql::column{table_info_stack_.top().table, id, ""};
|
auto c = sql::column{table_info_stack_.top().table, id, ""};
|
||||||
auto co = std::make_unique<query::condition<sql::column, V>>(c, query::basic_condition::operand_type::EQUAL, v);
|
auto co = std::make_unique<query::condition<sql::column, utils::placeholder>>(c, query::basic_condition::operand_type::EQUAL, utils::_);
|
||||||
entity_query_data_.where_clause = std::move(co);
|
entity_query_data_.where_clause = std::move(co);
|
||||||
entity_query_data_.pk_column_name = id;
|
entity_query_data_.pk_column_name = id;
|
||||||
} else if (pk_.is_varchar()) {
|
} else if (pk_.is_varchar()) {
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ public:
|
||||||
* @param info The database connection info data
|
* @param info The database connection info data
|
||||||
* @param sql_logger The logging handler
|
* @param sql_logger The logging handler
|
||||||
*/
|
*/
|
||||||
explicit connection(connection_info info, const logger_ptr &sql_logger = null_logger);
|
explicit connection(const connection_info& info, const logger_ptr &sql_logger = null_logger);
|
||||||
/**
|
/**
|
||||||
* @brief Creates a database connection from a connection string.
|
* @brief Creates a database connection from a connection string.
|
||||||
*
|
*
|
||||||
|
|
@ -147,7 +147,6 @@ private:
|
||||||
friend class session;
|
friend class session;
|
||||||
friend class statement_cache;
|
friend class statement_cache;
|
||||||
|
|
||||||
connection_info connection_info_;
|
|
||||||
std::unique_ptr<connection_impl> connection_;
|
std::unique_ptr<connection_impl> connection_;
|
||||||
std::shared_ptr<abstract_sql_logger> logger_ = std::make_shared<null_sql_logger>();
|
std::shared_ptr<abstract_sql_logger> logger_ = std::make_shared<null_sql_logger>();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -42,14 +42,13 @@ public:
|
||||||
[[nodiscard]] const class dialect &dialect() const;
|
[[nodiscard]] const class dialect &dialect() const;
|
||||||
|
|
||||||
[[nodiscard]] virtual std::string to_escaped_string(const utils::blob &value) const = 0;
|
[[nodiscard]] virtual std::string to_escaped_string(const utils::blob &value) const = 0;
|
||||||
|
[[nodiscard]] const connection_info &info() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit connection_impl(const connection_info &info);
|
explicit connection_impl(const connection_info &info);
|
||||||
|
|
||||||
[[nodiscard]] const connection_info &info() const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::reference_wrapper<const connection_info> info_;
|
connection_info info_;
|
||||||
std::reference_wrapper<const class dialect> dialect_;
|
std::reference_wrapper<const class dialect> dialect_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -139,7 +139,7 @@ public:
|
||||||
, creator_(std::move(creator)) {}
|
, creator_(std::move(creator)) {}
|
||||||
|
|
||||||
iterator begin() { return std::move(++iterator(this)); }
|
iterator begin() { return std::move(++iterator(this)); }
|
||||||
static iterator end() { return {}; }
|
iterator end() { return {}; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class query_result_iterator<Type>;
|
friend class query_result_iterator<Type>;
|
||||||
|
|
|
||||||
|
|
@ -18,15 +18,15 @@ column_generator::column_generator(std::vector<column> &column_infos,
|
||||||
seen_tables.insert(table_name);
|
seen_tables.insert(table_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<column> column_generator::generate( const object::repository& scm, const std::string& name, bool force_lazy ) {
|
std::vector<column> column_generator::generate( const object::repository& scm, const std::string& name, bool /*force_lazy*/ ) {
|
||||||
const auto info = scm.basic_info(name);
|
const auto info = scm.basic_info(name);
|
||||||
if (!info) {
|
if (!info) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<column> columns;
|
std::vector<column> columns;
|
||||||
for (const auto attr : info.value().get().definition()) {
|
for (const auto& attr : info.value().get().definition()) {
|
||||||
columns.push_back(column{attr.name()});
|
columns.emplace_back(attr.name());
|
||||||
}
|
}
|
||||||
return columns;
|
return columns;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,20 +28,17 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
connection::connection(connection_info info, const std::shared_ptr<abstract_sql_logger> &sql_logger)
|
connection::connection(const connection_info& info, const std::shared_ptr<abstract_sql_logger> &sql_logger)
|
||||||
: connection_info_(std::move(info))
|
: logger_(sql_logger)
|
||||||
, logger_(sql_logger)
|
|
||||||
{
|
{
|
||||||
connection_.reset(backend_provider::instance().create_connection(connection_info_.type, connection_info_));
|
connection_.reset(backend_provider::instance().create_connection(info.type, info));
|
||||||
}
|
}
|
||||||
|
|
||||||
connection::connection(const std::string& dns, const std::shared_ptr<abstract_sql_logger> &sql_logger)
|
connection::connection(const std::string& dns, const std::shared_ptr<abstract_sql_logger> &sql_logger)
|
||||||
: connection(connection_info::parse(dns), sql_logger)
|
: connection(connection_info::parse(dns), sql_logger)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
connection::connection(const connection &x)
|
connection::connection(const connection &x) {
|
||||||
: connection_info_(x.connection_info_)
|
|
||||||
{
|
|
||||||
if (x.connection_) {
|
if (x.connection_) {
|
||||||
throw std::runtime_error("couldn't copy connection with valid connection impl");
|
throw std::runtime_error("couldn't copy connection with valid connection impl");
|
||||||
}
|
}
|
||||||
|
|
@ -52,7 +49,6 @@ connection &connection::operator=(const connection &x) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
connection_info_ = x.connection_info_;
|
|
||||||
if (x.connection_) {
|
if (x.connection_) {
|
||||||
throw std::runtime_error("couldn't copy connection with valid connection impl");
|
throw std::runtime_error("couldn't copy connection with valid connection impl");
|
||||||
}
|
}
|
||||||
|
|
@ -60,13 +56,11 @@ connection &connection::operator=(const connection &x) {
|
||||||
}
|
}
|
||||||
|
|
||||||
connection::connection( connection&& x ) noexcept
|
connection::connection( connection&& x ) noexcept
|
||||||
: connection_info_(std::move(x.connection_info_))
|
: connection_(std::move(x.connection_))
|
||||||
, connection_(std::move(x.connection_))
|
|
||||||
, logger_(std::move(x.logger_))
|
, logger_(std::move(x.logger_))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
connection & connection::operator=(connection &&x) noexcept {
|
connection & connection::operator=(connection &&x) noexcept {
|
||||||
connection_info_ = std::move(x.connection_info_);
|
|
||||||
connection_ = std::move(x.connection_);
|
connection_ = std::move(x.connection_);
|
||||||
logger_ = std::move(x.logger_);
|
logger_ = std::move(x.logger_);
|
||||||
|
|
||||||
|
|
@ -80,7 +74,8 @@ connection::~connection() {
|
||||||
if (connection_->is_open()) {
|
if (connection_->is_open()) {
|
||||||
connection_->close();
|
connection_->close();
|
||||||
}
|
}
|
||||||
backend_provider::instance().destroy_connection(connection_info_.type, connection_.release());
|
connection_impl* impl = connection_.release();
|
||||||
|
backend_provider::instance().destroy_connection(impl->info().type, impl);
|
||||||
connection_ = nullptr;
|
connection_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,11 +103,11 @@ utils::result<bool, utils::error> connection::is_open() const
|
||||||
|
|
||||||
const connection_info &connection::info() const
|
const connection_info &connection::info() const
|
||||||
{
|
{
|
||||||
return connection_info_;
|
return connection_->info();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string connection::type() const {
|
std::string connection::type() const {
|
||||||
return connection_info_.type;
|
return connection_->info().type;
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::result<void, utils::error> connection::begin() const {
|
utils::result<void, utils::error> connection::begin() const {
|
||||||
|
|
|
||||||
|
|
@ -58,12 +58,12 @@ connection_pool::connection_pool(const std::string& dns, size_t count)
|
||||||
: info_(connection_info::parse(dns)) {
|
: info_(connection_info::parse(dns)) {
|
||||||
connection_repo_.reserve(count);
|
connection_repo_.reserve(count);
|
||||||
while (count) {
|
while (count) {
|
||||||
connection c(info_);
|
// connection c(info_);
|
||||||
auto&& cc = std::move(c);
|
// auto&& cc = std::move(c);
|
||||||
const auto ic = identifiable_connection{count, std::move(cc)};
|
// const auto ic = identifiable_connection{count, std::move(cc)};
|
||||||
connection_repo_.push_back(ic);
|
// connection_repo_.push_back(ic);
|
||||||
// connection_repo_.emplace_back(count, std::move(cc));
|
// connection_repo_.emplace_back(count, std::move(cc));
|
||||||
// connection_repo_.emplace_back(count, connection{info_});
|
connection_repo_.emplace_back(count, connection{info_});
|
||||||
auto &conn = connection_repo_.back();
|
auto &conn = connection_repo_.back();
|
||||||
idle_connections_.emplace(conn.id, &conn);
|
idle_connections_.emplace(conn.id, &conn);
|
||||||
// Todo: handle result
|
// Todo: handle result
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]")
|
||||||
REQUIRE(f.is_ok());
|
REQUIRE(f.is_ok());
|
||||||
|
|
||||||
const auto res = ses.find<flight>(2);
|
const auto res = ses.find<flight>(2);
|
||||||
|
std::cout << "res: " << res.err() << "\n";
|
||||||
REQUIRE(res.is_ok());
|
REQUIRE(res.is_ok());
|
||||||
const auto& rf = *res;
|
const auto& rf = *res;
|
||||||
REQUIRE(rf->id == (*f)->id);
|
REQUIRE(rf->id == (*f)->id);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue