Skip to content
Permalink
Browse files

Merge PR #3422: Fix #3411: Threaded access to Database

  • Loading branch information...
davidebeatrici committed Jun 18, 2018
2 parents a092a44 + 222def7 commit 6195761d290fd3ba4296215e525da8008ddf86d2
@@ -268,7 +268,7 @@ void ACLEditor::accept() {
const QString &descriptionText = rteChannelDescription->text();
mpcs.set_description(u8(descriptionText));
needs_update = true;
Database::setBlob(sha1(descriptionText), descriptionText.toUtf8());
g.db->setBlob(sha1(descriptionText), descriptionText.toUtf8());
}
if (pChannel->iPosition != qsbChannelPosition->value()) {
mpcs.set_position(qsbChannelPosition->value());
@@ -1040,7 +1040,7 @@ ConnectDialog::ConnectDialog(QWidget *p, bool autoconnect) : QDialog(p), bAutoCo
}

QList<QTreeWidgetItem *> ql;
QList<FavoriteServer> favorites = Database::getFavorites();
QList<FavoriteServer> favorites = g.db->getFavorites();

foreach(const FavoriteServer &fs, favorites) {
ServerItem *si = new ServerItem(fs);
@@ -1089,7 +1089,7 @@ ConnectDialog::ConnectDialog(QWidget *p, bool autoconnect) : QDialog(p), bAutoCo
qtwServers->setCurrentItem(NULL);
bLastFound = false;

qmPingCache = Database::getPingCache();
qmPingCache = g.db->getPingCache();

if (! g.s.qbaConnectDialogGeometry.isEmpty())
restoreGeometry(g.s.qbaConnectDialogGeometry);
@@ -1111,8 +1111,8 @@ ConnectDialog::~ConnectDialog() {
continue;
ql << si->toFavoriteServer();
}
Database::setFavorites(ql);
Database::setPingCache(qmPingCache);
g.db->setFavorites(ql);
g.db->setPingCache(qmPingCache);

g.s.qbaConnectDialogHeader = qtwServers->header()->saveState();
g.s.qbaConnectDialogGeometry = saveGeometry();
@@ -36,8 +36,8 @@ static bool execQueryAndLogFailure(QSqlQuery &query, const QString &queryString)
}


Database::Database() {
QSqlDatabase db = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"));
Database::Database(const QString &dbname) {
db = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), dbname);
QSettings qs;
QStringList datapaths;
int i;
@@ -105,7 +105,7 @@ Database::Database() {
f.setPermissions(f.permissions() & ~(QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup | QFile::ReadOther | QFile::WriteOther | QFile::ExeOther));
}

QSqlQuery query;
QSqlQuery query(db);

execQueryAndLogFailure(query, QLatin1String("CREATE TABLE IF NOT EXISTS `servers` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT, `hostname` TEXT, `port` INTEGER DEFAULT " MUMTEXT(DEFAULT_MUMBLE_PORT) ", `username` TEXT, `password` TEXT)"));
query.exec(QLatin1String("ALTER TABLE `servers` ADD COLUMN `url` TEXT")); // Upgrade path, failing this query is not noteworthy
@@ -168,13 +168,13 @@ Database::Database() {
}

Database::~Database() {
QSqlQuery query;
QSqlQuery query(db);
execQueryAndLogFailure(query, QLatin1String("PRAGMA journal_mode = DELETE"));
execQueryAndLogFailure(query, QLatin1String("VACUUM"));
}

QList<FavoriteServer> Database::getFavorites() {
QSqlQuery query;
QSqlQuery query(db);
QList<FavoriteServer> ql;

query.prepare(QLatin1String("SELECT `name`, `hostname`, `port`, `username`, `password`, `url` FROM `servers` ORDER BY `name`"));
@@ -194,7 +194,7 @@ QList<FavoriteServer> Database::getFavorites() {
}

void Database::setFavorites(const QList<FavoriteServer> &servers) {
QSqlQuery query;
QSqlQuery query(db);
QSqlDatabase::database().transaction();

query.prepare(QLatin1String("DELETE FROM `servers`"));
@@ -215,7 +215,7 @@ void Database::setFavorites(const QList<FavoriteServer> &servers) {
}

bool Database::isLocalIgnored(const QString &hash) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("SELECT `hash` FROM `ignored` WHERE `hash` = ?"));
query.addBindValue(hash);
@@ -224,7 +224,7 @@ bool Database::isLocalIgnored(const QString &hash) {
}

void Database::setLocalIgnored(const QString &hash, bool ignored) {
QSqlQuery query;
QSqlQuery query(db);

if (ignored)
query.prepare(QLatin1String("INSERT INTO `ignored` (`hash`) VALUES (?)"));
@@ -235,7 +235,7 @@ void Database::setLocalIgnored(const QString &hash, bool ignored) {
}

bool Database::isLocalMuted(const QString &hash) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("SELECT `hash` FROM `muted` WHERE `hash` = ?"));
query.addBindValue(hash);
@@ -244,7 +244,7 @@ bool Database::isLocalMuted(const QString &hash) {
}

void Database::setUserLocalVolume(const QString &hash, float volume) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("INSERT OR REPLACE INTO `volume` (`hash`, `volume`) VALUES (?,?)"));
query.addBindValue(hash);
@@ -253,7 +253,7 @@ void Database::setUserLocalVolume(const QString &hash, float volume) {
}

float Database::getUserLocalVolume(const QString &hash) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("SELECT `volume` FROM `volume` WHERE `hash` = ?"));
query.addBindValue(hash);
@@ -265,7 +265,7 @@ float Database::getUserLocalVolume(const QString &hash) {
}

void Database::setLocalMuted(const QString &hash, bool muted) {
QSqlQuery query;
QSqlQuery query(db);

if (muted)
query.prepare(QLatin1String("INSERT INTO `muted` (`hash`) VALUES (?)"));
@@ -276,7 +276,7 @@ void Database::setLocalMuted(const QString &hash, bool muted) {
}

bool Database::isChannelFiltered(const QByteArray &server_cert_digest, const int channel_id) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("SELECT `channel_id` FROM `filtered_channels` WHERE `server_cert_digest` = ? AND `channel_id` = ?"));
query.addBindValue(server_cert_digest);
@@ -287,7 +287,7 @@ bool Database::isChannelFiltered(const QByteArray &server_cert_digest, const int
}

void Database::setChannelFiltered(const QByteArray &server_cert_digest, const int channel_id, const bool hidden) {
QSqlQuery query;
QSqlQuery query(db);

if (hidden)
query.prepare(QLatin1String("INSERT INTO `filtered_channels` (`server_cert_digest`, `channel_id`) VALUES (?, ?)"));
@@ -301,7 +301,7 @@ void Database::setChannelFiltered(const QByteArray &server_cert_digest, const in
}

QMap<UnresolvedServerAddress, unsigned int> Database::getPingCache() {
QSqlQuery query;
QSqlQuery query(db);
QMap<UnresolvedServerAddress, unsigned int> map;

query.prepare(QLatin1String("SELECT `hostname`, `port`, `ping` FROM `pingcache`"));
@@ -313,7 +313,7 @@ QMap<UnresolvedServerAddress, unsigned int> Database::getPingCache() {
}

void Database::setPingCache(const QMap<UnresolvedServerAddress, unsigned int> &map) {
QSqlQuery query;
QSqlQuery query(db);
QMap<UnresolvedServerAddress, unsigned int>::const_iterator i;

QSqlDatabase::database().transaction();
@@ -333,7 +333,7 @@ void Database::setPingCache(const QMap<UnresolvedServerAddress, unsigned int> &m
}

bool Database::seenComment(const QString &hash, const QByteArray &commenthash) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("SELECT COUNT(*) FROM `comments` WHERE `who` = ? AND `comment` = ?"));
query.addBindValue(hash);
@@ -352,7 +352,7 @@ bool Database::seenComment(const QString &hash, const QByteArray &commenthash) {
}

void Database::setSeenComment(const QString &hash, const QByteArray &commenthash) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("REPLACE INTO `comments` (`who`, `comment`, `seen`) VALUES (?, ?, datetime('now'))"));
query.addBindValue(hash);
@@ -361,7 +361,7 @@ void Database::setSeenComment(const QString &hash, const QByteArray &commenthash
}

QByteArray Database::blob(const QByteArray &hash) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("SELECT `data` FROM `blobs` WHERE `hash` = ?"));
query.addBindValue(hash);
@@ -382,7 +382,7 @@ void Database::setBlob(const QByteArray &hash, const QByteArray &data) {
if (hash.isEmpty() || data.isEmpty())
return;

QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("REPLACE INTO `blobs` (`hash`, `data`, `seen`) VALUES (?, ?, datetime('now'))"));
query.addBindValue(hash);
@@ -392,7 +392,7 @@ void Database::setBlob(const QByteArray &hash, const QByteArray &data) {

QStringList Database::getTokens(const QByteArray &digest) {
QList<QString> qsl;
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("SELECT `token` FROM `tokens` WHERE `digest` = ?"));
query.addBindValue(digest);
@@ -404,7 +404,7 @@ QStringList Database::getTokens(const QByteArray &digest) {
}

void Database::setTokens(const QByteArray &digest, QStringList &tokens) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("DELETE FROM `tokens` WHERE `digest` = ?"));
query.addBindValue(digest);
@@ -420,7 +420,7 @@ void Database::setTokens(const QByteArray &digest, QStringList &tokens) {

QList<Shortcut> Database::getShortcuts(const QByteArray &digest) {
QList<Shortcut> ql;
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("SELECT `shortcut`,`target`,`suppress` FROM `shortcut` WHERE `digest` = ?"));
query.addBindValue(digest);
@@ -451,7 +451,7 @@ QList<Shortcut> Database::getShortcuts(const QByteArray &digest) {
}

bool Database::setShortcuts(const QByteArray &digest, QList<Shortcut> &shortcuts) {
QSqlQuery query;
QSqlQuery query(db);
bool updated = false;

query.prepare(QLatin1String("DELETE FROM `shortcut` WHERE `digest` = ?"));
@@ -493,7 +493,7 @@ bool Database::setShortcuts(const QByteArray &digest, QList<Shortcut> &shortcuts

const QMap<QString, QString> Database::getFriends() {
QMap<QString, QString> qm;
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("SELECT `name`, `hash` FROM `friends`"));
execQueryAndLogFailure(query);
@@ -503,7 +503,7 @@ const QMap<QString, QString> Database::getFriends() {
}

const QString Database::getFriend(const QString &hash) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("SELECT `name` FROM `friends` WHERE `hash` = ?"));
query.addBindValue(hash);
@@ -514,7 +514,7 @@ const QString Database::getFriend(const QString &hash) {
}

void Database::addFriend(const QString &name, const QString &hash) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("REPLACE INTO `friends` (`name`, `hash`) VALUES (?,?)"));
query.addBindValue(name);
@@ -523,15 +523,15 @@ void Database::addFriend(const QString &name, const QString &hash) {
}

void Database::removeFriend(const QString &hash) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("DELETE FROM `friends` WHERE `hash` = ?"));
query.addBindValue(hash);
execQueryAndLogFailure(query);
}

const QString Database::getDigest(const QString &hostname, unsigned short port) {
QSqlQuery query;
QSqlQuery query(db);

query.prepare(QLatin1String("SELECT `digest` FROM `cert` WHERE `hostname` = ? AND `port` = ?"));
query.addBindValue(hostname);
@@ -544,7 +544,7 @@ const QString Database::getDigest(const QString &hostname, unsigned short port)
}

void Database::setDigest(const QString &hostname, unsigned short port, const QString &digest) {
QSqlQuery query;
QSqlQuery query(db);
query.prepare(QLatin1String("REPLACE INTO `cert` (`hostname`,`port`,`digest`) VALUES (?,?,?)"));
query.addBindValue(hostname);
query.addBindValue(port);
@@ -553,7 +553,7 @@ void Database::setDigest(const QString &hostname, unsigned short port, const QSt
}

void Database::setPassword(const QString &hostname, unsigned short port, const QString &uname, const QString &pw) {
QSqlQuery query;
QSqlQuery query(db);
query.prepare(QLatin1String("UPDATE `servers` SET `password` = ? WHERE `hostname` = ? AND `port` = ? AND `username` = ?"));
query.addBindValue(pw);
query.addBindValue(hostname);
@@ -563,7 +563,7 @@ void Database::setPassword(const QString &hostname, unsigned short port, const Q
}

bool Database::getUdp(const QByteArray &digest) {
QSqlQuery query;
QSqlQuery query(db);
query.prepare(QLatin1String("SELECT COUNT(*) FROM `udp` WHERE `digest` = ? "));
query.addBindValue(digest);
execQueryAndLogFailure(query);
@@ -574,7 +574,7 @@ bool Database::getUdp(const QByteArray &digest) {
}

void Database::setUdp(const QByteArray &digest, bool udp) {
QSqlQuery query;
QSqlQuery query(db);
if (! udp)
query.prepare(QLatin1String("REPLACE INTO `udp` (`digest`) VALUES (?)"));
else
@@ -584,7 +584,7 @@ void Database::setUdp(const QByteArray &digest, bool udp) {
}

bool Database::fuzzyMatch(QString &name, QString &user, QString &pw, QString &hostname, unsigned short port) {
QSqlQuery query;
QSqlQuery query(db);
if (! user.isEmpty()) {
query.prepare(QLatin1String("SELECT `username`, `password`, `hostname`, `name` FROM `servers` WHERE `username` LIKE ? AND `hostname` LIKE ? AND `port`=?"));
query.addBindValue(user);

0 comments on commit 6195761

Please sign in to comment.
You can’t perform that action at this time.