PDO refactoring for code simplification (#2522)

* PDO refactor

* Automatic prefix when using the syntax `_tableName`
* Uniformity: MySQL is now PDO::ATTR_EMULATE_PREPARES = false just like SQLite and PostgreSQL, with consequences such as only one statement per query
* Use PDO methods exec(), query(), prepare() + execute() in a more efficient way
* Remove auto-update SQL code for versions older than FreshRSS 1.5 (3 years old)
* The name of the default category is set in PHP instead of in the DB (simplies SQL and allows changing the name according to the FreshRSS language)
* Rename `->bd` to `->pdo` (less of a frenshism, and more informative)
* Fix some requests, which were not compatible with MySQL prepared statements

* Whitespace

* Fix syntax for PostgreSQL sequences

+ MySQL install

* Minor formatting

* Fix lastInsertId for PostgreSQL

* Use PHP 5.6+ const

Take advantage of https://github.com/FreshRSS/FreshRSS/pull/2527
https://www.php.net/manual/en/migration56.new-features.php

* A bit of forgotten PHP 5.6 simplification for cURL

* Forgotten $s

* Mini fix custom user config

https://github.com/FreshRSS/FreshRSS/pull/2490/files#r326290346

* More work on install.php but not finished

* install.php working

* More cleaning of PDO in install

* Even more simplification

Take advantage of PDO->exec() to run multiple statements

* Disallow changing the name of the default category

https://github.com/FreshRSS/FreshRSS/pull/2522#discussion_r326967724
This commit is contained in:
Alexandre Alapetite
2019-09-29 16:22:50 +02:00
committed by GitHub
parent ec4307c1a6
commit e3e5954394
34 changed files with 891 additions and 1206 deletions

View File

@@ -1,13 +1,16 @@
<?php
global $SQL_CREATE_TABLES;
$SQL_CREATE_TABLES = array(
'CREATE TABLE IF NOT EXISTS `category` (
const SQL_CREATE_DB = <<<'SQL'
SELECT 1; -- Do nothing for SQLite
SQL;
const SQL_CREATE_TABLES = <<<'SQL'
CREATE TABLE IF NOT EXISTS `category` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` VARCHAR(255) NOT NULL,
UNIQUE (`name`)
);',
);
'CREATE TABLE IF NOT EXISTS `feed` (
CREATE TABLE IF NOT EXISTS `feed` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`url` VARCHAR(511) NOT NULL,
`category` SMALLINT DEFAULT 0,
@@ -26,12 +29,12 @@ $SQL_CREATE_TABLES = array(
`cache_nbUnreads` INT DEFAULT 0,
FOREIGN KEY (`category`) REFERENCES `category`(`id`) ON DELETE SET NULL ON UPDATE CASCADE,
UNIQUE (`url`)
);',
'CREATE INDEX IF NOT EXISTS feed_name_index ON `feed`(`name`);',
'CREATE INDEX IF NOT EXISTS feed_priority_index ON `feed`(`priority`);',
'CREATE INDEX IF NOT EXISTS feed_keep_history_index ON `feed`(`keep_history`);',
);
CREATE INDEX IF NOT EXISTS feed_name_index ON `feed`(`name`);
CREATE INDEX IF NOT EXISTS feed_priority_index ON `feed`(`priority`);
CREATE INDEX IF NOT EXISTS feed_keep_history_index ON `feed`(`keep_history`);
'CREATE TABLE IF NOT EXISTS `entry` (
CREATE TABLE IF NOT EXISTS `entry` (
`id` BIGINT NOT NULL,
`guid` VARCHAR(760) NOT NULL,
`title` VARCHAR(255) NOT NULL,
@@ -48,17 +51,16 @@ $SQL_CREATE_TABLES = array(
PRIMARY KEY (`id`),
FOREIGN KEY (`id_feed`) REFERENCES `feed`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE (`id_feed`,`guid`)
);',
'CREATE INDEX IF NOT EXISTS entry_is_favorite_index ON `entry`(`is_favorite`);',
'CREATE INDEX IF NOT EXISTS entry_is_read_index ON `entry`(`is_read`);',
'CREATE INDEX IF NOT EXISTS entry_lastSeen_index ON `entry`(`lastSeen`);', //v1.1.1
'INSERT OR IGNORE INTO `category` (id, name) VALUES(1, "%2$s");',
);
CREATE INDEX IF NOT EXISTS entry_is_favorite_index ON `entry`(`is_favorite`);
CREATE INDEX IF NOT EXISTS entry_is_read_index ON `entry`(`is_read`);
CREATE INDEX IF NOT EXISTS entry_lastSeen_index ON `entry`(`lastSeen`); -- //v1.1.1
global $SQL_CREATE_TABLE_ENTRYTMP;
$SQL_CREATE_TABLE_ENTRYTMP = array(
'CREATE TABLE IF NOT EXISTS `entrytmp` ( -- v1.7
INSERT OR IGNORE INTO `category` (id, name) VALUES(1, "Uncategorized");
SQL;
const SQL_CREATE_TABLE_ENTRYTMP = <<<'SQL'
CREATE TABLE IF NOT EXISTS `entrytmp` ( -- v1.7
`id` BIGINT NOT NULL,
`guid` VARCHAR(760) NOT NULL,
`title` VARCHAR(255) NOT NULL,
@@ -75,42 +77,40 @@ $SQL_CREATE_TABLE_ENTRYTMP = array(
PRIMARY KEY (`id`),
FOREIGN KEY (`id_feed`) REFERENCES `feed`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE (`id_feed`,`guid`)
);',
'CREATE INDEX IF NOT EXISTS entrytmp_date_index ON `entrytmp`(`date`);',
'CREATE INDEX IF NOT EXISTS `entry_feed_read_index` ON `entry`(`id_feed`,`is_read`);', //v1.7
);
CREATE INDEX IF NOT EXISTS entrytmp_date_index ON `entrytmp`(`date`);
global $SQL_CREATE_TABLE_TAGS;
$SQL_CREATE_TABLE_TAGS = array(
'CREATE TABLE IF NOT EXISTS `tag` ( -- v1.12
-- v1.7
CREATE INDEX IF NOT EXISTS `entry_feed_read_index` ON `entry`(`id_feed`,`is_read`);
SQL;
const SQL_CREATE_TABLE_TAGS = <<<'SQL'
CREATE TABLE IF NOT EXISTS `tag` ( -- v1.12
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` VARCHAR(63) NOT NULL,
`attributes` TEXT,
UNIQUE (`name`)
);',
'CREATE TABLE IF NOT EXISTS `entrytag` (
);
CREATE TABLE IF NOT EXISTS `entrytag` (
`id_tag` SMALLINT,
`id_entry` BIGINT,
PRIMARY KEY (`id_tag`,`id_entry`),
FOREIGN KEY (`id_tag`) REFERENCES `tag` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`id_entry`) REFERENCES `entry` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
);',
'CREATE INDEX entrytag_id_entry_index ON `entrytag` (`id_entry`);',
);
CREATE INDEX entrytag_id_entry_index ON `entrytag` (`id_entry`);
SQL;
define(
'SQL_INSERT_FEED',
'INSERT OR IGNORE INTO `feed` (url, category, name, website, description, ttl)
VALUES(:url, 1, :name, :website, :description, 86400);'
);
const SQL_INSERT_FEED = <<<'SQL'
INSERT OR IGNORE INTO `feed` (url, category, name, website, description, ttl)
VALUES(:url, 1, :name, :website, :description, 86400);
SQL;
global $SQL_DROP_TABLES;
$SQL_DROP_TABLES = [
'DROP TABLE IF EXISTS `entrytag`',
'DROP TABLE IF EXISTS `tag`',
'DROP TABLE IF EXISTS `entrytmp`',
'DROP TABLE IF EXISTS `entry`',
'DROP TABLE IF EXISTS `feed`',
'DROP TABLE IF EXISTS `category`',
];
const SQL_DROP_TABLES = <<<'SQL'
DROP TABLE IF EXISTS `entrytag`;
DROP TABLE IF EXISTS `tag`;
DROP TABLE IF EXISTS `entrytmp`;
DROP TABLE IF EXISTS `entry`;
DROP TABLE IF EXISTS `feed`;
DROP TABLE IF EXISTS `category`;
SQL;