From c76a318193cda63064625b2d92c719b7150d7d64 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Sun, 15 Sep 2019 21:36:53 +0200 Subject: CLI to export/import any database to/from SQLite (#2496) * CLI to export/import any database to/from SQLite Require PHP 5.5+ https://github.com/FreshRSS/FreshRSS/pull/2495 * Travis * Execution rights * Fix wrong static fields * Fix MySQL bad default buffering https://stackoverflow.com/questions/6895098/pdo-mysql-memory-consumption-with-large-result-set/6935271#6935271 https://php.net/manual/ref.pdo-mysql * Fix count on progression * Avoid static DB information To ease working with two DBs at the same time * Less static, simplify Needs some testing * Small corrections * Special case for SQLite to SQLite * Modify special case for SQLite * Remove special case for SQLite More uniform logic for the 3 databases. Fix wrong DROP TABLE for SQLite. * Drop indexes * Revert "Drop indexes" This reverts commit f28d2bae0935745c1c74ea38f2ee083f3fd4bf9d. * Fix deletion * Fix classic export * Update cli/README.md Co-Authored-By: Marien Fressinaud * Addressing part of review * Remove goto :cry: * Travis * Comment for SQLite case * Fix missing fields when inserting --- lib/Minz/ModelPdo.php | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'lib/Minz/ModelPdo.php') diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php index 14510c983..4d5e47da9 100644 --- a/lib/Minz/ModelPdo.php +++ b/lib/Minz/ModelPdo.php @@ -17,7 +17,6 @@ class Minz_ModelPdo { private static $sharedBd = null; private static $sharedPrefix; private static $sharedCurrentUser; - protected static $sharedDbType; /** * $bd variable représentant la base de données @@ -27,18 +26,21 @@ class Minz_ModelPdo { protected $current_user; protected $prefix; - public function dbType() { - return self::$sharedDbType; - } - /** * Créé la connexion à la base de données à l'aide des variables * HOST, BASE, USER et PASS définies dans le fichier de configuration */ - public function __construct($currentUser = null) { + public function __construct($currentUser = null, $currentPrefix = null, $currentDb = null) { if ($currentUser === null) { $currentUser = Minz_Session::param('currentUser'); } + if ($currentPrefix !== null) { + $this->prefix = $currentPrefix; + } + if ($currentDb != null) { + $this->bd = $currentDb; + return; + } if (self::$useSharedBd && self::$sharedBd != null && ($currentUser == null || $currentUser === self::$sharedCurrentUser)) { $this->bd = self::$sharedBd; @@ -65,6 +67,7 @@ class Minz_ModelPdo { $driver_options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8mb4'; $this->prefix = $db['prefix'] . $currentUser . '_'; $this->bd = new MinzPDOMySql($string, $db['user'], $db['password'], $driver_options); + $this->bd->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); break; case 'sqlite': $string = 'sqlite:' . join_path(DATA_PATH, 'users', $currentUser, 'db.sqlite'); @@ -89,7 +92,6 @@ class Minz_ModelPdo { break; } self::$sharedBd = $this->bd; - self::$sharedDbType = $db['type']; self::$sharedPrefix = $this->prefix; } catch (Exception $e) { throw new Minz_PDOConnectionException( @@ -114,17 +116,12 @@ class Minz_ModelPdo { public static function clean() { self::$sharedBd = null; + self::$sharedCurrentUser = ''; self::$sharedPrefix = ''; } - - public function disableBuffering() { - if ((self::$sharedDbType === 'mysql') && defined('PDO::MYSQL_ATTR_USE_BUFFERED_QUERY')) { - $this->bd->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); - } - } } -class MinzPDO extends PDO { +abstract class MinzPDO extends PDO { private static function check($statement) { if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) { invalidateHttpCache(); @@ -135,6 +132,8 @@ class MinzPDO extends PDO { return $statement; } + abstract public function dbType(); + public function prepare($statement, $driver_options = array()) { MinzPDO::check($statement); $statement = $this->compatibility($statement); @@ -155,18 +154,30 @@ class MinzPDO extends PDO { } class MinzPDOMySql extends MinzPDO { + public function dbType() { + return 'mysql'; + } + public function lastInsertId($name = null) { return parent::lastInsertId(); //We discard the name, only used by PostgreSQL } } class MinzPDOSQLite extends MinzPDO { + public function dbType() { + return 'sqlite'; + } + public function lastInsertId($name = null) { return parent::lastInsertId(); //We discard the name, only used by PostgreSQL } } class MinzPDOPGSQL extends MinzPDO { + public function dbType() { + return 'pgsql'; + } + protected function compatibility($statement) { return str_replace(array('`', ' LIKE '), array('"', ' ILIKE '), $statement); } -- cgit v1.2.3