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-2006 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith                                         |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
  10. // | API as well as database abstraction for PHP applications.            |
  11. // | This LICENSE is in the BSD license style.                            |
  12. // |                                                                      |
  13. // | Redistribution and use in source and binary forms, with or without   |
  14. // | modification, are permitted provided that the following conditions   |
  15. // | are met:                                                             |
  16. // |                                                                      |
  17. // | Redistributions of source code must retain the above copyright       |
  18. // | notice, this list of conditions and the following disclaimer.        |
  19. // |                                                                      |
  20. // | Redistributions in binary form must reproduce the above copyright    |
  21. // | notice, this list of conditions and the following disclaimer in the  |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // |                                                                      |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission.                                                  |
  28. // |                                                                      |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  40. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@pooteeweet.org>                           |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: sqlite.php,v 1.60 2006/08/12 15:44:03 lsmith Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Reverse/Common.php';
  49.  
  50. /**
  51.  * MDB2 SQlite driver for the schema reverse engineering module
  52.  *
  53.  * @package MDB2
  54.  * @category Database
  55.  * @author  Lukas Smith <smith@pooteeweet.org>
  56.  */
  57. class MDB2_Driver_Reverse_sqlite extends MDB2_Driver_Reverse_Common
  58. {
  59.     function _getTableColumns($sql)
  60.     {
  61.         $db =$this->getDBInstance();
  62.         if (PEAR::isError($db)) {
  63.             return $db;
  64.         }
  65.         $start_pos strpos($sql'(');
  66.         $end_pos strrpos($sql')');
  67.         $column_def substr($sql$start_pos+1$end_pos-$start_pos-1);
  68.         // replace the decimal length-places-separator with a colon
  69.         $column_def preg_replace('/(\d),(\d)/''\1:\2'$column_def);
  70.         $column_sql split(','$column_def);
  71.         $columns = array();
  72.         $count count($column_sql);
  73.         if ($count == 0{
  74.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  75.                 'unexpected empty table column definition list'__FUNCTION__);
  76.         }
  77.         $regexp '/^([^ ]+) (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]*))?\))?( UNSIGNED)?( PRIMARY KEY)?( DEFAULT (\'[^\']*\'|[^ ]+))?( NULL| NOT NULL)?$/i';
  78.         $regexp2 '/^([^ ]+) (PRIMARY|UNIQUE|CHECK)$/i';
  79.         for ($i=0$j=0; $i<$count; ++$i{
  80.             if (!preg_match($regexptrim($column_sql[$i])$matches)) {
  81.                 if (!preg_match($regexp2trim($column_sql[$i]))) {
  82.                     continue;
  83.                 }
  84.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  85.                     'unexpected table column SQL definition: "'.$column_sql[$i].'"'__FUNCTION__);
  86.             }
  87.             $columns[$j]['name'$matches[1];
  88.             $columns[$j]['type'strtolower($matches[2]);
  89.             if (isset($matches[4]&& strlen($matches[4])) {
  90.                 $columns[$j]['length'$matches[4];
  91.             }
  92.             if (isset($matches[6]&& strlen($matches[6])) {
  93.                 $columns[$j]['decimal'$matches[6];
  94.             }
  95.             if (isset($matches[7]&& strlen($matches[7])) {
  96.                 $columns[$j]['unsigned'= true;
  97.             }
  98.             if (isset($matches[8]&& strlen($matches[8])) {
  99.                 $columns[$j]['autoincrement'= true;
  100.             }
  101.             if (isset($matches[10]&& strlen($matches[10])) {
  102.                 $default $matches[10];
  103.                 if (strlen($default&& $default[0]=="'"{
  104.                     $default str_replace("''""'"substr($default1strlen($default)-2));
  105.                 }
  106.                 if ($default === 'NULL'{
  107.                     $default = null;
  108.                 }
  109.                 $columns[$j]['default'$default;
  110.             }
  111.             if (isset($matches[11]&& strlen($matches[11])) {
  112.                 $columns[$j]['notnull'($matches[11=== ' NOT NULL');
  113.             }
  114.             ++$j;
  115.         }
  116.         return $columns;
  117.     }
  118.  
  119.     // {{{ getTableFieldDefinition()
  120.  
  121.     /**
  122.      * Get the stucture of a field into an array
  123.      *
  124.      * @param string    $table         name of table that should be used in method
  125.      * @param string    $field_name     name of field that should be used in method
  126.      * @return mixed data array on success, a MDB2 error on failure
  127.      * @access public
  128.      */
  129.     function getTableFieldDefinition($table$field_name)
  130.     {
  131.         $db =$this->getDBInstance();
  132.         if (PEAR::isError($db)) {
  133.             return $db;
  134.         }
  135.  
  136.         $result $db->loadModule('Datatype'nulltrue);
  137.         if (PEAR::isError($result)) {
  138.             return $result;
  139.         }
  140.         $query "SELECT sql FROM sqlite_master WHERE type='table' AND ";
  141.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  142.             $query.= 'LOWER(name)='.$db->quote(strtolower($table)'text');
  143.         else {
  144.             $query.= 'name='.$db->quote($table'text');
  145.         }
  146.         $sql $db->queryOne($query);
  147.         if (PEAR::isError($sql)) {
  148.             return $sql;
  149.         }
  150.         $columns $this->_getTableColumns($sql);
  151.         foreach ($columns as $column{
  152.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  153.                 if ($db->options['field_case'== CASE_LOWER{
  154.                     $column['name'strtolower($column['name']);
  155.                 else {
  156.                     $column['name'strtoupper($column['name']);
  157.                 }
  158.             else {
  159.                 $column array_change_key_case($column$db->options['field_case']);
  160.             }
  161.             if ($field_name == $column['name']{
  162.                 list($types$length$unsigned$fixed$db->datatype->mapNativeDatatype($column);
  163.                 $notnull = false;
  164.                 if (!empty($column['notnull'])) {
  165.                     $notnull $column['notnull'];
  166.                 }
  167.                 $default = false;
  168.                 if (array_key_exists('default'$column)) {
  169.                     $default $column['default'];
  170.                     if (is_null($default&& $notnull{
  171.                         $default '';
  172.                     }
  173.                 }
  174.                 $autoincrement = false;
  175.                 if (!empty($column['autoincrement'])) {
  176.                     $autoincrement = true;
  177.                 }
  178.  
  179.                 $definition[0= array(
  180.                     'notnull' => $notnull,
  181.                     'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i''\\1'$column['type'])
  182.                 );
  183.                 if ($length > 0{
  184.                     $definition[0]['length'$length;
  185.                 }
  186.                 if (!is_null($unsigned)) {
  187.                     $definition[0]['unsigned'$unsigned;
  188.                 }
  189.                 if (!is_null($fixed)) {
  190.                     $definition[0]['fixed'$fixed;
  191.                 }
  192.                 if ($default !== false{
  193.                     $definition[0]['default'$default;
  194.                 }
  195.                 if ($autoincrement !== false{
  196.                     $definition[0]['autoincrement'$autoincrement;
  197.                 }
  198.                 foreach ($types as $key => $type{
  199.                     $definition[$key$definition[0];
  200.                     if ($type == 'clob' || $type == 'blob'{
  201.                         unset($definition[$key]['default']);
  202.                     }
  203.                     $definition[$key]['type'$type;
  204.                     $definition[$key]['mdb2type'$type;
  205.                 }
  206.                 return $definition;
  207.             }
  208.         }
  209.  
  210.         return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  211.             'it was not specified an existing table column'__FUNCTION__);
  212.     }
  213.  
  214.     // }}}
  215.     // {{{ getTableIndexDefinition()
  216.  
  217.     /**
  218.      * Get the stucture of an index into an array
  219.      *
  220.      * @param string    $table      name of table that should be used in method
  221.      * @param string    $index_name name of index that should be used in method
  222.      * @return mixed data array on success, a MDB2 error on failure
  223.      * @access public
  224.      */
  225.     function getTableIndexDefinition($table$index_name)
  226.     {
  227.         $db =$this->getDBInstance();
  228.         if (PEAR::isError($db)) {
  229.             return $db;
  230.         }
  231.  
  232.         $index_name $db->getIndexName($index_name);
  233.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  234.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  235.             $query.= "LOWER(name)=".$db->quote(strtolower($index_name)'text')." AND LOWER(tbl_name)=".$db->quote(strtolower($table)'text');
  236.         else {
  237.             $query.= 'name='.$db->quote($index_name'text').' AND tbl_name='.$db->quote($table'text');
  238.         }
  239.         $query.= " AND sql NOT NULL ORDER BY name";
  240.         $sql $db->queryOne($query'text');
  241.         if (PEAR::isError($sql)) {
  242.             return $sql;
  243.         }
  244.         if (!$sql{
  245.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  246.                 'it was not specified an existing table index'__FUNCTION__);
  247.         }
  248.  
  249.         $sql strtolower($sql);
  250.         $start_pos strpos($sql'(');
  251.         $end_pos strrpos($sql')');
  252.         $column_names substr($sql$start_pos+1$end_pos-$start_pos-1);
  253.         $column_names split(','$column_names);
  254.  
  255.         if (preg_match("/^create unique/"$sql)) {
  256.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  257.                 'it was not specified an existing table index'__FUNCTION__);
  258.         }
  259.  
  260.         $definition = array();
  261.         $count count($column_names);
  262.         for ($i=0; $i<$count; ++$i{
  263.             $column_name strtok($column_names[$i]," ");
  264.             $collation strtok(" ");
  265.             $definition['fields'][$column_name= array();
  266.             if (!empty($collation)) {
  267.                 $definition['fields'][$column_name]['sorting'=
  268.                     ($collation=='ASC' 'ascending' 'descending');
  269.             }
  270.         }
  271.  
  272.         if (empty($definition['fields'])) {
  273.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  274.                 'it was not specified an existing table index'__FUNCTION__);
  275.         }
  276.         return $definition;
  277.     }
  278.  
  279.     // }}}
  280.     // {{{ getTableConstraintDefinition()
  281.  
  282.     /**
  283.      * Get the stucture of a constraint into an array
  284.      *
  285.      * @param string    $table      name of table that should be used in method
  286.      * @param string    $index_name name of index that should be used in method
  287.      * @return mixed data array on success, a MDB2 error on failure
  288.      * @access public
  289.      */
  290.     function getTableConstraintDefinition($table$index_name)
  291.     {
  292.         $db =$this->getDBInstance();
  293.         if (PEAR::isError($db)) {
  294.             return $db;
  295.         }
  296.  
  297.         $index_name $db->getIndexName($index_name);
  298.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  299.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  300.             $query.= "LOWER(name)=".$db->quote(strtolower($index_name)'text')." AND LOWER(tbl_name)=".$db->quote(strtolower($table)'text');
  301.         else {
  302.             $query.= 'name='.$db->quote($index_name'text').' AND tbl_name='.$db->quote($table'text');
  303.         }
  304.         $query.= " AND sql NOT NULL ORDER BY name";
  305.         $sql $db->queryOne($query'text');
  306.         if (PEAR::isError($sql)) {
  307.             return $sql;
  308.         }
  309.         if (!$sql{
  310.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  311.                 'it was not specified an existing table index'__FUNCTION__);
  312.         }
  313.  
  314.         $sql strtolower($sql);
  315.         $start_pos strpos($sql'(');
  316.         $end_pos strrpos($sql')');
  317.         $column_names substr($sql$start_pos+1$end_pos-$start_pos-1);
  318.         $column_names split(','$column_names);
  319.  
  320.         if (!preg_match("/^create unique/"$sql)) {
  321.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  322.                 'it was not specified an existing table constraint'__FUNCTION__);
  323.         }
  324.  
  325.         $definition = array();
  326.         $definition['unique'= true;
  327.         $count count($column_names);
  328.         for ($i=0; $i<$count; ++$i{
  329.             $column_name strtok($column_names[$i]," ");
  330.             $collation strtok(" ");
  331.             $definition['fields'][$column_name= array();
  332.             if (!empty($collation)) {
  333.                 $definition['fields'][$column_name]['sorting'=
  334.                     ($collation=='ASC' 'ascending' 'descending');
  335.             }
  336.         }
  337.  
  338.         if (empty($definition['fields'])) {
  339.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  340.                 'it was not specified an existing table constraint'__FUNCTION__);
  341.         }
  342.         return $definition;
  343.     }
  344.  
  345.     // }}}
  346.     // {{{ tableInfo()
  347.  
  348.     /**
  349.      * Returns information about a table
  350.      *
  351.      * @param string         $result  a string containing the name of a table
  352.      * @param int            $mode    a valid tableInfo mode
  353.      *
  354.      * @return array  an associative array with the information requested.
  355.      *                  A MDB2_Error object on failure.
  356.      *
  357.      * @see MDB2_Driver_Common::tableInfo()
  358.      * @since Method available since Release 1.7.0
  359.      */
  360.     function tableInfo($result$mode = null)
  361.     {
  362.         if (is_string($result)) {
  363.            return parent::tableInfo($result$mode);
  364.         }
  365.  
  366.         $db =$this->getDBInstance();
  367.         if (PEAR::isError($db)) {
  368.             return $db;
  369.         }
  370.  
  371.         return $db->raiseError(MDB2_ERROR_NOT_CAPABLEnullnull,
  372.            'This DBMS can not obtain tableInfo from result sets'__FUNCTION__);
  373.     }
  374. }
  375.  
  376. ?>

Documentation generated on Mon, 11 Mar 2019 14:43:22 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.