"Fossies" - the Fresh Open Source Software Archive

Member "icingaweb2-2.9.1/library/vendor/Zend/Db/Adapter/Pdo/Abstract.php" (27 Jul 2021, 11506 Bytes) of package /linux/www/icingaweb2-2.9.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 /**
    3  * Zend Framework
    4  *
    5  * LICENSE
    6  *
    7  * This source file is subject to the new BSD license that is bundled
    8  * with this package in the file LICENSE.txt.
    9  * It is also available through the world-wide-web at this URL:
   10  * http://framework.zend.com/license/new-bsd
   11  * If you did not receive a copy of the license and are unable to
   12  * obtain it through the world-wide-web, please send an email
   13  * to license@zend.com so we can send you a copy immediately.
   14  *
   15  * @category   Zend
   16  * @package    Zend_Db
   17  * @subpackage Adapter
   18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
   20  * @version    $Id$
   21  */
   22 
   23 
   24 /**
   25  * @see Zend_Db_Adapter_Abstract
   26  */
   27 
   28 
   29 /**
   30  * @see Zend_Db_Statement_Pdo
   31  */
   32 
   33 
   34 /**
   35  * Class for connecting to SQL databases and performing common operations using PDO.
   36  *
   37  * @category   Zend
   38  * @package    Zend_Db
   39  * @subpackage Adapter
   40  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   41  * @license    http://framework.zend.com/license/new-bsd     New BSD License
   42  */
   43 abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
   44 {
   45 
   46     /**
   47      * Default class name for a DB statement.
   48      *
   49      * @var string
   50      */
   51     protected $_defaultStmtClass = 'Zend_Db_Statement_Pdo';
   52 
   53     /**
   54      * Creates a PDO DSN for the adapter from $this->_config settings.
   55      *
   56      * @return string
   57      */
   58     protected function _dsn()
   59     {
   60         // baseline of DSN parts
   61         $dsn = $this->_config;
   62 
   63         // don't pass the username, password, charset, persistent and driver_options in the DSN
   64         unset($dsn['username']);
   65         unset($dsn['password']);
   66         unset($dsn['options']);
   67         unset($dsn['charset']);
   68         unset($dsn['persistent']);
   69         unset($dsn['driver_options']);
   70 
   71         // use all remaining parts in the DSN
   72         foreach ($dsn as $key => $val) {
   73             $dsn[$key] = "$key=$val";
   74         }
   75 
   76         return $this->_pdoType . ':' . implode(';', $dsn);
   77     }
   78 
   79     /**
   80      * Creates a PDO object and connects to the database.
   81      *
   82      * @return void
   83      * @throws Zend_Db_Adapter_Exception
   84      */
   85     protected function _connect()
   86     {
   87         // if we already have a PDO object, no need to re-connect.
   88         if ($this->_connection) {
   89             return;
   90         }
   91 
   92         // get the dsn first, because some adapters alter the $_pdoType
   93         $dsn = $this->_dsn();
   94 
   95         // check for PDO extension
   96         if (!extension_loaded('pdo')) {
   97             /**
   98              * @see Zend_Db_Adapter_Exception
   99              */
  100             throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
  101         }
  102 
  103         // check the PDO driver is available
  104         if (!in_array($this->_pdoType, PDO::getAvailableDrivers())) {
  105             /**
  106              * @see Zend_Db_Adapter_Exception
  107              */
  108             throw new Zend_Db_Adapter_Exception('The ' . $this->_pdoType . ' driver is not currently installed');
  109         }
  110 
  111         // create PDO connection
  112         $q = $this->_profiler->queryStart('connect', Zend_Db_Profiler::CONNECT);
  113 
  114         // add the persistence flag if we find it in our config array
  115         if (isset($this->_config['persistent']) && ($this->_config['persistent'] == true)) {
  116             $this->_config['driver_options'][PDO::ATTR_PERSISTENT] = true;
  117         }
  118 
  119         try {
  120             $this->_connection = new PDO(
  121                 $dsn,
  122                 $this->_config['username'],
  123                 $this->_config['password'],
  124                 $this->_config['driver_options']
  125             );
  126 
  127             $this->_profiler->queryEnd($q);
  128 
  129             // set the PDO connection to perform case-folding on array keys, or not
  130             $this->_connection->setAttribute(PDO::ATTR_CASE, $this->_caseFolding);
  131 
  132             // always use exceptions.
  133             $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  134 
  135         } catch (PDOException $e) {
  136             $message = $e->getMessage();
  137             if ($e->getPrevious() !== null && preg_match('~^SQLSTATE\[HY000\] \[\d{1,4}\]\s$~', $message)) {
  138                 // See https://bugs.php.net/bug.php?id=76604
  139                 $message .= $e->getPrevious()->getMessage();
  140             }
  141 
  142             /**
  143              * @see Zend_Db_Adapter_Exception
  144              */
  145             throw new Zend_Db_Adapter_Exception($message, $e->getCode(), $e);
  146         }
  147 
  148     }
  149 
  150     /**
  151      * Test if a connection is active
  152      *
  153      * @return boolean
  154      */
  155     public function isConnected()
  156     {
  157         return ((bool) ($this->_connection instanceof PDO));
  158     }
  159 
  160     /**
  161      * Force the connection to close.
  162      *
  163      * @return void
  164      */
  165     public function closeConnection()
  166     {
  167         $this->_connection = null;
  168     }
  169 
  170     /**
  171      * Prepares an SQL statement.
  172      *
  173      * @param string $sql The SQL statement with placeholders.
  174      * @param array $bind An array of data to bind to the placeholders.
  175      * @return PDOStatement
  176      */
  177     public function prepare($sql)
  178     {
  179         $this->_connect();
  180         $stmtClass = $this->_defaultStmtClass;
  181         if (!class_exists($stmtClass)) {
  182             Zend_Loader::loadClass($stmtClass);
  183         }
  184         $stmt = new $stmtClass($this, $sql);
  185         $stmt->setFetchMode($this->_fetchMode);
  186         return $stmt;
  187     }
  188 
  189     /**
  190      * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
  191      *
  192      * As a convention, on RDBMS brands that support sequences
  193      * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
  194      * from the arguments and returns the last id generated by that sequence.
  195      * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
  196      * returns the last value generated for such a column, and the table name
  197      * argument is disregarded.
  198      *
  199      * On RDBMS brands that don't support sequences, $tableName and $primaryKey
  200      * are ignored.
  201      *
  202      * @param string $tableName   OPTIONAL Name of table.
  203      * @param string $primaryKey  OPTIONAL Name of primary key column.
  204      * @return string
  205      */
  206     public function lastInsertId($tableName = null, $primaryKey = null)
  207     {
  208         $this->_connect();
  209         return $this->_connection->lastInsertId();
  210     }
  211 
  212     /**
  213      * Special handling for PDO query().
  214      * All bind parameter names must begin with ':'
  215      *
  216      * @param string|Zend_Db_Select $sql The SQL statement with placeholders.
  217      * @param array $bind An array of data to bind to the placeholders.
  218      * @return Zend_Db_Statement_Pdo
  219      * @throws Zend_Db_Adapter_Exception To re-throw PDOException.
  220      */
  221     public function query($sql, $bind = array())
  222     {
  223         if (empty($bind) && $sql instanceof Zend_Db_Select) {
  224             $bind = $sql->getBind();
  225         }
  226 
  227         if (is_array($bind)) {
  228             foreach ($bind as $name => $value) {
  229                 if (!is_int($name) && !preg_match('/^:/', $name)) {
  230                     $newName = ":$name";
  231                     unset($bind[$name]);
  232                     $bind[$newName] = $value;
  233                 }
  234             }
  235         }
  236 
  237         try {
  238             return parent::query($sql, $bind);
  239         } catch (PDOException $e) {
  240             /**
  241              * @see Zend_Db_Statement_Exception
  242              */
  243             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
  244         }
  245     }
  246 
  247     /**
  248      * Executes an SQL statement and return the number of affected rows
  249      *
  250      * @param  mixed  $sql  The SQL statement with placeholders.
  251      *                      May be a string or Zend_Db_Select.
  252      * @return integer      Number of rows that were modified
  253      *                      or deleted by the SQL statement
  254      */
  255     public function exec($sql)
  256     {
  257         if ($sql instanceof Zend_Db_Select) {
  258             $sql = $sql->assemble();
  259         }
  260 
  261         try {
  262             $affected = $this->getConnection()->exec($sql);
  263 
  264             if ($affected === false) {
  265                 $errorInfo = $this->getConnection()->errorInfo();
  266                 /**
  267                  * @see Zend_Db_Adapter_Exception
  268                  */
  269                 throw new Zend_Db_Adapter_Exception($errorInfo[2]);
  270             }
  271 
  272             return $affected;
  273         } catch (PDOException $e) {
  274             /**
  275              * @see Zend_Db_Adapter_Exception
  276              */
  277             throw new Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e);
  278         }
  279     }
  280 
  281     /**
  282      * Quote a raw string.
  283      *
  284      * @param string $value     Raw string
  285      * @return string           Quoted string
  286      */
  287     protected function _quote($value)
  288     {
  289         if (is_int($value) || is_float($value)) {
  290             return $value;
  291         }
  292         $this->_connect();
  293         return $this->_connection->quote($value);
  294     }
  295 
  296     /**
  297      * Begin a transaction.
  298      */
  299     protected function _beginTransaction()
  300     {
  301         $this->_connect();
  302         $this->_connection->beginTransaction();
  303     }
  304 
  305     /**
  306      * Commit a transaction.
  307      */
  308     protected function _commit()
  309     {
  310         $this->_connect();
  311         $this->_connection->commit();
  312     }
  313 
  314     /**
  315      * Roll-back a transaction.
  316      */
  317     protected function _rollBack() {
  318         $this->_connect();
  319         $this->_connection->rollBack();
  320     }
  321 
  322     /**
  323      * Set the PDO fetch mode.
  324      *
  325      * @todo Support FETCH_CLASS and FETCH_INTO.
  326      *
  327      * @param int $mode A PDO fetch mode.
  328      * @return void
  329      * @throws Zend_Db_Adapter_Exception
  330      */
  331     public function setFetchMode($mode)
  332     {
  333         //check for PDO extension
  334         if (!extension_loaded('pdo')) {
  335             /**
  336              * @see Zend_Db_Adapter_Exception
  337              */
  338             throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
  339         }
  340         switch ($mode) {
  341             case PDO::FETCH_LAZY:
  342             case PDO::FETCH_ASSOC:
  343             case PDO::FETCH_NUM:
  344             case PDO::FETCH_BOTH:
  345             case PDO::FETCH_NAMED:
  346             case PDO::FETCH_OBJ:
  347                 $this->_fetchMode = $mode;
  348                 break;
  349             default:
  350                 /**
  351                  * @see Zend_Db_Adapter_Exception
  352                  */
  353                 throw new Zend_Db_Adapter_Exception("Invalid fetch mode '$mode' specified");
  354                 break;
  355         }
  356     }
  357 
  358     /**
  359      * Check if the adapter supports real SQL parameters.
  360      *
  361      * @param string $type 'positional' or 'named'
  362      * @return bool
  363      */
  364     public function supportsParameters($type)
  365     {
  366         switch ($type) {
  367             case 'positional':
  368             case 'named':
  369             default:
  370                 return true;
  371         }
  372     }
  373 
  374     /**
  375      * Retrieve server version in PHP style
  376      *
  377      * @return string
  378      */
  379     public function getServerVersion()
  380     {
  381         $this->_connect();
  382         try {
  383             $version = $this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
  384         } catch (PDOException $e) {
  385             // In case of the driver doesn't support getting attributes
  386             return null;
  387         }
  388         $matches = null;
  389         if (preg_match('/((?:[0-9]{1,2}\.){1,3}[0-9]{1,2})/', $version, $matches)) {
  390             return $matches[1];
  391         } else {
  392             return null;
  393         }
  394     }
  395 }