MediaSearch_Config
[ class tree: MediaSearch_Config ] [ index: MediaSearch_Config ] [ all elements ]

Source for file Sql.php

Documentation is available at Sql.php

  1. <?php
  2. /**
  3.  * MediaSearch
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * $Author$
  14.  * $Revision$
  15.  *
  16.  * @category    MediaSearch
  17.  * @package     MediaSearch_Config
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * @see MediaSearch_Config_Abstract
  23.  */
  24. require_once('MediaSearch/Config/Abstract.php');
  25. /**
  26.  * @see MediaSearch_Interface_Command
  27.  */
  28. require_once('MediaSearch/Interface/Command.php');
  29.  
  30. /**
  31.  * A concrete implemetation for SQL config files. The file can contain
  32.  * any SQL statement. The filename extensions need to be .sql.
  33.  *
  34.  * This type of config also implement MediaSearch_Interface_Command.
  35.  * Thatt means that you can execute the configuration on a database.
  36.  * To do this its neccessary to provide a proper PDO and Database name
  37.  * to the config object.
  38.  *
  39.  * @category   MediaSearch
  40.  * @package    MediaSearch_Config
  41.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  42.  */
  43.                              implements MediaSearch_Interface_Command {
  44.     const TRACE_LELVEL = 12;
  45.  
  46.     /**
  47.      * Optional PDO connection obejct. NEccessary for executing
  48.      * the config object on a database.
  49.      *
  50.      * @access protected
  51.      * @var PDO 
  52.      */
  53.     protected $pdoConnection;
  54.     /**
  55.      * The database name the PDO should work on.
  56.      *
  57.      * @access proteced
  58.      * @var string 
  59.      */
  60.     protected $databaseName;
  61.  
  62.     /**
  63.      * Sets PDO object and a database name. Thats neccessary if you want
  64.      * to execute the loaded SQL config on a databse.
  65.      *
  66.      * @accesss public
  67.      * @param PDO $pdo 
  68.      * @param string $databaseName 
  69.      */
  70.     public function setPdo(PDO $pdo$databaseName{
  71.         $this->pdoConnection = $pdo;
  72.         $this->databaseName  = $databaseName;
  73.     }
  74.  
  75.     /**
  76.      * MediaSearch_Interface_Command implementation.
  77.      *
  78.      * Executes the loaded SQL config on the database setted by setPdo().
  79.      *
  80.      * @todo Specify the return type of PDO!
  81.      * 
  82.      * @access public
  83.      * @throws RuntimeException On any error executing the SQL on the PDO.
  84.      * @return <type> 
  85.      */
  86.     public function execute({
  87.         if (null === $this->pdoConnection{
  88.             $message  'Please set a PDO connection object by ';
  89.             $message .= 'PDO::setPdo() before execute this object!';
  90.             throw new RuntimeException($message);
  91.         }
  92.  
  93.         $result $this->pdoConnection->query("USE {$this->databaseName};");
  94.  
  95.         if (false === $result) {
  96.             $message  = "Cant select database {$this->databaseName}.\n";
  97.             $message .= 'PDO-Error: ';
  98.             $message .= implode(', ', $this->pdoConnection->errorInfo());
  99.             throw new RuntimeException($message);
  100.         }
  101.  
  102.         $sql    = $this->get();
  103.         $result $this->pdoConnection->query($sql);
  104.  
  105.         if (false === $result{
  106.             $message  = "Cant execute sql:\n";
  107.             $message .= "$sql\n";
  108.             $message .= 'PDO-Error: ';
  109.             $message .= implode(', ', $this->pdoConnection->errorInfo());
  110.             throw new RuntimeException($message);
  111.         }
  112.  
  113.         return $result;
  114.     }

Documentation generated on Mon, 17 Aug 2009 14:54:05 +0200 by phpDocumentor 1.4.2