Commit: 73062503a65bfa62d5f2f365ee99269225580bb9 Author: Peter Kokot <peterko...@gmail.com> Wed, 5 Dec 2018 17:55:06 +0100 Parents: 3f343755266d5fd669b115f86ea279d4c49567f8 Branches: master
Link: http://git.php.net/?p=web/bugs.git;a=commitdiff;h=73062503a65bfa62d5f2f365ee99269225580bb9 Log: Refactor database classes Changed paths: D include/classes/bug_pdo.php M include/prepend.php A src/Database/Database.php A src/Database/Statement.php Diff: diff --git a/include/classes/bug_pdo.php b/include/classes/bug_pdo.php deleted file mode 100644 index 536762c..0000000 --- a/include/classes/bug_pdo.php +++ /dev/null @@ -1,73 +0,0 @@ -<?php -/** - * Thin PDO wrapper for bugs.php.net. - * - * @author Maciej Sobaczewski <so...@php.net> - */ - -class Bug_PDO extends PDO -{ - /** - * When creating new PDO object, automagically switch PDOStatement with - * own extended implementation. - */ - public function __construct($dsn, $username = '', $password = '', array $options = []) - { - parent::__construct($dsn, $username, $password, $options); - - $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['Bug_PDOStatement']); - } - - /** - * PDO puts apostrophes around the text so we need to strip the outermost - * characters. - */ - public function escape($text, $escape_wildcards = false) - { - return substr($this->quote($text), 1, -1); - } - - public function queryAll($query, $types = null, $fetchmode = null, $rekey = false, $force_array = false, $group = false) - { - return $this->query($query)->fetchAll(); - } -} - -class Bug_PDOStatement extends PDOStatement -{ - /** - * This allows chaining execute() method: - * $db->query('SELECT a FROM b WHERE c = ?')->execute('foo')->fetch(); - * PDOStatement::execute(), on the other hand, returns boolean. Change it to - * return $this and thus allow further method chaining. - */ - public function execute($input_parameters = NULL) - { - parent::execute($input_parameters); - - return $this; - } - - public function fetchAll($fetchode = null, $rekey = false, $force_array = false, $group = false) - { - return parent::fetchAll(); - } - - public function fetchCol($colnum) - { - return parent::fetchColumn($colnum); - } - - public function fetchOne($colnum = 0, $rownum = null) - { - return $this->fetch(PDO::FETCH_NUM)[0]; - } - - public function fetchRow($mode=NULL) - { - if (!$mode) { - $mode = PDO::FETCH_BOTH; - } - return $this->fetch($mode); - } -} diff --git a/include/prepend.php b/include/prepend.php index 60fdd8c..9e03d69 100644 --- a/include/prepend.php +++ b/include/prepend.php @@ -1,6 +1,7 @@ <?php use App\Autoloader; +use App\Database\Database; // Dual PSR-4 compatible class autoloader. When Composer is not available, an // application specific replacement class is used. Once Composer can be added @@ -66,13 +67,12 @@ define('DATABASE_DSN', "mysql:host={$site_data['db_host']};dbname={$site_data['d * Obtain the functions and variables used throughout the bug system */ require_once "{$ROOT_DIR}/include/functions.php"; -require 'classes/bug_pdo.php'; // Database connection (required always?) -$dbh = new Bug_PDO(DATABASE_DSN, $site_data['db_user'], $site_data['db_pass'], [ - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, - PDO::ATTR_EMULATE_PREPARES => false, +$dbh = new Database(DATABASE_DSN, $site_data['db_user'], $site_data['db_pass'], [ + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, + \PDO::ATTR_EMULATE_PREPARES => false, ]); // Last Updated.. diff --git a/src/Database/Database.php b/src/Database/Database.php new file mode 100644 index 0000000..37ae753 --- /dev/null +++ b/src/Database/Database.php @@ -0,0 +1,38 @@ +<?php + +namespace App\Database; + +use App\Database\Statement; + +/** + * Thin PDO wrapper for bugs.php.net. + * + * @author Maciej Sobaczewski <so...@php.net> + */ +class Database extends \PDO +{ + /** + * When creating new PDO object, automagically switch PDOStatement with own + * extended implementation. + */ + public function __construct(string $dsn, string $username = '', string $password = '', array $options = []) + { + parent::__construct($dsn, $username, $password, $options); + + $this->setAttribute(\PDO::ATTR_STATEMENT_CLASS, [Statement::class]); + } + + /** + * PDO puts apostrophes around the text so we need to strip the outermost + * characters. + */ + public function escape($text, $escape_wildcards = false) + { + return substr($this->quote($text), 1, -1); + } + + public function queryAll($query, $types = null, $fetchmode = null, $rekey = false, $force_array = false, $group = false) + { + return $this->query($query)->fetchAll(); + } +} diff --git a/src/Database/Statement.php b/src/Database/Statement.php new file mode 100644 index 0000000..75b545b --- /dev/null +++ b/src/Database/Statement.php @@ -0,0 +1,46 @@ +<?php + +namespace App\Database; + +/** + * PDOStatement wrapper class. + */ +class Statement extends \PDOStatement +{ + /** + * This allows chaining execute() method: + * $db->query('SELECT a FROM b WHERE c = ?')->execute('foo')->fetch(); + * \PDOStatement::execute(), on the other hand, returns boolean. Change it + * to return $this and thus allow further method chaining. + */ + public function execute($input_parameters = null) + { + parent::execute($input_parameters); + + return $this; + } + + public function fetchAll($fetchode = null, $rekey = false, $force_array = false, $group = false) + { + return parent::fetchAll(); + } + + public function fetchCol($colnum) + { + return parent::fetchColumn($colnum); + } + + public function fetchOne($colnum = 0, $rownum = null) + { + return $this->fetch(\PDO::FETCH_NUM)[0]; + } + + public function fetchRow($mode = null) + { + if (!$mode) { + $mode = \PDO::FETCH_BOTH; + } + + return $this->fetch($mode); + } +} -- PHP Webmaster List Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php