Verified Commit e6292d8d authored by Linus Jahn's avatar Linus Jahn 🔌
Browse files

Add using namespace SqlUtils in DB classes

parent b8dd8630
Pipeline #83992 passed with stage
in 2 minutes and 49 seconds
......@@ -47,6 +47,8 @@
#include "Kaidan.h"
using namespace SqlUtils;
#define DATABASE_CONVERT_TO_VERSION(n) \
if (d->version < n) { \
convertDatabaseToV##n(); \
......@@ -243,7 +245,7 @@ void Database::loadDatabaseInfo()
}
QSqlQuery query(db);
SqlUtils::execQuery(query, "SELECT version FROM " DB_TABLE_INFO);
execQuery(query, "SELECT version FROM " DB_TABLE_INFO);
QSqlRecord record = query.record();
int versionCol = record.indexOf("version");
......@@ -260,11 +262,11 @@ void Database::saveDatabaseInfo()
}
QSqlRecord updateRecord;
updateRecord.append(SqlUtils::createSqlField("version", d->version));
updateRecord.append(createSqlField("version", d->version));
auto db = currentDatabase();
QSqlQuery query(db);
SqlUtils::execQuery(
execQuery(
query,
db.driver()->sqlStatement(
QSqlDriver::UpdateStatement,
......@@ -318,7 +320,7 @@ void Database::createDbInfoTable()
{
auto db = currentDatabase();
QSqlQuery query(db);
SqlUtils::execQuery(
execQuery(
query,
SQL_CREATE_TABLE(
DB_TABLE_INFO,
......@@ -327,8 +329,8 @@ void Database::createDbInfoTable()
);
QSqlRecord insertRecord;
insertRecord.append(SqlUtils::createSqlField("version", DATABASE_LATEST_VERSION));
SqlUtils::execQuery(
insertRecord.append(createSqlField("version", DATABASE_LATEST_VERSION));
execQuery(
query,
db.driver()->sqlStatement(
QSqlDriver::InsertStatement,
......@@ -344,7 +346,7 @@ void Database::createRosterTable()
// TODO: remove lastExchanged and lastMessage
QSqlQuery query(currentDatabase());
SqlUtils::execQuery(
execQuery(
query,
SQL_CREATE_TABLE(
DB_TABLE_ROSTER,
......@@ -366,7 +368,7 @@ void Database::createMessagesTable()
// * remove columns isSent, isDelivered
QSqlQuery query(currentDatabase());
SqlUtils::execQuery(
execQuery(
query,
SQL_CREATE_TABLE(
DB_TABLE_MESSAGES,
......@@ -412,7 +414,7 @@ void Database::convertDatabaseToV3()
{
DATABASE_CONVERT_TO_VERSION(2);
QSqlQuery query(currentDatabase());
SqlUtils::execQuery(query, "ALTER TABLE Roster ADD avatarHash " SQL_TEXT);
execQuery(query, "ALTER TABLE Roster ADD avatarHash " SQL_TEXT);
d->version = 3;
}
......@@ -423,17 +425,17 @@ void Database::convertDatabaseToV4()
// SQLite doesn't support the ALTER TABLE drop columns feature, so we have to use a workaround.
// we copy all rows into a back-up table (but without `avatarHash`), and then delete the old table
// and copy everything to the normal table again
SqlUtils::execQuery(query, "CREATE TEMPORARY TABLE roster_backup(jid, name, lastExchanged,"
execQuery(query, "CREATE TEMPORARY TABLE roster_backup(jid, name, lastExchanged,"
"unreadMessages, lastMessage, lastOnline, activity, status, mood)");
SqlUtils::execQuery(query, "INSERT INTO roster_backup SELECT jid, name, lastExchanged, unreadMessages,"
execQuery(query, "INSERT INTO roster_backup SELECT jid, name, lastExchanged, unreadMessages,"
"lastMessage, lastOnline, activity, status, mood FROM " DB_TABLE_ROSTER);
SqlUtils::execQuery(query, "DROP TABLE Roster");
SqlUtils::execQuery(query, "CREATE TABLE Roster (jid " SQL_TEXT_NOT_NULL ", name " SQL_TEXT_NOT_NULL ","
execQuery(query, "DROP TABLE Roster");
execQuery(query, "CREATE TABLE Roster (jid " SQL_TEXT_NOT_NULL ", name " SQL_TEXT_NOT_NULL ","
"lastExchanged " SQL_TEXT_NOT_NULL ", unreadMessages " SQL_INTEGER ", lastMessage " SQL_TEXT ","
"lastOnline " SQL_TEXT ", activity " SQL_TEXT ", status " SQL_TEXT ", mood " SQL_TEXT ")");
SqlUtils::execQuery(query, "INSERT INTO Roster SELECT jid, name, lastExchanged, unreadMessages,"
execQuery(query, "INSERT INTO Roster SELECT jid, name, lastExchanged, unreadMessages,"
"lastMessage, lastOnline, activity, status, mood FROM Roster_backup");
SqlUtils::execQuery(query, "DROP TABLE Roster_backup");
execQuery(query, "DROP TABLE Roster_backup");
d->version = 4;
}
......@@ -441,9 +443,9 @@ void Database::convertDatabaseToV5()
{
DATABASE_CONVERT_TO_VERSION(4);
QSqlQuery query(currentDatabase());
SqlUtils::execQuery(query, "ALTER TABLE Messages ADD type " SQL_INTEGER);
SqlUtils::execQuery(query, "UPDATE Messages SET type = 0 WHERE type IS NULL");
SqlUtils::execQuery(query, "ALTER TABLE Messages ADD mediaUrl " SQL_TEXT);
execQuery(query, "ALTER TABLE Messages ADD type " SQL_INTEGER);
execQuery(query, "UPDATE Messages SET type = 0 WHERE type IS NULL");
execQuery(query, "ALTER TABLE Messages ADD mediaUrl " SQL_TEXT);
d->version = 5;
}
......@@ -455,7 +457,7 @@ void Database::convertDatabaseToV6()
"mediaContentType " SQL_TEXT,
"mediaLastModified " SQL_INTEGER,
"mediaLocation " SQL_TEXT }) {
SqlUtils::execQuery(query, QString("ALTER TABLE Messages ADD ").append(column));
execQuery(query, QString("ALTER TABLE Messages ADD ").append(column));
}
d->version = 6;
}
......@@ -464,8 +466,8 @@ void Database::convertDatabaseToV7()
{
DATABASE_CONVERT_TO_VERSION(6);
QSqlQuery query(currentDatabase());
SqlUtils::execQuery(query, "ALTER TABLE Messages ADD mediaThumb " SQL_BLOB);
SqlUtils::execQuery(query, "ALTER TABLE Messages ADD mediaHashes " SQL_TEXT);
execQuery(query, "ALTER TABLE Messages ADD mediaThumb " SQL_BLOB);
execQuery(query, "ALTER TABLE Messages ADD mediaHashes " SQL_TEXT);
d->version = 7;
}
......@@ -473,10 +475,10 @@ void Database::convertDatabaseToV8()
{
DATABASE_CONVERT_TO_VERSION(7);
QSqlQuery query(currentDatabase());
SqlUtils::execQuery(query, "CREATE TEMPORARY TABLE roster_backup(jid, name, lastExchanged, unreadMessages, lastMessage)");
SqlUtils::execQuery(query, "INSERT INTO roster_backup SELECT jid, name, lastExchanged, unreadMessages, lastMessage FROM Roster");
SqlUtils::execQuery(query, "DROP TABLE Roster");
SqlUtils::execQuery(
execQuery(query, "CREATE TEMPORARY TABLE roster_backup(jid, name, lastExchanged, unreadMessages, lastMessage)");
execQuery(query, "INSERT INTO roster_backup SELECT jid, name, lastExchanged, unreadMessages, lastMessage FROM Roster");
execQuery(query, "DROP TABLE Roster");
execQuery(
query,
SQL_CREATE_TABLE(
"Roster",
......@@ -488,8 +490,8 @@ void Database::convertDatabaseToV8()
)
);
SqlUtils::execQuery(query, "INSERT INTO Roster SELECT jid, name, lastExchanged, unreadMessages, lastMessage FROM Roster_backup");
SqlUtils::execQuery(query, "DROP TABLE roster_backup");
execQuery(query, "INSERT INTO Roster SELECT jid, name, lastExchanged, unreadMessages, lastMessage FROM Roster_backup");
execQuery(query, "DROP TABLE roster_backup");
d->version = 8;
}
......@@ -497,7 +499,7 @@ void Database::convertDatabaseToV9()
{
DATABASE_CONVERT_TO_VERSION(8);
QSqlQuery query(currentDatabase());
SqlUtils::execQuery(query, "ALTER TABLE Messages ADD edited " SQL_BOOL);
execQuery(query, "ALTER TABLE Messages ADD edited " SQL_BOOL);
d->version = 9;
}
......@@ -505,8 +507,8 @@ void Database::convertDatabaseToV10()
{
DATABASE_CONVERT_TO_VERSION(9);
QSqlQuery query(currentDatabase());
SqlUtils::execQuery(query, "ALTER TABLE Messages ADD isSpoiler " SQL_BOOL);
SqlUtils::execQuery(query, "ALTER TABLE Messages ADD spoilerHint " SQL_TEXT);
execQuery(query, "ALTER TABLE Messages ADD isSpoiler " SQL_BOOL);
execQuery(query, "ALTER TABLE Messages ADD spoilerHint " SQL_TEXT);
d->version = 10;
}
......@@ -514,10 +516,10 @@ void Database::convertDatabaseToV11()
{
DATABASE_CONVERT_TO_VERSION(10);
QSqlQuery query(currentDatabase());
SqlUtils::execQuery(query, "ALTER TABLE Messages ADD deliveryState " SQL_INTEGER);
SqlUtils::execQuery(query, "UPDATE Messages SET deliveryState = 2 WHERE isDelivered = 1");
SqlUtils::execQuery(query, "UPDATE Messages SET deliveryState = 1 WHERE deliveryState IS NULL");
SqlUtils::execQuery(query, "ALTER TABLE Messages ADD errorText " SQL_TEXT);
execQuery(query, "ALTER TABLE Messages ADD deliveryState " SQL_INTEGER);
execQuery(query, "UPDATE Messages SET deliveryState = 2 WHERE isDelivered = 1");
execQuery(query, "UPDATE Messages SET deliveryState = 1 WHERE deliveryState IS NULL");
execQuery(query, "ALTER TABLE Messages ADD errorText " SQL_TEXT);
d->version = 11;
}
......@@ -525,7 +527,7 @@ void Database::convertDatabaseToV12()
{
DATABASE_CONVERT_TO_VERSION(11);
QSqlQuery query(currentDatabase());
SqlUtils::execQuery(query, "ALTER TABLE Messages ADD replaceId " SQL_TEXT);
execQuery(query, "ALTER TABLE Messages ADD replaceId " SQL_TEXT);
d->version = 12;
}
......@@ -533,7 +535,7 @@ void Database::convertDatabaseToV13()
{
DATABASE_CONVERT_TO_VERSION(12);
QSqlQuery query(currentDatabase());
SqlUtils::execQuery(query, "ALTER TABLE Messages ADD stanzaId " SQL_TEXT);
SqlUtils::execQuery(query, "ALTER TABLE Messages ADD originId " SQL_TEXT);
execQuery(query, "ALTER TABLE Messages ADD stanzaId " SQL_TEXT);
execQuery(query, "ALTER TABLE Messages ADD originId " SQL_TEXT);
d->version = 13;
}
......@@ -42,6 +42,8 @@
#include "Globals.h"
#include "SqlUtils.h"
using namespace SqlUtils;
#define CHECK_MESSAGE_EXISTS_DEPTH_LIMIT "20"
MessageDb *MessageDb::s_instance = nullptr;
......@@ -123,60 +125,60 @@ QSqlRecord MessageDb::createUpdateRecord(const Message &oldMsg, const Message &n
QSqlRecord rec;
if (oldMsg.from() != newMsg.from())
rec.append(SqlUtils::createSqlField("author", newMsg.from()));
rec.append(createSqlField("author", newMsg.from()));
if (oldMsg.to() != newMsg.to())
rec.append(SqlUtils::createSqlField("recipient", newMsg.to()));
rec.append(createSqlField("recipient", newMsg.to()));
if (oldMsg.stamp() != newMsg.stamp())
rec.append(SqlUtils::createSqlField(
rec.append(createSqlField(
"timestamp",
newMsg.stamp().toString(Qt::ISODate)
));
if (oldMsg.id() != newMsg.id()) {
// TODO: remove as soon as 'NOT NULL' was removed from id column
if (newMsg.id().isEmpty())
rec.append(SqlUtils::createSqlField("id", QStringLiteral(" ")));
rec.append(createSqlField("id", QStringLiteral(" ")));
else
rec.append(SqlUtils::createSqlField("id", newMsg.id()));
rec.append(createSqlField("id", newMsg.id()));
}
if (oldMsg.body() != newMsg.body())
rec.append(SqlUtils::createSqlField("message", newMsg.body()));
rec.append(createSqlField("message", newMsg.body()));
if (oldMsg.deliveryState() != newMsg.deliveryState())
rec.append(SqlUtils::createSqlField("deliveryState", int(newMsg.deliveryState())));
rec.append(createSqlField("deliveryState", int(newMsg.deliveryState())));
if (oldMsg.errorText() != newMsg.errorText())
rec.append(SqlUtils::createSqlField("errorText", newMsg.errorText()));
rec.append(createSqlField("errorText", newMsg.errorText()));
if (oldMsg.mediaType() != newMsg.mediaType())
rec.append(SqlUtils::createSqlField("type", int(newMsg.mediaType())));
rec.append(createSqlField("type", int(newMsg.mediaType())));
if (oldMsg.outOfBandUrl() != newMsg.outOfBandUrl())
rec.append(SqlUtils::createSqlField("mediaUrl", newMsg.outOfBandUrl()));
rec.append(createSqlField("mediaUrl", newMsg.outOfBandUrl()));
if (oldMsg.mediaContentType() != newMsg.mediaContentType())
rec.append(SqlUtils::createSqlField(
rec.append(createSqlField(
"mediaContentType",
newMsg.mediaContentType()
));
if (oldMsg.mediaLocation() != newMsg.mediaLocation())
rec.append(SqlUtils::createSqlField(
rec.append(createSqlField(
"mediaLocation",
newMsg.mediaLocation()
));
if (oldMsg.mediaSize() != newMsg.mediaSize())
rec.append(SqlUtils::createSqlField("mediaSize", newMsg.mediaSize()));
rec.append(createSqlField("mediaSize", newMsg.mediaSize()));
if (oldMsg.mediaLastModified() != newMsg.mediaLastModified())
rec.append(SqlUtils::createSqlField(
rec.append(createSqlField(
"mediaLastModified",
newMsg.mediaLastModified().toMSecsSinceEpoch()
));
if (oldMsg.isEdited() != newMsg.isEdited())
rec.append(SqlUtils::createSqlField("edited", newMsg.isEdited()));
rec.append(createSqlField("edited", newMsg.isEdited()));
if (oldMsg.spoilerHint() != newMsg.spoilerHint())
rec.append(SqlUtils::createSqlField("spoilerHint", newMsg.spoilerHint()));
rec.append(createSqlField("spoilerHint", newMsg.spoilerHint()));
if (oldMsg.isSpoiler() != newMsg.isSpoiler())
rec.append(SqlUtils::createSqlField("isSpoiler", newMsg.isSpoiler()));
rec.append(createSqlField("isSpoiler", newMsg.isSpoiler()));
if (oldMsg.replaceId() != newMsg.replaceId())
rec.append(SqlUtils::createSqlField("replaceId", newMsg.replaceId()));
rec.append(createSqlField("replaceId", newMsg.replaceId()));
if (oldMsg.originId() != newMsg.originId())
rec.append(SqlUtils::createSqlField("originId", newMsg.originId()));
rec.append(createSqlField("originId", newMsg.originId()));
if (oldMsg.stanzaId() != newMsg.stanzaId())
rec.append(SqlUtils::createSqlField("stanzaId", newMsg.stanzaId()));
rec.append(createSqlField("stanzaId", newMsg.stanzaId()));
return rec;
}
......@@ -193,7 +195,7 @@ QFuture<QVector<Message>> MessageDb::fetchMessages(const QString &user1, const Q
{":limit", DB_QUERY_LIMIT_MESSAGES},
};
SqlUtils::execQuery(
execQuery(
query,
"SELECT * FROM " DB_TABLE_MESSAGES " "
"WHERE (author = :user1 AND recipient = :user2) OR "
......@@ -220,7 +222,7 @@ Message MessageDb::_fetchLastMessage(const QString &user1, const QString &user2)
{ QStringLiteral(":user2"), user2 },
};
SqlUtils::execQuery(
execQuery(
query,
"SELECT * FROM " DB_TABLE_MESSAGES " "
"WHERE (author = :user1 AND recipient = :user2) OR "
......@@ -242,7 +244,7 @@ QFuture<QDateTime> MessageDb::fetchLastMessageStamp()
{
return run([this]() {
auto query = createQuery();
SqlUtils::execQuery(query, "SELECT timestamp FROM Messages ORDER BY timestamp DESC LIMIT 1");
execQuery(query, "SELECT timestamp FROM Messages ORDER BY timestamp DESC LIMIT 1");
QDateTime stamp;
while (query.next()) {
......@@ -301,7 +303,7 @@ QFuture<void> MessageDb::addMessage(const Message &msg, MessageOrigin origin)
record.setValue("stanzaId", msg.stanzaId());
auto query = createQuery();
SqlUtils::execQuery(query, sqlDriver().sqlStatement(
execQuery(query, sqlDriver().sqlStatement(
QSqlDriver::InsertStatement,
DB_TABLE_MESSAGES,
record,
......@@ -314,7 +316,7 @@ QFuture<void> MessageDb::removeMessages(const QString &, const QString &)
{
return run([this]() {
auto query = createQuery();
SqlUtils::execQuery(query, "DELETE FROM " DB_TABLE_MESSAGES);
execQuery(query, "DELETE FROM " DB_TABLE_MESSAGES);
});
}
......@@ -324,7 +326,7 @@ QFuture<void> MessageDb::updateMessage(const QString &id,
return run([this, id, updateMsg]() {
// load current message item from db
auto query = createQuery();
SqlUtils::execQuery(
execQuery(
query,
"SELECT * FROM " DB_TABLE_MESSAGES " WHERE id = ? LIMIT 1",
QVector<QVariant> { id }
......@@ -344,7 +346,7 @@ QFuture<void> MessageDb::updateMessage(const QString &id,
QSqlRecord rec = createUpdateRecord(msgs.first(), msg);
auto &driver = sqlDriver();
SqlUtils::execQuery(
execQuery(
query,
driver.sqlStatement(
QSqlDriver::UpdateStatement,
......@@ -352,7 +354,7 @@ QFuture<void> MessageDb::updateMessage(const QString &id,
rec,
false
) +
SqlUtils::simpleWhereStatement(&driver, "id", id)
simpleWhereStatement(&driver, "id", id)
);
}
}
......@@ -397,7 +399,7 @@ bool MessageDb::_checkMessageExists(const Message &message)
QStringLiteral(")) ORDER BY timestamp DESC LIMIT " CHECK_MESSAGE_EXISTS_DEPTH_LIMIT);
auto query = createQuery();
SqlUtils::execQuery(query, querySql, bindValues);
execQuery(query, querySql, bindValues);
int count = 0;
if (query.next()) {
......@@ -416,7 +418,7 @@ QFuture<QVector<Message>> MessageDb::fetchPendingMessages(const QString &userJid
{":deliveryState", int(Enums::DeliveryState::Pending)},
};
SqlUtils::execQuery(
execQuery(
query,
"SELECT * FROM " DB_TABLE_MESSAGES " "
"WHERE (author = :user AND deliveryState = :deliveryState) "
......
......@@ -41,6 +41,8 @@
#include <QSqlQuery>
#include <QSqlRecord>
using namespace SqlUtils;
RosterDb *RosterDb::s_instance = nullptr;
RosterDb::RosterDb(Database *db, QObject *parent)
......@@ -81,11 +83,11 @@ QSqlRecord RosterDb::createUpdateRecord(const RosterItem &oldItem, const RosterI
{
QSqlRecord rec;
if (oldItem.jid() != newItem.jid())
rec.append(SqlUtils::createSqlField("jid", newItem.jid()));
rec.append(createSqlField("jid", newItem.jid()));
if (oldItem.name() != newItem.name())
rec.append(SqlUtils::createSqlField("name", newItem.name()));
rec.append(createSqlField("name", newItem.name()));
if (oldItem.unreadMessages() != newItem.unreadMessages())
rec.append(SqlUtils::createSqlField(
rec.append(createSqlField(
"unreadMessages",
newItem.unreadMessages()
));
......@@ -103,7 +105,7 @@ QFuture<void> RosterDb::addItems(const QVector<RosterItem> &items)
auto query = createQuery();
transaction();
SqlUtils::prepareQuery(query, sqlDriver().sqlStatement(
prepareQuery(query, sqlDriver().sqlStatement(
QSqlDriver::InsertStatement,
DB_TABLE_ROSTER,
sqlRecord(DB_TABLE_ROSTER),
......@@ -116,7 +118,7 @@ QFuture<void> RosterDb::addItems(const QVector<RosterItem> &items)
query.addBindValue(QLatin1String("")); // lastExchanged (NOT NULL)
query.addBindValue(item.unreadMessages());
query.addBindValue(QString()); // lastMessage
SqlUtils::execQuery(query);
execQuery(query);
}
commit();
......@@ -129,7 +131,7 @@ QFuture<void> RosterDb::updateItem(const QString &jid,
return run([this, jid, updateItem]() {
// load current roster item from db
auto query = createQuery();
SqlUtils::execQuery(
execQuery(
query,
"SELECT * FROM Roster WHERE jid = ? LIMIT 1",
QVector<QVariant>() << jid
......@@ -162,7 +164,7 @@ QFuture<void> RosterDb::replaceItems(const QHash<QString, RosterItem> &items)
return run([this, items]() {
// load current items
auto query = createQuery();
SqlUtils::execQuery(query, "SELECT * FROM Roster");
execQuery(query, "SELECT * FROM Roster");
QVector<RosterItem> currentItems;
parseItemsFromQuery(query, currentItems);
......@@ -204,7 +206,7 @@ QFuture<void> RosterDb::removeItems(const QString &, const QString &)
{
return run([this]() {
auto query = createQuery();
SqlUtils::execQuery(query, "DELETE FROM Roster");
execQuery(query, "DELETE FROM Roster");
});
}
......@@ -215,9 +217,9 @@ QFuture<void> RosterDb::setItemName(const QString &jid, const QString &name)
auto &driver = sqlDriver();
QSqlRecord rec;
rec.append(SqlUtils::createSqlField("name", name));
rec.append(createSqlField("name", name));
SqlUtils::execQuery(
execQuery(
query,
driver.sqlStatement(
QSqlDriver::UpdateStatement,
......@@ -225,7 +227,7 @@ QFuture<void> RosterDb::setItemName(const QString &jid, const QString &name)
rec,
false
) +
SqlUtils::simpleWhereStatement(&driver, "jid", jid)
simpleWhereStatement(&driver, "jid", jid)
);
});
}
......@@ -234,7 +236,7 @@ QFuture<QVector<RosterItem>> RosterDb::fetchItems(const QString &accountId)
{
return run([this, accountId]() {
auto query = createQuery();
SqlUtils::execQuery(query, "SELECT * FROM Roster");
execQuery(query, "SELECT * FROM Roster");
QVector<RosterItem> items;
parseItemsFromQuery(query, items);
......@@ -258,7 +260,7 @@ void RosterDb::updateItemByRecord(const QString &jid, const QSqlRecord &record)
{ "jid", jid }
};
SqlUtils::execQuery(
execQuery(
query,
driver.sqlStatement(
QSqlDriver::UpdateStatement,
......@@ -266,6 +268,6 @@ void RosterDb::updateItemByRecord(const QString &jid, const QSqlRecord &record)
record,
false
) +
SqlUtils::simpleWhereStatement(&driver, keyValuePairs)
simpleWhereStatement(&driver, keyValuePairs)
);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment