Automatically exported from code.google.com/p/planningalerts
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

423 linhas
15 KiB

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
  6. // | Stig. S. Bakken, Lukas Smith |
  7. // | All rights reserved. |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
  10. // | API as well as database abstraction for PHP applications. |
  11. // | This LICENSE is in the BSD license style. |
  12. // | |
  13. // | Redistribution and use in source and binary forms, with or without |
  14. // | modification, are permitted provided that the following conditions |
  15. // | are met: |
  16. // | |
  17. // | Redistributions of source code must retain the above copyright |
  18. // | notice, this list of conditions and the following disclaimer. |
  19. // | |
  20. // | Redistributions in binary form must reproduce the above copyright |
  21. // | notice, this list of conditions and the following disclaimer in the |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // | |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission. |
  28. // | |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  40. // | POSSIBILITY OF SUCH DAMAGE. |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@pooteeweet.org> |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: Common.php,v 1.28 2006/08/15 11:24:51 lsmith Exp $
  46. //
  47. /**
  48. * @package MDB2
  49. * @category Database
  50. */
  51. /**
  52. * These are constants for the tableInfo-function
  53. * they are bitwised or'ed. so if there are more constants to be defined
  54. * in the future, adjust MDB2_TABLEINFO_FULL accordingly
  55. */
  56. define('MDB2_TABLEINFO_ORDER', 1);
  57. define('MDB2_TABLEINFO_ORDERTABLE', 2);
  58. define('MDB2_TABLEINFO_FULL', 3);
  59. /**
  60. * Base class for the schema reverse engineering module that is extended by each MDB2 driver
  61. *
  62. * @package MDB2
  63. * @category Database
  64. * @author Lukas Smith <smith@pooteeweet.org>
  65. */
  66. class MDB2_Driver_Reverse_Common extends MDB2_Module_Common
  67. {
  68. // }}}
  69. // {{{ getTableFieldDefinition()
  70. /**
  71. * Get the stucture of a field into an array
  72. *
  73. * @param string $table name of table that should be used in method
  74. * @param string $fields name of field that should be used in method
  75. * @return mixed data array on success, a MDB2 error on failure
  76. * @access public
  77. */
  78. function getTableFieldDefinition($table, $field)
  79. {
  80. $db =& $this->getDBInstance();
  81. if (PEAR::isError($db)) {
  82. return $db;
  83. }
  84. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  85. 'method not implemented', __FUNCTION__);
  86. }
  87. // }}}
  88. // {{{ getTableIndexDefinition()
  89. /**
  90. * Get the stucture of an index into an array
  91. *
  92. * @param string $table name of table that should be used in method
  93. * @param string $index name of index that should be used in method
  94. * @return mixed data array on success, a MDB2 error on failure
  95. * @access public
  96. */
  97. function getTableIndexDefinition($table, $index)
  98. {
  99. $db =& $this->getDBInstance();
  100. if (PEAR::isError($db)) {
  101. return $db;
  102. }
  103. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  104. 'method not implemented', __FUNCTION__);
  105. }
  106. // }}}
  107. // {{{ getTableConstraintDefinition()
  108. /**
  109. * Get the stucture of an constraints into an array
  110. *
  111. * @param string $table name of table that should be used in method
  112. * @param string $index name of index that should be used in method
  113. * @return mixed data array on success, a MDB2 error on failure
  114. * @access public
  115. */
  116. function getTableConstraintDefinition($table, $index)
  117. {
  118. $db =& $this->getDBInstance();
  119. if (PEAR::isError($db)) {
  120. return $db;
  121. }
  122. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  123. 'method not implemented', __FUNCTION__);
  124. }
  125. // }}}
  126. // {{{ getSequenceDefinition()
  127. /**
  128. * Get the stucture of a sequence into an array
  129. *
  130. * @param string $sequence name of sequence that should be used in method
  131. * @return mixed data array on success, a MDB2 error on failure
  132. * @access public
  133. */
  134. function getSequenceDefinition($sequence)
  135. {
  136. $db =& $this->getDBInstance();
  137. if (PEAR::isError($db)) {
  138. return $db;
  139. }
  140. $start = $db->currId($sequence);
  141. if (PEAR::isError($start)) {
  142. return $start;
  143. }
  144. if ($db->supports('current_id')) {
  145. $start++;
  146. } else {
  147. $db->warnings[] = 'database does not support getting current
  148. sequence value, the sequence value was incremented';
  149. }
  150. $definition = array();
  151. if ($start != 1) {
  152. $definition = array('start' => $start);
  153. }
  154. return $definition;
  155. }
  156. // }}}
  157. // {{{ getTriggerDefinition()
  158. /**
  159. * Get the stucture of an trigger into an array
  160. *
  161. * @param string $trigger name of trigger that should be used in method
  162. * @return mixed data array on success, a MDB2 error on failure
  163. * @access public
  164. */
  165. function getTriggerDefinition($trigger)
  166. {
  167. $db =& $this->getDBInstance();
  168. if (PEAR::isError($db)) {
  169. return $db;
  170. }
  171. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  172. 'method not implemented', __FUNCTION__);
  173. }
  174. // }}}
  175. // {{{ tableInfo()
  176. /**
  177. * Returns information about a table or a result set
  178. *
  179. * The format of the resulting array depends on which <var>$mode</var>
  180. * you select. The sample output below is based on this query:
  181. * <pre>
  182. * SELECT tblFoo.fldID, tblFoo.fldPhone, tblBar.fldId
  183. * FROM tblFoo
  184. * JOIN tblBar ON tblFoo.fldId = tblBar.fldId
  185. * </pre>
  186. *
  187. * <ul>
  188. * <li>
  189. *
  190. * <kbd>null</kbd> (default)
  191. * <pre>
  192. * [0] => Array (
  193. * [table] => tblFoo
  194. * [name] => fldId
  195. * [type] => int
  196. * [len] => 11
  197. * [flags] => primary_key not_null
  198. * )
  199. * [1] => Array (
  200. * [table] => tblFoo
  201. * [name] => fldPhone
  202. * [type] => string
  203. * [len] => 20
  204. * [flags] =>
  205. * )
  206. * [2] => Array (
  207. * [table] => tblBar
  208. * [name] => fldId
  209. * [type] => int
  210. * [len] => 11
  211. * [flags] => primary_key not_null
  212. * )
  213. * </pre>
  214. *
  215. * </li><li>
  216. *
  217. * <kbd>MDB2_TABLEINFO_ORDER</kbd>
  218. *
  219. * <p>In addition to the information found in the default output,
  220. * a notation of the number of columns is provided by the
  221. * <samp>num_fields</samp> element while the <samp>order</samp>
  222. * element provides an array with the column names as the keys and
  223. * their location index number (corresponding to the keys in the
  224. * the default output) as the values.</p>
  225. *
  226. * <p>If a result set has identical field names, the last one is
  227. * used.</p>
  228. *
  229. * <pre>
  230. * [num_fields] => 3
  231. * [order] => Array (
  232. * [fldId] => 2
  233. * [fldTrans] => 1
  234. * )
  235. * </pre>
  236. *
  237. * </li><li>
  238. *
  239. * <kbd>MDB2_TABLEINFO_ORDERTABLE</kbd>
  240. *
  241. * <p>Similar to <kbd>MDB2_TABLEINFO_ORDER</kbd> but adds more
  242. * dimensions to the array in which the table names are keys and
  243. * the field names are sub-keys. This is helpful for queries that
  244. * join tables which have identical field names.</p>
  245. *
  246. * <pre>
  247. * [num_fields] => 3
  248. * [ordertable] => Array (
  249. * [tblFoo] => Array (
  250. * [fldId] => 0
  251. * [fldPhone] => 1
  252. * )
  253. * [tblBar] => Array (
  254. * [fldId] => 2
  255. * )
  256. * )
  257. * </pre>
  258. *
  259. * </li>
  260. * </ul>
  261. *
  262. * The <samp>flags</samp> element contains a space separated list
  263. * of extra information about the field. This data is inconsistent
  264. * between DBMS's due to the way each DBMS works.
  265. * + <samp>primary_key</samp>
  266. * + <samp>unique_key</samp>
  267. * + <samp>multiple_key</samp>
  268. * + <samp>not_null</samp>
  269. *
  270. * Most DBMS's only provide the <samp>table</samp> and <samp>flags</samp>
  271. * elements if <var>$result</var> is a table name. The following DBMS's
  272. * provide full information from queries:
  273. * + fbsql
  274. * + mysql
  275. *
  276. * If the 'portability' option has <samp>MDB2_PORTABILITY_FIX_CASE</samp>
  277. * turned on, the names of tables and fields will be lower or upper cased.
  278. *
  279. * @param object|string $result MDB2_result object from a query or a
  280. * string containing the name of a table.
  281. * While this also accepts a query result
  282. * resource identifier, this behavior is
  283. * deprecated.
  284. * @param int $mode either unused or one of the tableInfo modes:
  285. * <kbd>MDB2_TABLEINFO_ORDERTABLE</kbd>,
  286. * <kbd>MDB2_TABLEINFO_ORDER</kbd> or
  287. * <kbd>MDB2_TABLEINFO_FULL</kbd> (which does both).
  288. * These are bitwise, so the first two can be
  289. * combined using <kbd>|</kbd>.
  290. *
  291. * @return array an associative array with the information requested.
  292. * A MDB2_Error object on failure.
  293. *
  294. * @see MDB2_Driver_Common::setOption()
  295. */
  296. function tableInfo($result, $mode = null)
  297. {
  298. $db =& $this->getDBInstance();
  299. if (PEAR::isError($db)) {
  300. return $db;
  301. }
  302. if (!is_string($result)) {
  303. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  304. 'method not implemented', __FUNCTION__);
  305. }
  306. $db->loadModule('Manager', null, true);
  307. $fields = $db->manager->listTableFields($result);
  308. if (PEAR::isError($fields)) {
  309. return $fields;
  310. }
  311. $flags = array();
  312. $idxname_format = $db->getOption('idxname_format');
  313. $db->setOption('idxname_format', '%s');
  314. $indexes = $db->manager->listTableIndexes($result);
  315. if (PEAR::isError($indexes)) {
  316. $db->setOption('idxname_format', $idxname_format);
  317. return $indexes;
  318. }
  319. foreach ($indexes as $index) {
  320. $definition = $this->getTableIndexDefinition($result, $index);
  321. if (PEAR::isError($definition)) {
  322. $db->setOption('idxname_format', $idxname_format);
  323. return $definition;
  324. }
  325. if (count($definition['fields']) > 1) {
  326. foreach ($definition['fields'] as $field => $sort) {
  327. $flags[$field] = 'multiple_key';
  328. }
  329. }
  330. }
  331. $constraints = $db->manager->listTableConstraints($result);
  332. if (PEAR::isError($constraints)) {
  333. return $constraints;
  334. }
  335. foreach ($constraints as $constraint) {
  336. $definition = $this->getTableConstraintDefinition($result, $constraint);
  337. if (PEAR::isError($definition)) {
  338. $db->setOption('idxname_format', $idxname_format);
  339. return $definition;
  340. }
  341. $flag = !empty($definition['primary'])
  342. ? 'primary_key' : (!empty($definition['unique'])
  343. ? 'unique_key' : false);
  344. if ($flag) {
  345. foreach ($definition['fields'] as $field => $sort) {
  346. if (empty($flags[$field]) || $flags[$field] != 'primary_key') {
  347. $flags[$field] = $flag;
  348. }
  349. }
  350. }
  351. }
  352. if ($mode) {
  353. $res['num_fields'] = count($fields);
  354. }
  355. foreach ($fields as $i => $field) {
  356. $definition = $this->getTableFieldDefinition($result, $field);
  357. if (PEAR::isError($definition)) {
  358. $db->setOption('idxname_format', $idxname_format);
  359. return $definition;
  360. }
  361. $res[$i] = $definition[0];
  362. $res[$i]['name'] = $field;
  363. $res[$i]['table'] = $result;
  364. $res[$i]['type'] = preg_replace('/^([a-z]+).*$/i', '\\1', trim($definition[0]['nativetype']));
  365. // 'primary_key', 'unique_key', 'multiple_key'
  366. $res[$i]['flags'] = empty($flags[$field]) ? '' : $flags[$field];
  367. // not_null', 'unsigned', 'auto_increment', 'default_[rawencodedvalue]'
  368. if (!empty($res[$i]['notnull'])) {
  369. $res[$i]['flags'].= ' not_null';
  370. }
  371. if (!empty($res[$i]['unsigned'])) {
  372. $res[$i]['flags'].= ' unsigned';
  373. }
  374. if (!empty($res[$i]['auto_increment'])) {
  375. $res[$i]['flags'].= ' autoincrement';
  376. }
  377. if (!empty($res[$i]['default'])) {
  378. $res[$i]['flags'].= ' default_'.rawurlencode($res[$i]['default']);
  379. }
  380. if ($mode & MDB2_TABLEINFO_ORDER) {
  381. $res['order'][$res[$i]['name']] = $i;
  382. }
  383. if ($mode & MDB2_TABLEINFO_ORDERTABLE) {
  384. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  385. }
  386. }
  387. $db->setOption('idxname_format', $idxname_format);
  388. return $res;
  389. }
  390. }
  391. ?>