| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "database/databasequeries.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "definitions/globals.h" |
| 7 | #include "exceptions/applicationexception.h" |
| 8 | #include "miscellaneous/application.h" |
| 9 | #include "miscellaneous/iconfactory.h" |
| 10 | #include "miscellaneous/settings.h" |
| 11 | #include "services/abstract/category.h" |
| 12 | |
| 13 | #include <QSqlDriver> |
| 14 | #include <QUrl> |
| 15 | #include <QVariant> |
| 16 | |
| 17 | QMap<int, QString> DatabaseQueries::messageTableAttributes(bool only_msg_table, bool is_sqlite) { |
| 18 | QMap<int, QString> field_names; |
| 19 | |
| 20 | field_names[MSG_DB_ID_INDEX] = QSL("Messages.id"); |
| 21 | field_names[MSG_DB_READ_INDEX] = QSL("Messages.is_read"); |
| 22 | field_names[MSG_DB_IMPORTANT_INDEX] = QSL("Messages.is_important"); |
| 23 | field_names[MSG_DB_DELETED_INDEX] = QSL("Messages.is_deleted"); |
| 24 | field_names[MSG_DB_PDELETED_INDEX] = QSL("Messages.is_pdeleted"); |
| 25 | field_names[MSG_DB_FEED_CUSTOM_ID_INDEX] = QSL("Messages.feed"); |
| 26 | field_names[MSG_DB_TITLE_INDEX] = QSL("Messages.title"); |
| 27 | field_names[MSG_DB_URL_INDEX] = QSL("Messages.url"); |
| 28 | field_names[MSG_DB_AUTHOR_INDEX] = QSL("Messages.author"); |
| 29 | field_names[MSG_DB_DCREATED_INDEX] = QSL("Messages.date_created"); |
| 30 | field_names[MSG_DB_CONTENTS_INDEX] = QSL("Messages.contents"); |
| 31 | field_names[MSG_DB_ENCLOSURES_INDEX] = QSL("Messages.enclosures"); |
| 32 | field_names[MSG_DB_SCORE_INDEX] = QSL("Messages.score"); |
| 33 | field_names[MSG_DB_ACCOUNT_ID_INDEX] = QSL("Messages.account_id"); |
| 34 | field_names[MSG_DB_CUSTOM_ID_INDEX] = QSL("Messages.custom_id"); |
| 35 | field_names[MSG_DB_CUSTOM_HASH_INDEX] = QSL("Messages.custom_hash"); |
| 36 | field_names[MSG_DB_FEED_TITLE_INDEX] = only_msg_table ? QSL("Messages.feed") : QSL("Feeds.title"); |
| 37 | field_names[MSG_DB_FEED_IS_RTL_INDEX] = only_msg_table ? QSL("0") : QSL("Feeds.is_rtl"); |
| 38 | field_names[MSG_DB_HAS_ENCLOSURES] = QSL("CASE WHEN LENGTH(Messages.enclosures) > 10 " |
| 39 | "THEN 'true' " |
| 40 | "ELSE 'false' " |
| 41 | "END AS has_enclosures"); |
| 42 | |
| 43 | if (is_sqlite) { |
| 44 | field_names[MSG_DB_LABELS] = QSL("(SELECT GROUP_CONCAT(Labels.name) FROM Labels WHERE Messages.labels LIKE '%.' || " |
| 45 | "Labels.custom_id || '.%') as msg_labels"); |
| 46 | } |
| 47 | else { |
| 48 | field_names[MSG_DB_LABELS] = |
| 49 | QSL("(SELECT GROUP_CONCAT(Labels.name) FROM Labels WHERE Messages.labels LIKE CONCAT('%.', " |
| 50 | "Labels.custom_id, '.%')) as msg_labels"); |
| 51 | } |
| 52 | |
| 53 | field_names[MSG_DB_LABELS_IDS] = QSL("Messages.labels"); |
| 54 | |
| 55 | // TODO: zpomaluje zobrazenà seznamu zpráv |
| 56 | /* |
| 57 | field_names[MSG_DB_LABELS] = |
| 58 | QSL("(SELECT GROUP_CONCAT(Labels.name) FROM Labels WHERE Labels.custom_id IN (SELECT " |
| 59 | "LabelsInMessages.label FROM LabelsInMessages WHERE LabelsInMessages.account_id = " |
| 60 | "Messages.account_id AND LabelsInMessages.message = Messages.custom_id)) as msg_labels"); |
| 61 | */ |
| 62 | |
| 63 | return field_names; |
| 64 | } |
| 65 | |
| 66 | QString DatabaseQueries::serializeCustomData(const QVariantHash& data) { |
| 67 | if (!data.isEmpty()) { |
| 68 | return QString::fromUtf8(QJsonDocument::fromVariant(data).toJson(QJsonDocument::JsonFormat::Indented)); |
| 69 | } |
| 70 | else { |
| 71 | return QString(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | QVariantHash DatabaseQueries::deserializeCustomData(const QString& data) { |
| 76 | if (data.isEmpty()) { |
| 77 | return QVariantHash(); |
| 78 | } |
| 79 | else { |
| 80 | auto json = QJsonDocument::fromJson(data.toUtf8()); |
| 81 | |
| 82 | return json.object().toVariantHash(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | bool DatabaseQueries::isLabelAssignedToMessage(const QSqlDatabase& db, Label* label, const Message& msg) { |
| 87 | QSqlQuery q(db); |
| 88 | |
| 89 | q.setForwardOnly(true); |
| 90 | q.prepare(QSL("SELECT COUNT(*) FROM Messages " |
| 91 | "WHERE " |
| 92 | " Messages.labels LIKE :label AND " |
| 93 | " Messages.custom_id = :message AND " |
| 94 | " account_id = :account_id;")); |
| 95 | q.bindValue(QSL(":label"), QSL("%.%1.%").arg(label->customId())); |
| 96 | q.bindValue(QSL(":message"), msg.m_customId); |
| 97 | q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId()); |
| 98 | |
| 99 | q.exec() && q.next(); |
| 100 | |
| 101 | return q.record().value(0).toInt() > 0; |
| 102 | } |
| 103 | |
| 104 | bool DatabaseQueries::deassignLabelFromMessage(const QSqlDatabase& db, Label* label, const Message& msg) { |
| 105 | QSqlQuery q(db); |
| 106 | |
| 107 | q.setForwardOnly(true); |
| 108 | q.prepare(QSL("UPDATE Messages " |
| 109 | "SET labels = REPLACE(Messages.labels, :label, \".\") " |
| 110 | "WHERE Messages.custom_id = :message AND account_id = :account_id;")); |
| 111 | q.bindValue(QSL(":label"), QSL(".%1.").arg(label->customId())); |
| 112 | q.bindValue(QSL(":message"), msg.m_customId.isEmpty() ? QString::number(msg.m_id) : msg.m_customId); |
| 113 | q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId()); |
| 114 | |
| 115 | return q.exec(); |
| 116 | } |
| 117 | |
| 118 | bool DatabaseQueries::assignLabelToMessage(const QSqlDatabase& db, Label* label, const Message& msg) { |
| 119 | deassignLabelFromMessage(db, label, msg); |
| 120 | |
| 121 | QSqlQuery q(db); |
| 122 | |
| 123 | q.setForwardOnly(true); |
| 124 | |
| 125 | if (db.driverName() == QSL(APP_DB_MYSQL_DRIVER)) { |
| 126 | q.prepare(QSL("UPDATE Messages " |
| 127 | "SET labels = CONCAT(Messages.labels, :label) " |
| 128 | "WHERE Messages.custom_id = :message AND account_id = :account_id;")); |
| 129 | } |
| 130 | else { |
| 131 | q.prepare(QSL("UPDATE Messages " |
| 132 | "SET labels = Messages.labels || :label " |
| 133 | "WHERE Messages.custom_id = :message AND account_id = :account_id;")); |
| 134 | } |
| 135 | |
| 136 | q.bindValue(QSL(":label"), QSL("%1.").arg(label->customId())); |
| 137 | q.bindValue(QSL(":message"), msg.m_customId.isEmpty() ? QString::number(msg.m_id) : msg.m_customId); |
| 138 | q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId()); |
| 139 | |
| 140 | return q.exec(); |
| 141 | } |
| 142 | |
| 143 | bool DatabaseQueries::setLabelsForMessage(const QSqlDatabase& db, const QList<Label*>& labels, const Message& msg) { |
| 144 | QSqlQuery q(db); |
| 145 | |
| 146 | auto std_lbls = boolinq::from(labels) |
| 147 | .select([](Label* lbl) { |
| 148 | return lbl->customId(); |
| 149 | }) |
| 150 | .toStdList(); |
| 151 | |
| 152 | QStringList lbls = FROM_STD_LIST(QStringList, std_lbls); |
| 153 | QString lblss = QSL(".") + lbls.join('.') + QSL("."); |
| 154 | |
| 155 | q.setForwardOnly(true); |
| 156 | q.prepare(QSL("UPDATE Messages " |
| 157 | "SET labels = :labels " |
| 158 | "WHERE Messages.custom_id = :message AND account_id = :account_id;")); |
| 159 | q.bindValue(QSL(":labels"), lblss); |
| 160 | q.bindValue(QSL(":message"), msg.m_customId.isEmpty() ? QString::number(msg.m_id) : msg.m_customId); |
| 161 | q.bindValue(QSL(":account_id"), msg.m_accountId); |
| 162 | |
| 163 | return q.exec(); |
| 164 | } |
| 165 | |
| 166 | QList<Label*> DatabaseQueries::getLabelsForAccount(const QSqlDatabase& db, int account_id) { |
| 167 | QList<Label*> labels; |
| 168 | QSqlQuery q(db); |
| 169 | |
| 170 | q.setForwardOnly(true); |
| 171 | q.prepare(QSL("SELECT * FROM Labels WHERE account_id = :account_id;")); |
| 172 | q.bindValue(QSL(":account_id"), account_id); |
| 173 | |
| 174 | if (q.exec()) { |
| 175 | while (q.next()) { |
| 176 | Label* lbl = new Label(q.value(QSL("name")).toString(), QColor(q.value(QSL("color")).toString())); |
| 177 | |
| 178 | lbl->setId(q.value(QSL("id")).toInt()); |
| 179 | lbl->setCustomId(q.value(QSL("custom_id")).toString()); |
| 180 | |
| 181 | labels << lbl; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return labels; |
| 186 | } |
| 187 | |
| 188 | QList<Label*> DatabaseQueries::getLabelsForMessage(const QSqlDatabase& db, |
| 189 | const Message& msg, |
| 190 | const QList<Label*>& installed_labels) { |
| 191 | QList<Label*> labels; |
| 192 | QSqlQuery q(db); |
| 193 | |
| 194 | q.setForwardOnly(true); |
| 195 | q.prepare(QSL("SELECT labels FROM Messages WHERE custom_id = :message AND account_id = :account_id;")); |
| 196 | |
| 197 | q.bindValue(QSL(":account_id"), msg.m_accountId); |
| 198 | q.bindValue(QSL(":message"), msg.m_customId.isEmpty() ? QString::number(msg.m_id) : msg.m_customId); |
| 199 | |
| 200 | if (q.exec() && q.next()) { |
| 201 | auto label_ids = q.value(0).toString().split('.', |
| 202 | #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0 |
| 203 | Qt::SplitBehaviorFlags::SkipEmptyParts); |
| 204 | #else |
| 205 | QString::SplitBehavior::SkipEmptyParts); |
| 206 | #endif |
| 207 | |
| 208 | auto iter = boolinq::from(installed_labels); |
| 209 | |
| 210 | for (const QString& lbl_id : label_ids) { |
| 211 | Label* candidate_label = iter.firstOrDefault([&](const Label* lbl) { |
| 212 | return lbl->customId() == lbl_id; |
| 213 | }); |
| 214 | |
| 215 | if (candidate_label != nullptr) { |
| 216 | labels << candidate_label; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return labels; |
| 222 | } |
| 223 | |
| 224 | bool DatabaseQueries::updateLabel(const QSqlDatabase& db, Label* label) { |
| 225 | QSqlQuery q(db); |
| 226 | |
| 227 | q.setForwardOnly(true); |
| 228 | q.prepare(QSL("UPDATE Labels SET name = :name, color = :color " |
| 229 | "WHERE id = :id AND account_id = :account_id;")); |
| 230 | q.bindValue(QSL(":name"), label->title()); |
| 231 | q.bindValue(QSL(":color"), label->color().name()); |
| 232 | q.bindValue(QSL(":id"), label->id()); |
| 233 | q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId()); |
| 234 | |
| 235 | return q.exec(); |
| 236 | } |
| 237 | |
| 238 | bool DatabaseQueries::deleteLabel(const QSqlDatabase& db, Label* label) { |
| 239 | // NOTE: All dependecies are done via SQL foreign cascaded keys, so no |
| 240 | // extra removals are needed. |
| 241 | QSqlQuery q(db); |
| 242 | |
| 243 | q.setForwardOnly(true); |
| 244 | q.prepare(QSL("DELETE FROM Labels WHERE id = :id AND account_id = :account_id;")); |
| 245 | q.bindValue(QSL(":id"), label->id()); |
| 246 | q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId()); |
| 247 | |
| 248 | if (q.exec()) { |
| 249 | q.prepare(QSL("UPDATE Messages " |
| 250 | "SET labels = REPLACE(Messages.labels, :label, \".\") " |
| 251 | "WHERE account_id = :account_id;")); |
| 252 | q.bindValue(QSL(":label"), QSL(".%1.").arg(label->customId())); |
| 253 | q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId()); |
| 254 | |
| 255 | return q.exec(); |
| 256 | } |
| 257 | else { |
| 258 | return false; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | bool DatabaseQueries::createLabel(const QSqlDatabase& db, Label* label, int account_id) { |
| 263 | QSqlQuery q(db); |
| 264 | |
| 265 | q.setForwardOnly(true); |
| 266 | q.prepare(QSL("INSERT INTO Labels (name, color, custom_id, account_id) " |
| 267 | "VALUES (:name, :color, :custom_id, :account_id);")); |
| 268 | q.bindValue(QSL(":name"), label->title()); |
| 269 | q.bindValue(QSL(":color"), label->color().name()); |
| 270 | q.bindValue(QSL(":custom_id"), label->customId()); |
| 271 | q.bindValue(QSL(":account_id"), account_id); |
| 272 | |
| 273 | auto res = q.exec(); |
| 274 | |
| 275 | if (res && q.lastInsertId().isValid()) { |
| 276 | label->setId(q.lastInsertId().toInt()); |
| 277 | |
| 278 | // NOTE: This custom ID in this object will be probably |
| 279 | // overwritten in online-synchronized labels. |
| 280 | if (label->customId().isEmpty()) { |
| 281 | label->setCustomId(QString::number(label->id())); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // Fixup missing custom IDs. |
| 286 | q.prepare(QSL("UPDATE Labels SET custom_id = id WHERE custom_id IS NULL OR custom_id = '';")); |
| 287 | |
| 288 | return q.exec() && res; |
| 289 | } |
| 290 | |
| 291 | void DatabaseQueries::updateProbe(const QSqlDatabase& db, Search* probe) { |
| 292 | QSqlQuery q(db); |
| 293 | |
| 294 | q.setForwardOnly(true); |
| 295 | q.prepare(QSL("UPDATE Probes SET name = :name, fltr = :fltr, color = :color " |
| 296 | "WHERE id = :id AND account_id = :account_id;")); |
| 297 | q.bindValue(QSL(":name"), probe->title()); |
| 298 | q.bindValue(QSL(":fltr"), probe->filter()); |
| 299 | q.bindValue(QSL(":color"), probe->color().name()); |
| 300 | q.bindValue(QSL(":id"), probe->id()); |
| 301 | q.bindValue(QSL(":account_id"), probe->getParentServiceRoot()->accountId()); |
| 302 | |
| 303 | if (!q.exec()) { |
| 304 | throw ApplicationException(q.lastError().text()); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void DatabaseQueries::createProbe(const QSqlDatabase& db, Search* probe, int account_id) { |
| 309 | QSqlQuery q(db); |
| 310 | |
| 311 | q.setForwardOnly(true); |
| 312 | q.prepare(QSL("INSERT INTO Probes (name, color, fltr, account_id) " |
| 313 | "VALUES (:name, :color, :fltr, :account_id);")); |
| 314 | q.bindValue(QSL(":name"), probe->title()); |
| 315 | q.bindValue(QSL(":fltr"), probe->filter()); |
| 316 | q.bindValue(QSL(":color"), probe->color().name()); |
| 317 | q.bindValue(QSL(":account_id"), account_id); |
| 318 | |
| 319 | auto res = q.exec(); |
| 320 | |
| 321 | if (res && q.lastInsertId().isValid()) { |
| 322 | probe->setId(q.lastInsertId().toInt()); |
| 323 | probe->setCustomId(QString::number(probe->id())); |
| 324 | } |
| 325 | else { |
| 326 | throw ApplicationException(q.lastError().text()); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | QList<Search*> DatabaseQueries::getProbesForAccount(const QSqlDatabase& db, int account_id) { |
| 331 | QList<Search*> probes; |
| 332 | QSqlQuery q(db); |
| 333 | |
| 334 | q.setForwardOnly(true); |
| 335 | q.prepare(QSL("SELECT * FROM Probes WHERE account_id = :account_id;")); |
| 336 | q.bindValue(QSL(":account_id"), account_id); |
| 337 | |
| 338 | if (q.exec()) { |
| 339 | while (q.next()) { |
| 340 | Search* prob = new Search(q.value(QSL("name")).toString(), |
| 341 | q.value(QSL("fltr")).toString(), |
| 342 | QColor(q.value(QSL("color")).toString())); |
| 343 | |
| 344 | prob->setId(q.value(QSL("id")).toInt()); |
| 345 | prob->setCustomId(QString::number(prob->id())); |
| 346 | |
| 347 | probes << prob; |
| 348 | } |
| 349 | } |
| 350 | else { |
| 351 | throw ApplicationException(q.lastError().text()); |
| 352 | } |
| 353 | |
| 354 | return probes; |
| 355 | } |
| 356 | |
| 357 | void DatabaseQueries::deleteProbe(const QSqlDatabase& db, Search* probe) { |
| 358 | QSqlQuery q(db); |
| 359 | |
| 360 | q.setForwardOnly(true); |
| 361 | q.prepare(QSL("DELETE FROM Probes WHERE id = :id AND account_id = :account_id;")); |
| 362 | q.bindValue(QSL(":id"), probe->id()); |
| 363 | q.bindValue(QSL(":account_id"), probe->getParentServiceRoot()->accountId()); |
| 364 | |
| 365 | if (!q.exec()) { |
| 366 | throw ApplicationException(q.lastError().text()); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | bool DatabaseQueries::markLabelledMessagesReadUnread(const QSqlDatabase& db, Label* label, RootItem::ReadStatus read) { |
| 371 | QSqlQuery q(db); |
| 372 | |
| 373 | q.setForwardOnly(true); |
| 374 | q.prepare(QSL("UPDATE Messages SET is_read = :read " |
| 375 | "WHERE " |
| 376 | " is_deleted = 0 AND " |
| 377 | " is_pdeleted = 0 AND " |
| 378 | " account_id = :account_id AND " |
| 379 | " labels LIKE :label;")); |
| 380 | q.bindValue(QSL(":read"), read == RootItem::ReadStatus::Read ? 1 : 0); |
| 381 | q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId()); |
| 382 | q.bindValue(QSL(":label"), QSL("%.%1.%").arg(label->customId())); |
| 383 | |
| 384 | return q.exec(); |
| 385 | } |
| 386 | |
| 387 | bool DatabaseQueries::markImportantMessagesReadUnread(const QSqlDatabase& db, |
| 388 | int account_id, |
| 389 | RootItem::ReadStatus read) { |
| 390 | QSqlQuery q(db); |
| 391 | |
| 392 | q.setForwardOnly(true); |
| 393 | q.prepare(QSL("UPDATE Messages SET is_read = :read " |
| 394 | "WHERE is_important = 1 AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;")); |
| 395 | q.bindValue(QSL(":read"), read == RootItem::ReadStatus::Read ? 1 : 0); |
| 396 | q.bindValue(QSL(":account_id"), account_id); |
| 397 | return q.exec(); |
| 398 | } |
| 399 | |
| 400 | bool DatabaseQueries::markUnreadMessagesRead(const QSqlDatabase& db, int account_id) { |
| 401 | QSqlQuery q(db); |
| 402 | |
| 403 | q.setForwardOnly(true); |
| 404 | q.prepare(QSL("UPDATE Messages SET is_read = :read " |
| 405 | "WHERE is_read = 0 AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;")); |
| 406 | q.bindValue(QSL(":read"), 1); |
| 407 | q.bindValue(QSL(":account_id"), account_id); |
| 408 | return q.exec(); |
| 409 | } |
| 410 | |
| 411 | bool DatabaseQueries::markMessagesReadUnread(const QSqlDatabase& db, |
| 412 | const QStringList& ids, |
| 413 | RootItem::ReadStatus read) { |
| 414 | QSqlQuery q(db); |
| 415 | |
| 416 | q.setForwardOnly(true); |
| 417 | return q.exec(QString(QSL("UPDATE Messages SET is_read = %2 WHERE id IN (%1);")) |
| 418 | .arg(ids.join(QSL(", ")), read == RootItem::ReadStatus::Read ? QSL("1") : QSL("0"))); |
| 419 | } |
| 420 | |
| 421 | void DatabaseQueries::markMessagesReadUnreadImportant(const QSqlDatabase& db, |
| 422 | int account_id, |
| 423 | const QStringList& custom_ids, |
| 424 | RootItem::ReadStatus read, |
| 425 | RootItem::Importance important) { |
| 426 | auto stringed_ids = boolinq::from(custom_ids.begin(), custom_ids.end()) |
| 427 | .select([](const QString& id) { |
| 428 | return QSL("'%1'").arg(id); |
| 429 | }) |
| 430 | .toStdList(); |
| 431 | |
| 432 | QStringList textual_ids = FROM_STD_LIST(QStringList, stringed_ids); |
| 433 | QSqlQuery q(db); |
| 434 | QStringList setters; |
| 435 | |
| 436 | if (read != RootItem::ReadStatus::Unknown) { |
| 437 | setters.append(QSL("is_read = :read")); |
| 438 | } |
| 439 | |
| 440 | if (important != RootItem::Importance::Unknown) { |
| 441 | setters.append(QSL("is_important = :important")); |
| 442 | } |
| 443 | |
| 444 | q.setForwardOnly(true); |
| 445 | |
| 446 | QString statement = QSL("UPDATE Messages SET %1 " |
| 447 | " WHERE account_id = :account_id AND custom_id in (%2);") |
| 448 | .arg(setters.join(", "), textual_ids.join(", ")); |
| 449 | |
| 450 | if (!q.prepare(statement)) { |
| 451 | throw ApplicationException(q.lastError().text()); |
| 452 | } |
| 453 | |
| 454 | q.bindValue(QSL(":read"), int(read)); |
| 455 | q.bindValue(QSL(":important"), int(important)); |
| 456 | q.bindValue(QSL(":account_id"), account_id); |
| 457 | |
| 458 | if (!q.exec()) { |
| 459 | throw ApplicationException(q.lastError().text()); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | bool DatabaseQueries::markMessageImportant(const QSqlDatabase& db, int id, RootItem::Importance importance) { |
| 464 | QSqlQuery q(db); |
| 465 | |
| 466 | q.setForwardOnly(true); |
| 467 | |
| 468 | if (!q.prepare(QSL("UPDATE Messages SET is_important = :important WHERE id = :id;"))) { |
| 469 | qWarningNN << LOGSEC_DB << "Query preparation failed for message importance switch."; |
| 470 | return false; |
| 471 | } |
| 472 | |
| 473 | q.bindValue(QSL(":id"), id); |
| 474 | q.bindValue(QSL(":important"), (int)importance); |
| 475 | |
| 476 | // Commit changes. |
| 477 | return q.exec(); |
| 478 | } |
| 479 | |
| 480 | bool DatabaseQueries::markFeedsReadUnread(const QSqlDatabase& db, |
| 481 | const QStringList& ids, |
| 482 | int account_id, |
| 483 | RootItem::ReadStatus read) { |
| 484 | QSqlQuery q(db); |
| 485 | |
| 486 | q.setForwardOnly(true); |
| 487 | q.prepare(QSL("UPDATE Messages SET is_read = :read " |
| 488 | "WHERE feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;") |
| 489 | .arg(ids.join(QSL(", ")))); |
| 490 | q.bindValue(QSL(":read"), read == RootItem::ReadStatus::Read ? 1 : 0); |
| 491 | q.bindValue(QSL(":account_id"), account_id); |
| 492 | return q.exec(); |
| 493 | } |
| 494 | |
| 495 | bool DatabaseQueries::markBinReadUnread(const QSqlDatabase& db, int account_id, RootItem::ReadStatus read) { |
| 496 | QSqlQuery q(db); |
| 497 | |
| 498 | q.setForwardOnly(true); |
| 499 | q.prepare(QSL("UPDATE Messages SET is_read = :read " |
| 500 | "WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;")); |
| 501 | q.bindValue(QSL(":read"), read == RootItem::ReadStatus::Read ? 1 : 0); |
| 502 | q.bindValue(QSL(":account_id"), account_id); |
| 503 | return q.exec(); |
| 504 | } |
| 505 | |
| 506 | bool DatabaseQueries::markAccountReadUnread(const QSqlDatabase& db, int account_id, RootItem::ReadStatus read) { |
| 507 | QSqlQuery q(db); |
| 508 | |
| 509 | q.setForwardOnly(true); |
| 510 | q.prepare(QSL("UPDATE Messages SET is_read = :read WHERE is_pdeleted = 0 AND account_id = :account_id;")); |
| 511 | q.bindValue(QSL(":account_id"), account_id); |
| 512 | q.bindValue(QSL(":read"), read == RootItem::ReadStatus::Read ? 1 : 0); |
| 513 | return q.exec(); |
| 514 | } |
| 515 | |
| 516 | bool DatabaseQueries::switchMessagesImportance(const QSqlDatabase& db, const QStringList& ids) { |
| 517 | QSqlQuery q(db); |
| 518 | |
| 519 | q.setForwardOnly(true); |
| 520 | return q.exec(QSL("UPDATE Messages SET is_important = NOT is_important WHERE id IN (%1);").arg(ids.join(QSL(", ")))); |
| 521 | } |
| 522 | |
| 523 | bool DatabaseQueries::permanentlyDeleteMessages(const QSqlDatabase& db, const QStringList& ids) { |
| 524 | QSqlQuery q(db); |
| 525 | |
| 526 | q.setForwardOnly(true); |
| 527 | return q.exec(QSL("UPDATE Messages SET is_pdeleted = 1 WHERE id IN (%1);").arg(ids.join(QSL(", ")))); |
| 528 | } |
| 529 | |
| 530 | bool DatabaseQueries::deleteOrRestoreMessagesToFromBin(const QSqlDatabase& db, const QStringList& ids, bool deleted) { |
| 531 | QSqlQuery q(db); |
| 532 | |
| 533 | q.setForwardOnly(true); |
| 534 | return q.exec(QSL("UPDATE Messages SET is_deleted = %2, is_pdeleted = %3 WHERE id IN (%1);") |
| 535 | .arg(ids.join(QSL(", ")), QString::number(deleted ? 1 : 0), QString::number(0))); |
| 536 | } |
| 537 | |
| 538 | bool DatabaseQueries::restoreBin(const QSqlDatabase& db, int account_id) { |
| 539 | QSqlQuery q(db); |
| 540 | |
| 541 | q.setForwardOnly(true); |
| 542 | q.prepare(QSL("UPDATE Messages SET is_deleted = 0 " |
| 543 | "WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;")); |
| 544 | q.bindValue(QSL(":account_id"), account_id); |
| 545 | return q.exec(); |
| 546 | } |
| 547 | |
| 548 | bool DatabaseQueries::removeUnwantedArticlesFromFeed(const QSqlDatabase& db, |
| 549 | const Feed* feed, |
| 550 | const Feed::ArticleIgnoreLimit& feed_setup, |
| 551 | const Feed::ArticleIgnoreLimit& app_setup) { |
| 552 | // Feed setup has higher preference. |
| 553 | int amount_to_keep = |
| 554 | feed_setup.m_customizeLimitting ? feed_setup.m_keepCountOfArticles : app_setup.m_keepCountOfArticles; |
| 555 | bool dont_remove_unread = |
| 556 | feed_setup.m_customizeLimitting ? feed_setup.m_doNotRemoveUnread : app_setup.m_doNotRemoveUnread; |
| 557 | bool dont_remove_starred = |
| 558 | feed_setup.m_customizeLimitting ? feed_setup.m_doNotRemoveStarred : app_setup.m_doNotRemoveStarred; |
| 559 | bool recycle_dont_purge = |
| 560 | feed_setup.m_customizeLimitting ? feed_setup.m_moveToBinDontPurge : app_setup.m_moveToBinDontPurge; |
| 561 | |
| 562 | if (amount_to_keep <= 0) { |
| 563 | // No articles will be removed, quitting. |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | // We find datetime stamp of oldest article which will be NOT moved/removed. |
| 568 | QSqlQuery q(db); |
| 569 | |
| 570 | q.setForwardOnly(true); |
| 571 | q.prepare(QSL("SELECT Messages.date_created " |
| 572 | "FROM Messages " |
| 573 | "WHERE " |
| 574 | " Messages.account_id = :account_id AND " |
| 575 | " Messages.feed = :feed AND " |
| 576 | " Messages.is_deleted = 0 AND " |
| 577 | " Messages.is_pdeleted = 0 " |
| 578 | "ORDER BY Messages.date_created DESC " |
| 579 | "LIMIT 1 OFFSET :offset;")); |
| 580 | |
| 581 | q.bindValue(QSL(":offset"), amount_to_keep - 1); |
| 582 | q.bindValue(QSL(":feed"), feed->customId()); |
| 583 | q.bindValue(QSL(":account_id"), feed->getParentServiceRoot()->accountId()); |
| 584 | |
| 585 | if (!q.exec()) { |
| 586 | throw ApplicationException(q.lastError().text()); |
| 587 | } |
| 588 | |
| 589 | if (!q.next()) { |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | qlonglong last_kept_stamp = q.value(0).toLongLong(); |
| 594 | |
| 595 | if (recycle_dont_purge) { |
| 596 | // We mark all older articles as deleted. |
| 597 | q.prepare(QSL("UPDATE Messages " |
| 598 | "SET is_deleted = 1 " |
| 599 | "WHERE " |
| 600 | " Messages.account_id = :account_id AND " |
| 601 | " Messages.feed = :feed AND " |
| 602 | " Messages.is_deleted = 0 AND " |
| 603 | " Messages.is_pdeleted = 0 AND " |
| 604 | " Messages.is_important != :is_important AND " |
| 605 | " Messages.is_read != :is_read AND " |
| 606 | " Messages.date_created < :stamp")); |
| 607 | } |
| 608 | else { |
| 609 | // We purge all older articles. |
| 610 | q.prepare(QSL("DELETE FROM Messages " |
| 611 | "WHERE " |
| 612 | " Messages.account_id = :account_id AND " |
| 613 | " Messages.feed = :feed AND " |
| 614 | " (Messages.is_deleted = 1 OR Messages.is_important != :is_important) AND " |
| 615 | " (Messages.is_deleted = 1 OR Messages.is_read != :is_read) AND " |
| 616 | " Messages.date_created < :stamp")); |
| 617 | } |
| 618 | |
| 619 | q.bindValue(QSL(":is_important"), dont_remove_starred ? 1 : 2); |
| 620 | q.bindValue(QSL(":is_read"), dont_remove_unread ? 0 : 2); |
| 621 | q.bindValue(QSL(":feed"), feed->customId()); |
| 622 | q.bindValue(QSL(":stamp"), last_kept_stamp); |
| 623 | q.bindValue(QSL(":account_id"), feed->getParentServiceRoot()->accountId()); |
| 624 | |
| 625 | if (!q.exec()) { |
| 626 | throw ApplicationException(q.lastError().text()); |
| 627 | } |
| 628 | |
| 629 | int rows_deleted = q.numRowsAffected(); |
| 630 | |
| 631 | qDebugNN << LOGSEC_DB << "Feed cleanup has recycled/purged" << NONQUOTE_W_SPACE(rows_deleted) |
| 632 | << "old articles from feed" << QUOTE_W_SPACE_DOT(feed->customId()); |
| 633 | |
| 634 | return rows_deleted > 0; |
| 635 | } |
| 636 | |
| 637 | bool DatabaseQueries::purgeFeedArticles(const QSqlDatabase& database, const QList<Feed*>& feeds) { |
| 638 | QSqlQuery q(database); |
| 639 | |
| 640 | auto feed_clauses = boolinq::from(feeds) |
| 641 | .select([](Feed* feed) { |
| 642 | return QSL("(" |
| 643 | "Messages.feed = '%1' AND " |
| 644 | "Messages.account_id = %2 AND " |
| 645 | "Messages.is_important = 0" |
| 646 | ")") |
| 647 | .arg(feed->customId(), QString::number(feed->getParentServiceRoot()->accountId())); |
| 648 | }) |
| 649 | .toStdList(); |
| 650 | |
| 651 | qDebugNN << feed_clauses; |
| 652 | |
| 653 | QStringList feed_str_clauses = FROM_STD_LIST(QStringList, feed_clauses); |
| 654 | QString feed_clause = feed_str_clauses.join(QSL(" OR ")); |
| 655 | |
| 656 | q.setForwardOnly(true); |
| 657 | q.prepare(QSL("DELETE FROM Messages WHERE %1;").arg(feed_clause)); |
| 658 | |
| 659 | if (!q.exec()) { |
| 660 | throw ApplicationException(q.lastError().text()); |
| 661 | } |
| 662 | else { |
| 663 | return q.numRowsAffected() > 0; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | bool DatabaseQueries::purgeMessage(const QSqlDatabase& db, int message_id) { |
| 668 | QSqlQuery q(db); |
| 669 | |
| 670 | q.setForwardOnly(true); |
| 671 | q.prepare(QSL("DELETE FROM Messages WHERE id = :id;")); |
| 672 | q.bindValue(QSL(":id"), message_id); |
| 673 | |
| 674 | return q.exec(); |
| 675 | } |
| 676 | |
| 677 | bool DatabaseQueries::purgeImportantMessages(const QSqlDatabase& db) { |
| 678 | QSqlQuery q(db); |
| 679 | |
| 680 | q.setForwardOnly(true); |
| 681 | q.prepare(QSL("DELETE FROM Messages WHERE is_important = 1 AND is_deleted = :is_deleted;")); |
| 682 | |
| 683 | // Remove only messages which are NOT in recycle bin. |
| 684 | q.bindValue(QSL(":is_deleted"), 0); |
| 685 | |
| 686 | return q.exec(); |
| 687 | } |
| 688 | |
| 689 | bool DatabaseQueries::purgeReadMessages(const QSqlDatabase& db) { |
| 690 | QSqlQuery q(db); |
| 691 | |
| 692 | q.setForwardOnly(true); |
| 693 | q.prepare(QSL("DELETE FROM Messages " |
| 694 | "WHERE is_important = :is_important AND is_deleted = :is_deleted AND is_read = :is_read;")); |
| 695 | q.bindValue(QSL(":is_read"), 1); |
| 696 | |
| 697 | // Remove only messages which are NOT in recycle bin. |
| 698 | q.bindValue(QSL(":is_deleted"), 0); |
| 699 | |
| 700 | // Remove only messages which are NOT starred. |
| 701 | q.bindValue(QSL(":is_important"), 0); |
| 702 | |
| 703 | return q.exec(); |
| 704 | } |
| 705 | |
| 706 | bool DatabaseQueries::purgeOldMessages(const QSqlDatabase& db, int older_than_days) { |
| 707 | QSqlQuery q(db); |
| 708 | const qint64 since_epoch = older_than_days == 0 |
| 709 | ? QDateTime::currentDateTimeUtc().addYears(10).toMSecsSinceEpoch() |
| 710 | : QDateTime::currentDateTimeUtc().addDays(-older_than_days).toMSecsSinceEpoch(); |
| 711 | |
| 712 | q.setForwardOnly(true); |
| 713 | q.prepare(QSL("DELETE FROM Messages WHERE is_important = :is_important AND date_created < :date_created;")); |
| 714 | q.bindValue(QSL(":date_created"), since_epoch); |
| 715 | |
| 716 | // Remove only messages which are NOT starred. |
| 717 | q.bindValue(QSL(":is_important"), 0); |
| 718 | return q.exec(); |
| 719 | } |
| 720 | |
| 721 | bool DatabaseQueries::purgeRecycleBin(const QSqlDatabase& db) { |
| 722 | QSqlQuery q(db); |
| 723 | |
| 724 | q.setForwardOnly(true); |
| 725 | q.prepare(QSL("DELETE FROM Messages WHERE is_important = :is_important AND is_deleted = :is_deleted;")); |
| 726 | q.bindValue(QSL(":is_deleted"), 1); |
| 727 | |
| 728 | // Remove only messages which are NOT starred. |
| 729 | q.bindValue(QSL(":is_important"), 0); |
| 730 | return q.exec(); |
| 731 | } |
| 732 | |
| 733 | QMap<QString, ArticleCounts> DatabaseQueries::getMessageCountsForCategory(const QSqlDatabase& db, |
| 734 | const QString& custom_id, |
| 735 | int account_id, |
| 736 | bool include_total_counts, |
| 737 | bool* ok) { |
| 738 | QMap<QString, ArticleCounts> counts; |
| 739 | QSqlQuery q(db); |
| 740 | |
| 741 | q.setForwardOnly(true); |
| 742 | |
| 743 | if (include_total_counts) { |
| 744 | q.prepare(QSL("SELECT feed, SUM((is_read + 1) % 2), COUNT(*) FROM Messages " |
| 745 | "WHERE feed IN (SELECT custom_id FROM Feeds WHERE category = :category AND account_id = :account_id) " |
| 746 | "AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " |
| 747 | "GROUP BY feed;")); |
| 748 | } |
| 749 | else { |
| 750 | q.prepare(QSL("SELECT feed, SUM((is_read + 1) % 2) FROM Messages " |
| 751 | "WHERE feed IN (SELECT custom_id FROM Feeds WHERE category = :category AND account_id = :account_id) " |
| 752 | "AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " |
| 753 | "GROUP BY feed;")); |
| 754 | } |
| 755 | |
| 756 | q.bindValue(QSL(":category"), custom_id); |
| 757 | q.bindValue(QSL(":account_id"), account_id); |
| 758 | |
| 759 | if (q.exec()) { |
| 760 | while (q.next()) { |
| 761 | QString feed_custom_id = q.value(0).toString(); |
| 762 | ArticleCounts ac; |
| 763 | |
| 764 | ac.m_unread = q.value(1).toInt(); |
| 765 | |
| 766 | if (include_total_counts) { |
| 767 | ac.m_total = q.value(2).toInt(); |
| 768 | } |
| 769 | |
| 770 | counts.insert(feed_custom_id, ac); |
| 771 | } |
| 772 | |
| 773 | if (ok != nullptr) { |
| 774 | *ok = true; |
| 775 | } |
| 776 | } |
| 777 | else { |
| 778 | if (ok != nullptr) { |
| 779 | *ok = false; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | return counts; |
| 784 | } |
| 785 | |
| 786 | QMap<QString, ArticleCounts> DatabaseQueries::getMessageCountsForAccount(const QSqlDatabase& db, |
| 787 | int account_id, |
| 788 | bool include_total_counts, |
| 789 | bool* ok) { |
| 790 | QMap<QString, ArticleCounts> counts; |
| 791 | QSqlQuery q(db); |
| 792 | |
| 793 | q.setForwardOnly(true); |
| 794 | |
| 795 | if (include_total_counts) { |
| 796 | q.prepare(QSL("SELECT feed, SUM((is_read + 1) % 2), COUNT(*) FROM Messages " |
| 797 | "WHERE is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " |
| 798 | "GROUP BY feed;")); |
| 799 | } |
| 800 | else { |
| 801 | q.prepare(QSL("SELECT feed, SUM((is_read + 1) % 2) FROM Messages " |
| 802 | "WHERE is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " |
| 803 | "GROUP BY feed;")); |
| 804 | } |
| 805 | |
| 806 | q.bindValue(QSL(":account_id"), account_id); |
| 807 | |
| 808 | if (q.exec()) { |
| 809 | while (q.next()) { |
| 810 | QString feed_id = q.value(0).toString(); |
| 811 | ArticleCounts ac; |
| 812 | |
| 813 | ac.m_unread = q.value(1).toInt(); |
| 814 | |
| 815 | if (include_total_counts) { |
| 816 | ac.m_total = q.value(2).toInt(); |
| 817 | } |
| 818 | |
| 819 | counts.insert(feed_id, ac); |
| 820 | } |
| 821 | |
| 822 | if (ok != nullptr) { |
| 823 | *ok = true; |
| 824 | } |
| 825 | } |
| 826 | else { |
| 827 | if (ok != nullptr) { |
| 828 | *ok = false; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | return counts; |
| 833 | } |
| 834 | |
| 835 | ArticleCounts DatabaseQueries::getMessageCountsForFeed(const QSqlDatabase& db, |
| 836 | const QString& feed_custom_id, |
| 837 | int account_id, |
| 838 | bool* ok) { |
| 839 | QSqlQuery q(db); |
| 840 | |
| 841 | q.setForwardOnly(true); |
| 842 | q.prepare(QSL("SELECT COUNT(*), SUM(is_read) FROM Messages " |
| 843 | "WHERE feed = :feed AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;")); |
| 844 | |
| 845 | q.bindValue(QSL(":feed"), feed_custom_id); |
| 846 | q.bindValue(QSL(":account_id"), account_id); |
| 847 | |
| 848 | if (q.exec() && q.next()) { |
| 849 | if (ok != nullptr) { |
| 850 | *ok = true; |
| 851 | } |
| 852 | |
| 853 | ArticleCounts ac; |
| 854 | |
| 855 | ac.m_total = q.value(0).toInt(); |
| 856 | ac.m_unread = ac.m_total - q.value(1).toInt(); |
| 857 | |
| 858 | return ac; |
| 859 | } |
| 860 | else { |
| 861 | if (ok != nullptr) { |
| 862 | *ok = false; |
| 863 | } |
| 864 | |
| 865 | return {}; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | ArticleCounts DatabaseQueries::getMessageCountsForLabel(const QSqlDatabase& db, |
| 870 | Label* label, |
| 871 | int account_id, |
| 872 | bool* ok) { |
| 873 | QSqlQuery q(db); |
| 874 | |
| 875 | q.setForwardOnly(true); |
| 876 | q.prepare(QSL("SELECT COUNT(*), SUM(is_read) FROM Messages " |
| 877 | "WHERE " |
| 878 | " is_deleted = 0 AND " |
| 879 | " is_pdeleted = 0 AND " |
| 880 | " account_id = :account_id AND " |
| 881 | " labels LIKE :label;")); |
| 882 | |
| 883 | q.bindValue(QSL(":account_id"), account_id); |
| 884 | q.bindValue(QSL(":label"), QSL("%.%1.%").arg(label->customId())); |
| 885 | |
| 886 | if (q.exec() && q.next()) { |
| 887 | if (ok != nullptr) { |
| 888 | *ok = true; |
| 889 | } |
| 890 | |
| 891 | ArticleCounts ac; |
| 892 | |
| 893 | ac.m_total = q.value(0).toInt(); |
| 894 | ac.m_unread = ac.m_total - q.value(1).toInt(); |
| 895 | |
| 896 | return ac; |
| 897 | } |
| 898 | else { |
| 899 | if (ok != nullptr) { |
| 900 | *ok = false; |
| 901 | } |
| 902 | |
| 903 | return {}; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | ArticleCounts DatabaseQueries::getMessageCountsForProbe(const QSqlDatabase& db, Search* probe, int account_id) { |
| 908 | QSqlQuery q(db); |
| 909 | |
| 910 | q.setForwardOnly(true); |
| 911 | q.prepare(QSL("SELECT COUNT(*), SUM(is_read) FROM Messages " |
| 912 | "WHERE " |
| 913 | " is_deleted = 0 AND " |
| 914 | " is_pdeleted = 0 AND " |
| 915 | " account_id = :account_id AND " |
| 916 | " (title REGEXP :fltr OR contents REGEXP :fltr);")); |
| 917 | |
| 918 | q.bindValue(QSL(":account_id"), account_id); |
| 919 | q.bindValue(QSL(":fltr"), probe->filter()); |
| 920 | |
| 921 | if (q.exec() && q.next()) { |
| 922 | ArticleCounts ac; |
| 923 | |
| 924 | ac.m_total = q.value(0).toInt(); |
| 925 | ac.m_unread = ac.m_total - q.value(1).toInt(); |
| 926 | |
| 927 | return ac; |
| 928 | } |
| 929 | else { |
| 930 | throw ApplicationException(q.lastError().text()); |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | QMap<QString, ArticleCounts> DatabaseQueries::getMessageCountsForAllLabels(const QSqlDatabase& db, |
| 935 | int account_id, |
| 936 | bool* ok) { |
| 937 | QMap<QString, ArticleCounts> counts; |
| 938 | QSqlQuery q(db); |
| 939 | |
| 940 | q.setForwardOnly(true); |
| 941 | |
| 942 | if (db.driverName() == QSL(APP_DB_MYSQL_DRIVER)) { |
| 943 | q.prepare(QSL("SELECT l.custom_id, CONCAT('%.', l.custom_id,'.%') pid, SUM(m.is_read), COUNT(*) FROM Labels l " |
| 944 | "INNER JOIN Messages m " |
| 945 | " ON m.account_id = l.account_id AND m.labels LIKE pid " |
| 946 | "WHERE " |
| 947 | " m.is_deleted = 0 AND " |
| 948 | " m.is_pdeleted = 0 AND " |
| 949 | " m.account_id = :account_id " |
| 950 | "GROUP BY pid;")); |
| 951 | } |
| 952 | else { |
| 953 | q.prepare(QSL("SELECT l.custom_id, ('%.' || l.custom_id || '.%') pid, SUM(m.is_read), COUNT(*) FROM Labels l " |
| 954 | "INNER JOIN Messages m " |
| 955 | " ON m.account_id = l.account_id AND m.labels LIKE pid " |
| 956 | "WHERE " |
| 957 | " m.is_deleted = 0 AND " |
| 958 | " m.is_pdeleted = 0 AND " |
| 959 | " m.account_id = :account_id " |
| 960 | "GROUP BY pid;")); |
| 961 | } |
| 962 | |
| 963 | q.bindValue(QSL(":account_id"), account_id); |
| 964 | |
| 965 | if (q.exec()) { |
| 966 | while (q.next()) { |
| 967 | QString lbl_custom_id = q.value(0).toString(); |
| 968 | ArticleCounts ac; |
| 969 | |
| 970 | ac.m_total = q.value(3).toInt(); |
| 971 | ac.m_unread = ac.m_total - q.value(2).toInt(); |
| 972 | |
| 973 | counts.insert(lbl_custom_id, ac); |
| 974 | } |
| 975 | |
| 976 | if (ok != nullptr) { |
| 977 | *ok = true; |
| 978 | } |
| 979 | } |
| 980 | else { |
| 981 | if (ok != nullptr) { |
| 982 | *ok = false; |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | return counts; |
| 987 | } |
| 988 | |
| 989 | QMap<QString, ArticleCounts> DatabaseQueries::getCountOfAssignedLabelsToMessages(const QSqlDatabase& db, |
| 990 | const QList<Message>& messages, |
| 991 | int account_id, |
| 992 | bool* ok) { |
| 993 | QMap<QString, ArticleCounts> counts; |
| 994 | QSqlQuery q(db); |
| 995 | |
| 996 | q.setForwardOnly(true); |
| 997 | |
| 998 | auto msgs_std = boolinq::from(messages) |
| 999 | .select([](const Message& msg) { |
| 1000 | return QSL("m.custom_id = '%1'").arg(msg.m_customId); |
| 1001 | }) |
| 1002 | .toStdList(); |
| 1003 | |
| 1004 | QStringList msgs_lst = FROM_STD_LIST(QStringList, msgs_std); |
| 1005 | auto msgs = msgs_lst.join(QSL(" OR ")); |
| 1006 | |
| 1007 | if (db.driverName() == QSL(APP_DB_MYSQL_DRIVER)) { |
| 1008 | q.prepare(QSL("SELECT l.custom_id, CONCAT('%.', l.custom_id,'.%') pid, SUM(m.is_read), COUNT(*) FROM Labels l " |
| 1009 | "INNER JOIN Messages m " |
| 1010 | " ON m.account_id = l.account_id AND m.labels LIKE pid " |
| 1011 | "WHERE " |
| 1012 | " m.is_deleted = 0 AND " |
| 1013 | " m.is_pdeleted = 0 AND " |
| 1014 | " m.account_id = :account_id AND " |
| 1015 | " (%1) " |
| 1016 | "GROUP BY pid;") |
| 1017 | .arg(msgs)); |
| 1018 | } |
| 1019 | else { |
| 1020 | q.prepare(QSL("SELECT l.custom_id, ('%.' || l.custom_id || '.%') pid, SUM(m.is_read), COUNT(*) FROM Labels l " |
| 1021 | "INNER JOIN Messages m " |
| 1022 | " ON m.account_id = l.account_id AND m.labels LIKE pid " |
| 1023 | "WHERE " |
| 1024 | " m.is_deleted = 0 AND " |
| 1025 | " m.is_pdeleted = 0 AND " |
| 1026 | " m.account_id = :account_id AND " |
| 1027 | " (%1) " |
| 1028 | "GROUP BY pid;") |
| 1029 | .arg(msgs)); |
| 1030 | } |
| 1031 | |
| 1032 | q.bindValue(QSL(":account_id"), account_id); |
| 1033 | |
| 1034 | if (q.exec()) { |
| 1035 | while (q.next()) { |
| 1036 | QString lbl_custom_id = q.value(0).toString(); |
| 1037 | ArticleCounts ac; |
| 1038 | |
| 1039 | ac.m_total = q.value(3).toInt(); |
| 1040 | ac.m_unread = ac.m_total - q.value(2).toInt(); |
| 1041 | |
| 1042 | counts.insert(lbl_custom_id, ac); |
| 1043 | } |
| 1044 | |
| 1045 | if (ok != nullptr) { |
| 1046 | *ok = true; |
| 1047 | } |
| 1048 | } |
| 1049 | else { |
| 1050 | if (ok != nullptr) { |
| 1051 | *ok = false; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | return counts; |
| 1056 | } |
| 1057 | |
| 1058 | ArticleCounts DatabaseQueries::getImportantMessageCounts(const QSqlDatabase& db, int account_id, bool* ok) { |
| 1059 | QSqlQuery q(db); |
| 1060 | |
| 1061 | q.setForwardOnly(true); |
| 1062 | q.prepare(QSL("SELECT COUNT(*), SUM(is_read) FROM Messages " |
| 1063 | "WHERE is_important = 1 AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = " |
| 1064 | ":account_id;")); |
| 1065 | q.bindValue(QSL(":account_id"), account_id); |
| 1066 | |
| 1067 | if (q.exec() && q.next()) { |
| 1068 | if (ok != nullptr) { |
| 1069 | *ok = true; |
| 1070 | } |
| 1071 | |
| 1072 | ArticleCounts ac; |
| 1073 | |
| 1074 | ac.m_total = q.value(0).toInt(); |
| 1075 | ac.m_unread = ac.m_total - q.value(1).toInt(); |
| 1076 | |
| 1077 | return ac; |
| 1078 | } |
| 1079 | else { |
| 1080 | if (ok != nullptr) { |
| 1081 | *ok = false; |
| 1082 | } |
| 1083 | |
| 1084 | return {}; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | int DatabaseQueries::getUnreadMessageCounts(const QSqlDatabase& db, int account_id, bool* ok) { |
| 1089 | QSqlQuery q(db); |
| 1090 | |
| 1091 | q.setForwardOnly(true); |
| 1092 | q.prepare(QSL("SELECT COUNT(*) FROM Messages " |
| 1093 | "WHERE is_read = 0 AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;")); |
| 1094 | |
| 1095 | q.bindValue(QSL(":account_id"), account_id); |
| 1096 | |
| 1097 | if (q.exec() && q.next()) { |
| 1098 | if (ok != nullptr) { |
| 1099 | *ok = true; |
| 1100 | } |
| 1101 | |
| 1102 | return q.value(0).toInt(); |
| 1103 | } |
| 1104 | else { |
| 1105 | if (ok != nullptr) { |
| 1106 | *ok = false; |
| 1107 | } |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | ArticleCounts DatabaseQueries::getMessageCountsForBin(const QSqlDatabase& db, int account_id, bool* ok) { |
| 1114 | QSqlQuery q(db); |
| 1115 | |
| 1116 | q.setForwardOnly(true); |
| 1117 | q.prepare(QSL("SELECT COUNT(*), SUM(is_read) FROM Messages " |
| 1118 | "WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;")); |
| 1119 | |
| 1120 | q.bindValue(QSL(":account_id"), account_id); |
| 1121 | |
| 1122 | if (q.exec() && q.next()) { |
| 1123 | if (ok != nullptr) { |
| 1124 | *ok = true; |
| 1125 | } |
| 1126 | |
| 1127 | ArticleCounts ac; |
| 1128 | |
| 1129 | ac.m_total = q.value(0).toInt(); |
| 1130 | ac.m_unread = ac.m_total - q.value(1).toInt(); |
| 1131 | |
| 1132 | return ac; |
| 1133 | } |
| 1134 | else { |
| 1135 | if (ok != nullptr) { |
| 1136 | *ok = false; |
| 1137 | } |
| 1138 | |
| 1139 | return {}; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | QList<Message> DatabaseQueries::getUndeletedMessagesForProbe(const QSqlDatabase& db, const Search* probe) { |
| 1144 | QList<Message> messages; |
| 1145 | QSqlQuery q(db); |
| 1146 | |
| 1147 | q.prepare(QSL("SELECT %1 " |
| 1148 | "FROM Messages " |
| 1149 | "WHERE " |
| 1150 | " Messages.is_deleted = 0 AND " |
| 1151 | " Messages.is_pdeleted = 0 AND " |
| 1152 | " Messages.account_id = :account_id AND " |
| 1153 | " (title REGEXP :fltr OR contents REGEXP :fltr);") |
| 1154 | .arg(messageTableAttributes(true, db.driverName() == QSL(APP_DB_SQLITE_DRIVER)) |
| 1155 | .values() |
| 1156 | .join(QSL(", ")))); |
| 1157 | q.bindValue(QSL(":account_id"), probe->getParentServiceRoot()->accountId()); |
| 1158 | q.bindValue(QSL(":fltr"), probe->filter()); |
| 1159 | |
| 1160 | if (q.exec()) { |
| 1161 | while (q.next()) { |
| 1162 | bool decoded; |
| 1163 | Message message = Message::fromSqlRecord(q.record(), &decoded); |
| 1164 | |
| 1165 | if (decoded) { |
| 1166 | messages.append(message); |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | else { |
| 1171 | throw ApplicationException(q.lastError().text()); |
| 1172 | } |
| 1173 | |
| 1174 | return messages; |
| 1175 | } |
| 1176 | |
| 1177 | QList<Message> DatabaseQueries::getUndeletedMessagesWithLabel(const QSqlDatabase& db, const Label* label, bool* ok) { |
| 1178 | QList<Message> messages; |
| 1179 | QSqlQuery q(db); |
| 1180 | |
| 1181 | q.prepare(QSL("SELECT %1 " |
| 1182 | "FROM Messages " |
| 1183 | "INNER JOIN Feeds " |
| 1184 | "ON Messages.feed = Feeds.custom_id AND Messages.account_id = :account_id AND Messages.account_id = " |
| 1185 | "Feeds.account_id " |
| 1186 | "WHERE " |
| 1187 | " Messages.is_deleted = 0 AND " |
| 1188 | " Messages.is_pdeleted = 0 AND " |
| 1189 | " Messages.account_id = :account_id AND " |
| 1190 | " Messages.labels LIKE :label;") |
| 1191 | .arg(messageTableAttributes(false, db.driverName() == QSL(APP_DB_SQLITE_DRIVER)) |
| 1192 | .values() |
| 1193 | .join(QSL(", ")))); |
| 1194 | q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId()); |
| 1195 | q.bindValue(QSL(":label"), QSL("%.%1.%").arg(label->customId())); |
| 1196 | |
| 1197 | if (q.exec()) { |
| 1198 | while (q.next()) { |
| 1199 | bool decoded; |
| 1200 | Message message = Message::fromSqlRecord(q.record(), &decoded); |
| 1201 | |
| 1202 | if (decoded) { |
| 1203 | messages.append(message); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | if (ok != nullptr) { |
| 1208 | *ok = true; |
| 1209 | } |
| 1210 | } |
| 1211 | else { |
| 1212 | if (ok != nullptr) { |
| 1213 | *ok = false; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | return messages; |
| 1218 | } |
| 1219 | |
| 1220 | QList<Message> DatabaseQueries::getUndeletedLabelledMessages(const QSqlDatabase& db, int account_id, bool* ok) { |
| 1221 | QList<Message> messages; |
| 1222 | QSqlQuery q(db); |
| 1223 | |
| 1224 | q.prepare(QSL("SELECT %1 " |
| 1225 | "FROM Messages " |
| 1226 | "INNER JOIN Feeds " |
| 1227 | "ON Messages.feed = Feeds.custom_id AND Messages.account_id = Feeds.account_id " |
| 1228 | "WHERE " |
| 1229 | " Messages.is_deleted = 0 AND " |
| 1230 | " Messages.is_pdeleted = 0 AND " |
| 1231 | " Messages.account_id = :account_id AND " |
| 1232 | " LENGTH(Messages.labels) > 2;") |
| 1233 | .arg(messageTableAttributes(false, db.driverName() == QSL(APP_DB_SQLITE_DRIVER)) |
| 1234 | .values() |
| 1235 | .join(QSL(", ")))); |
| 1236 | q.bindValue(QSL(":account_id"), account_id); |
| 1237 | |
| 1238 | if (q.exec()) { |
| 1239 | while (q.next()) { |
| 1240 | bool decoded; |
| 1241 | Message message = Message::fromSqlRecord(q.record(), &decoded); |
| 1242 | |
| 1243 | if (decoded) { |
| 1244 | messages.append(message); |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | if (ok != nullptr) { |
| 1249 | *ok = true; |
| 1250 | } |
| 1251 | } |
| 1252 | else { |
| 1253 | auto a = q.lastError().text(); |
| 1254 | |
| 1255 | if (ok != nullptr) { |
| 1256 | *ok = false; |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | return messages; |
| 1261 | } |
| 1262 | |
| 1263 | QList<Message> DatabaseQueries::getUndeletedImportantMessages(const QSqlDatabase& db, int account_id, bool* ok) { |
| 1264 | QList<Message> messages; |
| 1265 | QSqlQuery q(db); |
| 1266 | |
| 1267 | q.setForwardOnly(true); |
| 1268 | q.prepare(QSL("SELECT %1 " |
| 1269 | "FROM Messages " |
| 1270 | "WHERE is_important = 1 AND is_deleted = 0 AND " |
| 1271 | " is_pdeleted = 0 AND account_id = :account_id;") |
| 1272 | .arg(messageTableAttributes(true, db.driverName() == QSL(APP_DB_SQLITE_DRIVER)) |
| 1273 | .values() |
| 1274 | .join(QSL(", ")))); |
| 1275 | q.bindValue(QSL(":account_id"), account_id); |
| 1276 | |
| 1277 | if (q.exec()) { |
| 1278 | while (q.next()) { |
| 1279 | bool decoded; |
| 1280 | Message message = Message::fromSqlRecord(q.record(), &decoded); |
| 1281 | |
| 1282 | if (decoded) { |
| 1283 | messages.append(message); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | if (ok != nullptr) { |
| 1288 | *ok = true; |
| 1289 | } |
| 1290 | } |
| 1291 | else { |
| 1292 | if (ok != nullptr) { |
| 1293 | *ok = false; |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | return messages; |
| 1298 | } |
| 1299 | |
| 1300 | QList<Message> DatabaseQueries::getUndeletedUnreadMessages(const QSqlDatabase& db, int account_id, bool* ok) { |
| 1301 | QList<Message> messages; |
| 1302 | QSqlQuery q(db); |
| 1303 | |
| 1304 | q.setForwardOnly(true); |
| 1305 | q.prepare(QSL("SELECT %1 " |
| 1306 | "FROM Messages " |
| 1307 | "WHERE is_read = 0 AND is_deleted = 0 AND " |
| 1308 | " is_pdeleted = 0 AND account_id = :account_id;") |
| 1309 | .arg(messageTableAttributes(true, db.driverName() == QSL(APP_DB_SQLITE_DRIVER)) |
| 1310 | .values() |
| 1311 | .join(QSL(", ")))); |
| 1312 | q.bindValue(QSL(":account_id"), account_id); |
| 1313 | |
| 1314 | if (q.exec()) { |
| 1315 | while (q.next()) { |
| 1316 | bool decoded; |
| 1317 | Message message = Message::fromSqlRecord(q.record(), &decoded); |
| 1318 | |
| 1319 | if (decoded) { |
| 1320 | messages.append(message); |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | if (ok != nullptr) { |
| 1325 | *ok = true; |
| 1326 | } |
| 1327 | } |
| 1328 | else { |
| 1329 | if (ok != nullptr) { |
| 1330 | *ok = false; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | return messages; |
| 1335 | } |
| 1336 | |
| 1337 | QList<Message> DatabaseQueries::getArticlesSlice(const QSqlDatabase& db, |
| 1338 | const QString& feed_custom_id, |
| 1339 | int account_id, |
| 1340 | bool newest_first, |
| 1341 | bool unread_only, |
| 1342 | bool starred_only, |
| 1343 | qint64 start_after_article_date, |
| 1344 | int row_offset, |
| 1345 | int row_limit) { |
| 1346 | QList<Message> messages; |
| 1347 | QSqlQuery q(db); |
| 1348 | QString feed_clause = !feed_custom_id.isEmpty() ? QSL("Messages.feed = :feed AND") : QString(); |
| 1349 | QString is_read_clause = unread_only ? QSL("Messages.is_read = :is_read AND ") : QString(); |
| 1350 | QString is_starred_clause = starred_only ? QSL("Messages.is_important = :is_important AND ") : QString(); |
| 1351 | QString account_id_clause = account_id > 0 ? QSL("Messages.account_id = :account_id AND ") : QString(); |
| 1352 | QString date_created_clause; |
| 1353 | |
| 1354 | if (start_after_article_date > 0) { |
| 1355 | if (newest_first) { |
| 1356 | date_created_clause = QSL("Messages.date_created < :date_created AND "); |
| 1357 | } |
| 1358 | else { |
| 1359 | date_created_clause = QSL("Messages.date_created > :date_created AND "); |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | q.setForwardOnly(true); |
| 1364 | q.prepare(QSL("SELECT %1 " |
| 1365 | "FROM Messages LEFT JOIN Feeds ON Messages.feed = Feeds.custom_id AND " |
| 1366 | " Messages.account_id = Feeds.account_id " |
| 1367 | "WHERE %3 " |
| 1368 | " %4 " |
| 1369 | " %5 " |
| 1370 | " %6 " |
| 1371 | " %7 " |
| 1372 | " Messages.is_deleted = 0 AND " |
| 1373 | " Messages.is_pdeleted = 0 " |
| 1374 | "ORDER BY Messages.date_created %2 " |
| 1375 | "LIMIT :row_limit OFFSET :row_offset;") |
| 1376 | .arg(messageTableAttributes(false, db.driverName() == QSL(APP_DB_SQLITE_DRIVER)).values().join(QSL(", ")), |
| 1377 | newest_first ? QSL("DESC") : QSL("ASC"), |
| 1378 | feed_clause, |
| 1379 | date_created_clause, |
| 1380 | account_id_clause, |
| 1381 | is_read_clause, |
| 1382 | is_starred_clause)); |
| 1383 | q.bindValue(QSL(":account_id"), account_id); |
| 1384 | q.bindValue(QSL(":row_limit"), row_limit); |
| 1385 | q.bindValue(QSL(":row_offset"), row_offset); |
| 1386 | q.bindValue(QSL(":feed"), feed_custom_id); |
| 1387 | q.bindValue(QSL(":is_read"), 0); |
| 1388 | q.bindValue(QSL(":is_important"), 1); |
| 1389 | q.bindValue(QSL(":date_created"), start_after_article_date); |
| 1390 | |
| 1391 | if (q.exec()) { |
| 1392 | while (q.next()) { |
| 1393 | bool decoded; |
| 1394 | Message message = Message::fromSqlRecord(q.record(), &decoded); |
| 1395 | |
| 1396 | if (decoded) { |
| 1397 | messages.append(message); |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | else { |
| 1402 | throw ApplicationException(q.lastError().driverText() + QSL(" ") + q.lastError().databaseText()); |
| 1403 | } |
| 1404 | |
| 1405 | return messages; |
| 1406 | } |
| 1407 | |
| 1408 | QList<Message> DatabaseQueries::getUndeletedMessagesForFeed(const QSqlDatabase& db, |
| 1409 | const QString& feed_custom_id, |
| 1410 | int account_id, |
| 1411 | bool* ok) { |
| 1412 | QList<Message> messages; |
| 1413 | QSqlQuery q(db); |
| 1414 | |
| 1415 | q.setForwardOnly(true); |
| 1416 | q.prepare(QSL("SELECT %1 " |
| 1417 | "FROM Messages " |
| 1418 | "WHERE is_deleted = 0 AND is_pdeleted = 0 AND " |
| 1419 | " feed = :feed AND account_id = :account_id;") |
| 1420 | .arg(messageTableAttributes(true, db.driverName() == QSL(APP_DB_SQLITE_DRIVER)) |
| 1421 | .values() |
| 1422 | .join(QSL(", ")))); |
| 1423 | q.bindValue(QSL(":feed"), feed_custom_id); |
| 1424 | q.bindValue(QSL(":account_id"), account_id); |
| 1425 | |
| 1426 | if (q.exec()) { |
| 1427 | while (q.next()) { |
| 1428 | bool decoded; |
| 1429 | Message message = Message::fromSqlRecord(q.record(), &decoded); |
| 1430 | |
| 1431 | if (decoded) { |
| 1432 | messages.append(message); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | if (ok != nullptr) { |
| 1437 | *ok = true; |
| 1438 | } |
| 1439 | } |
| 1440 | else { |
| 1441 | auto aa = q.lastError().text(); |
| 1442 | |
| 1443 | if (ok != nullptr) { |
| 1444 | *ok = false; |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | return messages; |
| 1449 | } |
| 1450 | |
| 1451 | QList<Message> DatabaseQueries::getUndeletedMessagesForBin(const QSqlDatabase& db, int account_id, bool* ok) { |
| 1452 | QList<Message> messages; |
| 1453 | QSqlQuery q(db); |
| 1454 | |
| 1455 | q.setForwardOnly(true); |
| 1456 | q.prepare(QSL("SELECT %1 " |
| 1457 | "FROM Messages " |
| 1458 | "WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;") |
| 1459 | .arg(messageTableAttributes(true, db.driverName() == QSL(APP_DB_SQLITE_DRIVER)) |
| 1460 | .values() |
| 1461 | .join(QSL(", ")))); |
| 1462 | q.bindValue(QSL(":account_id"), account_id); |
| 1463 | |
| 1464 | if (q.exec()) { |
| 1465 | while (q.next()) { |
| 1466 | bool decoded; |
| 1467 | Message message = Message::fromSqlRecord(q.record(), &decoded); |
| 1468 | |
| 1469 | if (decoded) { |
| 1470 | messages.append(message); |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | if (ok != nullptr) { |
| 1475 | *ok = true; |
| 1476 | } |
| 1477 | } |
| 1478 | else { |
| 1479 | if (ok != nullptr) { |
| 1480 | *ok = false; |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | return messages; |
| 1485 | } |
| 1486 | |
| 1487 | QList<Message> DatabaseQueries::getUndeletedMessagesForAccount(const QSqlDatabase& db, int account_id, bool* ok) { |
| 1488 | QList<Message> messages; |
| 1489 | QSqlQuery q(db); |
| 1490 | |
| 1491 | q.setForwardOnly(true); |
| 1492 | q.prepare(QSL("SELECT %1 " |
| 1493 | "FROM Messages " |
| 1494 | "WHERE is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;") |
| 1495 | .arg(messageTableAttributes(true, db.driverName() == QSL(APP_DB_SQLITE_DRIVER)) |
| 1496 | .values() |
| 1497 | .join(QSL(", ")))); |
| 1498 | q.bindValue(QSL(":account_id"), account_id); |
| 1499 | |
| 1500 | if (q.exec()) { |
| 1501 | while (q.next()) { |
| 1502 | bool decoded; |
| 1503 | Message message = Message::fromSqlRecord(q.record(), &decoded); |
| 1504 | |
| 1505 | if (decoded) { |
| 1506 | messages.append(message); |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | if (ok != nullptr) { |
| 1511 | *ok = true; |
| 1512 | } |
| 1513 | } |
| 1514 | else { |
| 1515 | if (ok != nullptr) { |
| 1516 | *ok = false; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | return messages; |
| 1521 | } |
| 1522 | |
| 1523 | QStringList DatabaseQueries::bagOfMessages(const QSqlDatabase& db, ServiceRoot::BagOfMessages bag, const Feed* feed) { |
| 1524 | QStringList ids; |
| 1525 | QSqlQuery q(db); |
| 1526 | QString query; |
| 1527 | |
| 1528 | q.setForwardOnly(true); |
| 1529 | |
| 1530 | switch (bag) { |
| 1531 | case ServiceRoot::BagOfMessages::Unread: |
| 1532 | query = QSL("is_read = 0"); |
| 1533 | break; |
| 1534 | |
| 1535 | case ServiceRoot::BagOfMessages::Starred: |
| 1536 | query = QSL("is_important = 1"); |
| 1537 | break; |
| 1538 | |
| 1539 | case ServiceRoot::BagOfMessages::Read: |
| 1540 | default: |
| 1541 | query = QSL("is_read = 1"); |
| 1542 | break; |
| 1543 | } |
| 1544 | |
| 1545 | q.prepare(QSL("SELECT custom_id " |
| 1546 | "FROM Messages " |
| 1547 | "WHERE %1 AND feed = :feed AND account_id = :account_id;") |
| 1548 | .arg(query)); |
| 1549 | |
| 1550 | q.bindValue(QSL(":account_id"), feed->getParentServiceRoot()->accountId()); |
| 1551 | q.bindValue(QSL(":feed"), feed->customId()); |
| 1552 | q.exec(); |
| 1553 | |
| 1554 | while (q.next()) { |
| 1555 | ids.append(q.value(0).toString()); |
| 1556 | } |
| 1557 | |
| 1558 | return ids; |
| 1559 | } |
| 1560 | |
| 1561 | QHash<QString, QStringList> DatabaseQueries::bagsOfMessages(const QSqlDatabase& db, const QList<Label*>& labels) { |
| 1562 | QHash<QString, QStringList> ids; |
| 1563 | QSqlQuery q(db); |
| 1564 | |
| 1565 | q.setForwardOnly(true); |
| 1566 | q.prepare(QSL("SELECT custom_id FROM Messages " |
| 1567 | "WHERE " |
| 1568 | " account_id = :account_id AND " |
| 1569 | " labels LIKE :label;")); |
| 1570 | |
| 1571 | for (const Label* lbl : labels) { |
| 1572 | q.bindValue(QSL(":label"), QSL("%.%1.%").arg(lbl->customId())); |
| 1573 | q.bindValue(QSL(":account_id"), lbl->getParentServiceRoot()->accountId()); |
| 1574 | q.exec(); |
| 1575 | |
| 1576 | QStringList ids_one_label; |
| 1577 | |
| 1578 | while (q.next()) { |
| 1579 | ids_one_label.append(q.value(0).toString()); |
| 1580 | } |
| 1581 | |
| 1582 | ids.insert(lbl->customId(), ids_one_label); |
| 1583 | } |
| 1584 | |
| 1585 | return ids; |
| 1586 | } |
| 1587 | |
| 1588 | UpdatedArticles DatabaseQueries::updateMessages(const QSqlDatabase& db, |
| 1589 | QList<Message>& messages, |
| 1590 | Feed* feed, |
| 1591 | bool force_update, |
| 1592 | QMutex* db_mutex, |
| 1593 | bool* ok) { |
| 1594 | if (messages.isEmpty()) { |
| 1595 | *ok = true; |
| 1596 | return {}; |
| 1597 | } |
| 1598 | |
| 1599 | UpdatedArticles updated_messages; |
| 1600 | int account_id = feed->getParentServiceRoot()->accountId(); |
| 1601 | auto feed_custom_id = feed->customId(); |
| 1602 | |
| 1603 | // Prepare queries. |
| 1604 | QSqlQuery query_select_with_url(db); |
| 1605 | QSqlQuery query_select_with_custom_id(db); |
| 1606 | QSqlQuery query_select_with_custom_id_for_feed(db); |
| 1607 | QSqlQuery query_select_with_id(db); |
| 1608 | QSqlQuery query_update(db); |
| 1609 | |
| 1610 | // Here we have query which will check for existence of the "same" message in given feed. |
| 1611 | // The two message are the "same" if: |
| 1612 | // 1) they belong to the SAME FEED AND, |
| 1613 | // 2) they have same URL AND, |
| 1614 | // 3) they have same AUTHOR AND, |
| 1615 | // 4) they have same TITLE. |
| 1616 | // NOTE: This only applies to messages from standard RSS/ATOM/JSON feeds without ID/GUID. |
| 1617 | query_select_with_url.setForwardOnly(true); |
| 1618 | query_select_with_url.prepare(QSL("SELECT id, date_created, is_read, is_important, contents, feed FROM Messages " |
| 1619 | "WHERE feed = :feed AND title = :title AND url = :url AND author = :author AND " |
| 1620 | "account_id = :account_id;")); |
| 1621 | |
| 1622 | // When we have custom ID of the message which is service-specific (synchronized services). |
| 1623 | query_select_with_custom_id.setForwardOnly(true); |
| 1624 | query_select_with_custom_id |
| 1625 | .prepare(QSL("SELECT id, date_created, is_read, is_important, contents, feed, title, author FROM Messages " |
| 1626 | "WHERE custom_id = :custom_id AND account_id = :account_id;")); |
| 1627 | |
| 1628 | // We have custom ID of message, but it is feed-specific not service-specific (standard RSS/ATOM/JSON). |
| 1629 | query_select_with_custom_id_for_feed.setForwardOnly(true); |
| 1630 | query_select_with_custom_id_for_feed |
| 1631 | .prepare(QSL("SELECT id, date_created, is_read, is_important, contents, title, author FROM Messages " |
| 1632 | "WHERE feed = :feed AND custom_id = :custom_id AND account_id = :account_id;")); |
| 1633 | |
| 1634 | // In some case, messages are already stored in the DB and they all have primary DB ID. |
| 1635 | // This is particularly the case when user runs some message filter manually on existing messages |
| 1636 | // of some feed. |
| 1637 | query_select_with_id.setForwardOnly(true); |
| 1638 | query_select_with_id |
| 1639 | .prepare(QSL("SELECT date_created, is_read, is_important, contents, feed, title, author FROM Messages " |
| 1640 | "WHERE id = :id AND account_id = :account_id;")); |
| 1641 | |
| 1642 | // Used to update existing messages. |
| 1643 | query_update.setForwardOnly(true); |
| 1644 | query_update.prepare(QSL("UPDATE Messages " |
| 1645 | "SET title = :title, is_read = :is_read, is_important = :is_important, is_deleted = " |
| 1646 | ":is_deleted, url = :url, author = :author, score = :score, date_created = :date_created, " |
| 1647 | "contents = :contents, enclosures = :enclosures, feed = :feed " |
| 1648 | "WHERE id = :id;")); |
| 1649 | |
| 1650 | QVector<Message*> msgs_to_insert; |
| 1651 | |
| 1652 | for (Message& message : messages) { |
| 1653 | int id_existing_message = -1; |
| 1654 | qint64 date_existing_message = 0; |
| 1655 | bool is_read_existing_message = false; |
| 1656 | bool is_important_existing_message = false; |
| 1657 | QString contents_existing_message; |
| 1658 | QString feed_id_existing_message; |
| 1659 | QString title_existing_message; |
| 1660 | QString author_existing_message; |
| 1661 | |
| 1662 | QMutexLocker lck(db_mutex); |
| 1663 | |
| 1664 | if (message.m_id > 0) { |
| 1665 | // We recognize directly existing message. |
| 1666 | // NOTE: Particularly for manual message filter execution. |
| 1667 | query_select_with_id.bindValue(QSL(":id"), message.m_id); |
| 1668 | query_select_with_id.bindValue(QSL(":account_id"), account_id); |
| 1669 | |
| 1670 | qDebugNN << LOGSEC_DB << "Checking if message with primary ID" << QUOTE_W_SPACE(message.m_id) |
| 1671 | << "is present in DB."; |
| 1672 | |
| 1673 | if (query_select_with_id.exec() && query_select_with_id.next()) { |
| 1674 | id_existing_message = message.m_id; |
| 1675 | date_existing_message = query_select_with_id.value(0).value<qint64>(); |
| 1676 | is_read_existing_message = query_select_with_id.value(1).toBool(); |
| 1677 | is_important_existing_message = query_select_with_id.value(2).toBool(); |
| 1678 | contents_existing_message = query_select_with_id.value(3).toString(); |
| 1679 | feed_id_existing_message = query_select_with_id.value(4).toString(); |
| 1680 | title_existing_message = query_select_with_id.value(5).toString(); |
| 1681 | author_existing_message = query_select_with_id.value(6).toString(); |
| 1682 | |
| 1683 | qDebugNN << LOGSEC_DB << "Message with direct DB ID is already present in DB and has DB ID" |
| 1684 | << QUOTE_W_SPACE_DOT(id_existing_message); |
| 1685 | } |
| 1686 | else if (query_select_with_id.lastError().isValid()) { |
| 1687 | qWarningNN << LOGSEC_DB << "Failed to check for existing message in DB via primary ID:" |
| 1688 | << QUOTE_W_SPACE_DOT(query_select_with_id.lastError().text()); |
| 1689 | } |
| 1690 | |
| 1691 | query_select_with_id.finish(); |
| 1692 | } |
| 1693 | else if (message.m_customId.isEmpty()) { |
| 1694 | // We need to recognize existing messages according to URL & AUTHOR & TITLE. |
| 1695 | // NOTE: This concerns articles from RSS/ATOM/JSON which do not |
| 1696 | // provide unique ID/GUID. |
| 1697 | query_select_with_url.bindValue(QSL(":feed"), unnulifyString(feed_custom_id)); |
| 1698 | query_select_with_url.bindValue(QSL(":title"), unnulifyString(message.m_title)); |
| 1699 | query_select_with_url.bindValue(QSL(":url"), unnulifyString(message.m_url)); |
| 1700 | query_select_with_url.bindValue(QSL(":author"), unnulifyString(message.m_author)); |
| 1701 | query_select_with_url.bindValue(QSL(":account_id"), account_id); |
| 1702 | |
| 1703 | qDebugNN << LOGSEC_DB << "Checking if message with title " << QUOTE_NO_SPACE(message.m_title) << ", url " |
| 1704 | << QUOTE_NO_SPACE(message.m_url) << "' and author " << QUOTE_NO_SPACE(message.m_author) |
| 1705 | << " is present in DB."; |
| 1706 | |
| 1707 | if (query_select_with_url.exec() && query_select_with_url.next()) { |
| 1708 | id_existing_message = query_select_with_url.value(0).toInt(); |
| 1709 | date_existing_message = query_select_with_url.value(1).value<qint64>(); |
| 1710 | is_read_existing_message = query_select_with_url.value(2).toBool(); |
| 1711 | is_important_existing_message = query_select_with_url.value(3).toBool(); |
| 1712 | contents_existing_message = query_select_with_url.value(4).toString(); |
| 1713 | feed_id_existing_message = query_select_with_url.value(5).toString(); |
| 1714 | title_existing_message = unnulifyString(message.m_title); |
| 1715 | author_existing_message = unnulifyString(message.m_author); |
| 1716 | |
| 1717 | qDebugNN << LOGSEC_DB << "Message with these attributes is already present in DB and has DB ID" |
| 1718 | << QUOTE_W_SPACE_DOT(id_existing_message); |
| 1719 | } |
| 1720 | else if (query_select_with_url.lastError().isValid()) { |
| 1721 | qWarningNN << LOGSEC_DB << "Failed to check for existing message in DB via URL/TITLE/AUTHOR:" |
| 1722 | << QUOTE_W_SPACE_DOT(query_select_with_url.lastError().text()); |
| 1723 | } |
| 1724 | |
| 1725 | query_select_with_url.finish(); |
| 1726 | } |
| 1727 | else { |
| 1728 | // We can recognize existing messages via their custom ID. |
| 1729 | if (feed->getParentServiceRoot()->isSyncable()) { |
| 1730 | // Custom IDs are service-wide. |
| 1731 | // NOTE: This concerns messages from custom accounts, like TT-RSS or Nextcloud News. |
| 1732 | query_select_with_custom_id.bindValue(QSL(":account_id"), account_id); |
| 1733 | query_select_with_custom_id.bindValue(QSL(":custom_id"), unnulifyString(message.m_customId)); |
| 1734 | |
| 1735 | qDebugNN << LOGSEC_DB << "Checking if message with service-specific custom ID" |
| 1736 | << QUOTE_W_SPACE(message.m_customId) << "is present in DB."; |
| 1737 | |
| 1738 | if (query_select_with_custom_id.exec() && query_select_with_custom_id.next()) { |
| 1739 | id_existing_message = query_select_with_custom_id.value(0).toInt(); |
| 1740 | date_existing_message = query_select_with_custom_id.value(1).value<qint64>(); |
| 1741 | is_read_existing_message = query_select_with_custom_id.value(2).toBool(); |
| 1742 | is_important_existing_message = query_select_with_custom_id.value(3).toBool(); |
| 1743 | contents_existing_message = query_select_with_custom_id.value(4).toString(); |
| 1744 | feed_id_existing_message = query_select_with_custom_id.value(5).toString(); |
| 1745 | title_existing_message = query_select_with_custom_id.value(6).toString(); |
| 1746 | author_existing_message = query_select_with_custom_id.value(7).toString(); |
| 1747 | |
| 1748 | qDebugNN << LOGSEC_DB << "Message with custom ID" << QUOTE_W_SPACE(message.m_customId) |
| 1749 | << "is already present in DB and has DB ID '" << id_existing_message << "'."; |
| 1750 | } |
| 1751 | else if (query_select_with_custom_id.lastError().isValid()) { |
| 1752 | qWarningNN << LOGSEC_DB << "Failed to check for existing message in DB via ID:" |
| 1753 | << QUOTE_W_SPACE_DOT(query_select_with_custom_id.lastError().text()); |
| 1754 | } |
| 1755 | |
| 1756 | query_select_with_custom_id.finish(); |
| 1757 | } |
| 1758 | else { |
| 1759 | // Custom IDs are feed-specific. |
| 1760 | // NOTE: This concerns articles with ID/GUID from standard RSS/ATOM/JSON feeds. |
| 1761 | query_select_with_custom_id_for_feed.bindValue(QSL(":account_id"), account_id); |
| 1762 | query_select_with_custom_id_for_feed.bindValue(QSL(":feed"), feed_custom_id); |
| 1763 | query_select_with_custom_id_for_feed.bindValue(QSL(":custom_id"), unnulifyString(message.m_customId)); |
| 1764 | |
| 1765 | qDebugNN << LOGSEC_DB << "Checking if message with feed-specific custom ID" << QUOTE_W_SPACE(message.m_customId) |
| 1766 | << "is present in DB."; |
| 1767 | |
| 1768 | if (query_select_with_custom_id_for_feed.exec() && query_select_with_custom_id_for_feed.next()) { |
| 1769 | id_existing_message = query_select_with_custom_id_for_feed.value(0).toInt(); |
| 1770 | date_existing_message = query_select_with_custom_id_for_feed.value(1).value<qint64>(); |
| 1771 | is_read_existing_message = query_select_with_custom_id_for_feed.value(2).toBool(); |
| 1772 | is_important_existing_message = query_select_with_custom_id_for_feed.value(3).toBool(); |
| 1773 | contents_existing_message = query_select_with_custom_id_for_feed.value(4).toString(); |
| 1774 | feed_id_existing_message = feed_custom_id; |
| 1775 | title_existing_message = query_select_with_custom_id_for_feed.value(5).toString(); |
| 1776 | author_existing_message = query_select_with_custom_id_for_feed.value(6).toString(); |
| 1777 | |
| 1778 | qDebugNN << LOGSEC_DB << "Message with custom ID" << QUOTE_W_SPACE(message.m_customId) |
| 1779 | << "is already present in DB and has DB ID" << QUOTE_W_SPACE_DOT(id_existing_message); |
| 1780 | } |
| 1781 | else if (query_select_with_custom_id_for_feed.lastError().isValid()) { |
| 1782 | qWarningNN << LOGSEC_DB << "Failed to check for existing message in DB via ID:" |
| 1783 | << QUOTE_W_SPACE_DOT(query_select_with_custom_id_for_feed.lastError().text()); |
| 1784 | } |
| 1785 | |
| 1786 | query_select_with_custom_id_for_feed.finish(); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | // Now, check if this message is already in the DB. |
| 1791 | if (id_existing_message >= 0) { |
| 1792 | message.m_id = id_existing_message; |
| 1793 | |
| 1794 | // Message is already in the DB. |
| 1795 | // |
| 1796 | // Now, we update it if at least one of next conditions is true: |
| 1797 | // 1) FOR SYNCHRONIZED SERVICES: |
| 1798 | // Message has custom ID AND (its date OR read status OR starred status are changed |
| 1799 | // or message was moved from other feed to current feed - this can particularly happen in Gmail feeds). |
| 1800 | // |
| 1801 | // 2) FOR NON-SYNCHRONIZED SERVICES (RSS/ATOM/JSON): |
| 1802 | // Message has custom ID/GUID and its title or author or contents are changed. |
| 1803 | // |
| 1804 | // 3) FOR ALL SERVICES: |
| 1805 | // Message has its date fetched from feed AND its date is different |
| 1806 | // from date in DB or content is changed. Date/time is considered different |
| 1807 | // when the difference is larger than MSG_DATETIME_DIFF_THRESSHOLD |
| 1808 | // |
| 1809 | // 4) FOR ALL SERVICES: |
| 1810 | // Message update is forced, we want to overwrite message as some arbitrary atribute was changed, |
| 1811 | // this particularly happens when manual message filter execution happens. |
| 1812 | bool ignore_contents_changes = |
| 1813 | qApp->settings()->value(GROUP(Messages), SETTING(Messages::IgnoreContentsChanges)).toBool(); |
| 1814 | bool cond_1 = |
| 1815 | !message.m_customId.isEmpty() && feed->getParentServiceRoot()->isSyncable() && |
| 1816 | (message.m_created.toMSecsSinceEpoch() != date_existing_message || |
| 1817 | message.m_isRead != is_read_existing_message || message.m_isImportant != is_important_existing_message || |
| 1818 | (message.m_feedId != feed_id_existing_message && message.m_feedId == feed_custom_id) || |
| 1819 | message.m_title != title_existing_message || |
| 1820 | (!ignore_contents_changes && message.m_contents != contents_existing_message)); |
| 1821 | bool cond_2 = !message.m_customId.isEmpty() && !feed->getParentServiceRoot()->isSyncable() && |
| 1822 | (message.m_title != title_existing_message || message.m_author != author_existing_message || |
| 1823 | (!ignore_contents_changes && message.m_contents != contents_existing_message)); |
| 1824 | bool cond_3 = (message.m_createdFromFeed && std::abs(message.m_created.toMSecsSinceEpoch() - |
| 1825 | date_existing_message) > MSG_DATETIME_DIFF_THRESSHOLD) || |
| 1826 | (!ignore_contents_changes && message.m_contents != contents_existing_message); |
| 1827 | |
| 1828 | if (cond_1 || cond_2 || cond_3 || force_update) { |
| 1829 | if (!feed->getParentServiceRoot()->isSyncable() && |
| 1830 | !qApp->settings()->value(GROUP(Messages), SETTING(Messages::MarkUnreadOnUpdated)).toBool()) { |
| 1831 | // Feed is not syncable, thus we got RSS/JSON/whatever. |
| 1832 | // Article is only updated, so we now prefer to keep original read state |
| 1833 | // pretty much the same way starred state is kept. |
| 1834 | message.m_isRead = is_read_existing_message; |
| 1835 | } |
| 1836 | |
| 1837 | // Message exists and is changed, update it. |
| 1838 | query_update.bindValue(QSL(":title"), unnulifyString(message.m_title)); |
| 1839 | query_update.bindValue(QSL(":is_read"), int(message.m_isRead)); |
| 1840 | query_update.bindValue(QSL(":is_important"), |
| 1841 | (feed->getParentServiceRoot()->isSyncable() || message.m_isImportant) |
| 1842 | ? int(message.m_isImportant) |
| 1843 | : is_important_existing_message); |
| 1844 | query_update.bindValue(QSL(":is_deleted"), int(message.m_isDeleted)); |
| 1845 | query_update.bindValue(QSL(":url"), unnulifyString(message.m_url)); |
| 1846 | query_update.bindValue(QSL(":author"), unnulifyString(message.m_author)); |
| 1847 | query_update.bindValue(QSL(":date_created"), message.m_created.toMSecsSinceEpoch()); |
| 1848 | query_update.bindValue(QSL(":contents"), unnulifyString(message.m_contents)); |
| 1849 | query_update.bindValue(QSL(":enclosures"), Enclosures::encodeEnclosuresToString(message.m_enclosures)); |
| 1850 | query_update.bindValue(QSL(":feed"), message.m_feedId); |
| 1851 | query_update.bindValue(QSL(":score"), message.m_score); |
| 1852 | query_update.bindValue(QSL(":id"), id_existing_message); |
| 1853 | |
| 1854 | if (query_update.exec()) { |
| 1855 | qDebugNN << LOGSEC_DB << "Overwriting message with title" << QUOTE_W_SPACE(message.m_title) << "URL" |
| 1856 | << QUOTE_W_SPACE(message.m_url) << "in DB."; |
| 1857 | |
| 1858 | if (!message.m_isRead) { |
| 1859 | updated_messages.m_unread.append(message); |
| 1860 | } |
| 1861 | |
| 1862 | updated_messages.m_all.append(message); |
| 1863 | message.m_insertedUpdated = true; |
| 1864 | } |
| 1865 | else if (query_update.lastError().isValid()) { |
| 1866 | qCriticalNN << LOGSEC_DB |
| 1867 | << "Failed to update message in DB:" << QUOTE_W_SPACE_DOT(query_update.lastError().text()); |
| 1868 | } |
| 1869 | |
| 1870 | query_update.finish(); |
| 1871 | } |
| 1872 | } |
| 1873 | else { |
| 1874 | msgs_to_insert.append(&message); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | if (!msgs_to_insert.isEmpty()) { |
| 1879 | QString bulk_insert = QSL("INSERT INTO Messages " |
| 1880 | "(feed, title, is_read, is_important, is_deleted, url, author, score, date_created, " |
| 1881 | "contents, enclosures, custom_id, custom_hash, account_id) " |
| 1882 | "VALUES %1;"); |
| 1883 | |
| 1884 | for (int i = 0; i < msgs_to_insert.size(); i += 1000) { |
| 1885 | QStringList vals; |
| 1886 | int batch_length = std::min(1000, int(msgs_to_insert.size()) - i); |
| 1887 | |
| 1888 | for (int l = i; l < (i + batch_length); l++) { |
| 1889 | Message* msg = msgs_to_insert[l]; |
| 1890 | |
| 1891 | if (msg->m_title.isEmpty()) { |
| 1892 | qCriticalNN << LOGSEC_DB << "Message" << QUOTE_W_SPACE(msg->m_customId) |
| 1893 | << "will not be inserted to DB because it does not meet DB constraints."; |
| 1894 | |
| 1895 | continue; |
| 1896 | } |
| 1897 | |
| 1898 | vals.append(QSL("\n(':feed', ':title', :is_read, :is_important, :is_deleted, " |
| 1899 | "':url', ':author', :score, :date_created, ':contents', ':enclosures', " |
| 1900 | "':custom_id', ':custom_hash', :account_id)") |
| 1901 | .replace(QSL(":feed"), unnulifyString(feed_custom_id)) |
| 1902 | .replace(QSL(":title"), DatabaseFactory::escapeQuery(unnulifyString(msg->m_title))) |
| 1903 | .replace(QSL(":is_read"), QString::number(int(msg->m_isRead))) |
| 1904 | .replace(QSL(":is_important"), QString::number(int(msg->m_isImportant))) |
| 1905 | .replace(QSL(":is_deleted"), QString::number(int(msg->m_isDeleted))) |
| 1906 | .replace(QSL(":url"), DatabaseFactory::escapeQuery(unnulifyString(msg->m_url))) |
| 1907 | .replace(QSL(":author"), DatabaseFactory::escapeQuery(unnulifyString(msg->m_author))) |
| 1908 | .replace(QSL(":date_created"), QString::number(msg->m_created.toMSecsSinceEpoch())) |
| 1909 | .replace(QSL(":contents"), DatabaseFactory::escapeQuery(unnulifyString(msg->m_contents))) |
| 1910 | .replace(QSL(":enclosures"), |
| 1911 | DatabaseFactory::escapeQuery(Enclosures::encodeEnclosuresToString(msg->m_enclosures))) |
| 1912 | .replace(QSL(":custom_id"), DatabaseFactory::escapeQuery(unnulifyString(msg->m_customId))) |
| 1913 | .replace(QSL(":custom_hash"), unnulifyString(msg->m_customHash)) |
| 1914 | .replace(QSL(":score"), QString::number(msg->m_score)) |
| 1915 | .replace(QSL(":account_id"), QString::number(account_id))); |
| 1916 | } |
| 1917 | |
| 1918 | if (!vals.isEmpty()) { |
| 1919 | QString final_bulk = bulk_insert.arg(vals.join(QSL(", "))); |
| 1920 | |
| 1921 | QMutexLocker lck(db_mutex); |
| 1922 | |
| 1923 | auto bulk_query = QSqlQuery(final_bulk, db); |
| 1924 | auto bulk_error = bulk_query.lastError(); |
| 1925 | |
| 1926 | if (bulk_error.isValid()) { |
| 1927 | QString txt = bulk_error.text() + bulk_error.databaseText() + bulk_error.driverText(); |
| 1928 | |
| 1929 | qCriticalNN << LOGSEC_DB << "Failed bulk insert of articles:" << QUOTE_W_SPACE_DOT(txt); |
| 1930 | } |
| 1931 | else { |
| 1932 | // OK, we bulk-inserted many messages but the thing is that they do not |
| 1933 | // have their DB IDs fetched in objects, therefore labels cannot be assigned etc. |
| 1934 | // |
| 1935 | // We can calculate real IDs because of how "auto-increment" algorithms work. |
| 1936 | // https://www.sqlite.org/autoinc.html |
| 1937 | // https://mariadb.com/kb/en/auto_increment |
| 1938 | int last_msg_id = bulk_query.lastInsertId().toInt(); |
| 1939 | |
| 1940 | for (int l = i, c = 1; l < (i + batch_length); l++, c++) { |
| 1941 | Message* msg = msgs_to_insert[l]; |
| 1942 | |
| 1943 | if (msg->m_title.isEmpty()) { |
| 1944 | // This article was not for sure inserted. Tweak |
| 1945 | // next ID calculation. |
| 1946 | c--; |
| 1947 | continue; |
| 1948 | } |
| 1949 | |
| 1950 | msg->m_insertedUpdated = true; |
| 1951 | msg->m_id = last_msg_id - batch_length + c; |
| 1952 | |
| 1953 | if (!msg->m_isRead) { |
| 1954 | updated_messages.m_unread.append(*msg); |
| 1955 | } |
| 1956 | |
| 1957 | updated_messages.m_all.append(*msg); |
| 1958 | } |
| 1959 | } |
| 1960 | } |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | const bool uses_online_labels = Globals::hasFlag(feed->getParentServiceRoot()->supportedLabelOperations(), |
| 1965 | ServiceRoot::LabelOperation::Synchronised); |
| 1966 | |
| 1967 | for (Message& message : messages) { |
| 1968 | if (!message.m_customId.isEmpty() || message.m_id > 0) { |
| 1969 | QMutexLocker lck(db_mutex); |
| 1970 | // bool lbls_changed = false; |
| 1971 | |
| 1972 | if (uses_online_labels) { |
| 1973 | // Store all labels obtained from server. |
| 1974 | setLabelsForMessage(db, message.m_assignedLabels, message); |
| 1975 | // lbls_changed = true; |
| 1976 | } |
| 1977 | |
| 1978 | // Adjust labels tweaked by filters. |
| 1979 | for (Label* assigned_by_filter : message.m_assignedLabelsByFilter) { |
| 1980 | assigned_by_filter->assignToMessage(message, false); |
| 1981 | // lbls_changed = true; |
| 1982 | } |
| 1983 | |
| 1984 | for (Label* removed_by_filter : message.m_deassignedLabelsByFilter) { |
| 1985 | removed_by_filter->deassignFromMessage(message, false); |
| 1986 | // lbls_changed = true; |
| 1987 | } |
| 1988 | |
| 1989 | // NOTE: This is likely not needed at all |
| 1990 | // as this feature was semi-broken anyway, it was trigerred |
| 1991 | // even when labels were added (via article filtering or on service server) |
| 1992 | // and the same labels were already assigned in local DB. |
| 1993 | // In other words, label assignment differences were not correctly |
| 1994 | // taken into account. |
| 1995 | /* |
| 1996 | if (lbls_changed && !message.m_insertedUpdated) { |
| 1997 | // This article was not inserted/updated in DB because its contents did not change |
| 1998 | // but its assigned labels were changed. Therefore we must count article |
| 1999 | // as updated. |
| 2000 | if (!message.m_isRead) { |
| 2001 | updated_messages.m_unread.append(message); |
| 2002 | } |
| 2003 | |
| 2004 | updated_messages.m_all.append(message); |
| 2005 | } |
| 2006 | */ |
| 2007 | } |
| 2008 | else { |
| 2009 | qCriticalNN << LOGSEC_DB << "Cannot set labels for message" << QUOTE_W_SPACE(message.m_title) |
| 2010 | << "because we don't have ID or custom ID."; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | // Now, fixup custom IDS for messages which initially did not have them, |
| 2015 | // just to keep the data consistent. |
| 2016 | QMutexLocker lck(db_mutex); |
| 2017 | |
| 2018 | QSqlQuery fixup_custom_ids_query(QSL("UPDATE Messages " |
| 2019 | "SET custom_id = id " |
| 2020 | "WHERE custom_id IS NULL OR custom_id = '';"), |
| 2021 | db); |
| 2022 | QSqlError fixup_custom_ids_error = fixup_custom_ids_query.lastError(); |
| 2023 | |
| 2024 | if (fixup_custom_ids_error.isValid()) { |
| 2025 | qCriticalNN << LOGSEC_DB |
| 2026 | << "Failed to set custom ID for all messages:" << QUOTE_W_SPACE_DOT(fixup_custom_ids_error.text()); |
| 2027 | } |
| 2028 | |
| 2029 | if (ok != nullptr) { |
| 2030 | *ok = true; |
| 2031 | } |
| 2032 | |
| 2033 | return updated_messages; |
| 2034 | } |
| 2035 | |
| 2036 | bool DatabaseQueries::purgeMessagesFromBin(const QSqlDatabase& db, bool clear_only_read, int account_id) { |
| 2037 | QSqlQuery q(db); |
| 2038 | |
| 2039 | q.setForwardOnly(true); |
| 2040 | |
| 2041 | if (clear_only_read) { |
| 2042 | q.prepare(QSL("UPDATE Messages SET is_pdeleted = 1 WHERE is_read = 1 AND is_deleted = 1 AND account_id = " |
| 2043 | ":account_id;")); |
| 2044 | } |
| 2045 | else { |
| 2046 | q.prepare(QSL("UPDATE Messages SET is_pdeleted = 1 WHERE is_deleted = 1 AND account_id = :account_id;")); |
| 2047 | } |
| 2048 | |
| 2049 | q.bindValue(QSL(":account_id"), account_id); |
| 2050 | return q.exec(); |
| 2051 | } |
| 2052 | |
| 2053 | bool DatabaseQueries::deleteAccount(const QSqlDatabase& db, ServiceRoot* account) { |
| 2054 | moveItem(account, false, true, {}, db); |
| 2055 | |
| 2056 | QSqlQuery query(db); |
| 2057 | |
| 2058 | query.setForwardOnly(true); |
| 2059 | QStringList queries; |
| 2060 | |
| 2061 | queries << QSL("DELETE FROM MessageFiltersInFeeds WHERE account_id = :account_id;") |
| 2062 | << QSL("DELETE FROM Messages WHERE account_id = :account_id;") |
| 2063 | << QSL("DELETE FROM Feeds WHERE account_id = :account_id;") |
| 2064 | << QSL("DELETE FROM Categories WHERE account_id = :account_id;") |
| 2065 | << QSL("DELETE FROM Labels WHERE account_id = :account_id;") |
| 2066 | << QSL("DELETE FROM Accounts WHERE id = :account_id;"); |
| 2067 | |
| 2068 | for (const QString& q : std::as_const(queries)) { |
| 2069 | query.prepare(q); |
| 2070 | query.bindValue(QSL(":account_id"), account->accountId()); |
| 2071 | |
| 2072 | if (!query.exec()) { |
| 2073 | qCriticalNN << LOGSEC_DB << "Removing of account from DB failed, this is critical: '" << query.lastError().text() |
| 2074 | << "'."; |
| 2075 | return false; |
| 2076 | } |
| 2077 | else { |
| 2078 | query.finish(); |
| 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | return true; |
| 2083 | } |
| 2084 | |
| 2085 | bool DatabaseQueries::deleteAccountData(const QSqlDatabase& db, |
| 2086 | int account_id, |
| 2087 | bool delete_messages_too, |
| 2088 | bool delete_labels_too) { |
| 2089 | bool result = true; |
| 2090 | QSqlQuery q(db); |
| 2091 | |
| 2092 | q.setForwardOnly(true); |
| 2093 | |
| 2094 | if (delete_messages_too) { |
| 2095 | q.prepare(QSL("DELETE FROM Messages WHERE account_id = :account_id;")); |
| 2096 | q.bindValue(QSL(":account_id"), account_id); |
| 2097 | result &= q.exec(); |
| 2098 | } |
| 2099 | |
| 2100 | q.prepare(QSL("DELETE FROM Feeds WHERE account_id = :account_id;")); |
| 2101 | q.bindValue(QSL(":account_id"), account_id); |
| 2102 | result &= q.exec(); |
| 2103 | |
| 2104 | q.prepare(QSL("DELETE FROM Categories WHERE account_id = :account_id;")); |
| 2105 | q.bindValue(QSL(":account_id"), account_id); |
| 2106 | result &= q.exec(); |
| 2107 | |
| 2108 | if (delete_labels_too) { |
| 2109 | q.prepare(QSL("DELETE FROM Labels WHERE account_id = :account_id;")); |
| 2110 | q.bindValue(QSL(":account_id"), account_id); |
| 2111 | result &= q.exec(); |
| 2112 | } |
| 2113 | |
| 2114 | return result; |
| 2115 | } |
| 2116 | |
| 2117 | bool DatabaseQueries::cleanLabelledMessages(const QSqlDatabase& db, bool clean_read_only, Label* label) { |
| 2118 | QSqlQuery q(db); |
| 2119 | |
| 2120 | q.setForwardOnly(true); |
| 2121 | |
| 2122 | if (clean_read_only) { |
| 2123 | q.prepare(QSL("UPDATE Messages SET is_deleted = :deleted " |
| 2124 | "WHERE " |
| 2125 | " is_deleted = 0 AND " |
| 2126 | " is_pdeleted = 0 AND " |
| 2127 | " is_read = 1 AND " |
| 2128 | " account_id = :account_id AND " |
| 2129 | " labels LIKE :label;")); |
| 2130 | } |
| 2131 | else { |
| 2132 | q.prepare(QSL("UPDATE Messages SET is_deleted = :deleted " |
| 2133 | "WHERE " |
| 2134 | " is_deleted = 0 AND " |
| 2135 | " is_pdeleted = 0 AND " |
| 2136 | " account_id = :account_id AND " |
| 2137 | " labels LIKE :label;")); |
| 2138 | } |
| 2139 | |
| 2140 | q.bindValue(QSL(":deleted"), 1); |
| 2141 | q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId()); |
| 2142 | q.bindValue(QSL(":label"), QSL("%.%1.%").arg(label->customId())); |
| 2143 | |
| 2144 | if (!q.exec()) { |
| 2145 | qWarningNN << LOGSEC_DB << "Cleaning of labelled messages failed:" << QUOTE_W_SPACE_DOT(q.lastError().text()); |
| 2146 | return false; |
| 2147 | } |
| 2148 | else { |
| 2149 | return true; |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | void DatabaseQueries::cleanProbedMessages(const QSqlDatabase& db, bool clean_read_only, Search* probe) { |
| 2154 | QSqlQuery q(db); |
| 2155 | |
| 2156 | q.setForwardOnly(true); |
| 2157 | |
| 2158 | if (clean_read_only) { |
| 2159 | q.prepare(QSL("UPDATE Messages SET is_deleted = :deleted " |
| 2160 | "WHERE " |
| 2161 | " is_deleted = 0 AND " |
| 2162 | " is_pdeleted = 0 AND " |
| 2163 | " is_read = 1 AND " |
| 2164 | " account_id = :account_id AND " |
| 2165 | " (title REGEXP :fltr OR contents REGEXP :fltr);")); |
| 2166 | } |
| 2167 | else { |
| 2168 | q.prepare(QSL("UPDATE Messages SET is_deleted = :deleted " |
| 2169 | "WHERE " |
| 2170 | " is_deleted = 0 AND " |
| 2171 | " is_pdeleted = 0 AND " |
| 2172 | " account_id = :account_id AND " |
| 2173 | " (title REGEXP :fltr OR contents REGEXP :fltr);")); |
| 2174 | } |
| 2175 | |
| 2176 | q.bindValue(QSL(":deleted"), 1); |
| 2177 | q.bindValue(QSL(":account_id"), probe->getParentServiceRoot()->accountId()); |
| 2178 | q.bindValue(QSL(":fltr"), probe->filter()); |
| 2179 | |
| 2180 | if (!q.exec()) { |
| 2181 | throw ApplicationException(q.lastError().text()); |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | bool DatabaseQueries::cleanImportantMessages(const QSqlDatabase& db, bool clean_read_only, int account_id) { |
| 2186 | QSqlQuery q(db); |
| 2187 | |
| 2188 | q.setForwardOnly(true); |
| 2189 | |
| 2190 | if (clean_read_only) { |
| 2191 | q.prepare(QSL("UPDATE Messages SET is_deleted = :deleted " |
| 2192 | "WHERE is_important = 1 AND is_deleted = 0 AND is_pdeleted = 0 AND is_read = 1 AND account_id = " |
| 2193 | ":account_id;")); |
| 2194 | } |
| 2195 | else { |
| 2196 | q.prepare(QSL("UPDATE Messages SET is_deleted = :deleted " |
| 2197 | "WHERE is_important = 1 AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;")); |
| 2198 | } |
| 2199 | |
| 2200 | q.bindValue(QSL(":deleted"), 1); |
| 2201 | q.bindValue(QSL(":account_id"), account_id); |
| 2202 | |
| 2203 | if (!q.exec()) { |
| 2204 | qWarningNN << LOGSEC_DB << "Cleaning of important messages failed: '" << q.lastError().text() << "'."; |
| 2205 | return false; |
| 2206 | } |
| 2207 | else { |
| 2208 | return true; |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | bool DatabaseQueries::cleanUnreadMessages(const QSqlDatabase& db, int account_id) { |
| 2213 | QSqlQuery q(db); |
| 2214 | |
| 2215 | q.setForwardOnly(true); |
| 2216 | q.prepare(QSL("UPDATE Messages SET is_deleted = :deleted " |
| 2217 | "WHERE is_deleted = 0 AND is_pdeleted = 0 AND is_read = 0 AND account_id = :account_id;")); |
| 2218 | |
| 2219 | q.bindValue(QSL(":deleted"), 1); |
| 2220 | q.bindValue(QSL(":account_id"), account_id); |
| 2221 | |
| 2222 | if (!q.exec()) { |
| 2223 | qWarningNN << LOGSEC_DB << "Cleaning of unread messages failed: '" << q.lastError().text() << "'."; |
| 2224 | return false; |
| 2225 | } |
| 2226 | else { |
| 2227 | return true; |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | bool DatabaseQueries::cleanFeeds(const QSqlDatabase& db, const QStringList& ids, bool clean_read_only, int account_id) { |
| 2232 | QSqlQuery q(db); |
| 2233 | |
| 2234 | q.setForwardOnly(true); |
| 2235 | |
| 2236 | if (clean_read_only) { |
| 2237 | q.prepare(QString("UPDATE Messages SET is_deleted = :deleted " |
| 2238 | "WHERE feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0 AND is_read = 1 AND account_id = " |
| 2239 | ":account_id;") |
| 2240 | .arg(ids.join(QSL(", ")))); |
| 2241 | } |
| 2242 | else { |
| 2243 | q.prepare(QString("UPDATE Messages SET is_deleted = :deleted " |
| 2244 | "WHERE feed IN (%1) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;") |
| 2245 | .arg(ids.join(QSL(", ")))); |
| 2246 | } |
| 2247 | |
| 2248 | q.bindValue(QSL(":deleted"), 1); |
| 2249 | q.bindValue(QSL(":account_id"), account_id); |
| 2250 | |
| 2251 | if (!q.exec()) { |
| 2252 | qWarningNN << LOGSEC_DB << "Cleaning of feeds failed: '" << q.lastError().text() << "'."; |
| 2253 | return false; |
| 2254 | } |
| 2255 | else { |
| 2256 | return true; |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | bool DatabaseQueries::purgeLeftoverMessageFilterAssignments(const QSqlDatabase& db, int account_id) { |
| 2261 | QSqlQuery q(db); |
| 2262 | |
| 2263 | q.setForwardOnly(true); |
| 2264 | q.prepare(QSL("DELETE FROM MessageFiltersInFeeds " |
| 2265 | "WHERE account_id = :account_id AND " |
| 2266 | "feed_custom_id NOT IN (SELECT custom_id FROM Feeds WHERE account_id = :account_id);")); |
| 2267 | q.bindValue(QSL(":account_id"), account_id); |
| 2268 | |
| 2269 | if (!q.exec()) { |
| 2270 | qWarningNN << LOGSEC_DB << "Removing of leftover message filter assignments failed: '" << q.lastError().text() |
| 2271 | << "'."; |
| 2272 | return false; |
| 2273 | } |
| 2274 | else { |
| 2275 | return true; |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | bool DatabaseQueries::purgeLeftoverMessages(const QSqlDatabase& db, int account_id) { |
| 2280 | QSqlQuery q(db); |
| 2281 | |
| 2282 | q.setForwardOnly(true); |
| 2283 | q.prepare(QSL("DELETE FROM Messages " |
| 2284 | "WHERE account_id = :account_id AND feed NOT IN (SELECT custom_id FROM Feeds WHERE account_id = " |
| 2285 | ":account_id);")); |
| 2286 | q.bindValue(QSL(":account_id"), account_id); |
| 2287 | |
| 2288 | if (!q.exec()) { |
| 2289 | qWarningNN << LOGSEC_DB << "Removing of leftover messages failed: '" << q.lastError().text() << "'."; |
| 2290 | return false; |
| 2291 | } |
| 2292 | else { |
| 2293 | return true; |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | void DatabaseQueries::storeAccountTree(const QSqlDatabase& db, RootItem* tree_root, int account_id) { |
| 2298 | // Iterate all children. |
| 2299 | auto str = tree_root->getSubTree<RootItem>(); |
| 2300 | |
| 2301 | for (RootItem* child : std::as_const(str)) { |
| 2302 | if (child->kind() == RootItem::Kind::Category) { |
| 2303 | createOverwriteCategory(db, child->toCategory(), account_id, child->parent()->id()); |
| 2304 | } |
| 2305 | else if (child->kind() == RootItem::Kind::Feed) { |
| 2306 | createOverwriteFeed(db, child->toFeed(), account_id, child->parent()->id()); |
| 2307 | } |
| 2308 | else if (child->kind() == RootItem::Kind::Labels) { |
| 2309 | // Add all labels. |
| 2310 | auto ch = child->childItems(); |
| 2311 | |
| 2312 | for (RootItem* lbl : std::as_const(ch)) { |
| 2313 | Label* label = lbl->toLabel(); |
| 2314 | |
| 2315 | createLabel(db, label, account_id); |
| 2316 | } |
| 2317 | } |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | QStringList DatabaseQueries::customIdsOfMessagesFromAccount(const QSqlDatabase& db, |
| 2322 | RootItem::ReadStatus target_read, |
| 2323 | int account_id, |
| 2324 | bool* ok) { |
| 2325 | QSqlQuery q(db); |
| 2326 | QStringList ids; |
| 2327 | |
| 2328 | q.setForwardOnly(true); |
| 2329 | q.prepare(QSL("SELECT custom_id FROM Messages " |
| 2330 | "WHERE is_read = :read AND is_pdeleted = 0 AND account_id = :account_id;")); |
| 2331 | q.bindValue(QSL(":account_id"), account_id); |
| 2332 | q.bindValue(QSL(":read"), target_read == RootItem::ReadStatus::Read ? 0 : 1); |
| 2333 | |
| 2334 | if (ok != nullptr) { |
| 2335 | *ok = q.exec(); |
| 2336 | } |
| 2337 | else { |
| 2338 | q.exec(); |
| 2339 | } |
| 2340 | |
| 2341 | while (q.next()) { |
| 2342 | ids.append(q.value(0).toString()); |
| 2343 | } |
| 2344 | |
| 2345 | return ids; |
| 2346 | } |
| 2347 | |
| 2348 | QStringList DatabaseQueries::customIdsOfMessagesFromLabel(const QSqlDatabase& db, |
| 2349 | Label* label, |
| 2350 | RootItem::ReadStatus target_read, |
| 2351 | bool* ok) { |
| 2352 | QSqlQuery q(db); |
| 2353 | QStringList ids; |
| 2354 | |
| 2355 | q.setForwardOnly(true); |
| 2356 | q.prepare(QSL("SELECT custom_id FROM Messages " |
| 2357 | "WHERE " |
| 2358 | " is_read = :read AND " |
| 2359 | " is_deleted = 0 AND " |
| 2360 | " is_pdeleted = 0 AND " |
| 2361 | " account_id = :account_id AND " |
| 2362 | " labels LIKE :label;")); |
| 2363 | q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId()); |
| 2364 | q.bindValue(QSL(":label"), QSL("%.%1.%").arg(label->customId())); |
| 2365 | q.bindValue(QSL(":read"), target_read == RootItem::ReadStatus::Read ? 0 : 1); |
| 2366 | |
| 2367 | if (ok != nullptr) { |
| 2368 | *ok = q.exec(); |
| 2369 | } |
| 2370 | else { |
| 2371 | q.exec(); |
| 2372 | } |
| 2373 | |
| 2374 | while (q.next()) { |
| 2375 | ids.append(q.value(0).toString()); |
| 2376 | } |
| 2377 | |
| 2378 | return ids; |
| 2379 | } |
| 2380 | |
| 2381 | void DatabaseQueries::markProbeReadUnread(const QSqlDatabase& db, Search* probe, RootItem::ReadStatus read) { |
| 2382 | QSqlQuery q(db); |
| 2383 | |
| 2384 | q.setForwardOnly(true); |
| 2385 | q.prepare(QSL("UPDATE Messages SET is_read = :read " |
| 2386 | "WHERE " |
| 2387 | " is_deleted = 0 AND " |
| 2388 | " is_pdeleted = 0 AND " |
| 2389 | " account_id = :account_id AND " |
| 2390 | " (title REGEXP :fltr OR contents REGEXP :fltr);")); |
| 2391 | q.bindValue(QSL(":read"), read == RootItem::ReadStatus::Read ? 1 : 0); |
| 2392 | q.bindValue(QSL(":account_id"), probe->getParentServiceRoot()->accountId()); |
| 2393 | q.bindValue(QSL(":fltr"), probe->filter()); |
| 2394 | |
| 2395 | if (!q.exec()) { |
| 2396 | throw ApplicationException(q.lastError().text()); |
| 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | QStringList DatabaseQueries::customIdsOfMessagesFromProbe(const QSqlDatabase& db, |
| 2401 | Search* probe, |
| 2402 | RootItem::ReadStatus target_read) { |
| 2403 | QSqlQuery q(db); |
| 2404 | QStringList ids; |
| 2405 | |
| 2406 | q.setForwardOnly(true); |
| 2407 | q.prepare(QSL("SELECT custom_id FROM Messages " |
| 2408 | "WHERE " |
| 2409 | " is_read = :read AND " |
| 2410 | " is_deleted = 0 AND " |
| 2411 | " is_pdeleted = 0 AND " |
| 2412 | " account_id = :account_id AND " |
| 2413 | " (title REGEXP :fltr OR contents REGEXP :fltr);")); |
| 2414 | q.bindValue(QSL(":account_id"), probe->getParentServiceRoot()->accountId()); |
| 2415 | q.bindValue(QSL(":read"), target_read == RootItem::ReadStatus::Read ? 0 : 1); |
| 2416 | q.bindValue(QSL(":fltr"), probe->filter()); |
| 2417 | |
| 2418 | if (!q.exec()) { |
| 2419 | throw ApplicationException(q.lastError().text()); |
| 2420 | } |
| 2421 | |
| 2422 | while (q.next()) { |
| 2423 | ids.append(q.value(0).toString()); |
| 2424 | } |
| 2425 | |
| 2426 | return ids; |
| 2427 | } |
| 2428 | |
| 2429 | QStringList DatabaseQueries::customIdsOfImportantMessages(const QSqlDatabase& db, |
| 2430 | RootItem::ReadStatus target_read, |
| 2431 | int account_id, |
| 2432 | bool* ok) { |
| 2433 | QSqlQuery q(db); |
| 2434 | QStringList ids; |
| 2435 | |
| 2436 | q.setForwardOnly(true); |
| 2437 | q.prepare(QSL("SELECT custom_id FROM Messages " |
| 2438 | "WHERE " |
| 2439 | "is_read = :read AND is_important = 1 AND is_deleted = 0 AND " |
| 2440 | "is_pdeleted = 0 AND account_id = :account_id;")); |
| 2441 | q.bindValue(QSL(":account_id"), account_id); |
| 2442 | q.bindValue(QSL(":read"), target_read == RootItem::ReadStatus::Read ? 0 : 1); |
| 2443 | |
| 2444 | if (ok != nullptr) { |
| 2445 | *ok = q.exec(); |
| 2446 | } |
| 2447 | else { |
| 2448 | q.exec(); |
| 2449 | } |
| 2450 | |
| 2451 | while (q.next()) { |
| 2452 | ids.append(q.value(0).toString()); |
| 2453 | } |
| 2454 | |
| 2455 | return ids; |
| 2456 | } |
| 2457 | |
| 2458 | QStringList DatabaseQueries::customIdsOfUnreadMessages(const QSqlDatabase& db, int account_id, bool* ok) { |
| 2459 | QSqlQuery q(db); |
| 2460 | QStringList ids; |
| 2461 | |
| 2462 | q.setForwardOnly(true); |
| 2463 | q.prepare(QSL("SELECT custom_id FROM Messages " |
| 2464 | "WHERE is_read = 0 AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;")); |
| 2465 | q.bindValue(QSL(":account_id"), account_id); |
| 2466 | |
| 2467 | if (ok != nullptr) { |
| 2468 | *ok = q.exec(); |
| 2469 | } |
| 2470 | else { |
| 2471 | q.exec(); |
| 2472 | } |
| 2473 | |
| 2474 | while (q.next()) { |
| 2475 | ids.append(q.value(0).toString()); |
| 2476 | } |
| 2477 | |
| 2478 | return ids; |
| 2479 | } |
| 2480 | |
| 2481 | QStringList DatabaseQueries::customIdsOfMessagesFromBin(const QSqlDatabase& db, |
| 2482 | RootItem::ReadStatus target_read, |
| 2483 | int account_id, |
| 2484 | bool* ok) { |
| 2485 | QSqlQuery q(db); |
| 2486 | QStringList ids; |
| 2487 | |
| 2488 | q.setForwardOnly(true); |
| 2489 | q.prepare(QSL("SELECT custom_id FROM Messages " |
| 2490 | "WHERE is_read = :read AND is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;")); |
| 2491 | q.bindValue(QSL(":account_id"), account_id); |
| 2492 | q.bindValue(QSL(":read"), target_read == RootItem::ReadStatus::Read ? 0 : 1); |
| 2493 | |
| 2494 | if (ok != nullptr) { |
| 2495 | *ok = q.exec(); |
| 2496 | } |
| 2497 | else { |
| 2498 | q.exec(); |
| 2499 | } |
| 2500 | |
| 2501 | while (q.next()) { |
| 2502 | ids.append(q.value(0).toString()); |
| 2503 | } |
| 2504 | |
| 2505 | return ids; |
| 2506 | } |
| 2507 | |
| 2508 | QStringList DatabaseQueries::customIdsOfMessagesFromFeed(const QSqlDatabase& db, |
| 2509 | const QString& feed_custom_id, |
| 2510 | RootItem::ReadStatus target_read, |
| 2511 | int account_id, |
| 2512 | bool* ok) { |
| 2513 | QSqlQuery q(db); |
| 2514 | QStringList ids; |
| 2515 | |
| 2516 | q.setForwardOnly(true); |
| 2517 | q.prepare(QSL("SELECT custom_id FROM Messages " |
| 2518 | "WHERE is_read = :read AND is_deleted = 0 AND " |
| 2519 | "is_pdeleted = 0 AND feed = :feed AND account_id = :account_id;")); |
| 2520 | q.bindValue(QSL(":account_id"), account_id); |
| 2521 | q.bindValue(QSL(":feed"), feed_custom_id); |
| 2522 | q.bindValue(QSL(":read"), target_read == RootItem::ReadStatus::Read ? 0 : 1); |
| 2523 | |
| 2524 | if (ok != nullptr) { |
| 2525 | *ok = q.exec(); |
| 2526 | } |
| 2527 | else { |
| 2528 | q.exec(); |
| 2529 | } |
| 2530 | |
| 2531 | while (q.next()) { |
| 2532 | ids.append(q.value(0).toString()); |
| 2533 | } |
| 2534 | |
| 2535 | return ids; |
| 2536 | } |
| 2537 | |
| 2538 | void DatabaseQueries::createOverwriteCategory(const QSqlDatabase& db, |
| 2539 | Category* category, |
| 2540 | int account_id, |
| 2541 | int new_parent_id) { |
| 2542 | QSqlQuery q(db); |
| 2543 | int next_sort_order; |
| 2544 | |
| 2545 | if (category->id() <= 0 || (category->parent() != nullptr && category->parent()->id() != new_parent_id)) { |
| 2546 | q.prepare(QSL("SELECT MAX(ordr) FROM Categories WHERE account_id = :account_id AND parent_id = :parent_id;")); |
| 2547 | q.bindValue(QSL(":account_id"), account_id); |
| 2548 | q.bindValue(QSL(":parent_id"), new_parent_id); |
| 2549 | |
| 2550 | if (!q.exec() || !q.next()) { |
| 2551 | throw ApplicationException(q.lastError().text()); |
| 2552 | } |
| 2553 | |
| 2554 | next_sort_order = (q.value(0).isNull() ? -1 : q.value(0).toInt()) + 1; |
| 2555 | q.finish(); |
| 2556 | } |
| 2557 | else { |
| 2558 | next_sort_order = category->sortOrder(); |
| 2559 | } |
| 2560 | |
| 2561 | if (category->id() <= 0) { |
| 2562 | // We need to insert category first. |
| 2563 | q.prepare(QSL("INSERT INTO " |
| 2564 | "Categories (parent_id, ordr, title, date_created, account_id) " |
| 2565 | "VALUES (0, 0, 'new', 0, %1);") |
| 2566 | .arg(QString::number(account_id))); |
| 2567 | |
| 2568 | if (!q.exec()) { |
| 2569 | throw ApplicationException(q.lastError().text()); |
| 2570 | } |
| 2571 | else { |
| 2572 | category->setId(q.lastInsertId().toInt()); |
| 2573 | } |
| 2574 | } |
| 2575 | else if (category->parent() != nullptr && category->parent()->id() != new_parent_id) { |
| 2576 | // Category is moving between parents. |
| 2577 | // 1. Move category to bottom of current parent. |
| 2578 | // 2. Assign proper new sort order. |
| 2579 | // |
| 2580 | // NOTE: The category will get reassigned to new parent usually after this method |
| 2581 | // completes by the caller. |
| 2582 | moveItem(category, false, true, {}, db); |
| 2583 | } |
| 2584 | |
| 2585 | // Restore to correct sort order. |
| 2586 | category->setSortOrder(next_sort_order); |
| 2587 | |
| 2588 | q.prepare("UPDATE Categories " |
| 2589 | "SET parent_id = :parent_id, ordr = :ordr, title = :title, description = :description, date_created = " |
| 2590 | ":date_created, " |
| 2591 | " icon = :icon, account_id = :account_id, custom_id = :custom_id " |
| 2592 | "WHERE id = :id;"); |
| 2593 | q.bindValue(QSL(":parent_id"), new_parent_id); |
| 2594 | q.bindValue(QSL(":title"), category->title()); |
| 2595 | q.bindValue(QSL(":description"), category->description()); |
| 2596 | q.bindValue(QSL(":date_created"), category->creationDate().toMSecsSinceEpoch()); |
| 2597 | q.bindValue(QSL(":icon"), qApp->icons()->toByteArray(category->icon())); |
| 2598 | q.bindValue(QSL(":account_id"), account_id); |
| 2599 | q.bindValue(QSL(":custom_id"), category->customId()); |
| 2600 | q.bindValue(QSL(":id"), category->id()); |
| 2601 | q.bindValue(QSL(":ordr"), category->sortOrder()); |
| 2602 | |
| 2603 | if (!q.exec()) { |
| 2604 | throw ApplicationException(q.lastError().text()); |
| 2605 | } |
| 2606 | } |
| 2607 | |
| 2608 | void DatabaseQueries::createOverwriteFeed(const QSqlDatabase& db, Feed* feed, int account_id, int new_parent_id) { |
| 2609 | QSqlQuery q(db); |
| 2610 | int next_sort_order; |
| 2611 | |
| 2612 | if (feed->id() <= 0 || (feed->parent() != nullptr && feed->parent()->id() != new_parent_id)) { |
| 2613 | // We either insert completely new feed or we move feed |
| 2614 | // to new parent. Get new viable sort order. |
| 2615 | q.prepare(QSL("SELECT MAX(ordr) FROM Feeds WHERE account_id = :account_id AND category = :category;")); |
| 2616 | q.bindValue(QSL(":account_id"), account_id); |
| 2617 | q.bindValue(QSL(":category"), new_parent_id); |
| 2618 | |
| 2619 | if (!q.exec() || !q.next()) { |
| 2620 | throw ApplicationException(q.lastError().text()); |
| 2621 | } |
| 2622 | |
| 2623 | next_sort_order = (q.value(0).isNull() ? -1 : q.value(0).toInt()) + 1; |
| 2624 | q.finish(); |
| 2625 | } |
| 2626 | else { |
| 2627 | next_sort_order = feed->sortOrder(); |
| 2628 | } |
| 2629 | |
| 2630 | if (feed->id() <= 0) { |
| 2631 | // We need to insert feed first. |
| 2632 | q.prepare(QSL("INSERT INTO " |
| 2633 | "Feeds (title, ordr, date_created, category, update_type, update_interval, account_id, custom_id) " |
| 2634 | "VALUES ('new', 0, 0, 0, 0, 1, %1, 'new');") |
| 2635 | .arg(QString::number(account_id))); |
| 2636 | |
| 2637 | if (!q.exec()) { |
| 2638 | throw ApplicationException(q.lastError().text()); |
| 2639 | } |
| 2640 | else { |
| 2641 | feed->setId(q.lastInsertId().toInt()); |
| 2642 | |
| 2643 | if (feed->customId().isEmpty()) { |
| 2644 | feed->setCustomId(QString::number(feed->id())); |
| 2645 | } |
| 2646 | } |
| 2647 | } |
| 2648 | else if (feed->parent() != nullptr && feed->parent()->id() != new_parent_id) { |
| 2649 | // Feed is moving between categories. |
| 2650 | // 1. Move feed to bottom of current category. |
| 2651 | // 2. Assign proper new sort order. |
| 2652 | // |
| 2653 | // NOTE: The feed will get reassigned to new parent usually after this method |
| 2654 | // completes by the caller. |
| 2655 | moveItem(feed, false, true, {}, db); |
| 2656 | } |
| 2657 | |
| 2658 | // Restore to correct sort order. |
| 2659 | feed->setSortOrder(next_sort_order); |
| 2660 | |
| 2661 | q.prepare("UPDATE Feeds " |
| 2662 | "SET " |
| 2663 | "title = :title, " |
| 2664 | "ordr = :ordr, " |
| 2665 | "description = :description, " |
| 2666 | "date_created = :date_created, " |
| 2667 | "icon = :icon, " |
| 2668 | "category = :category, " |
| 2669 | "source = :source, " |
| 2670 | "update_type = :update_type, " |
| 2671 | "update_interval = :update_interval, " |
| 2672 | "is_off = :is_off, " |
| 2673 | "is_quiet = :is_quiet, " |
| 2674 | "open_articles = :open_articles, " |
| 2675 | "is_rtl = :is_rtl, " |
| 2676 | "add_any_datetime_articles = :add_any_datetime_articles, " |
| 2677 | "datetime_to_avoid = :datetime_to_avoid, " |
| 2678 | "keep_article_customize = :keep_article_customize, " |
| 2679 | "keep_article_count = :keep_article_count, " |
| 2680 | "keep_unread_articles = :keep_unread_articles, " |
| 2681 | "keep_starred_articles = :keep_starred_articles, " |
| 2682 | "recycle_articles = :recycle_articles, " |
| 2683 | "account_id = :account_id, " |
| 2684 | "custom_id = :custom_id, " |
| 2685 | "custom_data = :custom_data " |
| 2686 | "WHERE id = :id;"); |
| 2687 | q.bindValue(QSL(":title"), feed->title()); |
| 2688 | q.bindValue(QSL(":description"), feed->description()); |
| 2689 | q.bindValue(QSL(":date_created"), feed->creationDate().toMSecsSinceEpoch()); |
| 2690 | q.bindValue(QSL(":icon"), qApp->icons()->toByteArray(feed->icon())); |
| 2691 | q.bindValue(QSL(":category"), new_parent_id); |
| 2692 | q.bindValue(QSL(":source"), feed->source()); |
| 2693 | q.bindValue(QSL(":update_type"), int(feed->autoUpdateType())); |
| 2694 | q.bindValue(QSL(":update_interval"), feed->autoUpdateInterval()); |
| 2695 | q.bindValue(QSL(":account_id"), account_id); |
| 2696 | q.bindValue(QSL(":custom_id"), feed->customId()); |
| 2697 | q.bindValue(QSL(":id"), feed->id()); |
| 2698 | q.bindValue(QSL(":ordr"), feed->sortOrder()); |
| 2699 | q.bindValue(QSL(":is_off"), feed->isSwitchedOff()); |
| 2700 | q.bindValue(QSL(":is_quiet"), feed->isQuiet()); |
| 2701 | q.bindValue(QSL(":open_articles"), feed->openArticlesDirectly()); |
| 2702 | q.bindValue(QSL(":is_rtl"), int(feed->rtlBehavior())); |
| 2703 | |
| 2704 | const Feed::ArticleIgnoreLimit art = feed->articleIgnoreLimit(); |
| 2705 | |
| 2706 | q.bindValue(QSL(":add_any_datetime_articles"), art.m_addAnyArticlesToDb); |
| 2707 | q.bindValue(QSL(":datetime_to_avoid"), |
| 2708 | (art.m_dtToAvoid.isValid() && art.m_dtToAvoid.toMSecsSinceEpoch() > 0) |
| 2709 | ? art.m_dtToAvoid.toMSecsSinceEpoch() |
| 2710 | : art.m_hoursToAvoid); |
| 2711 | |
| 2712 | q.bindValue(QSL(":keep_article_customize"), art.m_customizeLimitting); |
| 2713 | q.bindValue(QSL(":keep_article_count"), art.m_keepCountOfArticles); |
| 2714 | q.bindValue(QSL(":keep_unread_articles"), art.m_doNotRemoveUnread); |
| 2715 | q.bindValue(QSL(":keep_starred_articles"), art.m_doNotRemoveStarred); |
| 2716 | q.bindValue(QSL(":recycle_articles"), art.m_moveToBinDontPurge); |
| 2717 | |
| 2718 | auto custom_data = feed->customDatabaseData(); |
| 2719 | QString serialized_custom_data = serializeCustomData(custom_data); |
| 2720 | |
| 2721 | q.bindValue(QSL(":custom_data"), serialized_custom_data); |
| 2722 | |
| 2723 | if (!q.exec()) { |
| 2724 | throw ApplicationException(q.lastError().text()); |
| 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | void DatabaseQueries::createOverwriteAccount(const QSqlDatabase& db, ServiceRoot* account) { |
| 2729 | QSqlQuery q(db); |
| 2730 | |
| 2731 | if (account->accountId() <= 0) { |
| 2732 | // We need to insert account and generate sort order first. |
| 2733 | if (account->sortOrder() < 0) { |
| 2734 | if (!q.exec(QSL("SELECT MAX(ordr) FROM Accounts;"))) { |
| 2735 | throw ApplicationException(q.lastError().text()); |
| 2736 | } |
| 2737 | |
| 2738 | q.next(); |
| 2739 | |
| 2740 | int next_order = (q.value(0).isNull() ? -1 : q.value(0).toInt()) + 1; |
| 2741 | |
| 2742 | account->setSortOrder(next_order); |
| 2743 | q.finish(); |
| 2744 | } |
| 2745 | |
| 2746 | q.prepare(QSL("INSERT INTO Accounts (ordr, type) " |
| 2747 | "VALUES (0, :type);")); |
| 2748 | q.bindValue(QSL(":type"), account->code()); |
| 2749 | |
| 2750 | if (!q.exec()) { |
| 2751 | throw ApplicationException(q.lastError().text()); |
| 2752 | } |
| 2753 | else { |
| 2754 | account->setAccountId(q.lastInsertId().toInt()); |
| 2755 | } |
| 2756 | } |
| 2757 | |
| 2758 | // Now we construct the SQL update query. |
| 2759 | auto proxy = account->networkProxy(); |
| 2760 | |
| 2761 | q.prepare(QSL("UPDATE Accounts " |
| 2762 | "SET proxy_type = :proxy_type, proxy_host = :proxy_host, proxy_port = :proxy_port, " |
| 2763 | " proxy_username = :proxy_username, proxy_password = :proxy_password, ordr = :ordr, " |
| 2764 | " custom_data = :custom_data " |
| 2765 | "WHERE id = :id")); |
| 2766 | q.bindValue(QSL(":proxy_type"), proxy.type()); |
| 2767 | q.bindValue(QSL(":proxy_host"), proxy.hostName()); |
| 2768 | q.bindValue(QSL(":proxy_port"), proxy.port()); |
| 2769 | q.bindValue(QSL(":proxy_username"), proxy.user()); |
| 2770 | q.bindValue(QSL(":proxy_password"), TextFactory::encrypt(proxy.password())); |
| 2771 | q.bindValue(QSL(":id"), account->accountId()); |
| 2772 | q.bindValue(QSL(":ordr"), account->sortOrder()); |
| 2773 | |
| 2774 | auto custom_data = account->customDatabaseData(); |
| 2775 | QString serialized_custom_data = serializeCustomData(custom_data); |
| 2776 | |
| 2777 | q.bindValue(QSL(":custom_data"), serialized_custom_data); |
| 2778 | |
| 2779 | if (!q.exec()) { |
| 2780 | throw ApplicationException(q.lastError().text()); |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | bool DatabaseQueries::deleteFeed(const QSqlDatabase& db, Feed* feed, int account_id) { |
| 2785 | moveItem(feed, false, true, {}, db); |
| 2786 | |
| 2787 | QSqlQuery q(db); |
| 2788 | |
| 2789 | q.prepare(QSL("DELETE FROM Messages WHERE feed = :feed AND account_id = :account_id;")); |
| 2790 | q.bindValue(QSL(":feed"), feed->customId()); |
| 2791 | q.bindValue(QSL(":account_id"), account_id); |
| 2792 | |
| 2793 | if (!q.exec()) { |
| 2794 | return false; |
| 2795 | } |
| 2796 | |
| 2797 | // Remove feed itself. |
| 2798 | q.prepare(QSL("DELETE FROM Feeds WHERE custom_id = :feed AND account_id = :account_id;")); |
| 2799 | q.bindValue(QSL(":feed"), feed->customId()); |
| 2800 | q.bindValue(QSL(":account_id"), account_id); |
| 2801 | |
| 2802 | return q.exec() && purgeLeftoverMessageFilterAssignments(db, account_id); |
| 2803 | } |
| 2804 | |
| 2805 | bool DatabaseQueries::deleteCategory(const QSqlDatabase& db, Category* category) { |
| 2806 | moveItem(category, false, true, {}, db); |
| 2807 | |
| 2808 | QSqlQuery q(db); |
| 2809 | |
| 2810 | // Remove this category from database. |
| 2811 | q.setForwardOnly(true); |
| 2812 | q.prepare(QSL("DELETE FROM Categories WHERE id = :category;")); |
| 2813 | q.bindValue(QSL(":category"), category->id()); |
| 2814 | return q.exec(); |
| 2815 | } |
| 2816 | |
| 2817 | void DatabaseQueries::moveItem(RootItem* item, |
| 2818 | bool move_top, |
| 2819 | bool move_bottom, |
| 2820 | int move_index, |
| 2821 | const QSqlDatabase& db) { |
| 2822 | if (item->kind() != RootItem::Kind::Feed && item->kind() != RootItem::Kind::Category && |
| 2823 | item->kind() != RootItem::Kind::ServiceRoot) { |
| 2824 | return; |
| 2825 | } |
| 2826 | |
| 2827 | auto neighbors = item->parent()->childItems(); |
| 2828 | int max_sort_order = boolinq::from(neighbors) |
| 2829 | .select([=](RootItem* it) { |
| 2830 | return it->kind() == item->kind() ? it->sortOrder() : 0; |
| 2831 | }) |
| 2832 | .max(); |
| 2833 | |
| 2834 | if ((!move_top && !move_bottom && item->sortOrder() == move_index) || /* Item is already sorted OK. */ |
| 2835 | (!move_top && !move_bottom && |
| 2836 | move_index < 0) || /* Order cannot be smaller than 0 if we do not move to begin/end. */ |
| 2837 | (!move_top && !move_bottom && move_index > max_sort_order) || /* Cannot move past biggest sort order. */ |
| 2838 | (move_top && item->sortOrder() == 0) || /* Item is already on top. */ |
| 2839 | (move_bottom && item->sortOrder() == max_sort_order) || /* Item is already on bottom. */ |
| 2840 | max_sort_order <= 0) { /* We only have 1 item, nothing to sort. */ |
| 2841 | return; |
| 2842 | } |
| 2843 | |
| 2844 | QSqlQuery q(db); |
| 2845 | |
| 2846 | if (move_top) { |
| 2847 | move_index = 0; |
| 2848 | } |
| 2849 | else if (move_bottom) { |
| 2850 | move_index = max_sort_order; |
| 2851 | } |
| 2852 | |
| 2853 | int move_low = qMin(move_index, item->sortOrder()); |
| 2854 | int move_high = qMax(move_index, item->sortOrder()); |
| 2855 | QString parent_field, table_name; |
| 2856 | |
| 2857 | switch (item->kind()) { |
| 2858 | case RootItem::Kind::Feed: |
| 2859 | parent_field = QSL("category"); |
| 2860 | table_name = QSL("Feeds"); |
| 2861 | break; |
| 2862 | |
| 2863 | case RootItem::Kind::Category: |
| 2864 | parent_field = QSL("parent_id"); |
| 2865 | table_name = QSL("Categories"); |
| 2866 | break; |
| 2867 | |
| 2868 | case RootItem::Kind::ServiceRoot: |
| 2869 | table_name = QSL("Accounts"); |
| 2870 | break; |
| 2871 | } |
| 2872 | |
| 2873 | if (item->kind() == RootItem::Kind::ServiceRoot) { |
| 2874 | if (item->sortOrder() > move_index) { |
| 2875 | q.prepare(QSL("UPDATE Accounts SET ordr = ordr + 1 " |
| 2876 | "WHERE ordr < :move_high AND ordr >= :move_low;")); |
| 2877 | } |
| 2878 | else { |
| 2879 | q.prepare(QSL("UPDATE Accounts SET ordr = ordr - 1 " |
| 2880 | "WHERE ordr > :move_low AND ordr <= :move_high;")); |
| 2881 | } |
| 2882 | } |
| 2883 | else { |
| 2884 | if (item->sortOrder() > move_index) { |
| 2885 | q.prepare(QSL("UPDATE %1 SET ordr = ordr + 1 " |
| 2886 | "WHERE account_id = :account_id AND %2 = :category AND ordr < :move_high AND ordr >= :move_low;") |
| 2887 | .arg(table_name, parent_field)); |
| 2888 | } |
| 2889 | else { |
| 2890 | q.prepare(QSL("UPDATE %1 SET ordr = ordr - 1 " |
| 2891 | "WHERE account_id = :account_id AND %2 = :category AND ordr > :move_low AND ordr <= :move_high;") |
| 2892 | .arg(table_name, parent_field)); |
| 2893 | } |
| 2894 | |
| 2895 | q.bindValue(QSL(":account_id"), item->getParentServiceRoot()->accountId()); |
| 2896 | q.bindValue(QSL(":category"), item->parent()->id()); |
| 2897 | } |
| 2898 | |
| 2899 | q.bindValue(QSL(":move_low"), move_low); |
| 2900 | q.bindValue(QSL(":move_high"), move_high); |
| 2901 | |
| 2902 | if (!q.exec()) { |
| 2903 | throw ApplicationException(q.lastError().text()); |
| 2904 | } |
| 2905 | |
| 2906 | q.prepare(QSL("UPDATE %1 SET ordr = :ordr WHERE id = :id;").arg(table_name)); |
| 2907 | q.bindValue(QSL(":id"), |
| 2908 | item->kind() == RootItem::Kind::ServiceRoot ? item->toServiceRoot()->accountId() : item->id()); |
| 2909 | q.bindValue(QSL(":ordr"), move_index); |
| 2910 | |
| 2911 | if (!q.exec()) { |
| 2912 | throw ApplicationException(q.lastError().text()); |
| 2913 | } |
| 2914 | |
| 2915 | // Fix live sort orders. |
| 2916 | if (item->sortOrder() > move_index) { |
| 2917 | boolinq::from(neighbors) |
| 2918 | .where([=](RootItem* it) { |
| 2919 | return it->kind() == item->kind() && it->sortOrder() < move_high && it->sortOrder() >= move_low; |
| 2920 | }) |
| 2921 | .for_each([](RootItem* it) { |
| 2922 | it->setSortOrder(it->sortOrder() + 1); |
| 2923 | }); |
| 2924 | } |
| 2925 | else { |
| 2926 | boolinq::from(neighbors) |
| 2927 | .where([=](RootItem* it) { |
| 2928 | return it->kind() == item->kind() && it->sortOrder() > move_low && it->sortOrder() <= move_high; |
| 2929 | }) |
| 2930 | .for_each([](RootItem* it) { |
| 2931 | it->setSortOrder(it->sortOrder() - 1); |
| 2932 | }); |
| 2933 | } |
| 2934 | |
| 2935 | item->setSortOrder(move_index); |
| 2936 | } |
| 2937 | |
| 2938 | MessageFilter* DatabaseQueries::addMessageFilter(const QSqlDatabase& db, const QString& title, const QString& script) { |
| 2939 | if (!db.driver()->hasFeature(QSqlDriver::DriverFeature::LastInsertId)) { |
| 2940 | throw ApplicationException(QObject::tr("Cannot insert article filter, because current database cannot return last " |
| 2941 | "inserted row ID.")); |
| 2942 | } |
| 2943 | |
| 2944 | QSqlQuery q(db); |
| 2945 | |
| 2946 | q.prepare(QSL("INSERT INTO MessageFilters (name, script) VALUES(:name, :script);")); |
| 2947 | |
| 2948 | q.bindValue(QSL(":name"), title); |
| 2949 | q.bindValue(QSL(":script"), script); |
| 2950 | q.setForwardOnly(true); |
| 2951 | |
| 2952 | if (q.exec()) { |
| 2953 | auto* fltr = new MessageFilter(q.lastInsertId().toInt()); |
| 2954 | |
| 2955 | fltr->setName(title); |
| 2956 | fltr->setScript(script); |
| 2957 | |
| 2958 | return fltr; |
| 2959 | } |
| 2960 | else { |
| 2961 | throw ApplicationException(q.lastError().text()); |
| 2962 | } |
| 2963 | } |
| 2964 | |
| 2965 | void DatabaseQueries::removeMessageFilter(const QSqlDatabase& db, int filter_id, bool* ok) { |
| 2966 | QSqlQuery q(db); |
| 2967 | |
| 2968 | q.prepare(QSL("DELETE FROM MessageFilters WHERE id = :id;")); |
| 2969 | |
| 2970 | q.bindValue(QSL(":id"), filter_id); |
| 2971 | q.setForwardOnly(true); |
| 2972 | |
| 2973 | if (q.exec()) { |
| 2974 | if (ok != nullptr) { |
| 2975 | *ok = true; |
| 2976 | } |
| 2977 | } |
| 2978 | else { |
| 2979 | if (ok != nullptr) { |
| 2980 | *ok = false; |
| 2981 | } |
| 2982 | } |
| 2983 | } |
| 2984 | |
| 2985 | void DatabaseQueries::removeMessageFilterAssignments(const QSqlDatabase& db, int filter_id, bool* ok) { |
| 2986 | QSqlQuery q(db); |
| 2987 | |
| 2988 | q.prepare(QSL("DELETE FROM MessageFiltersInFeeds WHERE filter = :filter;")); |
| 2989 | |
| 2990 | q.bindValue(QSL(":filter"), filter_id); |
| 2991 | q.setForwardOnly(true); |
| 2992 | |
| 2993 | if (q.exec()) { |
| 2994 | if (ok != nullptr) { |
| 2995 | *ok = true; |
| 2996 | } |
| 2997 | } |
| 2998 | else { |
| 2999 | if (ok != nullptr) { |
| 3000 | *ok = false; |
| 3001 | } |
| 3002 | } |
| 3003 | } |
| 3004 | |
| 3005 | QList<MessageFilter*> DatabaseQueries::getMessageFilters(const QSqlDatabase& db, bool* ok) { |
| 3006 | QSqlQuery q(db); |
| 3007 | QList<MessageFilter*> filters; |
| 3008 | |
| 3009 | q.setForwardOnly(true); |
| 3010 | q.prepare(QSL("SELECT id, name, script FROM MessageFilters;")); |
| 3011 | |
| 3012 | if (q.exec()) { |
| 3013 | while (q.next()) { |
| 3014 | auto* filter = new MessageFilter(q.value(0).toInt()); |
| 3015 | |
| 3016 | filter->setName(q.value(1).toString()); |
| 3017 | filter->setScript(q.value(2).toString()); |
| 3018 | |
| 3019 | filters.append(filter); |
| 3020 | } |
| 3021 | |
| 3022 | if (ok != nullptr) { |
| 3023 | *ok = true; |
| 3024 | } |
| 3025 | } |
| 3026 | else { |
| 3027 | if (ok != nullptr) { |
| 3028 | *ok = false; |
| 3029 | } |
| 3030 | } |
| 3031 | |
| 3032 | return filters; |
| 3033 | } |
| 3034 | |
| 3035 | QMultiMap<QString, int> DatabaseQueries::messageFiltersInFeeds(const QSqlDatabase& db, int account_id, bool* ok) { |
| 3036 | QSqlQuery q(db); |
| 3037 | QMultiMap<QString, int> filters_in_feeds; |
| 3038 | |
| 3039 | q.prepare(QSL("SELECT filter, feed_custom_id FROM MessageFiltersInFeeds WHERE account_id = :account_id;")); |
| 3040 | |
| 3041 | q.bindValue(QSL(":account_id"), account_id); |
| 3042 | q.setForwardOnly(true); |
| 3043 | |
| 3044 | if (q.exec()) { |
| 3045 | while (q.next()) { |
| 3046 | filters_in_feeds.insert(q.value(1).toString(), q.value(0).toInt()); |
| 3047 | } |
| 3048 | |
| 3049 | if (ok != nullptr) { |
| 3050 | *ok = true; |
| 3051 | } |
| 3052 | } |
| 3053 | else { |
| 3054 | if (ok != nullptr) { |
| 3055 | *ok = false; |
| 3056 | } |
| 3057 | } |
| 3058 | |
| 3059 | return filters_in_feeds; |
| 3060 | } |
| 3061 | |
| 3062 | void DatabaseQueries::assignMessageFilterToFeed(const QSqlDatabase& db, |
| 3063 | const QString& feed_custom_id, |
| 3064 | int filter_id, |
| 3065 | int account_id, |
| 3066 | bool* ok) { |
| 3067 | QSqlQuery q(db); |
| 3068 | |
| 3069 | q.prepare(QSL("SELECT COUNT(*) FROM MessageFiltersInFeeds " |
| 3070 | "WHERE filter = :filter AND feed_custom_id = :feed_custom_id AND account_id = :account_id;")); |
| 3071 | q.setForwardOnly(true); |
| 3072 | q.bindValue(QSL(":filter"), filter_id); |
| 3073 | q.bindValue(QSL(":feed_custom_id"), feed_custom_id); |
| 3074 | q.bindValue(QSL(":account_id"), account_id); |
| 3075 | |
| 3076 | if (q.exec() && q.next()) { |
| 3077 | auto already_included_count = q.value(0).toInt(); |
| 3078 | |
| 3079 | if (already_included_count > 0) { |
| 3080 | if (ok != nullptr) { |
| 3081 | *ok = true; |
| 3082 | } |
| 3083 | |
| 3084 | return; |
| 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | q.prepare(QSL("INSERT INTO MessageFiltersInFeeds (filter, feed_custom_id, account_id) " |
| 3089 | "VALUES(:filter, :feed_custom_id, :account_id);")); |
| 3090 | q.bindValue(QSL(":filter"), filter_id); |
| 3091 | q.bindValue(QSL(":feed_custom_id"), feed_custom_id); |
| 3092 | q.bindValue(QSL(":account_id"), account_id); |
| 3093 | |
| 3094 | if (q.exec()) { |
| 3095 | if (ok != nullptr) { |
| 3096 | *ok = true; |
| 3097 | } |
| 3098 | } |
| 3099 | else { |
| 3100 | if (ok != nullptr) { |
| 3101 | *ok = false; |
| 3102 | } |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | void DatabaseQueries::updateMessageFilter(const QSqlDatabase& db, MessageFilter* filter, bool* ok) { |
| 3107 | QSqlQuery q(db); |
| 3108 | |
| 3109 | q.prepare(QSL("UPDATE MessageFilters SET name = :name, script = :script WHERE id = :id;")); |
| 3110 | |
| 3111 | q.bindValue(QSL(":name"), filter->name()); |
| 3112 | q.bindValue(QSL(":script"), filter->script()); |
| 3113 | q.bindValue(QSL(":id"), filter->id()); |
| 3114 | q.setForwardOnly(true); |
| 3115 | |
| 3116 | if (q.exec()) { |
| 3117 | if (ok != nullptr) { |
| 3118 | *ok = true; |
| 3119 | } |
| 3120 | } |
| 3121 | else { |
| 3122 | if (ok != nullptr) { |
| 3123 | *ok = false; |
| 3124 | } |
| 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | void DatabaseQueries::removeMessageFilterFromFeed(const QSqlDatabase& db, |
| 3129 | const QString& feed_custom_id, |
| 3130 | int filter_id, |
| 3131 | int account_id, |
| 3132 | bool* ok) { |
| 3133 | QSqlQuery q(db); |
| 3134 | |
| 3135 | q.prepare(QSL("DELETE FROM MessageFiltersInFeeds " |
| 3136 | "WHERE filter = :filter AND feed_custom_id = :feed_custom_id AND account_id = :account_id;")); |
| 3137 | |
| 3138 | q.bindValue(QSL(":filter"), filter_id); |
| 3139 | q.bindValue(QSL(":feed_custom_id"), feed_custom_id); |
| 3140 | q.bindValue(QSL(":account_id"), account_id); |
| 3141 | q.setForwardOnly(true); |
| 3142 | |
| 3143 | if (q.exec()) { |
| 3144 | if (ok != nullptr) { |
| 3145 | *ok = true; |
| 3146 | } |
| 3147 | } |
| 3148 | else { |
| 3149 | if (ok != nullptr) { |
| 3150 | *ok = false; |
| 3151 | } |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | QStringList DatabaseQueries::getAllGmailRecipients(const QSqlDatabase& db, int account_id) { |
| 3156 | QSqlQuery query(db); |
| 3157 | QStringList rec; |
| 3158 | |
| 3159 | query.prepare(QSL("SELECT DISTINCT author " |
| 3160 | "FROM Messages " |
| 3161 | "WHERE account_id = :account_id AND author IS NOT NULL AND author != '' " |
| 3162 | "ORDER BY lower(author) ASC;")); |
| 3163 | query.bindValue(QSL(":account_id"), account_id); |
| 3164 | |
| 3165 | if (query.exec()) { |
| 3166 | while (query.next()) { |
| 3167 | rec.append(query.value(0).toString()); |
| 3168 | } |
| 3169 | } |
| 3170 | else { |
| 3171 | qWarningNN << LOGSEC_GMAIL << "Query for all recipients failed: '" << query.lastError().text() << "'."; |
| 3172 | } |
| 3173 | |
| 3174 | return rec; |
| 3175 | } |
| 3176 | |
| 3177 | bool DatabaseQueries::storeNewOauthTokens(const QSqlDatabase& db, const QString& refresh_token, int account_id) { |
| 3178 | QSqlQuery query(db); |
| 3179 | |
| 3180 | query.prepare(QSL("SELECT custom_data FROM Accounts WHERE id = :id;")); |
| 3181 | query.bindValue(QSL(":id"), account_id); |
| 3182 | |
| 3183 | if (!query.exec() || !query.next()) { |
| 3184 | qWarningNN << LOGSEC_OAUTH << "Cannot fetch custom data column for storing of OAuth tokens, because of error:" |
| 3185 | << QUOTE_W_SPACE_DOT(query.lastError().text()); |
| 3186 | return false; |
| 3187 | } |
| 3188 | |
| 3189 | QVariantHash custom_data = deserializeCustomData(query.value(0).toString()); |
| 3190 | |
| 3191 | custom_data[QSL("refresh_token")] = refresh_token; |
| 3192 | |
| 3193 | query.clear(); |
| 3194 | query.prepare(QSL("UPDATE Accounts SET custom_data = :custom_data WHERE id = :id;")); |
| 3195 | query.bindValue(QSL(":custom_data"), serializeCustomData(custom_data)); |
| 3196 | query.bindValue(QSL(":id"), account_id); |
| 3197 | |
| 3198 | if (!query.exec()) { |
| 3199 | qWarningNN << LOGSEC_OAUTH |
| 3200 | << "Cannot store OAuth tokens, because of error:" << QUOTE_W_SPACE_DOT(query.lastError().text()); |
| 3201 | return false; |
| 3202 | } |
| 3203 | else { |
| 3204 | return true; |
| 3205 | } |
| 3206 | } |
| 3207 | |
| 3208 | QString DatabaseQueries::unnulifyString(const QString& str) { |
| 3209 | return str.isNull() ? QSL("") : str; |
| 3210 | } |