From d5c7e3b8f47772b17554c8c324303c8589e6f4e6 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Thu, 3 Dec 2015 10:53:48 +0100 Subject: [PATCH] Reworked attributes & code for Feeds and Categories too. Now each category and feed knows which account it belongs to. --- resources/misc/db_init_mysql.sql | 14 +++++++++---- resources/misc/db_init_sqlite.sql | 14 +++++++++---- resources/misc/db_update_mysql_3_4.sql | 7 ++++++- resources/misc/db_update_sqlite_3_4.sql | 6 ++++++ src/definitions/definitions.h.in | 1 + src/services/standard/standardcategory.cpp | 18 ++++------------- src/services/standard/standardfeed.cpp | 20 ++++++------------- src/services/standard/standardserviceroot.cpp | 4 ++-- 8 files changed, 45 insertions(+), 39 deletions(-) diff --git a/resources/misc/db_init_mysql.sql b/resources/misc/db_init_mysql.sql index 262d65e89..6c4569a53 100644 --- a/resources/misc/db_init_mysql.sql +++ b/resources/misc/db_init_mysql.sql @@ -34,10 +34,13 @@ DROP TABLE IF EXISTS Categories; CREATE TABLE IF NOT EXISTS Categories ( id INTEGER AUTO_INCREMENT PRIMARY KEY, parent_id INTEGER NOT NULL, - title VARCHAR(100) NOT NULL UNIQUE CHECK (title != ''), + title VARCHAR(100) NOT NULL CHECK (title != ''), description TEXT, date_created BIGINT NOT NULL CHECK (date_created != 0), - icon BLOB + icon BLOB, + account_id INTEGER NOT NULL, + + FOREIGN KEY (account_id) REFERENCES Accounts (id) ); -- ! DROP TABLE IF EXISTS Feeds; @@ -50,13 +53,16 @@ CREATE TABLE IF NOT EXISTS Feeds ( icon BLOB, category INTEGER NOT NULL CHECK (category >= -1), encoding TEXT NOT NULL CHECK (encoding != ''), - url VARCHAR(100) NOT NULL UNIQUE CHECK (url != ''), + url VARCHAR(100) NOT NULL CHECK (url != ''), protected INTEGER(1) NOT NULL CHECK (protected >= 0 AND protected <= 1), username TEXT, password TEXT, update_type INTEGER(1) NOT NULL CHECK (update_type >= 0), update_interval INTEGER NOT NULL DEFAULT 15 CHECK (update_interval >= 5), - type INTEGER NOT NULL CHECK (type >= 0) + type INTEGER NOT NULL CHECK (type >= 0), + account_id INTEGER NOT NULL, + + FOREIGN KEY (account_id) REFERENCES Accounts (id) ); -- ! DROP TABLE IF EXISTS Messages; diff --git a/resources/misc/db_init_sqlite.sql b/resources/misc/db_init_sqlite.sql index c5d9f3fe8..e64ccbad2 100644 --- a/resources/misc/db_init_sqlite.sql +++ b/resources/misc/db_init_sqlite.sql @@ -29,10 +29,13 @@ DROP TABLE IF EXISTS Categories; CREATE TABLE IF NOT EXISTS Categories ( id INTEGER PRIMARY KEY, parent_id INTEGER NOT NULL, - title TEXT NOT NULL UNIQUE CHECK (title != ''), + title TEXT NOT NULL CHECK (title != ''), description TEXT, date_created INTEGER NOT NULL CHECK (date_created != 0), - icon BLOB + icon BLOB, + account_id INTEGER NOT NULL, + + FOREIGN KEY (account_id) REFERENCES Accounts (id) ); -- ! DROP TABLE IF EXISTS Feeds; @@ -45,13 +48,16 @@ CREATE TABLE IF NOT EXISTS Feeds ( icon BLOB, category INTEGER NOT NULL CHECK (category >= -1), encoding TEXT NOT NULL CHECK (encoding != ''), - url TEXT NOT NULL UNIQUE CHECK (url != ''), + url TEXT NOT NULL CHECK (url != ''), protected INTEGER(1) NOT NULL CHECK (protected >= 0 AND protected <= 1), username TEXT, password TEXT, update_type INTEGER(1) NOT NULL CHECK (update_type >= 0), update_interval INTEGER NOT NULL CHECK (update_interval >= 5) DEFAULT 15, - type INTEGER NOT NULL CHECK (type >= 0) + type INTEGER NOT NULL CHECK (type >= 0), + account_id INTEGER NOT NULL, + + FOREIGN KEY (account_id) REFERENCES Accounts (id) ); -- ! DROP TABLE IF EXISTS Messages; diff --git a/resources/misc/db_update_mysql_3_4.sql b/resources/misc/db_update_mysql_3_4.sql index cb66ceb81..1d41ef9ca 100644 --- a/resources/misc/db_update_mysql_3_4.sql +++ b/resources/misc/db_update_mysql_3_4.sql @@ -19,5 +19,10 @@ CREATE TABLE IF NOT EXISTS TtRssAccounts ( ALTER TABLE Messages ADD COLUMN account_id INTEGER NOT NULL DEFAULT (1); -- ! - +ALTER TABLE Feeds +ADD COLUMN account_id INTEGER NOT NULL DEFAULT (1); +-- ! +ALTER TABLE Categories +ADD COLUMN account_id INTEGER NOT NULL DEFAULT (1); +-- ! UPDATE Information SET inf_value = '4' WHERE inf_key = 'schema_version'; \ No newline at end of file diff --git a/resources/misc/db_update_sqlite_3_4.sql b/resources/misc/db_update_sqlite_3_4.sql index 41f2135e7..1d41ef9ca 100644 --- a/resources/misc/db_update_sqlite_3_4.sql +++ b/resources/misc/db_update_sqlite_3_4.sql @@ -19,4 +19,10 @@ CREATE TABLE IF NOT EXISTS TtRssAccounts ( ALTER TABLE Messages ADD COLUMN account_id INTEGER NOT NULL DEFAULT (1); -- ! +ALTER TABLE Feeds +ADD COLUMN account_id INTEGER NOT NULL DEFAULT (1); +-- ! +ALTER TABLE Categories +ADD COLUMN account_id INTEGER NOT NULL DEFAULT (1); +-- ! UPDATE Information SET inf_value = '4' WHERE inf_key = 'schema_version'; \ No newline at end of file diff --git a/src/definitions/definitions.h.in b/src/definitions/definitions.h.in index f096b4033..508d718eb 100755 --- a/src/definitions/definitions.h.in +++ b/src/definitions/definitions.h.in @@ -208,6 +208,7 @@ #define FDS_DB_UPDATE_TYPE_INDEX 11 #define FDS_DB_UPDATE_INTERVAL_INDEX 12 #define FDS_DB_TYPE_INDEX 13 +#define FDS_DB_ACCOUNT_ID_INDEX 14 // Indexes of columns for feed models. #define FDS_MODEL_TITLE_INDEX 0 diff --git a/src/services/standard/standardcategory.cpp b/src/services/standard/standardcategory.cpp index dc7a940b3..1aabaa01f 100755 --- a/src/services/standard/standardcategory.cpp +++ b/src/services/standard/standardcategory.cpp @@ -174,13 +174,14 @@ bool StandardCategory::addItself(RootItem *parent) { query_add.setForwardOnly(true); query_add.prepare("INSERT INTO Categories " - "(parent_id, title, description, date_created, icon) " - "VALUES (:parent_id, :title, :description, :date_created, :icon);"); + "(parent_id, title, description, date_created, icon, account_id) " + "VALUES (:parent_id, :title, :description, :date_created, :icon, :account_id);"); query_add.bindValue(QSL(":parent_id"), parent->id()); query_add.bindValue(QSL(":title"), title()); query_add.bindValue(QSL(":description"), description()); query_add.bindValue(QSL(":date_created"), creationDate().toMSecsSinceEpoch()); query_add.bindValue(QSL(":icon"), qApp->icons()->toByteArray(icon())); + query_add.bindValue(QSL(":account_id"), parent->getParentServiceRoot()->accountId()); if (!query_add.exec()) { qDebug("Failed to add category to database: %s.", qPrintable(query_add.lastError().text())); @@ -189,18 +190,7 @@ bool StandardCategory::addItself(RootItem *parent) { return false; } - query_add.prepare(QSL("SELECT id FROM Categories WHERE title = :title;")); - query_add.bindValue(QSL(":title"), title()); - - if (query_add.exec() && query_add.next()) { - // New category was added, fetch is primary id - // from the database. - setId(query_add.value(0).toInt()); - } - else { - // Something failed. - return false; - } + setId(query_add.lastInsertId().toInt()); return true; } diff --git a/src/services/standard/standardfeed.cpp b/src/services/standard/standardfeed.cpp index cfaae4e08..bfc240d00 100755 --- a/src/services/standard/standardfeed.cpp +++ b/src/services/standard/standardfeed.cpp @@ -519,9 +519,8 @@ bool StandardFeed::removeItself() { query_remove.setForwardOnly(true); // Remove all messages from this standard feed. - query_remove.prepare(QSL("DELETE FROM Messages WHERE feed = :feed AND account_id = :account_id;")); + query_remove.prepare(QSL("DELETE FROM Messages WHERE feed = :feed;")); query_remove.bindValue(QSL(":feed"), id()); - query_remove.bindValue(QSL(":account_id"), const_cast(this)->serviceRoot()->accountId()); if (!query_remove.exec()) { return false; @@ -541,8 +540,8 @@ bool StandardFeed::addItself(RootItem *parent) { query_add_feed.setForwardOnly(true); query_add_feed.prepare("INSERT INTO Feeds " - "(title, description, date_created, icon, category, encoding, url, protected, username, password, update_type, update_interval, type) " - "VALUES (:title, :description, :date_created, :icon, :category, :encoding, :url, :protected, :username, :password, :update_type, :update_interval, :type);"); + "(title, description, date_created, icon, category, encoding, url, protected, username, password, update_type, update_interval, type, account_id) " + "VALUES (:title, :description, :date_created, :icon, :category, :encoding, :url, :protected, :username, :password, :update_type, :update_interval, :type, :account_id);"); query_add_feed.bindValue(QSL(":title"), title()); query_add_feed.bindValue(QSL(":description"), description()); query_add_feed.bindValue(QSL(":date_created"), creationDate().toMSecsSinceEpoch()); @@ -552,6 +551,7 @@ bool StandardFeed::addItself(RootItem *parent) { query_add_feed.bindValue(QSL(":url"), url()); query_add_feed.bindValue(QSL(":protected"), (int) passwordProtected()); query_add_feed.bindValue(QSL(":username"), username()); + query_add_feed.bindValue(QSL(":account_id"), parent->getParentServiceRoot()->accountId()); if (password().isEmpty()) { query_add_feed.bindValue(QSL(":password"), password()); @@ -571,16 +571,8 @@ bool StandardFeed::addItself(RootItem *parent) { return false; } - query_add_feed.prepare(QSL("SELECT id FROM Feeds WHERE url = :url;")); - query_add_feed.bindValue(QSL(":url"), url()); - if (query_add_feed.exec() && query_add_feed.next()) { - // New feed was added, fetch is primary id from the database. - setId(query_add_feed.value(0).toInt()); - } - else { - // Something failed. - return false; - } + // New feed was added, fetch is primary id from the database. + setId(query_add_feed.lastInsertId().toInt()); return true; } diff --git a/src/services/standard/standardserviceroot.cpp b/src/services/standard/standardserviceroot.cpp index d02680f62..9ffd1b723 100755 --- a/src/services/standard/standardserviceroot.cpp +++ b/src/services/standard/standardserviceroot.cpp @@ -119,12 +119,12 @@ bool StandardServiceRoot::deleteViaGui() { // TODO: todo // Remove all feeds. - if (!QSqlQuery(connection).exec(QSL("DELETE FROM Feeds;"))) { + if (!QSqlQuery(connection).exec(QSL("DELETE FROM Feeds WHERE account_id = %1;").arg(accountId()))) { return false; } // Remove all categories. - if (!QSqlQuery(connection).exec(QSL("DELETE FROM Categories;"))) { + if (!QSqlQuery(connection).exec(QSL("DELETE FROM Categories WHERE account_id = %1;").arg(accountId()))) { return false; }