PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/Zend/Db.php

https://github.com/testruby/Tine-2.0-Open-Source-Groupware-and-CRM
PHP | 276 lines | 124 code | 20 blank | 132 comment | 13 complexity | 598d27701d91506cb94e153a86217093 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-2009 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 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  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-2009 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. * is case-insensitive.
  164. *
  165. * First argument may alternatively be an object of type Zend_Config.
  166. * The adapter class base name is read from the 'adapter' property.
  167. * The adapter config parameters are read from the 'params' property.
  168. *
  169. * Second argument is optional and may be an associative array of key-value
  170. * pairs. This is used as the argument to the adapter constructor.
  171. *
  172. * If the first argument is of type Zend_Config, it is assumed to contain
  173. * all parameters, and the second argument is ignored.
  174. *
  175. * @param mixed $adapter String name of base adapter class, or Zend_Config object.
  176. * @param mixed $config OPTIONAL; an array or Zend_Config object with adapter parameters.
  177. * @return Zend_Db_Adapter_Abstract
  178. * @throws Zend_Db_Exception
  179. */
  180. public static function factory($adapter, $config = array())
  181. {
  182. if ($config instanceof Zend_Config) {
  183. $config = $config->toArray();
  184. }
  185. /*
  186. * Convert Zend_Config argument to plain string
  187. * adapter name and separate config object.
  188. */
  189. if ($adapter instanceof Zend_Config) {
  190. if (isset($adapter->params)) {
  191. $config = $adapter->params->toArray();
  192. }
  193. if (isset($adapter->adapter)) {
  194. $adapter = (string) $adapter->adapter;
  195. } else {
  196. $adapter = null;
  197. }
  198. }
  199. /*
  200. * Verify that adapter parameters are in an array.
  201. */
  202. if (!is_array($config)) {
  203. /**
  204. * @see Zend_Db_Exception
  205. */
  206. require_once 'Zend/Db/Exception.php';
  207. throw new Zend_Db_Exception('Adapter parameters must be in an array or a Zend_Config object');
  208. }
  209. /*
  210. * Verify that an adapter name has been specified.
  211. */
  212. if (!is_string($adapter) || empty($adapter)) {
  213. /**
  214. * @see Zend_Db_Exception
  215. */
  216. require_once 'Zend/Db/Exception.php';
  217. throw new Zend_Db_Exception('Adapter name must be specified in a string');
  218. }
  219. /*
  220. * Form full adapter class name
  221. */
  222. $adapterNamespace = 'Zend_Db_Adapter';
  223. if (isset($config['adapterNamespace'])) {
  224. if ($config['adapterNamespace'] != '') {
  225. $adapterNamespace = $config['adapterNamespace'];
  226. }
  227. unset($config['adapterNamespace']);
  228. }
  229. $adapterName = strtolower($adapterNamespace . '_' . $adapter);
  230. $adapterName = str_replace(' ', '_', ucwords(str_replace('_', ' ', $adapterName)));
  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::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 Zend_Db_Adapter_Abstract) {
  248. /**
  249. * @see Zend_Db_Exception
  250. */
  251. require_once 'Zend/Db/Exception.php';
  252. throw new Zend_Db_Exception("Adapter class '$adapterName' does not extend Zend_Db_Adapter_Abstract");
  253. }
  254. return $dbAdapter;
  255. }
  256. }