sqlite3.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 - 7 |
  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$
  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 Lorenzo Alberton <l.alberton@quipo.it>
  55. */
  56. class MDB2_Driver_Reverse_sqlite3 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 = explode("\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 (MDB2::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 = explode(',', $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|TINYINT|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. } else {
  129. $columns[$j]['default'] = null;
  130. }
  131. if (isset($matches[7]) && strlen($matches[7])) {
  132. $columns[$j]['notnull'] = ($matches[7] === ' NOT NULL');
  133. } else if (isset($matches[9]) && strlen($matches[9])) {
  134. $columns[$j]['notnull'] = ($matches[9] === ' NOT NULL');
  135. } else if (isset($matches[13]) && strlen($matches[13])) {
  136. $columns[$j]['notnull'] = ($matches[13] === ' NOT NULL');
  137. }
  138. ++$j;
  139. }
  140. return $columns;
  141. }
  142. // {{{ getTableFieldDefinition()
  143. /**
  144. * Get the stucture of a field into an array
  145. *
  146. * @param string $table_name name of table that should be used in method
  147. * @param string $field_name name of field that should be used in method
  148. * @return mixed data array on success, a MDB2 error on failure.
  149. * The returned array contains an array for each field definition,
  150. * with (some of) these indices:
  151. * [notnull] [nativetype] [length] [fixed] [default] [type] [mdb2type]
  152. * @access public
  153. */
  154. function getTableFieldDefinition($table_name, $field_name)
  155. {
  156. $db = $this->getDBInstance();
  157. if (MDB2::isError($db)) {
  158. return $db;
  159. }
  160. list($schema, $table) = $this->splitTableSchema($table_name);
  161. $result = $db->loadModule('Datatype', null, true);
  162. if (MDB2::isError($result)) {
  163. return $result;
  164. }
  165. $query = "SELECT sql FROM sqlite_master WHERE type='table' AND ";
  166. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  167. $query.= 'LOWER(name)='.$db->quote(strtolower($table), 'text');
  168. } else {
  169. $query.= 'name='.$db->quote($table, 'text');
  170. }
  171. $sql = $db->queryOne($query);
  172. if (MDB2::isError($sql)) {
  173. return $sql;
  174. }
  175. $columns = $this->_getTableColumns($sql);
  176. foreach ($columns as $column) {
  177. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  178. if ($db->options['field_case'] == CASE_LOWER) {
  179. $column['name'] = strtolower($column['name']);
  180. } else {
  181. $column['name'] = strtoupper($column['name']);
  182. }
  183. } else {
  184. $column = array_change_key_case($column, $db->options['field_case']);
  185. }
  186. if ($field_name == $column['name']) {
  187. $mapped_datatype = $db->datatype->mapNativeDatatype($column);
  188. if (MDB2::isError($mapped_datatype)) {
  189. return $mapped_datatype;
  190. }
  191. list($types, $length, $unsigned, $fixed) = $mapped_datatype;
  192. $notnull = false;
  193. if (!empty($column['notnull'])) {
  194. $notnull = $column['notnull'];
  195. }
  196. $default = false;
  197. if (array_key_exists('default', $column)) {
  198. $default = $column['default'];
  199. if ((null === $default) && $notnull) {
  200. $default = '';
  201. }
  202. }
  203. $autoincrement = false;
  204. if (!empty($column['autoincrement'])) {
  205. $autoincrement = true;
  206. }
  207. $definition[0] = array(
  208. 'notnull' => $notnull,
  209. 'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
  210. );
  211. if (null !== $length) {
  212. $definition[0]['length'] = $length;
  213. }
  214. if (null !== $unsigned) {
  215. $definition[0]['unsigned'] = $unsigned;
  216. }
  217. if (null !== $fixed) {
  218. $definition[0]['fixed'] = $fixed;
  219. }
  220. if ($default !== false) {
  221. $definition[0]['default'] = $default;
  222. }
  223. if ($autoincrement !== false) {
  224. $definition[0]['autoincrement'] = $autoincrement;
  225. }
  226. foreach ($types as $key => $type) {
  227. $definition[$key] = $definition[0];
  228. if ($type == 'clob' || $type == 'blob') {
  229. unset($definition[$key]['default']);
  230. }
  231. $definition[$key]['type'] = $type;
  232. $definition[$key]['mdb2type'] = $type;
  233. }
  234. return $definition;
  235. }
  236. }
  237. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  238. 'it was not specified an existing table column', __FUNCTION__);
  239. }
  240. // }}}
  241. // {{{ getTableIndexDefinition()
  242. /**
  243. * Get the stucture of an index into an array
  244. *
  245. * @param string $table_name name of table that should be used in method
  246. * @param string $index_name name of index that should be used in method
  247. * @return mixed data array on success, a MDB2 error on failure
  248. * @access public
  249. */
  250. function getTableIndexDefinition($table_name, $index_name)
  251. {
  252. $db = $this->getDBInstance();
  253. if (MDB2::isError($db)) {
  254. return $db;
  255. }
  256. list($schema, $table) = $this->splitTableSchema($table_name);
  257. $query = "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  258. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  259. $query.= 'LOWER(name)=%s AND LOWER(tbl_name)=' . $db->quote(strtolower($table), 'text');
  260. } else {
  261. $query.= 'name=%s AND tbl_name=' . $db->quote($table, 'text');
  262. }
  263. $query.= ' AND sql NOT NULL ORDER BY name';
  264. $index_name_mdb2 = $db->getIndexName($index_name);
  265. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  266. $qry = sprintf($query, $db->quote(strtolower($index_name_mdb2), 'text'));
  267. } else {
  268. $qry = sprintf($query, $db->quote($index_name_mdb2, 'text'));
  269. }
  270. $sql = $db->queryOne($qry, 'text');
  271. if (MDB2::isError($sql) || empty($sql)) {
  272. // fallback to the given $index_name, without transformation
  273. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  274. $qry = sprintf($query, $db->quote(strtolower($index_name), 'text'));
  275. } else {
  276. $qry = sprintf($query, $db->quote($index_name, 'text'));
  277. }
  278. $sql = $db->queryOne($qry, 'text');
  279. }
  280. if (MDB2::isError($sql)) {
  281. return $sql;
  282. }
  283. if (!$sql) {
  284. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  285. 'it was not specified an existing table index', __FUNCTION__);
  286. }
  287. $sql = strtolower($sql);
  288. $start_pos = strpos($sql, '(');
  289. $end_pos = strrpos($sql, ')');
  290. $column_names = substr($sql, $start_pos+1, $end_pos-$start_pos-1);
  291. $column_names = explode(',', $column_names);
  292. if (preg_match("/^create unique/", $sql)) {
  293. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  294. 'it was not specified an existing table index', __FUNCTION__);
  295. }
  296. $definition = array();
  297. $count = count($column_names);
  298. for ($i=0; $i<$count; ++$i) {
  299. $column_name = strtok($column_names[$i], ' ');
  300. $collation = strtok(' ');
  301. $definition['fields'][$column_name] = array(
  302. 'position' => $i+1
  303. );
  304. if (!empty($collation)) {
  305. $definition['fields'][$column_name]['sorting'] =
  306. ($collation=='ASC' ? 'ascending' : 'descending');
  307. }
  308. }
  309. if (empty($definition['fields'])) {
  310. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  311. 'it was not specified an existing table index', __FUNCTION__);
  312. }
  313. return $definition;
  314. }
  315. // }}}
  316. // {{{ getTableConstraintDefinition()
  317. /**
  318. * Get the stucture of a constraint into an array
  319. *
  320. * @param string $table_name name of table that should be used in method
  321. * @param string $constraint_name name of constraint that should be used in method
  322. * @return mixed data array on success, a MDB2 error on failure
  323. * @access public
  324. */
  325. function getTableConstraintDefinition($table_name, $constraint_name)
  326. {
  327. $db = $this->getDBInstance();
  328. if (MDB2::isError($db)) {
  329. return $db;
  330. }
  331. list($schema, $table) = $this->splitTableSchema($table_name);
  332. $query = "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  333. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  334. $query.= 'LOWER(name)=%s AND LOWER(tbl_name)=' . $db->quote(strtolower($table), 'text');
  335. } else {
  336. $query.= 'name=%s AND tbl_name=' . $db->quote($table, 'text');
  337. }
  338. $query.= ' AND sql NOT NULL ORDER BY name';
  339. $constraint_name_mdb2 = $db->getIndexName($constraint_name);
  340. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  341. $qry = sprintf($query, $db->quote(strtolower($constraint_name_mdb2), 'text'));
  342. } else {
  343. $qry = sprintf($query, $db->quote($constraint_name_mdb2, 'text'));
  344. }
  345. $sql = $db->queryOne($qry, 'text');
  346. if (MDB2::isError($sql) || empty($sql)) {
  347. // fallback to the given $index_name, without transformation
  348. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  349. $qry = sprintf($query, $db->quote(strtolower($constraint_name), 'text'));
  350. } else {
  351. $qry = sprintf($query, $db->quote($constraint_name, 'text'));
  352. }
  353. $sql = $db->queryOne($qry, 'text');
  354. }
  355. if (MDB2::isError($sql)) {
  356. return $sql;
  357. }
  358. //default values, eventually overridden
  359. $definition = array(
  360. 'primary' => false,
  361. 'unique' => false,
  362. 'foreign' => false,
  363. 'check' => false,
  364. 'fields' => array(),
  365. 'references' => array(
  366. 'table' => '',
  367. 'fields' => array(),
  368. ),
  369. 'onupdate' => '',
  370. 'ondelete' => '',
  371. 'match' => '',
  372. 'deferrable' => false,
  373. 'initiallydeferred' => false,
  374. );
  375. if (!$sql) {
  376. $query = "SELECT sql FROM sqlite_master WHERE type='table' AND ";
  377. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  378. $query.= 'LOWER(name)='.$db->quote(strtolower($table), 'text');
  379. } else {
  380. $query.= 'name='.$db->quote($table, 'text');
  381. }
  382. $query.= " AND sql NOT NULL ORDER BY name";
  383. $sql = $db->queryOne($query, 'text');
  384. if (MDB2::isError($sql)) {
  385. return $sql;
  386. }
  387. if ($constraint_name == 'primary') {
  388. // search in table definition for PRIMARY KEYs
  389. if (preg_match("/\bPRIMARY\s+KEY\b\s*\(([^)]+)/i", $sql, $tmp)) {
  390. $definition['primary'] = true;
  391. $definition['fields'] = array();
  392. $column_names = explode(',', $tmp[1]);
  393. $colpos = 1;
  394. foreach ($column_names as $column_name) {
  395. $definition['fields'][trim($column_name)] = array(
  396. 'position' => $colpos++
  397. );
  398. }
  399. return $definition;
  400. }
  401. if (preg_match("/\"([^\"]+)\"[^\,\"]+\bPRIMARY\s+KEY\b[^\,\)]*/i", $sql, $tmp)) {
  402. $definition['primary'] = true;
  403. $definition['fields'] = array();
  404. $column_names = explode(',', $tmp[1]);
  405. $colpos = 1;
  406. foreach ($column_names as $column_name) {
  407. $definition['fields'][trim($column_name)] = array(
  408. 'position' => $colpos++
  409. );
  410. }
  411. return $definition;
  412. }
  413. } else {
  414. // search in table definition for FOREIGN KEYs
  415. $pattern = "/\bCONSTRAINT\b\s+%s\s+
  416. \bFOREIGN\s+KEY\b\s*\(([^\)]+)\)\s*
  417. \bREFERENCES\b\s+([^\s]+)\s*\(([^\)]+)\)\s*
  418. (?:\bMATCH\s*([^\s]+))?\s*
  419. (?:\bON\s+UPDATE\s+([^\s,\)]+))?\s*
  420. (?:\bON\s+DELETE\s+([^\s,\)]+))?\s*
  421. /imsx";
  422. $found_fk = false;
  423. if (preg_match(sprintf($pattern, $constraint_name_mdb2), $sql, $tmp)) {
  424. $found_fk = true;
  425. } elseif (preg_match(sprintf($pattern, $constraint_name), $sql, $tmp)) {
  426. $found_fk = true;
  427. }
  428. if ($found_fk) {
  429. $definition['foreign'] = true;
  430. $definition['match'] = 'SIMPLE';
  431. $definition['onupdate'] = 'NO ACTION';
  432. $definition['ondelete'] = 'NO ACTION';
  433. $definition['references']['table'] = $tmp[2];
  434. $column_names = explode(',', $tmp[1]);
  435. $colpos = 1;
  436. foreach ($column_names as $column_name) {
  437. $definition['fields'][trim($column_name)] = array(
  438. 'position' => $colpos++
  439. );
  440. }
  441. $referenced_cols = explode(',', $tmp[3]);
  442. $colpos = 1;
  443. foreach ($referenced_cols as $column_name) {
  444. $definition['references']['fields'][trim($column_name)] = array(
  445. 'position' => $colpos++
  446. );
  447. }
  448. if (isset($tmp[4])) {
  449. $definition['match'] = $tmp[4];
  450. }
  451. if (isset($tmp[5])) {
  452. $definition['onupdate'] = $tmp[5];
  453. }
  454. if (isset($tmp[6])) {
  455. $definition['ondelete'] = $tmp[6];
  456. }
  457. return $definition;
  458. }
  459. }
  460. $sql = false;
  461. }
  462. if (!$sql) {
  463. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  464. $constraint_name . ' is not an existing table constraint', __FUNCTION__);
  465. }
  466. $sql = strtolower($sql);
  467. $start_pos = strpos($sql, '(');
  468. $end_pos = strrpos($sql, ')');
  469. $column_names = substr($sql, $start_pos+1, $end_pos-$start_pos-1);
  470. $column_names = explode(',', $column_names);
  471. if (!preg_match("/^create unique/", $sql)) {
  472. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  473. $constraint_name . ' is not an existing table constraint', __FUNCTION__);
  474. }
  475. $definition['unique'] = true;
  476. $count = count($column_names);
  477. for ($i=0; $i<$count; ++$i) {
  478. $column_name = strtok($column_names[$i]," ");
  479. $collation = strtok(" ");
  480. $definition['fields'][$column_name] = array(
  481. 'position' => $i+1
  482. );
  483. if (!empty($collation)) {
  484. $definition['fields'][$column_name]['sorting'] =
  485. ($collation=='ASC' ? 'ascending' : 'descending');
  486. }
  487. }
  488. if (empty($definition['fields'])) {
  489. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  490. $constraint_name . ' is not an existing table constraint', __FUNCTION__);
  491. }
  492. return $definition;
  493. }
  494. // }}}
  495. // {{{ getTriggerDefinition()
  496. /**
  497. * Get the structure of a trigger into an array
  498. *
  499. * EXPERIMENTAL
  500. *
  501. * WARNING: this function is experimental and may change the returned value
  502. * at any time until labelled as non-experimental
  503. *
  504. * @param string $trigger name of trigger that should be used in method
  505. * @return mixed data array on success, a MDB2 error on failure
  506. * @access public
  507. */
  508. function getTriggerDefinition($trigger)
  509. {
  510. $db = $this->getDBInstance();
  511. if (MDB2::isError($db)) {
  512. return $db;
  513. }
  514. $query = "SELECT name as trigger_name,
  515. tbl_name AS table_name,
  516. sql AS trigger_body,
  517. NULL AS trigger_type,
  518. NULL AS trigger_event,
  519. NULL AS trigger_comment,
  520. 1 AS trigger_enabled
  521. FROM sqlite_master
  522. WHERE type='trigger'";
  523. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  524. $query.= ' AND LOWER(name)='.$db->quote(strtolower($trigger), 'text');
  525. } else {
  526. $query.= ' AND name='.$db->quote($trigger, 'text');
  527. }
  528. $types = array(
  529. 'trigger_name' => 'text',
  530. 'table_name' => 'text',
  531. 'trigger_body' => 'text',
  532. 'trigger_type' => 'text',
  533. 'trigger_event' => 'text',
  534. 'trigger_comment' => 'text',
  535. 'trigger_enabled' => 'boolean',
  536. );
  537. $def = $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
  538. if (MDB2::isError($def)) {
  539. return $def;
  540. }
  541. if (empty($def)) {
  542. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  543. 'it was not specified an existing trigger', __FUNCTION__);
  544. }
  545. 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)) {
  546. $def['trigger_type'] = strtoupper($tmp[1]);
  547. $def['trigger_event'] = strtoupper($tmp[2]);
  548. }
  549. return $def;
  550. }
  551. // }}}
  552. // {{{ tableInfo()
  553. /**
  554. * Returns information about a table
  555. *
  556. * @param string $result a string containing the name of a table
  557. * @param int $mode a valid tableInfo mode
  558. *
  559. * @return array an associative array with the information requested.
  560. * A MDB2_Error object on failure.
  561. *
  562. * @see MDB2_Driver_Common::tableInfo()
  563. * @since Method available since Release 1.7.0
  564. */
  565. function tableInfo($result, $mode = null)
  566. {
  567. if (is_string($result)) {
  568. return parent::tableInfo($result, $mode);
  569. }
  570. $db = $this->getDBInstance();
  571. if (MDB2::isError($db)) {
  572. return $db;
  573. }
  574. return $db->raiseError(MDB2_ERROR_NOT_CAPABLE, null, null,
  575. 'This DBMS can not obtain tableInfo from result sets', __FUNCTION__);
  576. }
  577. }
  578. ?>