Files
FreshRSS/lib/Minz/Pdo.php
Alexis Degrugillier e1ee58816b Add a file for each PDO class (#3297)
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.
2020-12-28 16:44:08 +01:00

53 lines
1.4 KiB
PHP

<?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);
}
}