From 5443771c510794253a2640bfcf065265930d8b6c Mon Sep 17 00:00:00 2001 From: fnc12 Date: Fri, 18 Oct 2019 19:23:05 +0300 Subject: [PATCH 1/2] added in memory handling in copy ctor --- dev/storage_base.h | 7 ++++++- include/sqlite_orm/sqlite_orm.h | 7 ++++++- tests/tests4.cpp | 14 ++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/dev/storage_base.h b/dev/storage_base.h index 31e0dacd6..bf981befb 100644 --- a/dev/storage_base.h +++ b/dev/storage_base.h @@ -300,7 +300,12 @@ namespace sqlite_orm { on_open(other.on_open), pragma(std::bind(&storage_base::get_connection, this)), limit(std::bind(&storage_base::get_connection, this)), inMemory(other.inMemory), connection(std::make_unique(other.connection->filename)), - cachedForeignKeysCount(other.cachedForeignKeysCount) {} + cachedForeignKeysCount(other.cachedForeignKeysCount) { + if(this->inMemory) { + this->connection->retain(); + this->on_open_internal(this->connection->get()); + } + } ~storage_base() { if(this->isOpenedForever) { diff --git a/include/sqlite_orm/sqlite_orm.h b/include/sqlite_orm/sqlite_orm.h index 1c6fcd045..bac9e5b5c 100644 --- a/include/sqlite_orm/sqlite_orm.h +++ b/include/sqlite_orm/sqlite_orm.h @@ -8018,7 +8018,12 @@ namespace sqlite_orm { on_open(other.on_open), pragma(std::bind(&storage_base::get_connection, this)), limit(std::bind(&storage_base::get_connection, this)), inMemory(other.inMemory), connection(std::make_unique(other.connection->filename)), - cachedForeignKeysCount(other.cachedForeignKeysCount) {} + cachedForeignKeysCount(other.cachedForeignKeysCount) { + if(this->inMemory) { + this->connection->retain(); + this->on_open_internal(this->connection->get()); + } + } ~storage_base() { if(this->isOpenedForever) { diff --git a/tests/tests4.cpp b/tests/tests4.cpp index c5329fb2a..3982addbd 100644 --- a/tests/tests4.cpp +++ b/tests/tests4.cpp @@ -191,9 +191,15 @@ TEST_CASE("Join") { } TEST_CASE("Storage copy") { + struct User { + int id = 0; + }; + int calledCount = 0; - auto storage = make_storage({}); + auto storage = make_storage({}, make_table("users", make_column("id", &User::id))); + storage.sync_schema(); + storage.remove_all(); storage.on_open = [&calledCount](sqlite3 *) { ++calledCount; @@ -204,8 +210,12 @@ TEST_CASE("Storage copy") { auto storageCopy = storage; REQUIRE(storageCopy.on_open); - storageCopy.on_open(nullptr); REQUIRE(calledCount == 2); + storageCopy.on_open(nullptr); + REQUIRE(calledCount == 3); + + storageCopy.sync_schema(); + storageCopy.remove_all(); } TEST_CASE("Set null") { From a5da5a09e166871c0d00f3c2bbd31894299a3b88 Mon Sep 17 00:00:00 2001 From: fnc12 Date: Fri, 18 Oct 2019 19:30:56 +0300 Subject: [PATCH 2/2] code format --- include/sqlite_orm/sqlite_orm.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/sqlite_orm/sqlite_orm.h b/include/sqlite_orm/sqlite_orm.h index bac9e5b5c..67a28c633 100644 --- a/include/sqlite_orm/sqlite_orm.h +++ b/include/sqlite_orm/sqlite_orm.h @@ -8019,11 +8019,11 @@ namespace sqlite_orm { limit(std::bind(&storage_base::get_connection, this)), inMemory(other.inMemory), connection(std::make_unique(other.connection->filename)), cachedForeignKeysCount(other.cachedForeignKeysCount) { - if(this->inMemory) { - this->connection->retain(); - this->on_open_internal(this->connection->get()); - } + if(this->inMemory) { + this->connection->retain(); + this->on_open_internal(this->connection->get()); } + } ~storage_base() { if(this->isOpenedForever) {