This commit is contained in:
Martin Rotter 2021-03-05 21:13:40 +01:00 committed by Martin Rotter
parent b7ec33202c
commit 56b2b296b9
8 changed files with 24 additions and 11 deletions

View File

@ -6,7 +6,7 @@ CREATE TABLE Information (
INSERT INTO Information VALUES ('schema_version', '1'); INSERT INTO Information VALUES ('schema_version', '1');
-- ! -- !
CREATE TABLE Accounts ( CREATE TABLE Accounts (
id INTEGER PRIMARY KEY, id $$,
type TEXT NOT NULL CHECK (type != ''), /* ID of the account type. Each account defines its own, for example 'ttrss'. */ type TEXT NOT NULL CHECK (type != ''), /* ID of the account type. Each account defines its own, for example 'ttrss'. */
proxy_type INTEGER NOT NULL DEFAULT 0 CHECK (proxy_type >= 0), proxy_type INTEGER NOT NULL DEFAULT 0 CHECK (proxy_type >= 0),
proxy_host TEXT, proxy_host TEXT,
@ -18,7 +18,7 @@ CREATE TABLE Accounts (
); );
-- ! -- !
CREATE TABLE Categories ( CREATE TABLE Categories (
id INTEGER PRIMARY KEY, id $$,
parent_id INTEGER NOT NULL CHECK (parent_id >= -1), /* Root categories contain -1 here. */ parent_id INTEGER NOT NULL CHECK (parent_id >= -1), /* Root categories contain -1 here. */
title TEXT NOT NULL CHECK (title != ''), title TEXT NOT NULL CHECK (title != ''),
description TEXT, description TEXT,
@ -31,7 +31,7 @@ CREATE TABLE Categories (
); );
-- ! -- !
CREATE TABLE Feeds ( CREATE TABLE Feeds (
id INTEGER PRIMARY KEY, id $$,
title TEXT NOT NULL CHECK (title != ''), title TEXT NOT NULL CHECK (title != ''),
description TEXT, description TEXT,
date_created INTEGER, date_created INTEGER,
@ -49,7 +49,7 @@ CREATE TABLE Feeds (
); );
-- ! -- !
CREATE TABLE Messages ( CREATE TABLE Messages (
id INTEGER PRIMARY KEY, id $$,
is_read INTEGER(1) NOT NULL DEFAULT 0 CHECK (is_read >= 0 AND is_read <= 1), is_read INTEGER(1) NOT NULL DEFAULT 0 CHECK (is_read >= 0 AND is_read <= 1),
is_important INTEGER(1) NOT NULL DEFAULT 0 CHECK (is_important >= 0 AND is_important <= 1), is_important INTEGER(1) NOT NULL DEFAULT 0 CHECK (is_important >= 0 AND is_important <= 1),
is_deleted INTEGER(1) NOT NULL DEFAULT 0 CHECK (is_deleted >= 0 AND is_deleted <= 1), is_deleted INTEGER(1) NOT NULL DEFAULT 0 CHECK (is_deleted >= 0 AND is_deleted <= 1),
@ -70,7 +70,7 @@ CREATE TABLE Messages (
); );
-- ! -- !
CREATE TABLE MessageFilters ( CREATE TABLE MessageFilters (
id INTEGER PRIMARY KEY, id $$,
name TEXT NOT NULL CHECK (name != ''), name TEXT NOT NULL CHECK (name != ''),
script TEXT NOT NULL CHECK (script != '') script TEXT NOT NULL CHECK (script != '')
); );
@ -85,7 +85,7 @@ CREATE TABLE MessageFiltersInFeeds (
); );
-- ! -- !
CREATE TABLE Labels ( CREATE TABLE Labels (
id INTEGER PRIMARY KEY, id $$,
name TEXT NOT NULL CHECK (name != ''), name TEXT NOT NULL CHECK (name != ''),
color VARCHAR(7), color VARCHAR(7),
custom_id TEXT, custom_id TEXT,

View File

@ -47,5 +47,6 @@ QStringList DatabaseDriver::prepareScript(const QString& base_sql_folder,
} }
statements.replaceInStrings(APP_DB_NAME_PLACEHOLDER, database_name); statements.replaceInStrings(APP_DB_NAME_PLACEHOLDER, database_name);
statements.replaceInStrings(APP_DB_AUTO_INC_PRIM_KEY_PLACEHOLDER, autoIncrementPrimaryKey());
return statements; return statements;
} }

View File

@ -31,6 +31,7 @@ class DatabaseDriver : public QObject {
virtual QString humanDriverType() const = 0; virtual QString humanDriverType() const = 0;
virtual QString qtDriverCode() const = 0; virtual QString qtDriverCode() const = 0;
virtual DriverType driverType() const = 0; virtual DriverType driverType() const = 0;
virtual QString autoIncrementPrimaryKey() const = 0;
virtual bool vacuumDatabase() = 0; virtual bool vacuumDatabase() = 0;
virtual bool saveDatabase() = 0; virtual bool saveDatabase() = 0;
virtual void backupDatabase(const QString& backup_folder, const QString& backup_name) = 0; virtual void backupDatabase(const QString& backup_folder, const QString& backup_name) = 0;

View File

@ -296,3 +296,7 @@ QSqlDatabase MariaDbDriver::connection(const QString& connection_name, DatabaseD
return database; return database;
} }
} }
QString MariaDbDriver::autoIncrementPrimaryKey() const {
return QSL("INTEGER AUTO_INCREMENT PRIMARY KEY");
}

View File

@ -33,6 +33,7 @@ class MariaDbDriver : public DatabaseDriver {
virtual qint64 databaseDataSize(); virtual qint64 databaseDataSize();
virtual QSqlDatabase connection(const QString& connection_name, virtual QSqlDatabase connection(const QString& connection_name,
DatabaseDriver::DesiredStorageType desired_type = DatabaseDriver::DesiredStorageType::FromSettings); DatabaseDriver::DesiredStorageType desired_type = DatabaseDriver::DesiredStorageType::FromSettings);
virtual QString autoIncrementPrimaryKey() const;
QString interpretErrorCode(MariaDbError error_code) const; QString interpretErrorCode(MariaDbError error_code) const;

View File

@ -490,3 +490,7 @@ void SqliteDriver::backupDatabase(const QString& backup_folder, const QString& b
throw ApplicationException(tr("Database file not copied to output directory successfully.")); throw ApplicationException(tr("Database file not copied to output directory successfully."));
} }
} }
QString SqliteDriver::autoIncrementPrimaryKey() const {
return QSL("INTEGER PRIMARY KEY");
}

View File

@ -20,6 +20,7 @@ class SqliteDriver : public DatabaseDriver {
virtual QString humanDriverType() const; virtual QString humanDriverType() const;
virtual QString qtDriverCode() const; virtual QString qtDriverCode() const;
virtual void backupDatabase(const QString& backup_folder, const QString& backup_name); virtual void backupDatabase(const QString& backup_folder, const QString& backup_name);
virtual QString autoIncrementPrimaryKey() const;
private: private:
QSqlDatabase initializeInMemoryDatabase(); QSqlDatabase initializeInMemoryDatabase();

View File

@ -152,11 +152,12 @@
#define APP_DB_SQLITE_FILE "database.db" #define APP_DB_SQLITE_FILE "database.db"
// Keep this in sync with schema versions declared in SQL initialization code. // Keep this in sync with schema versions declared in SQL initialization code.
#define APP_DB_SCHEMA_VERSION "1" #define APP_DB_SCHEMA_VERSION "1"
#define APP_DB_UPDATE_FILE_PATTERN "db_update_%1_%2_%3.sql" #define APP_DB_UPDATE_FILE_PATTERN "db_update_%1_%2_%3.sql"
#define APP_DB_COMMENT_SPLIT "-- !\n" #define APP_DB_COMMENT_SPLIT "-- !\n"
#define APP_DB_INCLUDE_PLACEHOLDER "!!" #define APP_DB_INCLUDE_PLACEHOLDER "!!"
#define APP_DB_NAME_PLACEHOLDER "##" #define APP_DB_NAME_PLACEHOLDER "##"
#define APP_DB_AUTO_INC_PRIM_KEY_PLACEHOLDER "$$"
#define APP_CFG_PATH "config" #define APP_CFG_PATH "config"
#define APP_CFG_FILE "config.ini" #define APP_CFG_FILE "config.ini"