Source for file PDO.php

Documentation is available at PDO.php

  1. <?php
  2. /**
  3.  * This file contains the class XML_Query2XML_Driver_PDO.
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * @category  XML
  8.  * @package   XML_Query2XML
  9.  * @author    Lukas Feiler <lukas.feiler@lukasfeiler.com>
  10.  * @copyright 2006 Lukas Feiler
  11.  * @license   http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
  12.  * @version   CVS: $Id: PDO.php 276639 2009-03-01 13:17:08Z lukasfeiler $
  13.  * @link      http://pear.php.net/package/XML_Query2XML
  14.  */
  15.  
  16. /**
  17.  * XML_Query2XML_Driver_PDO extends XML_Query2XML_Driver.
  18.  */
  19. require_once 'XML/Query2XML.php';
  20.  
  21. /**
  22.  * Driver for the database abstraction layer PDO.
  23.  *
  24.  * usage:
  25.  * <code>
  26.  * $driver = XML_Query2XML_Driver::factory(new PDO(...));
  27.  * </code>
  28.  *
  29.  * @category  XML
  30.  * @package   XML_Query2XML
  31.  * @author    Lukas Feiler <lukas.feiler@lukasfeiler.com>
  32.  * @copyright 2006 Lukas Feiler
  33.  * @license   http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
  34.  * @version   Release: 1.7.2
  35.  * @link      http://pear.php.net/package/XML_Query2XML
  36.  * @since     Release 1.5.0RC1
  37.  */
  38. {
  39.     /**
  40.      * In instance of PDO
  41.      * @var PDO 
  42.      */
  43.     private $_db null;
  44.     
  45.     /**
  46.      * Constructor
  47.      *
  48.      * @param PDO $db An instance of PDO.
  49.      *
  50.      * @throws XML_Query2XML_DBException If PDO::ATTR_ERRMODE cannot be set to
  51.      *                                PDO::ERRMODE_EXCEPTION.
  52.      */
  53.     public function __construct(PDO $db)
  54.     {
  55.         $success $db->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
  56.         if (!$success{
  57.             // no unit tests for this one
  58.             throw new XML_Query2XML_DBException(
  59.                 'Could not set attribute PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION'
  60.             );
  61.         }
  62.         $this->_db $db;
  63.     }
  64.     
  65.     /**
  66.      * Execute a SQL SELECT stement and fetch all records from the result set.
  67.      *
  68.      * @param mixed  $sql        The SQL query as a string or an array.
  69.      * @param string $configPath The config path; used for exception messages.
  70.      *
  71.      * @return array An array of records.
  72.      * @throws XML_Query2XML_DBException If a database related error occures.
  73.      * @see XML_Query2XML_Driver::getAllRecords()
  74.      */
  75.     public function getAllRecords($sql$configPath)
  76.     {
  77.         $result =$this->_prepareAndExecute($sql$configPath);
  78.         try {
  79.             $records $result->fetchAll();
  80.         catch (PDOException $e{
  81.             /*
  82.              * unit tests: PDO/getXML/
  83.              *  throwDBException_nullResultSet_complexQuery_multipleRecords.phpt
  84.              *  throwDBException_nullResultSet_complexQuery_singleRecord.phpt
  85.              *  throwDBException_nullResultSet_simpleQuery_multipleRecords.phpt
  86.              *  throwDBException_nullResultSet_simpleQuery_singleRecord.phpt
  87.              */
  88.             throw new XML_Query2XML_DBException(
  89.                 $configPath ': Could not fetch records for the following SQL '
  90.                 . 'query: ' $sql['query'.  '; '
  91.                 . $e->getMessage()
  92.             );
  93.         }
  94.         return $records;
  95.     }
  96.     
  97.     /**
  98.      * Private method that will use PDO::query() for simple and
  99.      * PDO::prepare() & PDOStatement::execute() for complex query specifications.
  100.      *
  101.      * @param mixed  $sql        A string or an array.
  102.      * @param string $configPath The config path used for exception messages.
  103.      *
  104.      * @return PDOStatement 
  105.      * @throws XML_Query2XML_DBException If a database related error occures.
  106.      */
  107.     private function _prepareAndExecute($sql$configPath)
  108.     {
  109.         $query =$sql['query'];
  110.         if (isset($this->_preparedQueries[$query])) {
  111.             $queryHandle $this->_preparedQueries[$query];
  112.         else {
  113.             // PREPARE
  114.             $queryHandle $this->_db->prepare($query);
  115.             if ($queryHandle === false{
  116.                 /*
  117.                  * No unit test for this exception as neither the mysql, pgsql
  118.                  * or sqlite driver ever returns false from PDO::prepare().
  119.                  */
  120.                 throw new XML_Query2XML_DBException(
  121.                     $configPath ': Could not prepare the following SQL '
  122.                     . 'query - PDO::prepare() returned false: ' $query
  123.                 );
  124.             }
  125.             $this->_preparedQueries[$query=$queryHandle;
  126.         }
  127.         
  128.         // EXECUTE
  129.         try {
  130.             if (isset($sql['data'])) {
  131.                 $queryHandle->execute($sql['data']);
  132.             else {
  133.                 $queryHandle->execute();
  134.             }
  135.         catch (PDOException $e{
  136.             /*
  137.              * unit test: PDO/_prepareAndExecute
  138.              *  throwDBException_complexQuery.phpt
  139.              */
  140.             throw new XML_Query2XML_DBException(
  141.                 $configPath ': Could not execute the following SQL query: '
  142.                 . $query .  '; ' $e->getMessage()
  143.             );
  144.         }
  145.         $success $queryHandle->setFetchMode(PDO::FETCH_ASSOC);
  146.         if (!$success{
  147.             // no unit tests for this one
  148.             throw new XML_Query2XML_DBException(
  149.                 'Could not set fetch mode to PDO::FETCH_ASSOC'
  150.             );
  151.         }
  152.         return $queryHandle;
  153.     }
  154. }
  155. ?>

Documentation generated on Sun, 03 Apr 2011 13:13:09 +0200 by phpDocumentor 1.4.1