PageRenderTime 183ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 1ms

/includes/pear/MDB2/Driver/Reverse/sqlite.php

https://bitbucket.org/Yason/armory
PHP | 609 lines | 449 code | 33 blank | 127 comment | 109 complexity | 192640e220193c57fd586e06b8c65f0a MD5 | raw file
Possible License(s): GPL-3.0
  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, Lorenzo Alberton |
  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. // | Authors: Lukas Smith <smith@pooteeweet.org> |
  43. // | Lorenzo Alberton <l.alberton@quipo.it> |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: sqlite.php,v 1.80 2008/05/03 10:30:14 quipo Exp $
  47. //
  48. require_once 'MDB2/Driver/Reverse/Common.php';
  49. /**
  50. * MDB2 SQlite driver for the schema reverse engineering module
  51. *
  52. * @package MDB2
  53. * @category Database
  54. * @author Lukas Smith <smith@pooteeweet.org>
  55. */
  56. class MDB2_Driver_Reverse_sqlite extends MDB2_Driver_Reverse_Common
  57. {
  58. /**
  59. * Remove SQL comments from the field definition
  60. *
  61. * @access private
  62. */
  63. function _removeComments($sql) {
  64. $lines = split("\n", $sql);
  65. foreach ($lines as $k => $line) {
  66. $pieces = explode('--', $line);
  67. if (count($pieces) > 1 && (substr_count($pieces[0], '\'') % 2) == 0) {
  68. $lines[$k] = substr($line, 0, strpos($line, '--'));
  69. }
  70. }
  71. return implode("\n", $lines);
  72. }
  73. /**
  74. *
  75. */
  76. function _getTableColumns($sql)
  77. {
  78. $db =& $this->getDBInstance();
  79. if (PEAR::isError($db)) {
  80. return $db;
  81. }
  82. $start_pos = strpos($sql, '(');
  83. $end_pos = strrpos($sql, ')');
  84. $column_def = substr($sql, $start_pos+1, $end_pos-$start_pos-1);
  85. // replace the decimal length-places-separator with a colon
  86. $column_def = preg_replace('/(\d),(\d)/', '\1:\2', $column_def);
  87. $column_def = $this->_removeComments($column_def);
  88. $column_sql = split(',', $column_def);
  89. $columns = array();
  90. $count = count($column_sql);
  91. if ($count == 0) {
  92. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  93. 'unexpected empty table column definition list', __FUNCTION__);
  94. }
  95. $regexp = '/^\s*([^\s]+) +(CHAR|VARCHAR|VARCHAR2|TEXT|BOOLEAN|SMALLINT|INT|INTEGER|DECIMAL|BIGINT|DOUBLE|FLOAT|DATETIME|DATE|TIME|LONGTEXT|LONGBLOB)( ?\(([1-9][0-9]*)(:([1-9][0-9]*))?\))?( NULL| NOT NULL)?( UNSIGNED)?( NULL| NOT NULL)?( PRIMARY KEY)?( DEFAULT (\'[^\']*\'|[^ ]+))?( NULL| NOT NULL)?( PRIMARY KEY)?(\s*\-\-.*)?$/i';
  96. $regexp2 = '/^\s*([^ ]+) +(PRIMARY|UNIQUE|CHECK)$/i';
  97. for ($i=0, $j=0; $i<$count; ++$i) {
  98. if (!preg_match($regexp, trim($column_sql[$i]), $matches)) {
  99. if (!preg_match($regexp2, trim($column_sql[$i]))) {
  100. continue;
  101. }
  102. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  103. 'unexpected table column SQL definition: "'.$column_sql[$i].'"', __FUNCTION__);
  104. }
  105. $columns[$j]['name'] = trim($matches[1], implode('', $db->identifier_quoting));
  106. $columns[$j]['type'] = strtolower($matches[2]);
  107. if (isset($matches[4]) && strlen($matches[4])) {
  108. $columns[$j]['length'] = $matches[4];
  109. }
  110. if (isset($matches[6]) && strlen($matches[6])) {
  111. $columns[$j]['decimal'] = $matches[6];
  112. }
  113. if (isset($matches[8]) && strlen($matches[8])) {
  114. $columns[$j]['unsigned'] = true;
  115. }
  116. if (isset($matches[9]) && strlen($matches[9])) {
  117. $columns[$j]['autoincrement'] = true;
  118. }
  119. if (isset($matches[12]) && strlen($matches[12])) {
  120. $default = $matches[12];
  121. if (strlen($default) && $default[0]=="'") {
  122. $default = str_replace("''", "'", substr($default, 1, strlen($default)-2));
  123. }
  124. if ($default === 'NULL') {
  125. $default = null;
  126. }
  127. $columns[$j]['default'] = $default;
  128. }
  129. if (isset($matches[7]) && strlen($matches[7])) {
  130. $columns[$j]['notnull'] = ($matches[7] === ' NOT NULL');
  131. } else if (isset($matches[9]) && strlen($matches[9])) {
  132. $columns[$j]['notnull'] = ($matches[9] === ' NOT NULL');
  133. } else if (isset($matches[13]) && strlen($matches[13])) {
  134. $columns[$j]['notnull'] = ($matches[13] === ' NOT NULL');
  135. }
  136. ++$j;
  137. }
  138. return $columns;
  139. }
  140. // {{{ getTableFieldDefinition()
  141. /**
  142. * Get the stucture of a field into an array
  143. *
  144. * @param string $table_name name of table that should be used in method
  145. * @param string $field_name name of field that should be used in method
  146. * @return mixed data array on success, a MDB2 error on failure.
  147. * The returned array contains an array for each field definition,
  148. * with (some of) these indices:
  149. * [notnull] [nativetype] [length] [fixed] [default] [type] [mdb2type]
  150. * @access public
  151. */
  152. function getTableFieldDefinition($table_name, $field_name)
  153. {
  154. $db =& $this->getDBInstance();
  155. if (PEAR::isError($db)) {
  156. return $db;
  157. }
  158. list($schema, $table) = $this->splitTableSchema($table_name);
  159. $result = $db->loadModule('Datatype', null, true);
  160. if (PEAR::isError($result)) {
  161. return $result;
  162. }
  163. $query = "SELECT sql FROM sqlite_master WHERE type='table' AND ";
  164. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  165. $query.= 'LOWER(name)='.$db->quote(strtolower($table), 'text');
  166. } else {
  167. $query.= 'name='.$db->quote($table, 'text');
  168. }
  169. $sql = $db->queryOne($query);
  170. if (PEAR::isError($sql)) {
  171. return $sql;
  172. }
  173. $columns = $this->_getTableColumns($sql);
  174. foreach ($columns as $column) {
  175. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  176. if ($db->options['field_case'] == CASE_LOWER) {
  177. $column['name'] = strtolower($column['name']);
  178. } else {
  179. $column['name'] = strtoupper($column['name']);
  180. }
  181. } else {
  182. $column = array_change_key_case($column, $db->options['field_case']);
  183. }
  184. if ($field_name == $column['name']) {
  185. $mapped_datatype = $db->datatype->mapNativeDatatype($column);
  186. if (PEAR::isError($mapped_datatype)) {
  187. return $mapped_datatype;
  188. }
  189. list($types, $length, $unsigned, $fixed) = $mapped_datatype;
  190. $notnull = false;
  191. if (!empty($column['notnull'])) {
  192. $notnull = $column['notnull'];
  193. }
  194. $default = false;
  195. if (array_key_exists('default', $column)) {
  196. $default = $column['default'];
  197. if (is_null($default) && $notnull) {
  198. $default = '';
  199. }
  200. }
  201. $autoincrement = false;
  202. if (!empty($column['autoincrement'])) {
  203. $autoincrement = true;
  204. }
  205. $definition[0] = array(
  206. 'notnull' => $notnull,
  207. 'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
  208. );
  209. if (!is_null($length)) {
  210. $definition[0]['length'] = $length;
  211. }
  212. if (!is_null($unsigned)) {
  213. $definition[0]['unsigned'] = $unsigned;
  214. }
  215. if (!is_null($fixed)) {
  216. $definition[0]['fixed'] = $fixed;
  217. }
  218. if ($default !== false) {
  219. $definition[0]['default'] = $default;
  220. }
  221. if ($autoincrement !== false) {
  222. $definition[0]['autoincrement'] = $autoincrement;
  223. }
  224. foreach ($types as $key => $type) {
  225. $definition[$key] = $definition[0];
  226. if ($type == 'clob' || $type == 'blob') {
  227. unset($definition[$key]['default']);
  228. }
  229. $definition[$key]['type'] = $type;
  230. $definition[$key]['mdb2type'] = $type;
  231. }
  232. return $definition;
  233. }
  234. }
  235. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  236. 'it was not specified an existing table column', __FUNCTION__);
  237. }
  238. // }}}
  239. // {{{ getTableIndexDefinition()
  240. /**
  241. * Get the stucture of an index into an array
  242. *
  243. * @param string $table_name name of table that should be used in method
  244. * @param string $index_name name of index that should be used in method
  245. * @return mixed data array on success, a MDB2 error on failure
  246. * @access public
  247. */
  248. function getTableIndexDefinition($table_name, $index_name)
  249. {
  250. $db =& $this->getDBInstance();
  251. if (PEAR::isError($db)) {
  252. return $db;
  253. }
  254. list($schema, $table) = $this->splitTableSchema($table_name);
  255. $query = "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  256. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  257. $query.= 'LOWER(name)=%s AND LOWER(tbl_name)=' . $db->quote(strtolower($table), 'text');
  258. } else {
  259. $query.= 'name=%s AND tbl_name=' . $db->quote($table, 'text');
  260. }
  261. $query.= ' AND sql NOT NULL ORDER BY name';
  262. $index_name_mdb2 = $db->getIndexName($index_name);
  263. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  264. $qry = sprintf($query, $db->quote(strtolower($index_name_mdb2), 'text'));
  265. } else {
  266. $qry = sprintf($query, $db->quote($index_name_mdb2, 'text'));
  267. }
  268. $sql = $db->queryOne($qry, 'text');
  269. if (PEAR::isError($sql) || empty($sql)) {
  270. // fallback to the given $index_name, without transformation
  271. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  272. $qry = sprintf($query, $db->quote(strtolower($index_name), 'text'));
  273. } else {
  274. $qry = sprintf($query, $db->quote($index_name, 'text'));
  275. }
  276. $sql = $db->queryOne($qry, 'text');
  277. }
  278. if (PEAR::isError($sql)) {
  279. return $sql;
  280. }
  281. if (!$sql) {
  282. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  283. 'it was not specified an existing table index', __FUNCTION__);
  284. }
  285. $sql = strtolower($sql);
  286. $start_pos = strpos($sql, '(');
  287. $end_pos = strrpos($sql, ')');
  288. $column_names = substr($sql, $start_pos+1, $end_pos-$start_pos-1);
  289. $column_names = split(',', $column_names);
  290. if (preg_match("/^create unique/", $sql)) {
  291. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  292. 'it was not specified an existing table index', __FUNCTION__);
  293. }
  294. $definition = array();
  295. $count = count($column_names);
  296. for ($i=0; $i<$count; ++$i) {
  297. $column_name = strtok($column_names[$i], ' ');
  298. $collation = strtok(' ');
  299. $definition['fields'][$column_name] = array(
  300. 'position' => $i+1
  301. );
  302. if (!empty($collation)) {
  303. $definition['fields'][$column_name]['sorting'] =
  304. ($collation=='ASC' ? 'ascending' : 'descending');
  305. }
  306. }
  307. if (empty($definition['fields'])) {
  308. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  309. 'it was not specified an existing table index', __FUNCTION__);
  310. }
  311. return $definition;
  312. }
  313. // }}}
  314. // {{{ getTableConstraintDefinition()
  315. /**
  316. * Get the stucture of a constraint into an array
  317. *
  318. * @param string $table_name name of table that should be used in method
  319. * @param string $constraint_name name of constraint that should be used in method
  320. * @return mixed data array on success, a MDB2 error on failure
  321. * @access public
  322. */
  323. function getTableConstraintDefinition($table_name, $constraint_name)
  324. {
  325. $db =& $this->getDBInstance();
  326. if (PEAR::isError($db)) {
  327. return $db;
  328. }
  329. list($schema, $table) = $this->splitTableSchema($table_name);
  330. $query = "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  331. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  332. $query.= 'LOWER(name)=%s AND LOWER(tbl_name)=' . $db->quote(strtolower($table), 'text');
  333. } else {
  334. $query.= 'name=%s AND tbl_name=' . $db->quote($table, 'text');
  335. }
  336. $query.= ' AND sql NOT NULL ORDER BY name';
  337. $constraint_name_mdb2 = $db->getIndexName($constraint_name);
  338. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  339. $qry = sprintf($query, $db->quote(strtolower($constraint_name_mdb2), 'text'));
  340. } else {
  341. $qry = sprintf($query, $db->quote($constraint_name_mdb2, 'text'));
  342. }
  343. $sql = $db->queryOne($qry, 'text');
  344. if (PEAR::isError($sql) || empty($sql)) {
  345. // fallback to the given $index_name, without transformation
  346. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  347. $qry = sprintf($query, $db->quote(strtolower($constraint_name), 'text'));
  348. } else {
  349. $qry = sprintf($query, $db->quote($constraint_name, 'text'));
  350. }
  351. $sql = $db->queryOne($qry, 'text');
  352. }
  353. if (PEAR::isError($sql)) {
  354. return $sql;
  355. }
  356. //default values, eventually overridden
  357. $definition = array(
  358. 'primary' => false,
  359. 'unique' => false,
  360. 'foreign' => false,
  361. 'check' => false,
  362. 'fields' => array(),
  363. 'references' => array(
  364. 'table' => '',
  365. 'fields' => array(),
  366. ),
  367. 'onupdate' => '',
  368. 'ondelete' => '',
  369. 'match' => '',
  370. 'deferrable' => false,
  371. 'initiallydeferred' => false,
  372. );
  373. if (!$sql) {
  374. $query = "SELECT sql FROM sqlite_master WHERE type='table' AND ";
  375. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  376. $query.= 'LOWER(name)='.$db->quote(strtolower($table), 'text');
  377. } else {
  378. $query.= 'name='.$db->quote($table, 'text');
  379. }
  380. $query.= " AND sql NOT NULL ORDER BY name";
  381. $sql = $db->queryOne($query, 'text');
  382. if (PEAR::isError($sql)) {
  383. return $sql;
  384. }
  385. if ($constraint_name == 'primary') {
  386. // search in table definition for PRIMARY KEYs
  387. if (preg_match("/\bPRIMARY\s+KEY\b\s*\(([^)]+)/i", $sql, $tmp)) {
  388. $definition['primary'] = true;
  389. $definition['fields'] = array();
  390. $column_names = split(',', $tmp[1]);
  391. $colpos = 1;
  392. foreach ($column_names as $column_name) {
  393. $definition['fields'][trim($column_name)] = array(
  394. 'position' => $colpos++
  395. );
  396. }
  397. return $definition;
  398. }
  399. if (preg_match("/\"([^\"]+)\"[^\,\"]+\bPRIMARY\s+KEY\b[^\,\)]*/i", $sql, $tmp)) {
  400. $definition['primary'] = true;
  401. $definition['fields'] = array();
  402. $column_names = split(',', $tmp[1]);
  403. $colpos = 1;
  404. foreach ($column_names as $column_name) {
  405. $definition['fields'][trim($column_name)] = array(
  406. 'position' => $colpos++
  407. );
  408. }
  409. return $definition;
  410. }
  411. } else {
  412. // search in table definition for FOREIGN KEYs
  413. $pattern = "/\bCONSTRAINT\b\s+%s\s+
  414. \bFOREIGN\s+KEY\b\s*\(([^\)]+)\)\s*
  415. \bREFERENCES\b\s+([^\s]+)\s*\(([^\)]+)\)\s*
  416. (?:\bMATCH\s*([^\s]+))?\s*
  417. (?:\bON\s+UPDATE\s+([^\s,\)]+))?\s*
  418. (?:\bON\s+DELETE\s+([^\s,\)]+))?\s*
  419. /imsx";
  420. $found_fk = false;
  421. if (preg_match(sprintf($pattern, $constraint_name_mdb2), $sql, $tmp)) {
  422. $found_fk = true;
  423. } elseif (preg_match(sprintf($pattern, $constraint_name), $sql, $tmp)) {
  424. $found_fk = true;
  425. }
  426. if ($found_fk) {
  427. $definition['foreign'] = true;
  428. $definition['match'] = 'SIMPLE';
  429. $definition['onupdate'] = 'NO ACTION';
  430. $definition['ondelete'] = 'NO ACTION';
  431. $definition['references']['table'] = $tmp[2];
  432. $column_names = split(',', $tmp[1]);
  433. $colpos = 1;
  434. foreach ($column_names as $column_name) {
  435. $definition['fields'][trim($column_name)] = array(
  436. 'position' => $colpos++
  437. );
  438. }
  439. $referenced_cols = split(',', $tmp[3]);
  440. $colpos = 1;
  441. foreach ($referenced_cols as $column_name) {
  442. $definition['references']['fields'][trim($column_name)] = array(
  443. 'position' => $colpos++
  444. );
  445. }
  446. if (isset($tmp[4])) {
  447. $definition['match'] = $tmp[4];
  448. }
  449. if (isset($tmp[5])) {
  450. $definition['onupdate'] = $tmp[5];
  451. }
  452. if (isset($tmp[6])) {
  453. $definition['ondelete'] = $tmp[6];
  454. }
  455. return $definition;
  456. }
  457. }
  458. $sql = false;
  459. }
  460. if (!$sql) {
  461. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  462. $constraint_name . ' is not an existing table constraint', __FUNCTION__);
  463. }
  464. $sql = strtolower($sql);
  465. $start_pos = strpos($sql, '(');
  466. $end_pos = strrpos($sql, ')');
  467. $column_names = substr($sql, $start_pos+1, $end_pos-$start_pos-1);
  468. $column_names = split(',', $column_names);
  469. if (!preg_match("/^create unique/", $sql)) {
  470. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  471. $constraint_name . ' is not an existing table constraint', __FUNCTION__);
  472. }
  473. $definition['unique'] = true;
  474. $count = count($column_names);
  475. for ($i=0; $i<$count; ++$i) {
  476. $column_name = strtok($column_names[$i]," ");
  477. $collation = strtok(" ");
  478. $definition['fields'][$column_name] = array(
  479. 'position' => $i+1
  480. );
  481. if (!empty($collation)) {
  482. $definition['fields'][$column_name]['sorting'] =
  483. ($collation=='ASC' ? 'ascending' : 'descending');
  484. }
  485. }
  486. if (empty($definition['fields'])) {
  487. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  488. $constraint_name . ' is not an existing table constraint', __FUNCTION__);
  489. }
  490. return $definition;
  491. }
  492. // }}}
  493. // {{{ getTriggerDefinition()
  494. /**
  495. * Get the structure of a trigger into an array
  496. *
  497. * EXPERIMENTAL
  498. *
  499. * WARNING: this function is experimental and may change the returned value
  500. * at any time until labelled as non-experimental
  501. *
  502. * @param string $trigger name of trigger that should be used in method
  503. * @return mixed data array on success, a MDB2 error on failure
  504. * @access public
  505. */
  506. function getTriggerDefinition($trigger)
  507. {
  508. $db =& $this->getDBInstance();
  509. if (PEAR::isError($db)) {
  510. return $db;
  511. }
  512. $query = "SELECT name as trigger_name,
  513. tbl_name AS table_name,
  514. sql AS trigger_body,
  515. NULL AS trigger_type,
  516. NULL AS trigger_event,
  517. NULL AS trigger_comment,
  518. 1 AS trigger_enabled
  519. FROM sqlite_master
  520. WHERE type='trigger'";
  521. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  522. $query.= ' AND LOWER(name)='.$db->quote(strtolower($trigger), 'text');
  523. } else {
  524. $query.= ' AND name='.$db->quote($trigger, 'text');
  525. }
  526. $types = array(
  527. 'trigger_name' => 'text',
  528. 'table_name' => 'text',
  529. 'trigger_body' => 'text',
  530. 'trigger_type' => 'text',
  531. 'trigger_event' => 'text',
  532. 'trigger_comment' => 'text',
  533. 'trigger_enabled' => 'boolean',
  534. );
  535. $def = $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
  536. if (PEAR::isError($def)) {
  537. return $def;
  538. }
  539. if (empty($def)) {
  540. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  541. 'it was not specified an existing trigger', __FUNCTION__);
  542. }
  543. if (preg_match("/^create\s+(?:temp|temporary)?trigger\s+(?:if\s+not\s+exists\s+)?.*(before|after)?\s+(insert|update|delete)/Uims", $def['trigger_body'], $tmp)) {
  544. $def['trigger_type'] = strtoupper($tmp[1]);
  545. $def['trigger_event'] = strtoupper($tmp[2]);
  546. }
  547. return $def;
  548. }
  549. // }}}
  550. // {{{ tableInfo()
  551. /**
  552. * Returns information about a table
  553. *
  554. * @param string $result a string containing the name of a table
  555. * @param int $mode a valid tableInfo mode
  556. *
  557. * @return array an associative array with the information requested.
  558. * A MDB2_Error object on failure.
  559. *
  560. * @see MDB2_Driver_Common::tableInfo()
  561. * @since Method available since Release 1.7.0
  562. */
  563. function tableInfo($result, $mode = null)
  564. {
  565. if (is_string($result)) {
  566. return parent::tableInfo($result, $mode);
  567. }
  568. $db =& $this->getDBInstance();
  569. if (PEAR::isError($db)) {
  570. return $db;
  571. }
  572. return $db->raiseError(MDB2_ERROR_NOT_CAPABLE, null, null,
  573. 'This DBMS can not obtain tableInfo from result sets', __FUNCTION__);
  574. }
  575. }
  576. ?>