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.
 
 
 
 
 
 

369 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: mysql.php,v 1.59 2006/08/12 15:44:03 lsmith Exp $
  46. //
  47. require_once 'MDB2/Driver/Reverse/Common.php';
  48. /**
  49. * MDB2 MySQL driver for the schema reverse engineering module
  50. *
  51. * @package MDB2
  52. * @category Database
  53. * @author Lukas Smith <smith@pooteeweet.org>
  54. */
  55. class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
  56. {
  57. // {{{ getTableFieldDefinition()
  58. /**
  59. * Get the stucture of a field into an array
  60. *
  61. * @param string $table name of table that should be used in method
  62. * @param string $field_name name of field that should be used in method
  63. * @return mixed data array on success, a MDB2 error on failure
  64. * @access public
  65. */
  66. function getTableFieldDefinition($table, $field_name)
  67. {
  68. $db =& $this->getDBInstance();
  69. if (PEAR::isError($db)) {
  70. return $db;
  71. }
  72. $result = $db->loadModule('Datatype', null, true);
  73. if (PEAR::isError($result)) {
  74. return $result;
  75. }
  76. $table = $db->quoteIdentifier($table, true);
  77. $query = "SHOW COLUMNS FROM $table LIKE ".$db->quote($field_name);
  78. $columns = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
  79. if (PEAR::isError($columns)) {
  80. return $columns;
  81. }
  82. foreach ($columns as $column) {
  83. $column = array_change_key_case($column, CASE_LOWER);
  84. $column['name'] = $column['field'];
  85. unset($column['field']);
  86. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  87. if ($db->options['field_case'] == CASE_LOWER) {
  88. $column['name'] = strtolower($column['name']);
  89. } else {
  90. $column['name'] = strtoupper($column['name']);
  91. }
  92. } else {
  93. $column = array_change_key_case($column, $db->options['field_case']);
  94. }
  95. if ($field_name == $column['name']) {
  96. list($types, $length, $unsigned, $fixed) = $db->datatype->mapNativeDatatype($column);
  97. $notnull = false;
  98. if (empty($column['null']) || $column['null'] !== 'YES') {
  99. $notnull = true;
  100. }
  101. $default = false;
  102. if (array_key_exists('default', $column)) {
  103. $default = $column['default'];
  104. if (is_null($default) && $notnull) {
  105. $default = '';
  106. }
  107. }
  108. $autoincrement = false;
  109. if (!empty($column['extra']) && $column['extra'] == 'auto_increment') {
  110. $autoincrement = true;
  111. }
  112. $definition[0] = array(
  113. 'notnull' => $notnull,
  114. 'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
  115. );
  116. if ($length > 0) {
  117. $definition[0]['length'] = $length;
  118. }
  119. if (!is_null($unsigned)) {
  120. $definition[0]['unsigned'] = $unsigned;
  121. }
  122. if (!is_null($fixed)) {
  123. $definition[0]['fixed'] = $fixed;
  124. }
  125. if ($default !== false) {
  126. $definition[0]['default'] = $default;
  127. }
  128. if ($autoincrement !== false) {
  129. $definition[0]['autoincrement'] = $autoincrement;
  130. }
  131. foreach ($types as $key => $type) {
  132. $definition[$key] = $definition[0];
  133. if ($type == 'clob' || $type == 'blob') {
  134. unset($definition[$key]['default']);
  135. }
  136. $definition[$key]['type'] = $type;
  137. $definition[$key]['mdb2type'] = $type;
  138. }
  139. return $definition;
  140. }
  141. }
  142. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  143. 'it was not specified an existing table column', __FUNCTION__);
  144. }
  145. // }}}
  146. // {{{ getTableIndexDefinition()
  147. /**
  148. * Get the stucture of an index into an array
  149. *
  150. * @param string $table name of table that should be used in method
  151. * @param string $index_name name of index that should be used in method
  152. * @return mixed data array on success, a MDB2 error on failure
  153. * @access public
  154. */
  155. function getTableIndexDefinition($table, $index_name)
  156. {
  157. $db =& $this->getDBInstance();
  158. if (PEAR::isError($db)) {
  159. return $db;
  160. }
  161. $index_name = $db->getIndexName($index_name);
  162. $table = $db->quoteIdentifier($table, true);
  163. $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = ".$db->quote($index_name)." */";
  164. $result = $db->query($query);
  165. if (PEAR::isError($result)) {
  166. return $result;
  167. }
  168. $definition = array();
  169. while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  170. $row = array_change_key_case($row, CASE_LOWER);
  171. $key_name = $row['key_name'];
  172. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  173. if ($db->options['field_case'] == CASE_LOWER) {
  174. $key_name = strtolower($key_name);
  175. } else {
  176. $key_name = strtoupper($key_name);
  177. }
  178. }
  179. if ($index_name == $key_name) {
  180. if (!$row['non_unique']) {
  181. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  182. 'it was not specified an existing table index', __FUNCTION__);
  183. }
  184. $column_name = $row['column_name'];
  185. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  186. if ($db->options['field_case'] == CASE_LOWER) {
  187. $column_name = strtolower($column_name);
  188. } else {
  189. $column_name = strtoupper($column_name);
  190. }
  191. }
  192. $definition['fields'][$column_name] = array();
  193. if (!empty($row['collation'])) {
  194. $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
  195. ? 'ascending' : 'descending');
  196. }
  197. }
  198. }
  199. $result->free();
  200. if (empty($definition['fields'])) {
  201. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  202. 'it was not specified an existing table index', __FUNCTION__);
  203. }
  204. return $definition;
  205. }
  206. // }}}
  207. // {{{ getTableConstraintDefinition()
  208. /**
  209. * Get the stucture of a constraint into an array
  210. *
  211. * @param string $table name of table that should be used in method
  212. * @param string $index_name name of index that should be used in method
  213. * @return mixed data array on success, a MDB2 error on failure
  214. * @access public
  215. */
  216. function getTableConstraintDefinition($table, $index_name)
  217. {
  218. $db =& $this->getDBInstance();
  219. if (PEAR::isError($db)) {
  220. return $db;
  221. }
  222. if (strtolower($index_name) != 'primary') {
  223. $index_name = $db->getIndexName($index_name);
  224. }
  225. $table = $db->quoteIdentifier($table, true);
  226. $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = ".$db->quote($index_name)." */";
  227. $result = $db->query($query);
  228. if (PEAR::isError($result)) {
  229. return $result;
  230. }
  231. $definition = array();
  232. while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  233. $row = array_change_key_case($row, CASE_LOWER);
  234. $key_name = $row['key_name'];
  235. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  236. if ($db->options['field_case'] == CASE_LOWER) {
  237. $key_name = strtolower($key_name);
  238. } else {
  239. $key_name = strtoupper($key_name);
  240. }
  241. }
  242. if ($index_name == $key_name) {
  243. if ($row['non_unique']) {
  244. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  245. 'it was not specified an existing table constraint', __FUNCTION__);
  246. }
  247. if ($row['key_name'] == 'PRIMARY') {
  248. $definition['primary'] = true;
  249. } else {
  250. $definition['unique'] = true;
  251. }
  252. $column_name = $row['column_name'];
  253. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  254. if ($db->options['field_case'] == CASE_LOWER) {
  255. $column_name = strtolower($column_name);
  256. } else {
  257. $column_name = strtoupper($column_name);
  258. }
  259. }
  260. $definition['fields'][$column_name] = array();
  261. if (!empty($row['collation'])) {
  262. $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
  263. ? 'ascending' : 'descending');
  264. }
  265. }
  266. }
  267. $result->free();
  268. if (empty($definition['fields'])) {
  269. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  270. 'it was not specified an existing table constraint', __FUNCTION__);
  271. }
  272. return $definition;
  273. }
  274. // }}}
  275. // {{{ tableInfo()
  276. /**
  277. * Returns information about a table or a result set
  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 a valid tableInfo mode
  285. *
  286. * @return array an associative array with the information requested.
  287. * A MDB2_Error object on failure.
  288. *
  289. * @see MDB2_Driver_Common::setOption()
  290. */
  291. function tableInfo($result, $mode = null)
  292. {
  293. if (is_string($result)) {
  294. return parent::tableInfo($result, $mode);
  295. }
  296. $db =& $this->getDBInstance();
  297. if (PEAR::isError($db)) {
  298. return $db;
  299. }
  300. $id = MDB2::isResultCommon($result) ? $result->getResource() : $result;
  301. if (!is_resource($id)) {
  302. return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  303. 'Could not generate result ressource', __FUNCTION__);
  304. }
  305. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  306. if ($db->options['field_case'] == CASE_LOWER) {
  307. $case_func = 'strtolower';
  308. } else {
  309. $case_func = 'strtoupper';
  310. }
  311. } else {
  312. $case_func = 'strval';
  313. }
  314. $count = @mysql_num_fields($id);
  315. $res = array();
  316. if ($mode) {
  317. $res['num_fields'] = $count;
  318. }
  319. $db->loadModule('Datatype', null, true);
  320. for ($i = 0; $i < $count; $i++) {
  321. $res[$i] = array(
  322. 'table' => $case_func(@mysql_field_table($id, $i)),
  323. 'name' => $case_func(@mysql_field_name($id, $i)),
  324. 'type' => @mysql_field_type($id, $i),
  325. 'length' => @mysql_field_len($id, $i),
  326. 'flags' => @mysql_field_flags($id, $i),
  327. );
  328. if ($res[$i]['type'] == 'string') {
  329. $res[$i]['type'] = 'char';
  330. } elseif ($res[$i]['type'] == 'unknown') {
  331. $res[$i]['type'] = 'decimal';
  332. }
  333. $mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]);
  334. if (PEAR::isError($mdb2type_info)) {
  335. return $mdb2type_info;
  336. }
  337. $res[$i]['mdb2type'] = $mdb2type_info[0][0];
  338. if ($mode & MDB2_TABLEINFO_ORDER) {
  339. $res['order'][$res[$i]['name']] = $i;
  340. }
  341. if ($mode & MDB2_TABLEINFO_ORDERTABLE) {
  342. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  343. }
  344. }
  345. return $res;
  346. }
  347. }
  348. ?>