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; }