camper/src/database.cpp

104 lines
2.9 KiB
C++
Raw Normal View History

Add a very ugly login page to test database connection I want to perform all SQL queries in a thread, to avoid freezing the UI, that sometimes might happen when there is a lot of data to fetch; should not happen very often, though. Neither libpq nor Qt SQL allow queries on the same connection from differents threads, and, in Qt SQL, all queries must be performed from the same thread where the connection was established. In Qt5 i had to either create a connection per thread, or use a QThread-derived object to hold the connection and use signals and slots to pass query and response data between the UI and database threads; it was usable but not pretty. With Qt6 and Concurrent’s QThreadPool now i can use QFutures instead, that are not as cumbersome as with Qt5, because i no longer need QFutureWatcher. I still have the problem that all queries must be done from within the same thread, and QThreadPool uses an arbitrary thread. The solution is to create a “pool” with a single, non-expirable thread, and call all Concurrent::run onto that pool. I have to test it properly, and first need to open the database to test whether that, at least, works. I added a simple “login page” for that, and to make a first attempt to error messages; i use a control that is like Kirigami’s InlineMessage for now, but i am not sure. I also do not know how i will configure database’s connection details. I usually make use of pg_service.conf, because then the application only need to know its service name, but i am not sure whether other people would find it as comfortable as i do.
2024-12-16 11:59:19 +00:00
#include "database.h"
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
Add a very ugly login page to test database connection I want to perform all SQL queries in a thread, to avoid freezing the UI, that sometimes might happen when there is a lot of data to fetch; should not happen very often, though. Neither libpq nor Qt SQL allow queries on the same connection from differents threads, and, in Qt SQL, all queries must be performed from the same thread where the connection was established. In Qt5 i had to either create a connection per thread, or use a QThread-derived object to hold the connection and use signals and slots to pass query and response data between the UI and database threads; it was usable but not pretty. With Qt6 and Concurrent’s QThreadPool now i can use QFutures instead, that are not as cumbersome as with Qt5, because i no longer need QFutureWatcher. I still have the problem that all queries must be done from within the same thread, and QThreadPool uses an arbitrary thread. The solution is to create a “pool” with a single, non-expirable thread, and call all Concurrent::run onto that pool. I have to test it properly, and first need to open the database to test whether that, at least, works. I added a simple “login page” for that, and to make a first attempt to error messages; i use a control that is like Kirigami’s InlineMessage for now, but i am not sure. I also do not know how i will configure database’s connection details. I usually make use of pg_service.conf, because then the application only need to know its service name, but i am not sure whether other people would find it as comfortable as i do.
2024-12-16 11:59:19 +00:00
Database::Database(QObject *parent)
: QObject{parent}
, m_pool{}
{
m_pool.setMaxThreadCount(1);
m_pool.setExpiryTimeout(-1);
}
Database &Database::getInstance()
{
static Database instance;
return instance;
}
Database *Database::create(QQmlEngine *qmlEngine, QJSEngine *)
{
Database &instance = getInstance();
Q_ASSERT(qmlEngine->thread() == instance.thread());
static QQmlEngine *engine = nullptr;
if (engine) {
Q_ASSERT(qmlEngine == engine);
} else {
engine = qmlEngine;
}
QJSEngine::setObjectOwnership(&instance, QJSEngine::CppOwnership);
return &instance;
}
QFuture<void> Database::open(const QString &user,
const QString &password,
const QString &hostName,
bool usePort,
int portNumber,
const QString &databaseName,
const QString &connectOptions)
Add a very ugly login page to test database connection I want to perform all SQL queries in a thread, to avoid freezing the UI, that sometimes might happen when there is a lot of data to fetch; should not happen very often, though. Neither libpq nor Qt SQL allow queries on the same connection from differents threads, and, in Qt SQL, all queries must be performed from the same thread where the connection was established. In Qt5 i had to either create a connection per thread, or use a QThread-derived object to hold the connection and use signals and slots to pass query and response data between the UI and database threads; it was usable but not pretty. With Qt6 and Concurrent’s QThreadPool now i can use QFutures instead, that are not as cumbersome as with Qt5, because i no longer need QFutureWatcher. I still have the problem that all queries must be done from within the same thread, and QThreadPool uses an arbitrary thread. The solution is to create a “pool” with a single, non-expirable thread, and call all Concurrent::run onto that pool. I have to test it properly, and first need to open the database to test whether that, at least, works. I added a simple “login page” for that, and to make a first attempt to error messages; i use a control that is like Kirigami’s InlineMessage for now, but i am not sure. I also do not know how i will configure database’s connection details. I usually make use of pg_service.conf, because then the application only need to know its service name, but i am not sure whether other people would find it as comfortable as i do.
2024-12-16 11:59:19 +00:00
{
return QtConcurrent::run(
&m_pool,
[this, user, password, hostName, usePort, portNumber, databaseName, connectOptions]() {
QString errorMessage;
QString connectionName;
{
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName(hostName);
if (usePort) {
db.setPort(portNumber);
}
db.setDatabaseName(databaseName);
db.setConnectOptions(connectOptions);
if (db.open(user, password)) {
QSqlQuery q(db);
if (q.exec("SET search_path TO camper, public")) {
emit opened();
return;
}
errorMessage = q.lastError().text();
} else {
errorMessage = db.lastError().text();
}
connectionName = db.connectionName();
}
QSqlDatabase::removeDatabase(connectionName);
emit errorOcurred(errorMessage);
});
Add a very ugly login page to test database connection I want to perform all SQL queries in a thread, to avoid freezing the UI, that sometimes might happen when there is a lot of data to fetch; should not happen very often, though. Neither libpq nor Qt SQL allow queries on the same connection from differents threads, and, in Qt SQL, all queries must be performed from the same thread where the connection was established. In Qt5 i had to either create a connection per thread, or use a QThread-derived object to hold the connection and use signals and slots to pass query and response data between the UI and database threads; it was usable but not pretty. With Qt6 and Concurrent’s QThreadPool now i can use QFutures instead, that are not as cumbersome as with Qt5, because i no longer need QFutureWatcher. I still have the problem that all queries must be done from within the same thread, and QThreadPool uses an arbitrary thread. The solution is to create a “pool” with a single, non-expirable thread, and call all Concurrent::run onto that pool. I have to test it properly, and first need to open the database to test whether that, at least, works. I added a simple “login page” for that, and to make a first attempt to error messages; i use a control that is like Kirigami’s InlineMessage for now, but i am not sure. I also do not know how i will configure database’s connection details. I usually make use of pg_service.conf, because then the application only need to know its service name, but i am not sure whether other people would find it as comfortable as i do.
2024-12-16 11:59:19 +00:00
}
QFuture<void> Database::close()
{
return QtConcurrent::run(&m_pool, [this]() {
QString connectionName;
{
QSqlDatabase db = QSqlDatabase::database();
if (!db.isValid()) {
return;
}
connectionName = db.connectionName();
db.close();
}
QSqlDatabase::removeDatabase(connectionName);
emit closed();
});
}
void Database::checkError(const QSqlQuery &query)
{
QSqlError lastError = query.lastError();
if (!lastError.isValid()) {
return;
}
QString errorMessage = lastError.text();
emit getInstance().errorOcurred(errorMessage);
throw std::runtime_error(errorMessage.toStdString());
}
#include "moc_database.cpp"