MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file sqlite.php

Documentation is available at sqlite.php

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

Documentation generated on Mon, 11 Mar 2019 15:51:27 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.