added Postgres isolation_level configuration

- also added a new constants the isolation_level and spatial_ref_sys
This commit is contained in:
silverqx
2023-01-31 11:21:36 +01:00
parent 362814d746
commit e9693d7867
9 changed files with 81 additions and 43 deletions

View File

@@ -109,18 +109,20 @@ std::shared_ptr<DatabaseManager> setupManager()
// Specifies what time zone all QDateTime-s will have
{qt_timezone, QVariant::fromValue(Qt::UTC)},
// Examples of qt_timezone
// {qt_timezone, QVariant::fromValue(QTimeZone("Europe/Bratislava"))},
// {qt_timezone, "Europe/Prague"}, // Will be converted to QTimeZone("Europe/Prague")
// {qt_timezone, QVariant::fromValue(QTimeZone("UTC+04"))},
// {qt_timezone, "-03:00"},
// {qt_timezone, 3600}, // Offset from UTC
// {qt_timezone, QVariant::fromValue(Qt::LocalTime)},
// {qt_timezone, {}}, // The same as Qt::LocalTime
{prefix_, EMPTY},
{prefix_indexes, true},
// {qt_timezone, QVariant::fromValue(QTimeZone("Europe/Bratislava"))},
// {qt_timezone, "Europe/Prague"}, // Will be converted to QTimeZone("Europe/Prague")
// {qt_timezone, QVariant::fromValue(QTimeZone("UTC+04"))},
// {qt_timezone, "-03:00"},
// {qt_timezone, 3600}, // Offset from UTC
// {qt_timezone, QVariant::fromValue(Qt::LocalTime)},
// {qt_timezone, {}}, // The same as Qt::LocalTime
{prefix_, EMPTY},
{prefix_indexes, true},
// {isolation_level, QStringLiteral("REPEATABLE READ")}, // Postgres default is READ COMMITTED
// {synchronous_commit, QStringLiteral("off")}, // Postgres default is on
// ConnectionFactory provides a default value for this (for reference only)
// {dont_drop, QStringList {QStringLiteral("spatial_ref_sys")}},
{options_, ConfigUtils::postgresSslOptions()},
// {dont_drop, QStringList {spatial_ref_sys}},
{options_, ConfigUtils::postgresSslOptions()},
}},
// SQLite connection

View File

@@ -36,6 +36,9 @@ namespace Orm::Connectors
void parseConfigOptions(QVariantHash &options) const override;
protected:
/*! Set the connection transaction isolation level. */
static void configureIsolationLevel(const QSqlDatabase &connection,
const QVariantHash &config);
/*! Set the connection character set and collation. */
static void configureEncoding(const QSqlDatabase &connection,
const QVariantHash &config);

View File

@@ -106,6 +106,8 @@ namespace Orm::Constants
SHAREDLIB_EXPORT extern const QString prefix_indexes;
SHAREDLIB_EXPORT extern const QString return_qdatetime;
SHAREDLIB_EXPORT extern const QString application_name;
SHAREDLIB_EXPORT extern const QString synchronous_commit;
SHAREDLIB_EXPORT extern const QString spatial_ref_sys;
SHAREDLIB_EXPORT extern const QString H127001;
SHAREDLIB_EXPORT extern const QString LOCALHOST;

View File

@@ -111,6 +111,10 @@ namespace Orm::Constants
return_qdatetime = QStringLiteral("return_qdatetime");
inline const QString
application_name = QStringLiteral("application_name");
inline const QString
synchronous_commit = QStringLiteral("synchronous_commit");
inline const QString
spatial_ref_sys = QStringLiteral("spatial_ref_sys");
inline const QString H127001 = QStringLiteral("127.0.0.1");
inline const QString LOCALHOST = QStringLiteral("localhost");

View File

@@ -19,6 +19,7 @@ using Orm::Constants::LOCAL;
using Orm::Constants::NAME;
using Orm::Constants::TMPL_DQUOTES;
using Orm::Constants::charset_;
using Orm::Constants::isolation_level;
using Orm::Constants::schema_;
using Orm::Constants::timezone_;
@@ -42,6 +43,9 @@ PostgresConnector::connect(const QVariantHash &config) const
// Create and open new database connection
const auto connection = createConnection(name, config, options);
// Transaction isolation level
configureIsolationLevel(connection, config);
// Connection encoding
configureEncoding(connection, config);
@@ -76,6 +80,22 @@ void PostgresConnector::parseConfigOptions(QVariantHash &/*unused*/) const
/* protected */
void PostgresConnector::configureIsolationLevel(const QSqlDatabase &connection,
const QVariantHash &config)
{
if (!config.contains(isolation_level))
return;
QSqlQuery query(connection);
if (query.exec(QStringLiteral("set session characteristics as "
"transaction isolation level %1;")
.arg(config[isolation_level].value<QString>())))
return;
throw Exceptions::QueryError(m_configureErrorMessage.arg(__tiny_func__), query);
}
void PostgresConnector::configureEncoding(const QSqlDatabase &connection,
const QVariantHash &config)
{

View File

@@ -95,6 +95,8 @@ namespace Orm::Constants
const QString prefix_indexes = QStringLiteral("prefix_indexes");
const QString return_qdatetime = QStringLiteral("return_qdatetime");
const QString application_name = QStringLiteral("application_name");
const QString synchronous_commit = QStringLiteral("synchronous_commit");
const QString spatial_ref_sys = QStringLiteral("spatial_ref_sys");
const QString H127001 = QStringLiteral("127.0.0.1");
const QString LOCALHOST = QStringLiteral("localhost");

View File

@@ -232,23 +232,25 @@ std::pair<std::reference_wrapper<const QVariantHash>, bool>
Databases::postgresConfiguration()
{
static const QVariantHash config {
{driver_, QPSQL},
{application_name, QStringLiteral("TinyORM tests (TinyUtils)")},
{host_, qEnvironmentVariable("DB_PGSQL_HOST", H127001)},
{port_, qEnvironmentVariable("DB_PGSQL_PORT", P5432)},
{database_, qEnvironmentVariable("DB_PGSQL_DATABASE", EMPTY)},
{schema_, qEnvironmentVariable("DB_PGSQL_SCHEMA", PUBLIC)},
{username_, qEnvironmentVariable("DB_PGSQL_USERNAME", postgres_)},
{password_, qEnvironmentVariable("DB_PGSQL_PASSWORD", EMPTY)},
{charset_, qEnvironmentVariable("DB_PGSQL_CHARSET", UTF8)},
{timezone_, UTC},
{driver_, QPSQL},
{application_name, QStringLiteral("TinyORM tests (TinyUtils)")},
{host_, qEnvironmentVariable("DB_PGSQL_HOST", H127001)},
{port_, qEnvironmentVariable("DB_PGSQL_PORT", P5432)},
{database_, qEnvironmentVariable("DB_PGSQL_DATABASE", EMPTY)},
{schema_, qEnvironmentVariable("DB_PGSQL_SCHEMA", PUBLIC)},
{username_, qEnvironmentVariable("DB_PGSQL_USERNAME", postgres_)},
{password_, qEnvironmentVariable("DB_PGSQL_PASSWORD", EMPTY)},
{charset_, qEnvironmentVariable("DB_PGSQL_CHARSET", UTF8)},
{timezone_, UTC},
// Specifies what time zone all QDateTime-s will have
{qt_timezone, QVariant::fromValue(Qt::UTC)},
{prefix_, EMPTY},
{prefix_indexes, true},
{qt_timezone, QVariant::fromValue(Qt::UTC)},
{prefix_, EMPTY},
{prefix_indexes, true},
// {isolation_level, QStringLiteral("REPEATABLE READ")}, // Postgres default is READ COMMITTED
// {synchronous_commit, QStringLiteral("off")}, // Postgres default is on
// ConnectionFactory provides a default value for this, this is only for reference
// {dont_drop, QStringList {QStringLiteral("spatial_ref_sys")}},
{options_, ConfigUtils::postgresSslOptions()},
// {dont_drop, QStringList {spatial_ref_sys}},
{options_, ConfigUtils::postgresSslOptions()},
};
// Environment variables were undefined

View File

@@ -30,6 +30,7 @@ using Orm::Constants::prefix_indexes;
using Orm::Constants::qt_timezone;
using Orm::Constants::return_qdatetime;
using Orm::Constants::schema_;
using Orm::Constants::spatial_ref_sys;
using Orm::Constants::username_;
using Orm::DatabaseManager;
@@ -238,7 +239,7 @@ void tst_DatabaseManager::default_PostgreSQL_ConfigurationValues() const
{prefix_, EMPTY},
{prefix_indexes, false},
{options_, QVariantHash()},
{dont_drop, QStringList {QStringLiteral("spatial_ref_sys")}},
{dont_drop, QStringList {spatial_ref_sys}},
}));
// Connection configuration
@@ -250,7 +251,7 @@ void tst_DatabaseManager::default_PostgreSQL_ConfigurationValues() const
{prefix_, EMPTY},
{prefix_indexes, false},
{options_, QVariantHash()},
{dont_drop, QStringList {QStringLiteral("spatial_ref_sys")}},
{dont_drop, QStringList {spatial_ref_sys}},
{qt_timezone, QVariant::fromValue(
QtTimeZoneConfig {QtTimeZoneType::DontConvert, {}})},
}));

View File

@@ -100,23 +100,25 @@ std::shared_ptr<DatabaseManager> setupManager()
// PostgreSQL connection
{QStringLiteral("tinyorm_testdata_tom_postgres"), { // shell:connection
{driver_, QPSQL},
{application_name, QStringLiteral("tom_testdata")},
{host_, qEnvironmentVariable("DB_PGSQL_HOST", H127001)},
{port_, qEnvironmentVariable("DB_PGSQL_PORT", P5432)},
{database_, qEnvironmentVariable("DB_PGSQL_DATABASE", EMPTY)},
{schema_, qEnvironmentVariable("DB_PGSQL_SCHEMA", PUBLIC)},
{username_, qEnvironmentVariable("DB_PGSQL_USERNAME", postgres_)},
{password_, qEnvironmentVariable("DB_PGSQL_PASSWORD", EMPTY)},
{charset_, qEnvironmentVariable("DB_PGSQL_CHARSET", UTF8)},
{timezone_, UTC},
{driver_, QPSQL},
{application_name, QStringLiteral("tom_testdata")},
{host_, qEnvironmentVariable("DB_PGSQL_HOST", H127001)},
{port_, qEnvironmentVariable("DB_PGSQL_PORT", P5432)},
{database_, qEnvironmentVariable("DB_PGSQL_DATABASE", EMPTY)},
{schema_, qEnvironmentVariable("DB_PGSQL_SCHEMA", PUBLIC)},
{username_, qEnvironmentVariable("DB_PGSQL_USERNAME", postgres_)},
{password_, qEnvironmentVariable("DB_PGSQL_PASSWORD", EMPTY)},
{charset_, qEnvironmentVariable("DB_PGSQL_CHARSET", UTF8)},
{timezone_, UTC},
// Specifies what time zone all QDateTime-s will have
{qt_timezone, QVariant::fromValue(Qt::UTC)},
{prefix_, EMPTY},
{prefix_indexes, true},
{qt_timezone, QVariant::fromValue(Qt::UTC)},
{prefix_, EMPTY},
{prefix_indexes, true},
// {isolation_level, QStringLiteral("REPEATABLE READ")}, // Postgres default is READ COMMITTED
// {synchronous_commit, QStringLiteral("off")}, // Postgres default is on
// ConnectionFactory provides a default value for this (for reference only)
// {dont_drop, QStringList {QStringLiteral("spatial_ref_sys")}},
{options_, ConfigUtils::postgresSslOptions()},
// {dont_drop, QStringList {spatial_ref_sys}},
{options_, ConfigUtils::postgresSslOptions()},
}},
// SQLite connection