PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Db.php

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