Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.21 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Thin compatibilty layer between MDB2 and PDO for bugs.php.net.
  4.  *
  5.  * Please mind that it doesn't implement full feature set, only
  6.  * those used by old codebase. New code interacting with the
  7.  * database should be written using normal PDO's approach.
  8.  *
  9.  * @author Maciej Sobaczewski <sobak@php.net>
  10.  */
  11.  
  12. // Define missing constants
  13. define('MDB2_FETCHMODE_ASSOC', null);
  14.  
  15. class Bug_PDO extends PDO
  16. {
  17.     /**
  18.      * When creating new PDO object, automagically switches PDOStatement with
  19.      * own extended implementation.
  20.      */
  21.     public function __construct($dsn, $username = '', $password = '', array $options = [])
  22.     {
  23.         parent::__construct($dsn, $username, $password, $options);
  24.  
  25.         $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['Bug_PDOStatement']);
  26.     }
  27.  
  28.     /**
  29.      * MDB2::espace() doesn't put apostrophes around the text and PDO does so we strip
  30.      * outermost characters to remove duplicates.
  31.      */
  32.     public function escape($text, $escape_wildcards = false)
  33.     {
  34.         return substr($this->quote($text), 1, -1);
  35.     }
  36.  
  37.     public function queryAll($query, $types = null, $fetchmode = null, $rekey = false, $force_array = false, $group = false)
  38.     {
  39.         return $this->query($query)->fetchAll();
  40.     }
  41. }
  42.  
  43. class Bug_PDOStatement extends PDOStatement
  44. {
  45.     /**
  46.      * MDB2 allows for chaining execute() method like so:
  47.      * $db->query('SELECT a FROM b WHERE c = ?')->execute('foo')->fetch();
  48.      * PDOStatement::execute(), on the other hand, returns boolean we change
  49.      * it to return $this and thus allow futher method chaining.
  50.      */
  51.     public function execute(array $input_parameters = [])
  52.     {
  53.         parent::execute($input_parameters);
  54.  
  55.         return $this;
  56.     }
  57.  
  58.     public function fetchAll($fetchode = null, $rekey = false, $force_array = false, $group = false)
  59.     {
  60.         return parent::fetchAll();
  61.     }
  62.  
  63.     public function fetchCol($colnum)
  64.     {
  65.         return parent::fetchColumn($colnum);
  66.     }
  67.  
  68.     public function fetchOne($colnum = 0, $rownum = null)
  69.     {
  70.         return $this->fetch(PDO::FETCH_NUM)[0];
  71.     }
  72.  
  73.     public function fetchRow($mode)
  74.     {
  75.         return $this->fetch();
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement