File indexing completed on 2024-04-28 06:00:01

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_Db
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 
0024 /**
0025  * Class for connecting to SQL databases and performing common operations.
0026  *
0027  * @category   Zend
0028  * @package    Zend_Db
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  */
0032 class Zend_Db
0033 {
0034 
0035     /**
0036      * Use the PROFILER constant in the config of a Zend_Db_Adapter.
0037      */
0038     const PROFILER = 'profiler';
0039 
0040     /**
0041      * Use the CASE_FOLDING constant in the config of a Zend_Db_Adapter.
0042      */
0043     const CASE_FOLDING = 'caseFolding';
0044 
0045     /**
0046      * Use the FETCH_MODE constant in the config of a Zend_Db_Adapter.
0047      */
0048     const FETCH_MODE = 'fetchMode';
0049 
0050     /**
0051      * Use the AUTO_QUOTE_IDENTIFIERS constant in the config of a Zend_Db_Adapter.
0052      */
0053     const AUTO_QUOTE_IDENTIFIERS = 'autoQuoteIdentifiers';
0054 
0055     /**
0056      * Use the ALLOW_SERIALIZATION constant in the config of a Zend_Db_Adapter.
0057      */
0058     const ALLOW_SERIALIZATION = 'allowSerialization';
0059 
0060     /**
0061      * Use the AUTO_RECONNECT_ON_UNSERIALIZE constant in the config of a Zend_Db_Adapter.
0062      */
0063     const AUTO_RECONNECT_ON_UNSERIALIZE = 'autoReconnectOnUnserialize';
0064 
0065     /**
0066      * Use the INT_TYPE, BIGINT_TYPE, and FLOAT_TYPE with the quote() method.
0067      */
0068     const INT_TYPE    = 0;
0069     const BIGINT_TYPE = 1;
0070     const FLOAT_TYPE  = 2;
0071 
0072     /**
0073      * PDO constant values discovered by this script result:
0074      *
0075      * $list = array(
0076      *    'PARAM_BOOL', 'PARAM_NULL', 'PARAM_INT', 'PARAM_STR', 'PARAM_LOB',
0077      *    'PARAM_STMT', 'PARAM_INPUT_OUTPUT', 'FETCH_LAZY', 'FETCH_ASSOC',
0078      *    'FETCH_NUM', 'FETCH_BOTH', 'FETCH_OBJ', 'FETCH_BOUND',
0079      *    'FETCH_COLUMN', 'FETCH_CLASS', 'FETCH_INTO', 'FETCH_FUNC',
0080      *    'FETCH_GROUP', 'FETCH_UNIQUE', 'FETCH_CLASSTYPE', 'FETCH_SERIALIZE',
0081      *    'FETCH_NAMED', 'ATTR_AUTOCOMMIT', 'ATTR_PREFETCH', 'ATTR_TIMEOUT',
0082      *    'ATTR_ERRMODE', 'ATTR_SERVER_VERSION', 'ATTR_CLIENT_VERSION',
0083      *    'ATTR_SERVER_INFO', 'ATTR_CONNECTION_STATUS', 'ATTR_CASE',
0084      *    'ATTR_CURSOR_NAME', 'ATTR_CURSOR', 'ATTR_ORACLE_NULLS',
0085      *    'ATTR_PERSISTENT', 'ATTR_STATEMENT_CLASS', 'ATTR_FETCH_TABLE_NAMES',
0086      *    'ATTR_FETCH_CATALOG_NAMES', 'ATTR_DRIVER_NAME',
0087      *    'ATTR_STRINGIFY_FETCHES', 'ATTR_MAX_COLUMN_LEN', 'ERRMODE_SILENT',
0088      *    'ERRMODE_WARNING', 'ERRMODE_EXCEPTION', 'CASE_NATURAL',
0089      *    'CASE_LOWER', 'CASE_UPPER', 'NULL_NATURAL', 'NULL_EMPTY_STRING',
0090      *    'NULL_TO_STRING', 'ERR_NONE', 'FETCH_ORI_NEXT',
0091      *    'FETCH_ORI_PRIOR', 'FETCH_ORI_FIRST', 'FETCH_ORI_LAST',
0092      *    'FETCH_ORI_ABS', 'FETCH_ORI_REL', 'CURSOR_FWDONLY', 'CURSOR_SCROLL',
0093      *    'ERR_CANT_MAP', 'ERR_SYNTAX', 'ERR_CONSTRAINT', 'ERR_NOT_FOUND',
0094      *    'ERR_ALREADY_EXISTS', 'ERR_NOT_IMPLEMENTED', 'ERR_MISMATCH',
0095      *    'ERR_TRUNCATED', 'ERR_DISCONNECTED', 'ERR_NO_PERM',
0096      * );
0097      *
0098      * $const = array();
0099      * foreach ($list as $name) {
0100      *    $const[$name] = constant("PDO::$name");
0101      * }
0102      * var_export($const);
0103      */
0104     const ATTR_AUTOCOMMIT = 0;
0105     const ATTR_CASE = 8;
0106     const ATTR_CLIENT_VERSION = 5;
0107     const ATTR_CONNECTION_STATUS = 7;
0108     const ATTR_CURSOR = 10;
0109     const ATTR_CURSOR_NAME = 9;
0110     const ATTR_DRIVER_NAME = 16;
0111     const ATTR_ERRMODE = 3;
0112     const ATTR_FETCH_CATALOG_NAMES = 15;
0113     const ATTR_FETCH_TABLE_NAMES = 14;
0114     const ATTR_MAX_COLUMN_LEN = 18;
0115     const ATTR_ORACLE_NULLS = 11;
0116     const ATTR_PERSISTENT = 12;
0117     const ATTR_PREFETCH = 1;
0118     const ATTR_SERVER_INFO = 6;
0119     const ATTR_SERVER_VERSION = 4;
0120     const ATTR_STATEMENT_CLASS = 13;
0121     const ATTR_STRINGIFY_FETCHES = 17;
0122     const ATTR_TIMEOUT = 2;
0123     const CASE_LOWER = 2;
0124     const CASE_NATURAL = 0;
0125     const CASE_UPPER = 1;
0126     const CURSOR_FWDONLY = 0;
0127     const CURSOR_SCROLL = 1;
0128     const ERR_ALREADY_EXISTS = NULL;
0129     const ERR_CANT_MAP = NULL;
0130     const ERR_CONSTRAINT = NULL;
0131     const ERR_DISCONNECTED = NULL;
0132     const ERR_MISMATCH = NULL;
0133     const ERR_NO_PERM = NULL;
0134     const ERR_NONE = '00000';
0135     const ERR_NOT_FOUND = NULL;
0136     const ERR_NOT_IMPLEMENTED = NULL;
0137     const ERR_SYNTAX = NULL;
0138     const ERR_TRUNCATED = NULL;
0139     const ERRMODE_EXCEPTION = 2;
0140     const ERRMODE_SILENT = 0;
0141     const ERRMODE_WARNING = 1;
0142     const FETCH_ASSOC = 2;
0143     const FETCH_BOTH = 4;
0144     const FETCH_BOUND = 6;
0145     const FETCH_CLASS = 8;
0146     const FETCH_CLASSTYPE = 262144;
0147     const FETCH_COLUMN = 7;
0148     const FETCH_FUNC = 10;
0149     const FETCH_GROUP = 65536;
0150     const FETCH_INTO = 9;
0151     const FETCH_LAZY = 1;
0152     const FETCH_NAMED = 11;
0153     const FETCH_NUM = 3;
0154     const FETCH_OBJ = 5;
0155     const FETCH_ORI_ABS = 4;
0156     const FETCH_ORI_FIRST = 2;
0157     const FETCH_ORI_LAST = 3;
0158     const FETCH_ORI_NEXT = 0;
0159     const FETCH_ORI_PRIOR = 1;
0160     const FETCH_ORI_REL = 5;
0161     const FETCH_SERIALIZE = 524288;
0162     const FETCH_UNIQUE = 196608;
0163     const NULL_EMPTY_STRING = 1;
0164     const NULL_NATURAL = 0;
0165     const NULL_TO_STRING = NULL;
0166     const PARAM_BOOL = 5;
0167     const PARAM_INPUT_OUTPUT = -2147483648;
0168     const PARAM_INT = 1;
0169     const PARAM_LOB = 3;
0170     const PARAM_NULL = 0;
0171     const PARAM_STMT = 4;
0172     const PARAM_STR = 2;
0173 
0174     /**
0175      * Factory for Zend_Db_Adapter_Abstract classes.
0176      *
0177      * First argument may be a string containing the base of the adapter class
0178      * name, e.g. 'Mysqli' corresponds to class Zend_Db_Adapter_Mysqli.  This
0179      * name is currently case-insensitive, but is not ideal to rely on this behavior.
0180      * If your class is named 'My_Company_Pdo_Mysql', where 'My_Company' is the namespace
0181      * and 'Pdo_Mysql' is the adapter name, it is best to use the name exactly as it
0182      * is defined in the class.  This will ensure proper use of the factory API.
0183      *
0184      * First argument may alternatively be an object of type Zend_Config.
0185      * The adapter class base name is read from the 'adapter' property.
0186      * The adapter config parameters are read from the 'params' property.
0187      *
0188      * Second argument is optional and may be an associative array of key-value
0189      * pairs.  This is used as the argument to the adapter constructor.
0190      *
0191      * If the first argument is of type Zend_Config, it is assumed to contain
0192      * all parameters, and the second argument is ignored.
0193      *
0194      * @param  mixed $adapter String name of base adapter class, or Zend_Config object.
0195      * @param  mixed $config  OPTIONAL; an array or Zend_Config object with adapter parameters.
0196      * @return Zend_Db_Adapter_Abstract
0197      * @throws Zend_Db_Exception
0198      */
0199     public static function factory($adapter, $config = array())
0200     {
0201         if ($config instanceof Zend_Config) {
0202             $config = $config->toArray();
0203         }
0204 
0205         /*
0206          * Convert Zend_Config argument to plain string
0207          * adapter name and separate config object.
0208          */
0209         if ($adapter instanceof Zend_Config) {
0210             if (isset($adapter->params)) {
0211                 $config = $adapter->params->toArray();
0212             }
0213             if (isset($adapter->adapter)) {
0214                 $adapter = (string) $adapter->adapter;
0215             } else {
0216                 $adapter = null;
0217             }
0218         }
0219 
0220         /*
0221          * Verify that adapter parameters are in an array.
0222          */
0223         if (!is_array($config)) {
0224             /**
0225              * @see Zend_Db_Exception
0226              */
0227             // require_once 'Zend/Db/Exception.php';
0228             throw new Zend_Db_Exception('Adapter parameters must be in an array or a Zend_Config object');
0229         }
0230 
0231         /*
0232          * Verify that an adapter name has been specified.
0233          */
0234         if (!is_string($adapter) || empty($adapter)) {
0235             /**
0236              * @see Zend_Db_Exception
0237              */
0238             // require_once 'Zend/Db/Exception.php';
0239             throw new Zend_Db_Exception('Adapter name must be specified in a string');
0240         }
0241 
0242         /*
0243          * Form full adapter class name
0244          */
0245         $adapterNamespace = 'Zend_Db_Adapter';
0246         if (isset($config['adapterNamespace'])) {
0247             if ($config['adapterNamespace'] != '') {
0248                 $adapterNamespace = $config['adapterNamespace'];
0249             }
0250             unset($config['adapterNamespace']);
0251         }
0252 
0253         // Adapter no longer normalized- see http://framework.zend.com/issues/browse/ZF-5606
0254         $adapterName = $adapterNamespace . '_';
0255         $adapterName .= str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($adapter))));
0256 
0257         /*
0258          * Load the adapter class.  This throws an exception
0259          * if the specified class cannot be loaded.
0260          */
0261         if (!class_exists($adapterName)) {
0262             // require_once 'Zend/Loader.php';
0263             Zend_Loader::loadClass($adapterName);
0264         }
0265 
0266         /*
0267          * Create an instance of the adapter class.
0268          * Pass the config to the adapter class constructor.
0269          */
0270         $dbAdapter = new $adapterName($config);
0271 
0272         /*
0273          * Verify that the object created is a descendent of the abstract adapter type.
0274          */
0275         if (! $dbAdapter instanceof Zend_Db_Adapter_Abstract) {
0276             /**
0277              * @see Zend_Db_Exception
0278              */
0279             // require_once 'Zend/Db/Exception.php';
0280             throw new Zend_Db_Exception("Adapter class '$adapterName' does not extend Zend_Db_Adapter_Abstract");
0281         }
0282 
0283         return $dbAdapter;
0284     }
0285 
0286 }