summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton <anton@picapica.im>2026-08-12 00:18:12 +0400
committerAnton <anton@picapica.im>2026-08-12 00:18:12 +0400
commit0763c7de460da23923e62f0d8033bc6b8c2888a9 (patch)
tree5b3825708e3c05d91fd2ef40f885e6d718e6e274
parent5ae6a1ff696f70b8eb1eac042a2cc278d4e7ceed (diff)
downloadpica-pica-0763c7de460da23923e62f0d8033bc6b8c2888a9.tar.gz
pica-pica-0763c7de460da23923e62f0d8033bc6b8c2888a9.zip
pica-client: changes for required for multithreaded DB access
-rw-r--r--pica-client/accounts.h3
-rw-r--r--pica-client/contacts.cpp24
-rw-r--r--pica-client/contacts.h3
-rw-r--r--pica-client/history.cpp16
-rw-r--r--pica-client/history.h3
-rw-r--r--pica-client/nodes.cpp14
-rw-r--r--pica-client/nodes.h7
-rw-r--r--pica-client/settings.cpp10
-rw-r--r--pica-client/settings.h3
9 files changed, 47 insertions, 36 deletions
diff --git a/pica-client/accounts.h b/pica-client/accounts.h
index 36d155f..de718f0 100644
--- a/pica-client/accounts.h
+++ b/pica-client/accounts.h
@@ -21,6 +21,7 @@
#include <QtSql>
#include <QSqlDatabase>
#include <QStringList>
+#include <QMetaType>
class Accounts
{
@@ -55,4 +56,6 @@ private:
};
+Q_DECLARE_METATYPE(Accounts::AccountRecord)
+
#endif // ACCOUNTS_H
diff --git a/pica-client/contacts.cpp b/pica-client/contacts.cpp
index ec57c8c..0fd259a 100644
--- a/pica-client/contacts.cpp
+++ b/pica-client/contacts.cpp
@@ -19,10 +19,10 @@
//#include <QMessageBox>
-Contacts::Contacts(QString storage, QByteArray user_account_id)
+Contacts::Contacts(QString storage, QByteArray user_account_id, QString connectionName)
: account_id_(user_account_id)
{
- dbconn = QSqlDatabase::database();
+ dbconn = connectionName.isEmpty() ? QSqlDatabase::database() : QSqlDatabase::database(connectionName);
dbconn.setDatabaseName(storage);
if (!dbconn.open())
@@ -34,7 +34,7 @@ Contacts::Contacts(QString storage, QByteArray user_account_id)
//check if table contacts exists
//...
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.exec("PRAGMA foreign_keys=ON;");
@@ -53,7 +53,7 @@ QString Contacts::GetLastError()
bool Contacts::Exists(QByteArray id)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.setForwardOnly(true);
query.prepare("select count(*) from contacts where account_id=:account_id and id=:id");
@@ -71,7 +71,7 @@ bool Contacts::Exists(QByteArray id)
void Contacts::Add(QByteArray id, ContactType type)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
if (!Exists(id))
{
@@ -95,7 +95,7 @@ void Contacts::Add(QByteArray id, ContactType type)
void Contacts::Delete(QByteArray id)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.prepare("delete from contacts where id=:id and account_id=:account_id");
query.bindValue(":id", id);
@@ -109,7 +109,7 @@ QList<Contacts::ContactRecord> Contacts::GetContacts(ContactType type)
{
QList<ContactRecord> L;
ContactRecord r;
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.setForwardOnly(true);
query.prepare("select id, name from contacts where account_id=:account_id and type=:type");
@@ -135,7 +135,7 @@ QList<Contacts::ContactRecord> Contacts::GetContacts(ContactType type)
QString Contacts::GetContactCert(QByteArray id)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.prepare("select cert_pem from contacts where id=:id and account_id=:account_id");
query.bindValue(":id", id);
@@ -154,7 +154,7 @@ QString Contacts::GetContactCert(QByteArray id)
QString Contacts::GetContactName(QByteArray id)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.prepare("select name from contacts where id=:id and account_id=:account_id");
query.bindValue(":id", id);
@@ -173,7 +173,7 @@ QString Contacts::GetContactName(QByteArray id)
void Contacts::SetContactCert(QByteArray id, QString &cert_pem)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.prepare("update contacts set cert_pem=:cert_pem where id=:id and account_id=:account_id");
query.bindValue(":cert_pem", cert_pem);
@@ -187,7 +187,7 @@ void Contacts::SetContactCert(QByteArray id, QString &cert_pem)
void Contacts::SetContactName(QByteArray id, QString name)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.prepare("update contacts set name=:name where id=:id and account_id=:account_id");
query.bindValue(":name", name);
@@ -200,7 +200,7 @@ void Contacts::SetContactName(QByteArray id, QString name)
void Contacts::SetContactType(QByteArray id, ContactType type)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.prepare("update contacts set type=:type where id=:id and account_id=:account_id");
query.bindValue(":type", type);
diff --git a/pica-client/contacts.h b/pica-client/contacts.h
index e460ad3..03a2e14 100644
--- a/pica-client/contacts.h
+++ b/pica-client/contacts.h
@@ -41,7 +41,8 @@ public:
ContactType type;
};
- Contacts(QString storage, QByteArray user_account_id);
+ // See Nodes::Nodes() for what connectionName selects and why.
+ Contacts(QString storage, QByteArray user_account_id, QString connectionName = QString());
bool Exists(QByteArray id);
void Add(QByteArray id, ContactType type = regular);
diff --git a/pica-client/history.cpp b/pica-client/history.cpp
index 6daa862..1ae0b9d 100644
--- a/pica-client/history.cpp
+++ b/pica-client/history.cpp
@@ -17,10 +17,10 @@
#include "history.h"
#include <QDebug>
-History::History(QString storage, QByteArray my_id)
+History::History(QString storage, QByteArray my_id, QString connectionName)
: me_(my_id)
{
- dbconn = QSqlDatabase::database();
+ dbconn = connectionName.isEmpty() ? QSqlDatabase::database() : QSqlDatabase::database(connectionName);
dbconn.setDatabaseName(storage);
if (!dbconn.open())
@@ -29,7 +29,7 @@ History::History(QString storage, QByteArray my_id)
return;
}
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.exec("PRAGMA foreign_keys=ON;");
@@ -38,7 +38,7 @@ History::History(QString storage, QByteArray my_id)
void History::Add(QByteArray peer_id, QString message, bool is_me)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.prepare(
"insert into \"history\" (\"contact_id\", \"account_id\", \"is_me\", \"is_delivered\", \"message\", \"id\", \"timestamp\") \
@@ -68,7 +68,7 @@ void History::Add(QByteArray peer_id, QString message, bool is_me)
void History::SetDelivered(QByteArray peer_id)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.prepare("update history set is_delivered=1 where id in \
(select id from history where \
@@ -86,7 +86,7 @@ QList<History::HistoryRecord> History::GetMessages(QByteArray peer_id, QString k
{
QList<HistoryRecord> L;
HistoryRecord r;
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.setForwardOnly(true);
query.prepare("select contact_id, is_me, is_delivered, timestamp, message from history \
@@ -121,7 +121,7 @@ QList<History::HistoryRecord> History::GetMessages(QByteArray peer_id, quint32 s
{
QList<HistoryRecord> L;
HistoryRecord r;
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.setForwardOnly(true);
query.prepare("select contact_id, is_me, is_delivered, timestamp, message from history \
@@ -156,7 +156,7 @@ QList<History::HistoryRecord> History::GetMessages(QByteArray peer_id, quint32 s
QMap<QByteArray, QList<QString> > History::GetUndeliveredMessages()
{
QMap<QByteArray, QList<QString> > M;
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.setForwardOnly(true);
query.prepare("select contact_id, message from history \
diff --git a/pica-client/history.h b/pica-client/history.h
index c1c7615..53f8ff7 100644
--- a/pica-client/history.h
+++ b/pica-client/history.h
@@ -35,7 +35,8 @@ public:
QString message;
};
- History(QString storage, QByteArray my_id);
+ // See Nodes::Nodes() for what connectionName selects and why.
+ History(QString storage, QByteArray my_id, QString connectionName = QString());
void Add(QByteArray peer_id, QString message, bool is_me);//is_me - true - message from me to peer, false - from peer to me
void SetDelivered(QByteArray peer_id); //mark first undelivered message from me to peer as delivered
QList<HistoryRecord> GetMessages(QByteArray peer_id, quint32 start_timestamp, quint32 end_timestamp);
diff --git a/pica-client/nodes.cpp b/pica-client/nodes.cpp
index 048d2cc..cf6cadb 100644
--- a/pica-client/nodes.cpp
+++ b/pica-client/nodes.cpp
@@ -16,9 +16,9 @@
*/
#include "nodes.h"
-Nodes::Nodes(QString storage)
+Nodes::Nodes(QString storage, QString connectionName)
{
- dbconn = QSqlDatabase::database();
+ dbconn = connectionName.isEmpty() ? QSqlDatabase::database() : QSqlDatabase::database(connectionName);
dbconn.setDatabaseName(storage);
if (!dbconn.open())
@@ -31,7 +31,7 @@ Nodes::Nodes(QString storage)
void Nodes::Add(NodeRecord &n)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.prepare("insert into nodes (address, port, last_active, inactive_count) values (:addr, :port, strftime('%s','now'), 0)");
query.bindValue(":addr", n.address);
@@ -43,7 +43,7 @@ void Nodes::Add(NodeRecord &n)
void Nodes::Delete(NodeRecord &n)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.prepare("delete from nodes where address = :addr and port = :port");
query.bindValue(":addr", n.address);
@@ -57,7 +57,7 @@ QList<Nodes::NodeRecord> Nodes::GetNodes()
{
QList<NodeRecord> L;
NodeRecord r;
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.setForwardOnly(true);
query.exec("select address, port from nodes order by inactive_count asc, last_active desc");
@@ -79,7 +79,7 @@ QList<Nodes::NodeRecord> Nodes::GetNodes()
void Nodes::MakeClean()
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
quint32 count = 0;
query.exec("select count(*) from nodes");
@@ -115,7 +115,7 @@ void Nodes::MakeClean()
void Nodes::UpdateStatus(NodeRecord &n, bool alive)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
if (alive)
query.prepare("update nodes set last_active = strftime('%s','now'), inactive_count = 0 where address = :addr and port = :port");
diff --git a/pica-client/nodes.h b/pica-client/nodes.h
index 63b09a8..2cd2c1e 100644
--- a/pica-client/nodes.h
+++ b/pica-client/nodes.h
@@ -31,7 +31,12 @@ public:
quint16 port;
};
- Nodes(QString storage);
+ // connectionName selects which QSqlDatabase connection to use (see
+ // QSqlDatabase::database()). A connection can only be used from the
+ // thread that created it, so callers running on a thread other than
+ // the GUI thread must pass the name of a connection opened on their
+ // own thread. Empty (the default) uses Qt's default connection.
+ Nodes(QString storage, QString connectionName = QString());
void Add(NodeRecord &n);
void Delete(NodeRecord &n);
QList<NodeRecord> GetNodes();
diff --git a/pica-client/settings.cpp b/pica-client/settings.cpp
index 4c7088b..e4e3004 100644
--- a/pica-client/settings.cpp
+++ b/pica-client/settings.cpp
@@ -16,9 +16,9 @@
*/
#include "settings.h"
-Settings::Settings(QString storage)
+Settings::Settings(QString storage, QString connectionName)
{
- dbconn = QSqlDatabase::database();
+ dbconn = connectionName.isEmpty() ? QSqlDatabase::database() : QSqlDatabase::database(connectionName);
dbconn.setDatabaseName(storage);
if (!dbconn.open())
@@ -36,7 +36,7 @@ QString Settings::GetLastError()
bool Settings::isEmpty()
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.exec("select count(*) from settings");
@@ -54,7 +54,7 @@ bool Settings::isEmpty()
QVariant Settings::loadValue(QString name, QVariant defval)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.prepare("select value from settings where name = :name");
query.bindValue(":name", name);
@@ -74,7 +74,7 @@ QVariant Settings::loadValue(QString name, QVariant defval)
void Settings::storeValue(QString name, QString val)
{
- QSqlQuery query;
+ QSqlQuery query(dbconn);
query.prepare("insert into settings (name, value) values (:name, :val)");
query.bindValue(":name", name);
diff --git a/pica-client/settings.h b/pica-client/settings.h
index be95ca2..622acfc 100644
--- a/pica-client/settings.h
+++ b/pica-client/settings.h
@@ -24,7 +24,8 @@
class Settings
{
public:
- Settings(QString storage);
+ // See Nodes::Nodes() for what connectionName selects and why.
+ Settings(QString storage, QString connectionName = QString());
QVariant loadValue(QString name, QVariant defval);
void storeValue(QString name, QString val);