8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Db.php 16203 2009-06-21 18:56:17Z thomas $
25 * Class for connecting to SQL databases and performing common operations.
29 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
30 * @license http://framework.zend.com/license/new-bsd New BSD License
36 * Use the PROFILER constant in the config of a Zend_Db_Adapter.
38 const PROFILER
= 'profiler';
41 * Use the CASE_FOLDING constant in the config of a Zend_Db_Adapter.
43 const CASE_FOLDING
= 'caseFolding';
46 * Use the AUTO_QUOTE_IDENTIFIERS constant in the config of a Zend_Db_Adapter.
48 const AUTO_QUOTE_IDENTIFIERS
= 'autoQuoteIdentifiers';
51 * Use the ALLOW_SERIALIZATION constant in the config of a Zend_Db_Adapter.
53 const ALLOW_SERIALIZATION
= 'allowSerialization';
56 * Use the AUTO_RECONNECT_ON_UNSERIALIZE constant in the config of a Zend_Db_Adapter.
58 const AUTO_RECONNECT_ON_UNSERIALIZE
= 'autoReconnectOnUnserialize';
61 * Use the INT_TYPE, BIGINT_TYPE, and FLOAT_TYPE with the quote() method.
64 const BIGINT_TYPE
= 1;
68 * PDO constant values discovered by this script result:
71 * 'PARAM_BOOL', 'PARAM_NULL', 'PARAM_INT', 'PARAM_STR', 'PARAM_LOB',
72 * 'PARAM_STMT', 'PARAM_INPUT_OUTPUT', 'FETCH_LAZY', 'FETCH_ASSOC',
73 * 'FETCH_NUM', 'FETCH_BOTH', 'FETCH_OBJ', 'FETCH_BOUND',
74 * 'FETCH_COLUMN', 'FETCH_CLASS', 'FETCH_INTO', 'FETCH_FUNC',
75 * 'FETCH_GROUP', 'FETCH_UNIQUE', 'FETCH_CLASSTYPE', 'FETCH_SERIALIZE',
76 * 'FETCH_NAMED', 'ATTR_AUTOCOMMIT', 'ATTR_PREFETCH', 'ATTR_TIMEOUT',
77 * 'ATTR_ERRMODE', 'ATTR_SERVER_VERSION', 'ATTR_CLIENT_VERSION',
78 * 'ATTR_SERVER_INFO', 'ATTR_CONNECTION_STATUS', 'ATTR_CASE',
79 * 'ATTR_CURSOR_NAME', 'ATTR_CURSOR', 'ATTR_ORACLE_NULLS',
80 * 'ATTR_PERSISTENT', 'ATTR_STATEMENT_CLASS', 'ATTR_FETCH_TABLE_NAMES',
81 * 'ATTR_FETCH_CATALOG_NAMES', 'ATTR_DRIVER_NAME',
82 * 'ATTR_STRINGIFY_FETCHES', 'ATTR_MAX_COLUMN_LEN', 'ERRMODE_SILENT',
83 * 'ERRMODE_WARNING', 'ERRMODE_EXCEPTION', 'CASE_NATURAL',
84 * 'CASE_LOWER', 'CASE_UPPER', 'NULL_NATURAL', 'NULL_EMPTY_STRING',
85 * 'NULL_TO_STRING', 'ERR_NONE', 'FETCH_ORI_NEXT',
86 * 'FETCH_ORI_PRIOR', 'FETCH_ORI_FIRST', 'FETCH_ORI_LAST',
87 * 'FETCH_ORI_ABS', 'FETCH_ORI_REL', 'CURSOR_FWDONLY', 'CURSOR_SCROLL',
88 * 'ERR_CANT_MAP', 'ERR_SYNTAX', 'ERR_CONSTRAINT', 'ERR_NOT_FOUND',
89 * 'ERR_ALREADY_EXISTS', 'ERR_NOT_IMPLEMENTED', 'ERR_MISMATCH',
90 * 'ERR_TRUNCATED', 'ERR_DISCONNECTED', 'ERR_NO_PERM',
94 * foreach ($list as $name) {
95 * $const[$name] = constant("PDO::$name");
99 const ATTR_AUTOCOMMIT
= 0;
101 const ATTR_CLIENT_VERSION
= 5;
102 const ATTR_CONNECTION_STATUS
= 7;
103 const ATTR_CURSOR
= 10;
104 const ATTR_CURSOR_NAME
= 9;
105 const ATTR_DRIVER_NAME
= 16;
106 const ATTR_ERRMODE
= 3;
107 const ATTR_FETCH_CATALOG_NAMES
= 15;
108 const ATTR_FETCH_TABLE_NAMES
= 14;
109 const ATTR_MAX_COLUMN_LEN
= 18;
110 const ATTR_ORACLE_NULLS
= 11;
111 const ATTR_PERSISTENT
= 12;
112 const ATTR_PREFETCH
= 1;
113 const ATTR_SERVER_INFO
= 6;
114 const ATTR_SERVER_VERSION
= 4;
115 const ATTR_STATEMENT_CLASS
= 13;
116 const ATTR_STRINGIFY_FETCHES
= 17;
117 const ATTR_TIMEOUT
= 2;
118 const CASE_LOWER
= 2;
119 const CASE_NATURAL
= 0;
120 const CASE_UPPER
= 1;
121 const CURSOR_FWDONLY
= 0;
122 const CURSOR_SCROLL
= 1;
123 const ERR_ALREADY_EXISTS
= NULL;
124 const ERR_CANT_MAP
= NULL;
125 const ERR_CONSTRAINT
= NULL;
126 const ERR_DISCONNECTED
= NULL;
127 const ERR_MISMATCH
= NULL;
128 const ERR_NO_PERM
= NULL;
129 const ERR_NONE
= '00000';
130 const ERR_NOT_FOUND
= NULL;
131 const ERR_NOT_IMPLEMENTED
= NULL;
132 const ERR_SYNTAX
= NULL;
133 const ERR_TRUNCATED
= NULL;
134 const ERRMODE_EXCEPTION
= 2;
135 const ERRMODE_SILENT
= 0;
136 const ERRMODE_WARNING
= 1;
137 const FETCH_ASSOC
= 2;
138 const FETCH_BOTH
= 4;
139 const FETCH_BOUND
= 6;
140 const FETCH_CLASS
= 8;
141 const FETCH_CLASSTYPE
= 262144;
142 const FETCH_COLUMN
= 7;
143 const FETCH_FUNC
= 10;
144 const FETCH_GROUP
= 65536;
145 const FETCH_INTO
= 9;
146 const FETCH_LAZY
= 1;
147 const FETCH_NAMED
= 11;
150 const FETCH_ORI_ABS
= 4;
151 const FETCH_ORI_FIRST
= 2;
152 const FETCH_ORI_LAST
= 3;
153 const FETCH_ORI_NEXT
= 0;
154 const FETCH_ORI_PRIOR
= 1;
155 const FETCH_ORI_REL
= 5;
156 const FETCH_SERIALIZE
= 524288;
157 const FETCH_UNIQUE
= 196608;
158 const NULL_EMPTY_STRING
= 1;
159 const NULL_NATURAL
= 0;
160 const NULL_TO_STRING
= NULL;
161 const PARAM_BOOL
= 5;
162 const PARAM_INPUT_OUTPUT
= -2147483648;
165 const PARAM_NULL
= 0;
166 const PARAM_STMT
= 4;
170 * Factory for Zend_Db_Adapter_Abstract classes.
172 * First argument may be a string containing the base of the adapter class
173 * name, e.g. 'Mysqli' corresponds to class Zend_Db_Adapter_Mysqli. This
174 * is case-insensitive.
176 * First argument may alternatively be an object of type Zend_Config.
177 * The adapter class base name is read from the 'adapter' property.
178 * The adapter config parameters are read from the 'params' property.
180 * Second argument is optional and may be an associative array of key-value
181 * pairs. This is used as the argument to the adapter constructor.
183 * If the first argument is of type Zend_Config, it is assumed to contain
184 * all parameters, and the second argument is ignored.
186 * @param mixed $adapter String name of base adapter class, or Zend_Config object.
187 * @param mixed $config OPTIONAL; an array or Zend_Config object with adapter parameters.
188 * @return Zend_Db_Adapter_Abstract
189 * @throws Zend_Db_Exception
191 public static function factory($adapter, $config = array())
193 if ($config instanceof Zend_Config
) {
194 $config = $config->toArray();
198 * Convert Zend_Config argument to plain string
199 * adapter name and separate config object.
201 if ($adapter instanceof Zend_Config
) {
202 if (isset($adapter->params
)) {
203 $config = $adapter->params
->toArray();
205 if (isset($adapter->adapter
)) {
206 $adapter = (string) $adapter->adapter
;
213 * Verify that adapter parameters are in an array.
215 if (!is_array($config)) {
217 * @see Zend_Db_Exception
219 require_once 'Zend/Db/Exception.php';
220 throw new Zend_Db_Exception('Adapter parameters must be in an array or a Zend_Config object');
224 * Verify that an adapter name has been specified.
226 if (!is_string($adapter) ||
empty($adapter)) {
228 * @see Zend_Db_Exception
230 require_once 'Zend/Db/Exception.php';
231 throw new Zend_Db_Exception('Adapter name must be specified in a string');
235 * Form full adapter class name
237 $adapterNamespace = 'Zend_Db_Adapter';
238 if (isset($config['adapterNamespace'])) {
239 if ($config['adapterNamespace'] != '') {
240 $adapterNamespace = $config['adapterNamespace'];
242 unset($config['adapterNamespace']);
244 $adapterName = strtolower($adapterNamespace . '_' . $adapter);
245 $adapterName = str_replace(' ', '_', ucwords(str_replace('_', ' ', $adapterName)));
248 * Load the adapter class. This throws an exception
249 * if the specified class cannot be loaded.
251 if (!class_exists($adapterName)) {
252 require_once 'Zend/Loader.php';
253 Zend_Loader
::loadClass($adapterName);
257 * Create an instance of the adapter class.
258 * Pass the config to the adapter class constructor.
260 $dbAdapter = new $adapterName($config);
263 * Verify that the object created is a descendent of the abstract adapter type.
265 if (! $dbAdapter instanceof Zend_Db_Adapter_Abstract
) {
267 * @see Zend_Db_Exception
269 require_once 'Zend/Db/Exception.php';
270 throw new Zend_Db_Exception("Adapter class '$adapterName' does not extend Zend_Db_Adapter_Abstract");