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

/inc/MDB2/Driver/Reverse/mysql.php

https://github.com/chregu/fluxcms
PHP | 437 lines | 296 code | 24 blank | 117 comment | 89 complexity | d5d5d3c022d15536e2d3b37f002be4e7 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2007 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$
  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. * @author Lorenzo Alberton <l.alberton@quipo.it>
  55. */
  56. class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
  57. {
  58. // {{{ getTableFieldDefinition()
  59. /**
  60. * Get the structure of a field into an array
  61. *
  62. * @param string $table name of table that should be used in method
  63. * @param string $field_name name of field that should be used in method
  64. * @return mixed data array on success, a MDB2 error on failure
  65. * @access public
  66. */
  67. function getTableFieldDefinition($table, $field_name)
  68. {
  69. $db =& $this->getDBInstance();
  70. if (PEAR::isError($db)) {
  71. return $db;
  72. }
  73. $result = $db->loadModule('Datatype', null, true);
  74. if (PEAR::isError($result)) {
  75. return $result;
  76. }
  77. $table = $db->quoteIdentifier($table, true);
  78. $query = "SHOW COLUMNS FROM $table LIKE ".$db->quote($field_name);
  79. $columns = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
  80. if (PEAR::isError($columns)) {
  81. return $columns;
  82. }
  83. foreach ($columns as $column) {
  84. $column = array_change_key_case($column, CASE_LOWER);
  85. $column['name'] = $column['field'];
  86. unset($column['field']);
  87. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  88. if ($db->options['field_case'] == CASE_LOWER) {
  89. $column['name'] = strtolower($column['name']);
  90. } else {
  91. $column['name'] = strtoupper($column['name']);
  92. }
  93. } else {
  94. $column = array_change_key_case($column, $db->options['field_case']);
  95. }
  96. if ($field_name == $column['name']) {
  97. $mapped_datatype = $db->datatype->mapNativeDatatype($column);
  98. if (PEAR::IsError($mapped_datatype)) {
  99. return $mapped_datatype;
  100. }
  101. list($types, $length, $unsigned, $fixed) = $mapped_datatype;
  102. $notnull = false;
  103. if (empty($column['null']) || $column['null'] !== 'YES') {
  104. $notnull = true;
  105. }
  106. $default = false;
  107. if (array_key_exists('default', $column)) {
  108. $default = $column['default'];
  109. if (is_null($default) && $notnull) {
  110. $default = '';
  111. }
  112. }
  113. $autoincrement = false;
  114. if (!empty($column['extra']) && $column['extra'] == 'auto_increment') {
  115. $autoincrement = true;
  116. }
  117. $definition[0] = array(
  118. 'notnull' => $notnull,
  119. 'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
  120. );
  121. if (!is_null($length)) {
  122. $definition[0]['length'] = $length;
  123. }
  124. if (!is_null($unsigned)) {
  125. $definition[0]['unsigned'] = $unsigned;
  126. }
  127. if (!is_null($fixed)) {
  128. $definition[0]['fixed'] = $fixed;
  129. }
  130. if ($default !== false) {
  131. $definition[0]['default'] = $default;
  132. }
  133. if ($autoincrement !== false) {
  134. $definition[0]['autoincrement'] = $autoincrement;
  135. }
  136. foreach ($types as $key => $type) {
  137. $definition[$key] = $definition[0];
  138. if ($type == 'clob' || $type == 'blob') {
  139. unset($definition[$key]['default']);
  140. }
  141. $definition[$key]['type'] = $type;
  142. $definition[$key]['mdb2type'] = $type;
  143. }
  144. return $definition;
  145. }
  146. }
  147. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  148. 'it was not specified an existing table column', __FUNCTION__);
  149. }
  150. // }}}
  151. // {{{ getTableIndexDefinition()
  152. /**
  153. * Get the structure of an index into an array
  154. *
  155. * @param string $table name of table that should be used in method
  156. * @param string $constraint_name name of constraint that should be used in method
  157. * @return mixed data array on success, a MDB2 error on failure
  158. * @access public
  159. */
  160. function getTableIndexDefinition($table, $constraint_name)
  161. {
  162. $db =& $this->getDBInstance();
  163. if (PEAR::isError($db)) {
  164. return $db;
  165. }
  166. $table = $db->quoteIdentifier($table, true);
  167. $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
  168. $constraint_name_mdb2 = $db->getIndexName($constraint_name);
  169. $result = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2)));
  170. if (!PEAR::isError($result) && !is_null($result)) {
  171. // apply 'idxname_format' only if the query succeeded, otherwise
  172. // fallback to the given $index_name, without transformation
  173. $constraint_name = $constraint_name_mdb2;
  174. }
  175. $result = $db->query(sprintf($query, $db->quote($constraint_name)));
  176. if (PEAR::isError($result)) {
  177. return $result;
  178. }
  179. $colpos = 1;
  180. $definition = array();
  181. while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  182. $row = array_change_key_case($row, CASE_LOWER);
  183. $key_name = $row['key_name'];
  184. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  185. if ($db->options['field_case'] == CASE_LOWER) {
  186. $key_name = strtolower($key_name);
  187. } else {
  188. $key_name = strtoupper($key_name);
  189. }
  190. }
  191. if ($constraint_name == $key_name) {
  192. if (!$row['non_unique']) {
  193. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  194. $constraint_name . ' is not an existing table constraint', __FUNCTION__);
  195. }
  196. $column_name = $row['column_name'];
  197. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  198. if ($db->options['field_case'] == CASE_LOWER) {
  199. $column_name = strtolower($column_name);
  200. } else {
  201. $column_name = strtoupper($column_name);
  202. }
  203. }
  204. $definition['fields'][$column_name] = array(
  205. 'position' => $colpos++
  206. );
  207. if (!empty($row['collation'])) {
  208. $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
  209. ? 'ascending' : 'descending');
  210. }
  211. }
  212. }
  213. $result->free();
  214. if (empty($definition['fields'])) {
  215. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  216. $constraint_name . ' is not an existing table constraint', __FUNCTION__);
  217. }
  218. return $definition;
  219. }
  220. // }}}
  221. // {{{ getTableConstraintDefinition()
  222. /**
  223. * Get the structure of a constraint into an array
  224. *
  225. * @param string $table name of table that should be used in method
  226. * @param string $index_name name of index that should be used in method
  227. * @return mixed data array on success, a MDB2 error on failure
  228. * @access public
  229. */
  230. function getTableConstraintDefinition($table, $index_name)
  231. {
  232. $db =& $this->getDBInstance();
  233. if (PEAR::isError($db)) {
  234. return $db;
  235. }
  236. $table = $db->quoteIdentifier($table, true);
  237. $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
  238. if (strtolower($index_name) != 'primary') {
  239. $index_name_mdb2 = $db->getIndexName($index_name);
  240. $result = $db->queryRow(sprintf($query, $db->quote($index_name_mdb2)));
  241. if (!PEAR::isError($result) && !is_null($result)) {
  242. // apply 'idxname_format' only if the query succeeded, otherwise
  243. // fallback to the given $index_name, without transformation
  244. $index_name = $index_name_mdb2;
  245. }
  246. }
  247. $result = $db->query(sprintf($query, $db->quote($index_name)));
  248. if (PEAR::isError($result)) {
  249. return $result;
  250. }
  251. $colpos = 1;
  252. $definition = array();
  253. while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  254. $row = array_change_key_case($row, CASE_LOWER);
  255. $key_name = $row['key_name'];
  256. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  257. if ($db->options['field_case'] == CASE_LOWER) {
  258. $key_name = strtolower($key_name);
  259. } else {
  260. $key_name = strtoupper($key_name);
  261. }
  262. }
  263. if ($index_name == $key_name) {
  264. if ($row['non_unique']) {
  265. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  266. 'it was not specified an existing table constraint', __FUNCTION__);
  267. }
  268. if ($row['key_name'] == 'PRIMARY') {
  269. $definition['primary'] = true;
  270. } else {
  271. $definition['unique'] = true;
  272. }
  273. $column_name = $row['column_name'];
  274. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  275. if ($db->options['field_case'] == CASE_LOWER) {
  276. $column_name = strtolower($column_name);
  277. } else {
  278. $column_name = strtoupper($column_name);
  279. }
  280. }
  281. $definition['fields'][$column_name] = array(
  282. 'position' => $colpos++
  283. );
  284. if (!empty($row['collation'])) {
  285. $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
  286. ? 'ascending' : 'descending');
  287. }
  288. }
  289. }
  290. $result->free();
  291. if (empty($definition['fields'])) {
  292. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  293. 'it was not specified an existing table constraint', __FUNCTION__);
  294. }
  295. return $definition;
  296. }
  297. // }}}
  298. // {{{ getTriggerDefinition()
  299. /**
  300. * Get the structure of a trigger into an array
  301. *
  302. * EXPERIMENTAL
  303. *
  304. * WARNING: this function is experimental and may change the returned value
  305. * at any time until labelled as non-experimental
  306. *
  307. * @param string $trigger name of trigger that should be used in method
  308. * @return mixed data array on success, a MDB2 error on failure
  309. * @access public
  310. */
  311. function getTriggerDefinition($trigger)
  312. {
  313. $db =& $this->getDBInstance();
  314. if (PEAR::isError($db)) {
  315. return $db;
  316. }
  317. $query = 'SELECT trigger_name,
  318. event_object_table AS table_name,
  319. action_statement AS trigger_body,
  320. action_timing AS trigger_type,
  321. event_manipulation AS trigger_event
  322. FROM information_schema.triggers
  323. WHERE trigger_name = '. $db->quote($trigger, 'text');
  324. $types = array(
  325. 'trigger_name' => 'text',
  326. 'table_name' => 'text',
  327. 'trigger_body' => 'text',
  328. 'trigger_type' => 'text',
  329. 'trigger_event' => 'text',
  330. );
  331. $def = $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
  332. if (PEAR::isError($def)) {
  333. return $def;
  334. }
  335. $def['trigger_comment'] = '';
  336. $def['trigger_enabled'] = true;
  337. return $def;
  338. }
  339. // }}}
  340. // {{{ tableInfo()
  341. /**
  342. * Returns information about a table or a result set
  343. *
  344. * @param object|string $result MDB2_result object from a query or a
  345. * string containing the name of a table.
  346. * While this also accepts a query result
  347. * resource identifier, this behavior is
  348. * deprecated.
  349. * @param int $mode a valid tableInfo mode
  350. *
  351. * @return array an associative array with the information requested.
  352. * A MDB2_Error object on failure.
  353. *
  354. * @see MDB2_Driver_Common::setOption()
  355. */
  356. function tableInfo($result, $mode = null)
  357. {
  358. if (is_string($result)) {
  359. return parent::tableInfo($result, $mode);
  360. }
  361. $db =& $this->getDBInstance();
  362. if (PEAR::isError($db)) {
  363. return $db;
  364. }
  365. $resource = MDB2::isResultCommon($result) ? $result->getResource() : $result;
  366. if (!is_resource($resource)) {
  367. return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  368. 'Could not generate result resource', __FUNCTION__);
  369. }
  370. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  371. if ($db->options['field_case'] == CASE_LOWER) {
  372. $case_func = 'strtolower';
  373. } else {
  374. $case_func = 'strtoupper';
  375. }
  376. } else {
  377. $case_func = 'strval';
  378. }
  379. $count = @mysql_num_fields($resource);
  380. $res = array();
  381. if ($mode) {
  382. $res['num_fields'] = $count;
  383. }
  384. $db->loadModule('Datatype', null, true);
  385. for ($i = 0; $i < $count; $i++) {
  386. $res[$i] = array(
  387. 'table' => $case_func(@mysql_field_table($resource, $i)),
  388. 'name' => $case_func(@mysql_field_name($resource, $i)),
  389. 'type' => @mysql_field_type($resource, $i),
  390. 'length' => @mysql_field_len($resource, $i),
  391. 'flags' => @mysql_field_flags($resource, $i),
  392. );
  393. if ($res[$i]['type'] == 'string') {
  394. $res[$i]['type'] = 'char';
  395. } elseif ($res[$i]['type'] == 'unknown') {
  396. $res[$i]['type'] = 'decimal';
  397. }
  398. $mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]);
  399. if (PEAR::isError($mdb2type_info)) {
  400. return $mdb2type_info;
  401. }
  402. $res[$i]['mdb2type'] = $mdb2type_info[0][0];
  403. if ($mode & MDB2_TABLEINFO_ORDER) {
  404. $res['order'][$res[$i]['name']] = $i;
  405. }
  406. if ($mode & MDB2_TABLEINFO_ORDERTABLE) {
  407. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  408. }
  409. }
  410. return $res;
  411. }
  412. }
  413. ?>