From 3f4c86f80f7cf7fd0ba9260d46eafc55d46fe9af Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Mon, 28 Dec 2020 18:11:34 -0500 Subject: Add a file for each PDO class (#3301) Before, we had 5 classes in the ModelPdo file. It was bad for 2 reasons. The first reason is that it is considered bad practice to have multiple class in one file. This is especially true when using autoloading. On top of that it is less readable considering the size of the file. The second reason is that so far we were lucky. Everytime we needed to access the database, it was through the ModelPdo class which loads all the other classes. If we want to access directly the connection, it wont be loaded. On top of that, the system is configured to work on a single database, but as we have every connection definition in a single file, all classes were loaded at the same time. Thus using memory and processing time for nothing. Now, we have a file for each class. To work with autoloading, classes were slightly renamed to match autoloading rules. --- lib/Minz/ModelPdo.php | 101 +++---------------------------------------------- lib/Minz/Pdo.php | 52 +++++++++++++++++++++++++ lib/Minz/PdoMysql.php | 21 ++++++++++ lib/Minz/PdoPgsql.php | 22 +++++++++++ lib/Minz/PdoSqlite.php | 21 ++++++++++ 5 files changed, 121 insertions(+), 96 deletions(-) create mode 100644 lib/Minz/Pdo.php create mode 100644 lib/Minz/PdoMysql.php create mode 100644 lib/Minz/PdoPgsql.php create mode 100644 lib/Minz/PdoSqlite.php (limited to 'lib') diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php index ad18dcb81..7c6c82129 100644 --- a/lib/Minz/ModelPdo.php +++ b/lib/Minz/ModelPdo.php @@ -1,8 +1,9 @@ -*/ + */ /** * La classe Model_sql représente le modèle interragissant avec les bases de données @@ -63,12 +64,12 @@ class Minz_ModelPdo { $dsn .= ';port=' . $dbServer['port']; } $driver_options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8mb4'; - $this->pdo = new MinzPDOMySql($dsn . $dsnParams, $db['user'], $db['password'], $driver_options); + $this->pdo = new Minz_PdoMysql($dsn . $dsnParams, $db['user'], $db['password'], $driver_options); $this->pdo->setPrefix($db['prefix'] . $currentUser . '_'); break; case 'sqlite': $dsn = 'sqlite:' . join_path(DATA_PATH, 'users', $currentUser, 'db.sqlite'); - $this->pdo = new MinzPDOSQLite($dsn . $dsnParams, $db['user'], $db['password'], $driver_options); + $this->pdo = new Minz_PdoSqlite($dsn . $dsnParams, $db['user'], $db['password'], $driver_options); $this->pdo->setPrefix(''); break; case 'pgsql': @@ -79,7 +80,7 @@ class Minz_ModelPdo { if (!empty($dbServer['port'])) { $dsn .= ';port=' . $dbServer['port']; } - $this->pdo = new MinzPDOPGSQL($dsn . $dsnParams, $db['user'], $db['password'], $driver_options); + $this->pdo = new Minz_PdoPgsql($dsn . $dsnParams, $db['user'], $db['password'], $driver_options); $this->pdo->setPrefix($db['prefix'] . $currentUser . '_'); break; default: @@ -115,95 +116,3 @@ class Minz_ModelPdo { self::$sharedCurrentUser = ''; } } - -abstract class MinzPDO extends PDO { - public function __construct($dsn, $username = null, $passwd = null, $options = null) { - parent::__construct($dsn, $username, $passwd, $options); - $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); - } - - abstract public function dbType(); - - private $prefix = ''; - public function prefix() { return $this->prefix; } - public function setPrefix($prefix) { $this->prefix = $prefix; } - - private function autoPrefix($sql) { - return str_replace('`_', '`' . $this->prefix, $sql); - } - - protected function preSql($statement) { - if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) { - invalidateHttpCache(); - } - return $this->autoPrefix($statement); - } - - public function lastInsertId($name = null) { - if ($name != null) { - $name = $this->preSql($name); - } - return parent::lastInsertId($name); - } - - public function prepare($statement, $driver_options = array()) { - $statement = $this->preSql($statement); - return parent::prepare($statement, $driver_options); - } - - public function exec($statement) { - $statement = $this->preSql($statement); - return parent::exec($statement); - } - - public function query($query, $fetch_mode = null, ...$fetch_mode_args) { - $query = $this->preSql($query); - return $fetch_mode ? parent::query($query, $fetch_mode, ...$fetch_mode_args) : parent::query($query); - } -} - -class MinzPDOMySql extends MinzPDO { - public function __construct($dsn, $username = null, $passwd = null, $options = null) { - parent::__construct($dsn, $username, $passwd, $options); - $this->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); - } - - 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 __construct($dsn, $username = null, $passwd = null, $options = null) { - parent::__construct($dsn, $username, $passwd, $options); - $this->exec('PRAGMA foreign_keys = ON;'); - } - - 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 __construct($dsn, $username = null, $passwd = null, $options = null) { - parent::__construct($dsn, $username, $passwd, $options); - $this->exec("SET NAMES 'UTF8';"); - } - - public function dbType() { - return 'pgsql'; - } - - protected function preSql($statement) { - $statement = parent::preSql($statement); - return str_replace(array('`', ' LIKE '), array('"', ' ILIKE '), $statement); - } -} diff --git a/lib/Minz/Pdo.php b/lib/Minz/Pdo.php new file mode 100644 index 000000000..d334c0533 --- /dev/null +++ b/lib/Minz/Pdo.php @@ -0,0 +1,52 @@ + + */ + +abstract class Minz_Pdo extends PDO { + public function __construct($dsn, $username = null, $passwd = null, $options = null) { + parent::__construct($dsn, $username, $passwd, $options); + $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + } + + abstract public function dbType(); + + private $prefix = ''; + public function prefix() { return $this->prefix; } + public function setPrefix($prefix) { $this->prefix = $prefix; } + + private function autoPrefix($sql) { + return str_replace('`_', '`' . $this->prefix, $sql); + } + + protected function preSql($statement) { + if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) { + invalidateHttpCache(); + } + return $this->autoPrefix($statement); + } + + public function lastInsertId($name = null) { + if ($name != null) { + $name = $this->preSql($name); + } + return parent::lastInsertId($name); + } + + public function prepare($statement, $driver_options = array()) { + $statement = $this->preSql($statement); + return parent::prepare($statement, $driver_options); + } + + public function exec($statement) { + $statement = $this->preSql($statement); + return parent::exec($statement); + } + + public function query($query, $fetch_mode = null, ...$fetch_mode_args) { + $query = $this->preSql($query); + return $fetch_mode ? parent::query($query, $fetch_mode, ...$fetch_mode_args) : parent::query($query); + } +} diff --git a/lib/Minz/PdoMysql.php b/lib/Minz/PdoMysql.php new file mode 100644 index 000000000..c46d88f1a --- /dev/null +++ b/lib/Minz/PdoMysql.php @@ -0,0 +1,21 @@ + + */ + +class Minz_PdoMysql extends Minz_Pdo { + public function __construct($dsn, $username = null, $passwd = null, $options = null) { + parent::__construct($dsn, $username, $passwd, $options); + $this->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); + } + + public function dbType() { + return 'mysql'; + } + + public function lastInsertId($name = null) { + return parent::lastInsertId(); //We discard the name, only used by PostgreSQL + } +} diff --git a/lib/Minz/PdoPgsql.php b/lib/Minz/PdoPgsql.php new file mode 100644 index 000000000..7d1a1912b --- /dev/null +++ b/lib/Minz/PdoPgsql.php @@ -0,0 +1,22 @@ + + */ + +class Minz_PdoPgsql extends Minz_Pdo { + public function __construct($dsn, $username = null, $passwd = null, $options = null) { + parent::__construct($dsn, $username, $passwd, $options); + $this->exec("SET NAMES 'UTF8';"); + } + + public function dbType() { + return 'pgsql'; + } + + protected function preSql($statement) { + $statement = parent::preSql($statement); + return str_replace(array('`', ' LIKE '), array('"', ' ILIKE '), $statement); + } +} diff --git a/lib/Minz/PdoSqlite.php b/lib/Minz/PdoSqlite.php new file mode 100644 index 000000000..c577ad887 --- /dev/null +++ b/lib/Minz/PdoSqlite.php @@ -0,0 +1,21 @@ + + */ + +class Minz_PdoSqlite extends Minz_Pdo { + public function __construct($dsn, $username = null, $passwd = null, $options = null) { + parent::__construct($dsn, $username, $passwd, $options); + $this->exec('PRAGMA foreign_keys = ON;'); + } + + public function dbType() { + return 'sqlite'; + } + + public function lastInsertId($name = null) { + return parent::lastInsertId(); //We discard the name, only used by PostgreSQL + } +} -- cgit v1.2.3