[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/ -> db_pdo.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id: db_pdo.php 5828 2012-05-08 16:06:16Z Tomm $
  10   */
  11  
  12  class dbpdoEngine {
  13  
  14      /**
  15       * The database class to store PDO objects
  16       *
  17       * @var object
  18       */
  19      public $db;
  20      
  21      /**
  22       * The last query resource that ran
  23       *
  24       * @var object
  25       */
  26      public $last_query = "";
  27      
  28      public $seek_array = array();
  29      
  30      public $queries = 0;
  31  
  32      /**
  33       * Connect to the database.
  34       *
  35       * @param string The database DSN.
  36       * @param string The database username. (depends on DSN)
  37       * @param string The database user's password. (depends on DSN)
  38       * @param array The databases driver options (optional)
  39       * @return boolean True on success
  40       */
  41  	function __construct($dsn, $username="", $password="", $driver_options=array())
  42      {
  43          try
  44          {
  45              $this->db = new PDO($dsn, $user, $password, $driver_options);
  46          } 
  47          catch(PDOException $exception)
  48          {
  49              die('Connection failed: '.$exception->getMessage());
  50          }
  51          
  52          $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  53          
  54          return true;
  55      }
  56      
  57      /**
  58       * Query the database.
  59       *
  60       * @param string The query SQL.
  61       * @return resource The query data.
  62       */
  63  	function query($string)
  64      {
  65          ++$this->queries;
  66          
  67          $query = $this->db->query($string, PDO::FETCH_BOTH);        
  68          $this->last_query = $query;
  69          
  70          $query->guid = $this->queries;
  71          
  72          return $query;
  73      }
  74      
  75      /**
  76       * Return a result array for a query.
  77       *
  78       * @param resource The query resource.
  79       * @return array The array of results.
  80       */
  81  	function fetch_array($query)
  82      {
  83          if(!is_object($query))
  84          {
  85              return;
  86          }
  87          
  88          if($this->seek_array[$query->guid])
  89          {
  90              $array = $query->fetch(PDO::FETCH_BOTH, $this->seek[$query->guid]['offset'], $this->seek[$query->guid]['row']);
  91          }
  92          else
  93          {
  94              $array = $query->fetch(PDO::FETCH_BOTH);
  95          }
  96          
  97          return $array;
  98      }
  99      
 100      /**
 101       * Moves internal row pointer to the next row
 102       *
 103       * @param resource The query resource.
 104       * @param int The pointer to move the row to.
 105       */
 106  	function seek($query, $row)
 107      {
 108          if(!is_object($query))
 109          {
 110              return;
 111          }
 112          
 113          $this->seek_array[$query->guid] = array('offset' => PDO::FETCH_ORI_ABS, 'row' => $row);
 114      }
 115      
 116      /**
 117       * Return the number of rows resulting from a query.
 118       *
 119       * @param resource The query resource.
 120       * @return int The number of rows in the result.
 121       */
 122  	function num_rows($query)
 123      {
 124          if(!is_object($query))
 125          {
 126              return;
 127          }
 128          
 129          return count($query->rowCount());
 130      }
 131      
 132      /**
 133       * Return the last id number of inserted data.
 134       *
 135       * @param string The name of the insert id to check. (Optional)
 136       * @return int The id number.
 137       */
 138  	function insert_id($name="")
 139      {
 140          return $this->db->lastInsertId($name);
 141      }
 142      
 143      /**
 144       * Return an error number.
 145       *
 146       * @param resource The query resource.
 147       * @return int The error number of the current error.
 148       */
 149  	function error_number($query)
 150      {
 151          if(!is_object($query) || !method_exists($query, "errorCode"))
 152          {
 153              return;
 154          }
 155          
 156          $errorcode = $query->errorCode();
 157          
 158          return $errorcode;
 159      }
 160      
 161      /**
 162       * Return an error string.
 163       *
 164       * @param resource The query resource.
 165       * @return int The error string of the current error.
 166       */
 167  	function error_string($query)
 168      {
 169          if(!is_object($query) || !method_exists($query, "errorInfo"))
 170          {
 171              return $this->db->errorInfo();
 172          }
 173          return $query->errorInfo();
 174      }
 175      
 176      /**
 177       * Roll back the last query.
 178       *
 179       * @return boolean true on success, false otherwise.
 180       */
 181  	function roll_back()
 182      {
 183          //return $this->db->rollBack();
 184      }
 185      
 186      /**
 187       * Returns the number of affected rows in a query.
 188       *
 189       * @return int The number of affected rows.
 190       */
 191  	function affected_rows($query)
 192      {
 193          return $query->rowCount();
 194      }
 195      
 196      /**
 197       * Return the number of fields.
 198       *
 199       * @param resource The query resource.
 200       * @return int The number of fields.
 201       */
 202  	function num_fields($query)
 203      {
 204          return $query->columnCount();
 205      }
 206      
 207  	function escape_string($string)
 208      {
 209          $string = $this->db->quote($string);    
 210          
 211          // Remove ' from the begginging of the string and at the end of the string, because we already use it in insert_query
 212          $string = substr($string, 1);
 213          $string = substr($string, 0, -1);
 214          
 215          return $string;
 216      }
 217      
 218      /**
 219       * Return a selected attribute
 220       *
 221       * @param constant The attribute to check.
 222       * @return string The value of the attribute.
 223       */
 224  	function get_attribute($attribute)
 225      {
 226          $attribute = $this->db->getAttribute(constant("PDO::".$attribute.""));
 227          
 228          return $attribute;
 229      }
 230  }
 231  
 232  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1