From a55d986def7b3bc83aea42a5c46893ed1dcfba70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20A=C3=9Fmann?= Date: Sun, 4 Jun 2017 13:57:41 +0200 Subject: [PATCH 1/2] Implemented get/set_default_isolation_level() functions to change the transaction isolation level default per-connection --- connector_api/connection.h | 13 ++++++++++++- tests/MockDb.h | 18 +++++++++++++++++- tests/Select.cpp | 20 +++++++++++++++++--- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/connector_api/connection.h b/connector_api/connection.h index 440704ac..d3455fde 100644 --- a/connector_api/connection.h +++ b/connector_api/connection.h @@ -130,8 +130,19 @@ namespace sqlpp return t._prepare(*this); } + + //! set the transaction isolation level for the current connection + /// time of effect is connector-specific, for most is will only affect new transactions + void set_default_isolation_level(sqlpp::isolation_level); + + //! read the default transaction isolation level for the current connection + sqlpp::isolation_level get_default_isolation_level(); + //! start transaction - void start_transaction(isolation_level isolation = isolation_level::undefined); + void start_transaction(); + + //! start transaction with defined isolation level (optional only for connectors that support it) + void start_transaction(isolation_level isolation /* = isolation_level::undefined */); //! commit transaction (or throw transaction if the transaction has been finished already) void commit_transaction(); diff --git a/tests/MockDb.h b/tests/MockDb.h index 91ab34d0..8e5d5308 100644 --- a/tests/MockDb.h +++ b/tests/MockDb.h @@ -38,6 +38,7 @@ // an object to store internal Mock flags and values to validate in tests struct InternalMockData { sqlpp::isolation_level _last_isolation_level; + sqlpp::isolation_level _default_isolation_level; }; template @@ -251,12 +252,26 @@ struct MockDbT : public sqlpp::connection return {name}; } + void start_transaction() + { + _mock_data._last_isolation_level = _mock_data._default_isolation_level; + } + void start_transaction(sqlpp::isolation_level level) { - // store temporarily to verify the expected level was used in testcases _mock_data._last_isolation_level = level; } + void set_default_isolation_level(sqlpp::isolation_level level) + { + _mock_data._default_isolation_level = level; + } + + sqlpp::isolation_level get_default_isolation_level() + { + return _mock_data._default_isolation_level; + } + void rollback_transaction(bool) {} @@ -266,6 +281,7 @@ struct MockDbT : public sqlpp::connection void report_rollback_failure(std::string) {} + // temporary data store to verify the expected results were produced InternalMockData _mock_data; }; diff --git a/tests/Select.cpp b/tests/Select.cpp index b475b642..8343b8bd 100644 --- a/tests/Select.cpp +++ b/tests/Select.cpp @@ -183,8 +183,22 @@ int Select(int, char* []) for_each_field(row, to_cerr{}); } - auto transaction = start_transaction(db, sqlpp::isolation_level::read_committed); - std::cout << (db._mock_data._last_isolation_level == sqlpp::isolation_level::read_committed) << std::endl; - + { + auto transaction = start_transaction(db, sqlpp::isolation_level::read_committed); + if (db._mock_data._last_isolation_level != sqlpp::isolation_level::read_committed) + { + std::cout << "Error: transaction isolation level does not match expacted level" << std::endl; + } + + } + db.set_default_isolation_level(sqlpp::isolation_level::read_uncommitted); + { + auto transaction = start_transaction(db); + if (db._mock_data._last_isolation_level != sqlpp::isolation_level::read_uncommitted) + { + std::cout << "Error: transaction isolation level does not match default level" << std::endl; + } + } + return 0; } From 9d395c2bc6111f9443777e8b23f290c5e5134d5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20A=C3=9Fmann?= Date: Sun, 4 Jun 2017 15:19:00 +0200 Subject: [PATCH 2/2] Fix typo --- tests/Select.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Select.cpp b/tests/Select.cpp index 8343b8bd..b27ade1f 100644 --- a/tests/Select.cpp +++ b/tests/Select.cpp @@ -187,7 +187,7 @@ int Select(int, char* []) auto transaction = start_transaction(db, sqlpp::isolation_level::read_committed); if (db._mock_data._last_isolation_level != sqlpp::isolation_level::read_committed) { - std::cout << "Error: transaction isolation level does not match expacted level" << std::endl; + std::cout << "Error: transaction isolation level does not match expected level" << std::endl; } }