0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Implemented get/set_default_isolation_level() functions to change the

transaction isolation level default per-connection
This commit is contained in:
Volker Aßmann 2017-06-04 13:57:41 +02:00
parent 9413206bd1
commit a55d986def
3 changed files with 46 additions and 5 deletions

View File

@ -130,8 +130,19 @@ namespace sqlpp
return t._prepare(*this); 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 //! 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) //! commit transaction (or throw transaction if the transaction has been finished already)
void commit_transaction(); void commit_transaction();

View File

@ -38,6 +38,7 @@
// an object to store internal Mock flags and values to validate in tests // an object to store internal Mock flags and values to validate in tests
struct InternalMockData { struct InternalMockData {
sqlpp::isolation_level _last_isolation_level; sqlpp::isolation_level _last_isolation_level;
sqlpp::isolation_level _default_isolation_level;
}; };
template <bool enforceNullResultTreatment> template <bool enforceNullResultTreatment>
@ -251,12 +252,26 @@ struct MockDbT : public sqlpp::connection
return {name}; return {name};
} }
void start_transaction()
{
_mock_data._last_isolation_level = _mock_data._default_isolation_level;
}
void start_transaction(sqlpp::isolation_level 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; _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) void rollback_transaction(bool)
{} {}
@ -266,6 +281,7 @@ struct MockDbT : public sqlpp::connection
void report_rollback_failure(std::string) void report_rollback_failure(std::string)
{} {}
// temporary data store to verify the expected results were produced
InternalMockData _mock_data; InternalMockData _mock_data;
}; };

View File

@ -183,8 +183,22 @@ int Select(int, char* [])
for_each_field(row, to_cerr{}); 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; return 0;
} }