Guest User

Untitled

a guest
Aug 27th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.40 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This class creates database connections using PDO as a backend.
  5.  * Upon construction PDO::ATTR_ERRMODE is set to PDO::ERRMODE_EXCEPTION.
  6.  * Upon connection failure, PDOException is thrown and execution is stopped.
  7.  * @param string $dsn
  8.  * @param string $user
  9.  * @param string $pass
  10.  * @param array $options [optional]
  11.  * @version v 1.0 2012/05/05 09:00:00
  12.  * @author Dustin David Novo
  13.  */
  14. class PDOManager {
  15.     /** Member Variables */
  16.  
  17.     /**
  18.      * PDO database connection handle.
  19.      * @var PDO
  20.      */
  21.     private $connection;
  22.  
  23.     /**
  24.      * PDOStatement handle
  25.      * @var PDOStatement
  26.      */
  27.     private $sth;
  28.  
  29.     /**
  30.      * Holds the column count of the most recent query.
  31.      * @var int
  32.      */
  33.     private $columnCount;
  34.  
  35.     /**
  36.      * Holds the insert ID of the last inserted object.
  37.      * @var int
  38.      */
  39.     private $lastInsertId;
  40.  
  41.     /**
  42.      * Holds the number of affected rows from the last query.
  43.      * @var int
  44.      */
  45.     private $affectedRowCount;
  46.  
  47.     /**
  48.      * Holds the most recent database query.
  49.      * @var string
  50.      */
  51.     private $lastQuery;
  52.  
  53.     /** Methods */
  54.  
  55.     /**
  56.      * Constructor calls PDOManager::openConnection()
  57.      * @see PDOManager::openConnection()
  58.      * @param string $dsn - Host and database to connect to.
  59.      * @param string $user - Username to connect with database. [optional]
  60.      * @param string $pass - Password to connect with database. [optional]
  61.      * @param array $options - Driver options to use when connecting. [optional]
  62.      */
  63.     function __construct($dsn, $user = NULL, $pass = NULL, $options = NULL) {
  64.         $this->openConnection($dsn, $user, $pass, $options);
  65.     }
  66.  
  67.     /**
  68.      * Creates new PDO and assigns it to $this->connection.
  69.      * Sets PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION.
  70.      * Upon connection failure PDOException is thrown and
  71.      * execution is halted.
  72.      *
  73.      * This function uses references passed by the constructor.
  74.      * @param string $dsn - Host and database to connect to.
  75.      * @param string $user - Username to connect with database.
  76.      * @param string $pass - Password to connect with database.
  77.      * @param array $options - Driver options to use when connecting. [optional]
  78.      */
  79.     public function openConnection(&$dsn, &$user, &$pass, &$options = NULL) {
  80.         try {
  81.             $this->connection = new PDO($dsn, $user, $pass, $options);
  82.             $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  83.         }
  84.         catch (PDOException $exc) {
  85.             echo "Connecting to database failed: " . $exc->getMessage();
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Closes connection by setting $this->connection = NULL.
  91.      * Returns true if connection has been destroyed.
  92.      * Returns false if there was no connection to destroy.
  93.      * @return boolean
  94.      */
  95.     public function closeConnection() {
  96.         if (isset($this->connection)) {
  97.             $this->connection = NULL;
  98.             return TRUE;
  99.         }
  100.         else {
  101.             return FALSE;
  102.         }
  103.     }
  104.  
  105.     /**
  106.      * Queries the current connection and returns mixed results
  107.      * depending on $fetch_style used. If only $boundParams is to be used
  108.      * simply pass NULL for $fetch_style and $fetch_class parameters.
  109.      *
  110.      * Function also sets the lastInsertId, affectedRowCount, columnCount,
  111.      * and lastQuery variable values for use if needed.
  112.      *
  113.      * @param string $sql - The SQL query to execute.
  114.      * @param int $fetch_style - The PDO::FETCH_* attribute to use. [optional]
  115.      * @param string $fetch_class - Used if PDO::FETCH_CLASS is chosen. [optional]
  116.      * @param array $boundParams - Array of parameters to bind. [optional]
  117.      * @return mixed <br/>If no $fetch_style is defined, function will return
  118.      * the affected row count as INT.
  119.      *
  120.      * If a $fetch_style is defined the function will return
  121.      * an array of all results (fetchAll) using the given $fetch_style.
  122.      *
  123.      * NOTE: If PDO::FETCH_CLASS is chosen function will expect $fetch_class
  124.      * to exist as well. However, if $fetch_class is left NULL the function
  125.      * will still return results as if PDO::FETCH_OBJ was the defined
  126.      * $fetch_style.
  127.      * @throws PDOException
  128.      */
  129.     public function query($sql, $fetch_style = NULL, $fetch_class = NULL, $boundParams = NULL) {
  130.         $this->lastQuery = $sql;
  131.         $waiting = true;
  132.         while ($waiting) {
  133.             try {
  134.                 $this->sth = $this->connection->prepare($sql);
  135.                 $this->sth->closeCursor();
  136.  
  137.                 $this->connection->beginTransaction();
  138.                
  139.                 if ($boundParams == NULL) {
  140.                     $this->sth->execute();
  141.                 }
  142.                 else {
  143.                     $this->sth->execute($boundParams);
  144.                 }
  145.                 $this->lastInsertId = $this->connection->lastInsertId();
  146.                 $this->columnCount = $this->sth->columnCount();
  147.                 $this->affectedRowCount = $this->sth->rowCount();
  148.                 sleep(1);
  149.                 $this->connection->commit();
  150.                 $waiting = false;
  151.  
  152.                 if ($fetch_style !== NULL) {
  153.                     if ($fetch_style === PDO::FETCH_CLASS && is_string($fetch_class) && $fetch_class != "") {
  154.                         return $this->sth->fetchAll($fetch_style, $fetch_class);
  155.                     }
  156.                     else {
  157.                         return $this->sth->fetchAll($fetch_style);
  158.                     }
  159.                 }
  160.                 else {
  161.                     return $this->getAffectedRowCount();
  162.                 }
  163.             }
  164.             catch (PDOException $exc) {
  165.                 if (stripos($exc->getMessage(), 'DATABASE IS LOCKED') !== FALSE) {
  166. // commit the transaction after waiting
  167. // also make it sleep for a bit
  168.                     usleep(500000);
  169.                     //$this->connection->commit();
  170.                 }
  171.                 else {
  172.                     $this->connection->rollBack();
  173.                     throw $exc;
  174.                 }
  175.             }
  176.         }
  177.     }
  178.  
  179.     /**
  180.      * Returns the last inserted row's id by calling PDO::lastInsertId().
  181.      *
  182.      * BEWARE: When using transactions with certain
  183.      * databases, be sure to call this function BEFORE committing.
  184.      * If called after the commit(), the function will simply return 0
  185.      * rather than the insert id.
  186.      * @see PDO::lastInsertId()
  187.      * @return int
  188.      */
  189.     public function getLastInsertId() {
  190.         return $this->lastInsertId;
  191.     }
  192.  
  193.     /**
  194.      * Returns the number of rows affected by the most recent query
  195.      * by calling PDOStatement::rowCount().
  196.      *
  197.      * BEWARE: When using transaction with certain
  198.      * databases, be sure to call this function BEFORE committing.
  199.      * @see PDOStatement::rowCount()
  200.      * @return int
  201.      */
  202.     public function getAffectedRowCount() {
  203.         return $this->affectedRowCount;
  204.     }
  205.  
  206.     /**
  207.      * Returns the number of columns in the last result set by calling
  208.      * PDOStatement::columnCount().
  209.      * @return int
  210.      */
  211.     public function getColumnCount() {
  212.         return $this->columnCount;
  213.     }
  214.  
  215.     /**
  216.      * Returns the last query ran whether it succeeded or not.
  217.      * @return string
  218.      */
  219.     public function getLastQuery() {
  220.         return $this->lastQuery;
  221.     }
  222.  
  223. }
  224.  
  225. ?>
Add Comment
Please, Sign In to add comment