Compare commits
No commits in common. "167e2ef3825ea68385da2759aa0bd36f099ff63c" and "0d08ad41797d98c9bed1851719c30afc943c2d25" have entirely different histories.
167e2ef382
...
0d08ad4179
|
|
@ -4,7 +4,6 @@
|
|||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/connection_info.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
|
@ -12,6 +11,7 @@
|
|||
#include <unordered_map>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
class connection_pool;
|
||||
|
||||
struct identifiable_connection {
|
||||
|
|
@ -49,8 +49,7 @@ public:
|
|||
|
||||
connection_ptr acquire();
|
||||
connection_ptr try_acquire();
|
||||
connection_ptr try_acquire(size_t id);
|
||||
connection_ptr acquire(size_t id, std::chrono::milliseconds timeout = std::chrono::milliseconds(5000));
|
||||
connection_ptr acquire(size_t id);
|
||||
|
||||
void release(identifiable_connection *c);
|
||||
void release(connection_ptr &c);
|
||||
|
|
|
|||
|
|
@ -87,19 +87,18 @@ matador::utils::result<void, matador::utils::error> matador::orm::schema::drop()
|
|||
drop_sql_commands.push_back(ctx.sql);
|
||||
// determine constraints and drop them
|
||||
for (auto it = node->info().endpoint_begin(); it != node->info().endpoint_end(); ++it) {
|
||||
const auto& endpoint = *(it->second);
|
||||
if (endpoint.type() == object::relation_type::BELONGS_TO) {
|
||||
continue;
|
||||
}
|
||||
const auto drop_fk_ctx = query::query::alter()
|
||||
.table( endpoint.node().name() )
|
||||
.drop_constraint( "FK_" + endpoint.node().name() + "_" + endpoint.foreign_endpoint()->field_name() )
|
||||
.compile(*c);
|
||||
std::cout << drop_fk_ctx.sql << std::endl;
|
||||
if (auto result = c->execute(drop_fk_ctx.sql); !result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
const auto ctx = query::query::alter()
|
||||
.table( "" )
|
||||
.add_constraint( it->second->node().name() )
|
||||
.foreign_key( "" )
|
||||
.references( "" ).compile(*c);
|
||||
|
||||
}
|
||||
// for ( const auto& [sql, command] : ctx.additional_commands ) {
|
||||
// if (auto result = c->execute(ctx.sql); !result) {
|
||||
// return utils::failure(result.err());
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
// execute additional commands (e.g. ALTER TABLE ADD FK)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#include "matador/sql/connection_pool.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
|
||||
|
|
@ -97,29 +98,42 @@ connection_ptr connection_pool::try_acquire() {
|
|||
return get_next_connection();
|
||||
}
|
||||
|
||||
connection_ptr connection_pool::try_acquire(const size_t id) {
|
||||
std::unique_lock lock(mutex_);
|
||||
const auto it = idle_connections_.find(id);
|
||||
if (it == idle_connections_.end()) {
|
||||
return {nullptr, this};
|
||||
}
|
||||
return {it->second, this};
|
||||
}
|
||||
|
||||
connection_ptr connection_pool::acquire(const size_t id, const std::chrono::milliseconds timeout) {
|
||||
connection_ptr connection_pool::acquire(const size_t id) {
|
||||
using namespace std::chrono_literals;
|
||||
std::unique_lock lock(mutex_);
|
||||
|
||||
if (!cv.wait_for(lock, timeout, [this, id] {
|
||||
return idle_connections_.find(id) != idle_connections_.end();
|
||||
})) {
|
||||
if (!cv.wait_for(lock,
|
||||
5s,
|
||||
[this, id] {
|
||||
return idle_connections_.find(id) != idle_connections_.end();
|
||||
})) {
|
||||
return {nullptr, this};
|
||||
}
|
||||
}
|
||||
|
||||
const auto it = idle_connections_.find(id);
|
||||
auto it = idle_connections_.find(id);
|
||||
auto next_connection = it->second;
|
||||
auto node = idle_connections_.extract(it);
|
||||
inuse_connections_.insert(std::move(node));
|
||||
return {next_connection, this};
|
||||
|
||||
// using namespace std::chrono_literals;
|
||||
// pointer next_connection{nullptr};
|
||||
// auto try_count{0};
|
||||
// std::unique_lock lock(mutex_);
|
||||
//
|
||||
// do {
|
||||
// if (auto it = idle_connections_.find(id); it != idle_connections_.end()) {
|
||||
// next_connection = it->second;
|
||||
// auto node = idle_connections_.extract(it);
|
||||
// inuse_connections_.insert(std::move(node));
|
||||
// } else {
|
||||
// lock.unlock();
|
||||
// std::this_thread::sleep_for(100ms);
|
||||
// lock.lock();
|
||||
// }
|
||||
// } while(try_count++ < 5);
|
||||
//
|
||||
// return {next_connection, this};
|
||||
}
|
||||
|
||||
void connection_pool::release(identifiable_connection* c) {
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
using namespace matador;
|
||||
using namespace matador::test;
|
||||
|
||||
TEST_CASE_METHOD(SchemaFixture, "Test schema one-two-many", "[schema]") {
|
||||
TEST_CASE_METHOD(SchemaFixture, "Test schema", "[schema]") {
|
||||
using namespace matador::test;
|
||||
matador::orm::schema repo(pool/*, "NoopSchema"*/);
|
||||
|
||||
|
|
|
|||
|
|
@ -57,8 +57,6 @@ TEST_CASE_METHOD(ConnectionPoolFixture, "Create connection pool", "[connection p
|
|||
}
|
||||
|
||||
TEST_CASE_METHOD(ConnectionPoolFixture, "Acquire connection by id", "[connection pool]") {
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
connection_pool pool("noop://noop.db", 4);
|
||||
|
||||
REQUIRE(pool.size() == 4);
|
||||
|
|
@ -71,10 +69,8 @@ TEST_CASE_METHOD(ConnectionPoolFixture, "Acquire connection by id", "[connection
|
|||
REQUIRE(ptr.id().value() > 0);
|
||||
REQUIRE(ptr->is_open());
|
||||
|
||||
auto same_ptr = pool.try_acquire(ptr.id().value());
|
||||
REQUIRE(!same_ptr.valid());
|
||||
auto same_ptr = pool.acquire(ptr.id().value());
|
||||
|
||||
same_ptr = pool.acquire(ptr.id().value(), 3000ms);
|
||||
REQUIRE(!same_ptr.valid());
|
||||
|
||||
const auto connection_id = ptr.id().value();
|
||||
|
|
|
|||
Loading…
Reference in New Issue