aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2020-12-28 19:53:55 +0100
committerGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2020-12-28 19:53:55 +0100
commit0a2d9b3b54ee51a3965d79c68409ef045ad5834b (patch)
tree12cbfd4d1f57a4ba445cc75637c95ba7c7720fb0 /lib
parentc246e5d74b1fb88ada602764f247942f2eebc4ca (diff)
Revert "Add a file for each PDO class (#3297)"
This reverts commit e1ee58816ba76734e4115fc12898b13de665b220.
Diffstat (limited to 'lib')
-rw-r--r--lib/Minz/ModelPdo.php101
-rw-r--r--lib/Minz/Pdo.php52
-rw-r--r--lib/Minz/PdoMysql.php21
-rw-r--r--lib/Minz/PdoPgsql.php22
-rw-r--r--lib/Minz/PdoSqlite.php21
5 files changed, 96 insertions, 121 deletions
diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php
index 7c6c82129..ad18dcb81 100644
--- a/lib/Minz/ModelPdo.php
+++ b/lib/Minz/ModelPdo.php
@@ -1,9 +1,8 @@
<?php
-
/**
* MINZ - Copyright 2011 Marien Fressinaud
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
- */
+*/
/**
* La classe Model_sql représente le modèle interragissant avec les bases de données
@@ -64,12 +63,12 @@ class Minz_ModelPdo {
$dsn .= ';port=' . $dbServer['port'];
}
$driver_options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8mb4';
- $this->pdo = new Minz_PdoMysql($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
+ $this->pdo = new MinzPDOMySql($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 Minz_PdoSqlite($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
+ $this->pdo = new MinzPDOSQLite($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
$this->pdo->setPrefix('');
break;
case 'pgsql':
@@ -80,7 +79,7 @@ class Minz_ModelPdo {
if (!empty($dbServer['port'])) {
$dsn .= ';port=' . $dbServer['port'];
}
- $this->pdo = new Minz_PdoPgsql($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
+ $this->pdo = new MinzPDOPGSQL($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
$this->pdo->setPrefix($db['prefix'] . $currentUser . '_');
break;
default:
@@ -116,3 +115,95 @@ 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
deleted file mode 100644
index d334c0533..000000000
--- a/lib/Minz/Pdo.php
+++ /dev/null
@@ -1,52 +0,0 @@
-<?php
-
-/**
- * MINZ - Copyright 2011 Marien Fressinaud
- * Sous licence AGPL3 <http://www.gnu.org/licenses/>
- */
-
-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
deleted file mode 100644
index 3c5aa057c..000000000
--- a/lib/Minz/PdoMysql.php
+++ /dev/null
@@ -1,21 +0,0 @@
-<?php
-
-/**
- * MINZ - Copyright 2011 Marien Fressinaud
- * Sous licence AGPL3 <http://www.gnu.org/licenses/>
- */
-
-class Minz_PdoMysql 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
- }
-}
diff --git a/lib/Minz/PdoPgsql.php b/lib/Minz/PdoPgsql.php
deleted file mode 100644
index 7d1a1912b..000000000
--- a/lib/Minz/PdoPgsql.php
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php
-
-/**
- * MINZ - Copyright 2011 Marien Fressinaud
- * Sous licence AGPL3 <http://www.gnu.org/licenses/>
- */
-
-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
deleted file mode 100644
index c577ad887..000000000
--- a/lib/Minz/PdoSqlite.php
+++ /dev/null
@@ -1,21 +0,0 @@
-<?php
-
-/**
- * MINZ - Copyright 2011 Marien Fressinaud
- * Sous licence AGPL3 <http://www.gnu.org/licenses/>
- */
-
-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
- }
-}