DevHeads.net

com web/bugs: Refactor database classes: include/classes/bug_pdo.php include/prepend.php src/Database/Database.php src/Database/Statement.php

Link: <a href="http://git.php.net/?p=web/bugs.git;a=commitdiff;h=73062503a65bfa62d5f2f365ee99269225580bb9" title="http://git.php.net/?p=web/bugs.git;a=commitdiff;h=73062503a65bfa62d5f2f365ee99269225580bb9">http://git.php.net/?p=web/bugs.git;a=commitdiff;h=73062503a65bfa62d5f2f3...</a>

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 < ... at php dot net>
- */
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 < ... at php dot 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);
+ }
+}