Db.php 9.7 KB

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