[ Index ]

PHP Cross Reference of MyBB 1.8.37

title

Body

[close]

/inc/ -> db_pdo.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  class dbpdoEngine
  12  {
  13      /**
  14       * The database class to store PDO objects
  15       *
  16       * @var PDO
  17       */
  18      private $db;
  19  
  20      /**
  21       * The last query resource that ran
  22       *
  23       * @var PDOStatement
  24       */
  25      public $last_query;
  26  
  27      /**
  28       * Array used to seek through result sets. This is used when using the `fetch_field` method with a row specified.
  29       *
  30       * @var array Array keyed by object hashes for {@see PDOStatement} instances.
  31       */
  32      private $seek_array = array();
  33  
  34      /**
  35       * Connect to the database.
  36       *
  37       * @param string $dsn The database DSN.
  38       * @param string $username The database username. (depends on DSN)
  39       * @param string $password The database user's password. (depends on DSN)
  40       * @param array $driver_options The databases driver options (optional)
  41       *
  42       * @throws Exception Thrown when failing to connect to the database.
  43       */
  44  	function __construct($dsn, $username="", $password="", $driver_options=array())
  45      {
  46          try
  47          {
  48              $driver_options =
  49                  $driver_options +
  50                  array(
  51                      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  52                      PDO::ATTR_EMULATE_PREPARES => false,
  53                  )
  54              ;
  55  
  56              $this->db = new PDO($dsn, $username, $password, $driver_options);
  57          }
  58          catch(PDOException $exception)
  59          {
  60              throw new Exception('Unable to connect to database server');
  61          }
  62      }
  63  
  64      /**
  65       * Query the database.
  66       *
  67       * @param string $string The query SQL.
  68       *
  69       * @return PDOStatement The query data.
  70       */
  71  	public function query($string)
  72      {
  73          $query = $this->db->query($string, PDO::FETCH_BOTH);
  74          $this->last_query = $query;
  75  
  76          return $query;
  77      }
  78  
  79      /**
  80       * Return a result array for a query.
  81       *
  82       * @param PDOStatement $query The query resource.
  83       * @param int $resulttype One of PDO's constants: FETCH_ASSOC, FETCH_BOUND, FETCH_CLASS, FETCH_INTO, FETCH_LAZY, FETCH_NAMED, FETCH_NUM, FETCH_OBJ or FETCH_BOTH
  84       * @return array The array of results.
  85       */
  86  	public function fetch_array($query, $resulttype=PDO::FETCH_BOTH)
  87      {
  88          switch($resulttype)
  89          {
  90              case PDO::FETCH_ASSOC:
  91              case PDO::FETCH_BOUND:
  92              case PDO::FETCH_CLASS:
  93              case PDO::FETCH_INTO:
  94              case PDO::FETCH_LAZY:
  95              case PDO::FETCH_NAMED:
  96              case PDO::FETCH_NUM:
  97              case PDO::FETCH_OBJ:
  98                  break;
  99              default:
 100                  $resulttype = PDO::FETCH_BOTH;
 101                  break;
 102          }
 103  
 104          $hash = spl_object_hash($query);
 105  
 106          if(isset($this->seek_array[$hash]))
 107          {
 108              $array = $query->fetch($resulttype, $this->seek_array[$hash]['offset'], $this->seek_array[$hash]['row']);
 109          }
 110          else
 111          {
 112              $array = $query->fetch($resulttype);
 113          }
 114  
 115          return $array;
 116      }
 117  
 118      /**
 119       * Moves internal row pointer to the next row
 120       *
 121       * @param PDOStatement $query The query resource.
 122       * @param int $row The pointer to move the row to.
 123       */
 124  	public function seek($query, $row)
 125      {
 126          $hash = spl_object_hash($query);
 127  
 128          $this->seek_array[$hash] = array('offset' => PDO::FETCH_ORI_ABS, 'row' => $row);
 129      }
 130  
 131      /**
 132       * Return the number of rows resulting from a query.
 133       *
 134       * @param PDOStatement $query The query resource.
 135       * @return int The number of rows in the result.
 136       */
 137  	public function num_rows($query)
 138      {
 139          if(stripos($query->queryString, 'SELECT') !== false)
 140          {
 141              $query = $this->db->query($query->queryString);
 142              $result = $query->fetchAll();
 143              return count($result);
 144          }
 145          else
 146          {
 147              return $query->rowCount();
 148          }
 149      }
 150  
 151      /**
 152       * Return the last id number of inserted data.
 153       *
 154       * @param string|null $name The name of the insert id to check. (Optional)
 155       * @return int The id number.
 156       */
 157  	public function insert_id($name=null)
 158      {
 159          return $this->db->lastInsertId($name);
 160      }
 161  
 162      /**
 163       * Return an error number.
 164       *
 165       * @param PDOStatement $query The query resource.
 166       * @return int The error number of the current error.
 167       */
 168  	public function error_number($query)
 169      {
 170          if(!method_exists($query, "errorCode"))
 171          {
 172              return 0;
 173          }
 174  
 175          return $query->errorCode();
 176      }
 177  
 178      /**
 179       * Return an error string.
 180       *
 181       * @param PDOStatement $query The query resource.
 182       * @return array The error string of the current error.
 183       */
 184  	public function error_string($query)
 185      {
 186          if(!method_exists($query, "errorInfo"))
 187          {
 188              return $this->db->errorInfo();
 189          }
 190  
 191          return $query->errorInfo();
 192      }
 193  
 194      /**
 195       * Returns the number of affected rows in a query.
 196       *
 197       * @param PDOStatement $query
 198       * @return int The number of affected rows.
 199       */
 200  	public function affected_rows($query)
 201      {
 202          return $query->rowCount();
 203      }
 204  
 205      /**
 206       * Return the number of fields.
 207       *
 208       * @param PDOStatement $query The query resource.
 209       * @return int The number of fields.
 210       */
 211  	public function num_fields($query)
 212      {
 213          return $query->columnCount();
 214      }
 215  
 216      /**
 217       * Escape a string according to the pdo escape format.
 218       *
 219       * @param string $string The string to be escaped.
 220       * @return string The escaped string.
 221       */
 222  	public function escape_string($string)
 223      {
 224          $string = $this->db->quote($string);
 225  
 226          // Remove ' from the beginning of the string and at the end of the string, because we already use it in insert_query
 227          $string = substr($string, 1);
 228          $string = substr($string, 0, -1);
 229  
 230          return $string;
 231      }
 232  
 233      /**
 234       * Return a selected attribute
 235       *
 236       * @param string $attribute The attribute to check.
 237       * @return string The value of the attribute.
 238       */
 239  	public function get_attribute($attribute)
 240      {
 241          $attribute = $this->db->getAttribute(constant("PDO::{$attribute}"));
 242  
 243          return $attribute;
 244      }
 245  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref