mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-10-27 13:38:54 -04:00
Optimise Minz_ModelPdo::class (#4119)
* - Fix typo, - remove unnecessary null in property, - remove unused property, - add phpDoc, - add ext PDO in composer.json, - use strict comparison, - indentation * Translate * Update lib/Minz/ModelPdo.php Co-authored-by: Frans de Jonge <fransdejonge@gmail.com> * The code is more explicite * Fix phpstan * Fix phpstan expect one * Fix phpstan * Return in back... * make fix-all * Fix exception and more types * Fix more types * Remove ext- in composer.json Co-authored-by: Luc SANCHEZ <l.sanchez-ext@ubitransport.com> Co-authored-by: Frans de Jonge <fransdejonge@gmail.com> Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
This commit is contained in:
6 files changed
+66
-25
No files matched your search
@@ -209,7 +209,7 @@ class FreshRSS_importExport_Controller extends FreshRSS_ActionController {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
private function ttrssXmlToJson($xml) {
|
||||
private function ttrssXmlToJson(string $xml) {
|
||||
$table = (array)simplexml_load_string($xml, null, LIBXML_NOBLANKS | LIBXML_NOCDATA);
|
||||
$table['items'] = isset($table['article']) ? $table['article'] : array();
|
||||
unset($table['article']);
|
||||
|
||||
+51
-10
@@ -6,20 +6,41 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* La classe Model_sql représente le modèle interragissant avec les bases de données
|
||||
* The Model_sql class represents the model for interacting with databases.
|
||||
*/
|
||||
class Minz_ModelPdo {
|
||||
|
||||
/**
|
||||
* Partage la connexion à la base de données entre toutes les instances.
|
||||
* Shares the connection to the database between all instances.
|
||||
*/
|
||||
public static $usesSharedPdo = true;
|
||||
private static $sharedPdo = null;
|
||||
|
||||
/**
|
||||
* @var Minz_Pdo|null
|
||||
*/
|
||||
private static $sharedPdo;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private static $sharedCurrentUser;
|
||||
|
||||
/**
|
||||
* @var Minz_Pdo|null
|
||||
*/
|
||||
protected $pdo;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $current_user;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Minz_ConfigurationNamespaceException
|
||||
* @throws Minz_PDOConnectionException
|
||||
* @throws PDOException
|
||||
*/
|
||||
private function dbConnect() {
|
||||
$db = Minz_Configuration::get('system')->db;
|
||||
$driver_options = isset($db['pdo_options']) && is_array($db['pdo_options']) ? $db['pdo_options'] : [];
|
||||
@@ -67,22 +88,25 @@ class Minz_ModelPdo {
|
||||
}
|
||||
|
||||
/**
|
||||
* Créé la connexion à la base de données à l'aide des variables
|
||||
* HOST, BASE, USER et PASS définies dans le fichier de configuration
|
||||
* Create the connection to the database using the variables
|
||||
* HOST, BASE, USER and PASS variables defined in the configuration file
|
||||
* @param string|null $currentUser
|
||||
* @param Minz_Pdo|null $currentPdo
|
||||
* @throws Minz_ConfigurationNamespaceException
|
||||
* @throws Minz_PDOConnectionException
|
||||
*/
|
||||
public function __construct($currentUser = null, $currentPdo = null) {
|
||||
if ($currentUser === null) {
|
||||
$currentUser = Minz_Session::param('currentUser');
|
||||
}
|
||||
if ($currentPdo != null) {
|
||||
if ($currentPdo !== null) {
|
||||
$this->pdo = $currentPdo;
|
||||
return;
|
||||
}
|
||||
if ($currentUser == '') {
|
||||
throw new Minz_PDOConnectionException('Current user must not be empty!', '', Minz_Exception::ERROR);
|
||||
}
|
||||
if (self::$usesSharedPdo && self::$sharedPdo != null &&
|
||||
($currentUser == '' || $currentUser === self::$sharedCurrentUser)) {
|
||||
if (self::$usesSharedPdo && self::$sharedPdo !== null && $currentUser === self::$sharedCurrentUser) {
|
||||
$this->pdo = self::$sharedPdo;
|
||||
$this->current_user = self::$sharedCurrentUser;
|
||||
return;
|
||||
@@ -99,9 +123,11 @@ class Minz_ModelPdo {
|
||||
} catch (PDOException $e) {
|
||||
$ex = $e;
|
||||
if (empty($e->errorInfo[0]) || $e->errorInfo[0] !== '08006') {
|
||||
//We are only only interested in: SQLSTATE connection exception / connection failure
|
||||
//We are only interested in: SQLSTATE connection exception / connection failure
|
||||
break;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$ex = $e;
|
||||
}
|
||||
sleep(2);
|
||||
}
|
||||
@@ -114,19 +140,34 @@ class Minz_ModelPdo {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function beginTransaction() {
|
||||
$this->pdo->beginTransaction();
|
||||
}
|
||||
public function inTransaction() {
|
||||
|
||||
public function inTransaction(): bool {
|
||||
return $this->pdo->inTransaction();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function commit() {
|
||||
$this->pdo->commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function rollBack() {
|
||||
$this->pdo->rollBack();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public static function clean() {
|
||||
self::$sharedPdo = null;
|
||||
self::$sharedCurrentUser = '';
|
||||
|
||||
+7
-7
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
abstract class Minz_Pdo extends PDO {
|
||||
public function __construct($dsn, $username = null, $passwd = null, $options = null) {
|
||||
public function __construct(string $dsn, $username = null, $passwd = null, $options = null) {
|
||||
parent::__construct($dsn, $username, $passwd, $options);
|
||||
$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
||||
}
|
||||
@@ -14,18 +14,18 @@ abstract class Minz_Pdo extends PDO {
|
||||
abstract public function dbType();
|
||||
|
||||
private $prefix = '';
|
||||
public function prefix() {
|
||||
public function prefix(): string {
|
||||
return $this->prefix;
|
||||
}
|
||||
public function setPrefix($prefix) {
|
||||
public function setPrefix(string $prefix) {
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
private function autoPrefix($sql) {
|
||||
private function autoPrefix(string $sql): string {
|
||||
return str_replace('`_', '`' . $this->prefix, $sql);
|
||||
}
|
||||
|
||||
protected function preSql($statement) {
|
||||
protected function preSql(string $statement): string {
|
||||
if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) {
|
||||
invalidateHttpCache();
|
||||
}
|
||||
@@ -43,14 +43,14 @@ abstract class Minz_Pdo extends PDO {
|
||||
|
||||
// PHP8+: PDO::prepare(string $query, array $options = []): PDOStatement|false
|
||||
#[\ReturnTypeWillChange]
|
||||
public function prepare($statement, $driver_options = array()) {
|
||||
public function prepare(string $statement, array $driver_options = []) {
|
||||
$statement = $this->preSql($statement);
|
||||
return parent::prepare($statement, $driver_options);
|
||||
}
|
||||
|
||||
// PHP8+: PDO::exec(string $statement): int|false
|
||||
#[\ReturnTypeWillChange]
|
||||
public function exec($statement) {
|
||||
public function exec(string $statement) {
|
||||
$statement = $this->preSql($statement);
|
||||
return parent::exec($statement);
|
||||
}
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
*/
|
||||
|
||||
class Minz_PdoMysql extends Minz_Pdo {
|
||||
public function __construct($dsn, $username = null, $passwd = null, $options = null) {
|
||||
public function __construct(string $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() {
|
||||
public function dbType(): string {
|
||||
return 'mysql';
|
||||
}
|
||||
|
||||
|
||||
@@ -6,16 +6,16 @@
|
||||
*/
|
||||
|
||||
class Minz_PdoPgsql extends Minz_Pdo {
|
||||
public function __construct($dsn, $username = null, $passwd = null, $options = null) {
|
||||
public function __construct(string $dsn, $username = null, $passwd = null, $options = null) {
|
||||
parent::__construct($dsn, $username, $passwd, $options);
|
||||
$this->exec("SET NAMES 'UTF8';");
|
||||
}
|
||||
|
||||
public function dbType() {
|
||||
public function dbType(): string {
|
||||
return 'pgsql';
|
||||
}
|
||||
|
||||
protected function preSql($statement) {
|
||||
protected function preSql(string $statement): string {
|
||||
$statement = parent::preSql($statement);
|
||||
return str_replace(array('`', ' LIKE '), array('"', ' ILIKE '), $statement);
|
||||
}
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
*/
|
||||
|
||||
class Minz_PdoSqlite extends Minz_Pdo {
|
||||
public function __construct($dsn, $username = null, $passwd = null, $options = null) {
|
||||
public function __construct(string $dsn, $username = null, $passwd = null, $options = null) {
|
||||
parent::__construct($dsn, $username, $passwd, $options);
|
||||
$this->exec('PRAGMA foreign_keys = ON;');
|
||||
}
|
||||
|
||||
public function dbType() {
|
||||
public function dbType(): string {
|
||||
return 'sqlite';
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user