PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Db/Db.php

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