1: <?php
2:
3: /**
4: * Zend Framework
5: *
6: * LICENSE
7: *
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.
15: *
16: * @category Zend
17: * @package Zend_Db
18: * @copyright Copyright (c) 2005-2010 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 23405 2010-11-19 19:46:10Z bittarman $
21: */
22:
23:
24: /**
25: * Class for connecting to SQL databases and performing common operations.
26: *
27: * @category Zend
28: * @package Zend_Db
29: * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
30: * @license http://framework.zend.com/license/new-bsd New BSD License
31: */
32: class Zend_Db
33: {
34:
35: /**
36: * Use the PROFILER constant in the config of a Zend_Db_Adapter.
37: */
38: const PROFILER = 'profiler';
39:
40: /**
41: * Use the CASE_FOLDING constant in the config of a Zend_Db_Adapter.
42: */
43: const CASE_FOLDING = 'caseFolding';
44:
45: /**
46: * Use the FETCH_MODE constant in the config of a Zend_Db_Adapter.
47: */
48: const FETCH_MODE = 'fetchMode';
49:
50: /**
51: * Use the AUTO_QUOTE_IDENTIFIERS constant in the config of a Zend_Db_Adapter.
52: */
53: const AUTO_QUOTE_IDENTIFIERS = 'autoQuoteIdentifiers';
54:
55: /**
56: * Use the ALLOW_SERIALIZATION constant in the config of a Zend_Db_Adapter.
57: */
58: const ALLOW_SERIALIZATION = 'allowSerialization';
59:
60: /**
61: * Use the AUTO_RECONNECT_ON_UNSERIALIZE constant in the config of a Zend_Db_Adapter.
62: */
63: const AUTO_RECONNECT_ON_UNSERIALIZE = 'autoReconnectOnUnserialize';
64:
65: /**
66: * Use the INT_TYPE, BIGINT_TYPE, and FLOAT_TYPE with the quote() method.
67: */
68: const INT_TYPE = 0;
69: const BIGINT_TYPE = 1;
70: const FLOAT_TYPE = 2;
71:
72: /**
73: * PDO constant values discovered by this script result:
74: *
75: * $list = array(
76: * 'PARAM_BOOL', 'PARAM_NULL', 'PARAM_INT', 'PARAM_STR', 'PARAM_LOB',
77: * 'PARAM_STMT', 'PARAM_INPUT_OUTPUT', 'FETCH_LAZY', 'FETCH_ASSOC',
78: * 'FETCH_NUM', 'FETCH_BOTH', 'FETCH_OBJ', 'FETCH_BOUND',
79: * 'FETCH_COLUMN', 'FETCH_CLASS', 'FETCH_INTO', 'FETCH_FUNC',
80: * 'FETCH_GROUP', 'FETCH_UNIQUE', 'FETCH_CLASSTYPE', 'FETCH_SERIALIZE',
81: * 'FETCH_NAMED', 'ATTR_AUTOCOMMIT', 'ATTR_PREFETCH', 'ATTR_TIMEOUT',
82: * 'ATTR_ERRMODE', 'ATTR_SERVER_VERSION', 'ATTR_CLIENT_VERSION',
83: * 'ATTR_SERVER_INFO', 'ATTR_CONNECTION_STATUS', 'ATTR_CASE',
84: * 'ATTR_CURSOR_NAME', 'ATTR_CURSOR', 'ATTR_ORACLE_NULLS',
85: * 'ATTR_PERSISTENT', 'ATTR_STATEMENT_CLASS', 'ATTR_FETCH_TABLE_NAMES',
86: * 'ATTR_FETCH_CATALOG_NAMES', 'ATTR_DRIVER_NAME',
87: * 'ATTR_STRINGIFY_FETCHES', 'ATTR_MAX_COLUMN_LEN', 'ERRMODE_SILENT',
88: * 'ERRMODE_WARNING', 'ERRMODE_EXCEPTION', 'CASE_NATURAL',
89: * 'CASE_LOWER', 'CASE_UPPER', 'NULL_NATURAL', 'NULL_EMPTY_STRING',
90: * 'NULL_TO_STRING', 'ERR_NONE', 'FETCH_ORI_NEXT',
91: * 'FETCH_ORI_PRIOR', 'FETCH_ORI_FIRST', 'FETCH_ORI_LAST',
92: * 'FETCH_ORI_ABS', 'FETCH_ORI_REL', 'CURSOR_FWDONLY', 'CURSOR_SCROLL',
93: * );
94: *
95: * $const = array();
96: * foreach ($list as $name) {
97: * $const[$name] = constant("PDO::$name");
98: * }
99: * var_export($const);
100: */
101: const ATTR_AUTOCOMMIT = 0;
102: const ATTR_CASE = 8;
103: const ATTR_CLIENT_VERSION = 5;
104: const ATTR_CONNECTION_STATUS = 7;
105: const ATTR_CURSOR = 10;
106: const ATTR_CURSOR_NAME = 9;
107: const ATTR_DRIVER_NAME = 16;
108: const ATTR_ERRMODE = 3;
109: const ATTR_FETCH_CATALOG_NAMES = 15;
110: const ATTR_FETCH_TABLE_NAMES = 14;
111: const ATTR_MAX_COLUMN_LEN = 18;
112: const ATTR_ORACLE_NULLS = 11;
113: const ATTR_PERSISTENT = 12;
114: const ATTR_PREFETCH = 1;
115: const ATTR_SERVER_INFO = 6;
116: const ATTR_SERVER_VERSION = 4;
117: const ATTR_STATEMENT_CLASS = 13;
118: const ATTR_STRINGIFY_FETCHES = 17;
119: const ATTR_TIMEOUT = 2;
120: const CASE_LOWER = 2;
121: const CASE_NATURAL = 0;
122: const CASE_UPPER = 1;
123: const CURSOR_FWDONLY = 0;
124: const CURSOR_SCROLL = 1;
125: const ERR_NONE = '00000';
126: const ERRMODE_EXCEPTION = 2;
127: const ERRMODE_SILENT = 0;
128: const ERRMODE_WARNING = 1;
129: const FETCH_ASSOC = 2;
130: const FETCH_BOTH = 4;
131: const FETCH_BOUND = 6;
132: const FETCH_CLASS = 8;
133: const FETCH_CLASSTYPE = 262144;
134: const FETCH_COLUMN = 7;
135: const FETCH_FUNC = 10;
136: const FETCH_GROUP = 65536;
137: const FETCH_INTO = 9;
138: const FETCH_LAZY = 1;
139: const FETCH_NAMED = 11;
140: const FETCH_NUM = 3;
141: const FETCH_OBJ = 5;
142: const FETCH_ORI_ABS = 4;
143: const FETCH_ORI_FIRST = 2;
144: const FETCH_ORI_LAST = 3;
145: const FETCH_ORI_NEXT = 0;
146: const FETCH_ORI_PRIOR = 1;
147: const FETCH_ORI_REL = 5;
148: const FETCH_SERIALIZE = 524288;
149: const FETCH_UNIQUE = 196608;
150: const NULL_EMPTY_STRING = 1;
151: const NULL_NATURAL = 0;
152: const NULL_TO_STRING = NULL;
153: const PARAM_BOOL = 5;
154: const PARAM_INPUT_OUTPUT = -2147483648;
155: const PARAM_INT = 1;
156: const PARAM_LOB = 3;
157: const PARAM_NULL = 0;
158: const PARAM_STMT = 4;
159: const PARAM_STR = 2;
160:
161: /**
162: * Factory for Zend_Db_Adapter_Abstract classes.
163: *
164: * First argument may be a string containing the base of the adapter class
165: * name, e.g. 'Mysqli' corresponds to class Zend_Db_Adapter_Mysqli. This
166: * name is currently case-insensitive, but is not ideal to rely on this behavior.
167: * If your class is named 'My_Company_Pdo_Mysql', where 'My_Company' is the namespace
168: * and 'Pdo_Mysql' is the adapter name, it is best to use the name exactly as it
169: * is defined in the class. This will ensure proper use of the factory API.
170: *
171: * First argument may alternatively be an object of type Zend_Config.
172: * The adapter class base name is read from the 'adapter' property.
173: * The adapter config parameters are read from the 'params' property.
174: *
175: * Second argument is optional and may be an associative array of key-value
176: * pairs. This is used as the argument to the adapter constructor.
177: *
178: * If the first argument is of type Zend_Config, it is assumed to contain
179: * all parameters, and the second argument is ignored.
180: *
181: * @param mixed $adapter String name of base adapter class, or Zend_Config object.
182: * @param mixed $config OPTIONAL; an array or Zend_Config object with adapter parameters.
183: * @return Zend_Db_Adapter_Abstract
184: * @throws Zend_Db_Exception
185: */
186: public static function factory($adapter, $config = array())
187: {
188: if ($config instanceof Zend_Config) {
189: $config = $config->toArray();
190: }
191:
192: /*
193: * Convert Zend_Config argument to plain string
194: * adapter name and separate config object.
195: */
196: if ($adapter instanceof Zend_Config || $adapter instanceof Config_Config) {
197: if (isset($adapter->params)) {
198: $config = $adapter->params->toArray();
199: }
200: if (isset($adapter->adapter)) {
201: $adapter = (string) $adapter->adapter;
202: } else {
203: $adapter = null;
204: }
205: }
206:
207: /*
208: * Verify that adapter parameters are in an array.
209: */
210: if (!is_array($config)) {
211: /**
212: * @see Zend_Db_Exception
213: */
214: require_once 'Zend/Db/Exception.php';
215: throw new Zend_Db_Exception('Adapter parameters must be in an array or a Zend_Config object');
216: }
217:
218: /*
219: * Verify that an adapter name has been specified.
220: */
221: if (!is_string($adapter) || empty($adapter)) {
222: /**
223: * @see Zend_Db_Exception
224: */
225: require_once 'Zend/Db/Exception.php';
226: throw new Zend_Db_Exception('Adapter name must be specified in a string');
227: }
228:
229: /*
230: * Form full adapter class name
231: */
232: $adapterNamespace = 'Zend_Db_Adapter';
233: if (isset($config['adapterNamespace'])) {
234: if ($config['adapterNamespace'] != '') {
235: $adapterNamespace = $config['adapterNamespace'];
236: }
237: unset($config['adapterNamespace']);
238: }
239:
240: // Adapter no longer normalized- see http://framework.zend.com/issues/browse/ZF-5606
241: $adapterName = $adapterNamespace . '_';
242: $adapterName .= str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($adapter))));
243:
244: /*
245: * Load the adapter class. This throws an exception
246: * if the specified class cannot be loaded.
247: */
248: if (!class_exists($adapterName)) {
249: require_once 'Zend/Loader.php';
250: Zend_Loader::loadClass($adapterName);
251: }
252:
253: /*
254: * Create an instance of the adapter class.
255: * Pass the config to the adapter class constructor.
256: */
257: $dbAdapter = new $adapterName($config);
258:
259: /*
260: * Verify that the object created is a descendent of the abstract adapter type.
261: */
262: if (! $dbAdapter instanceof Zend_Db_Adapter_Abstract) {
263: /**
264: * @see Zend_Db_Exception
265: */
266: require_once 'Zend/Db/Exception.php';
267: throw new Zend_Db_Exception("Adapter class '$adapterName' does not extend Zend_Db_Adapter_Abstract");
268: }
269:
270: return $dbAdapter;
271: }
272:
273: }
274: