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.48 2006/05/14 05:51:45 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');
  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 (\'[^\']*\'|[^ ]+))?( NOT NULL)?$/i';
  78.         for ($i=0$j=0; $i<$count; ++$i{
  79.             if (!preg_match($regexptrim($column_sql[$i])$matches)) {
  80.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  81.                     'unexpected table column SQL definition: "'.$column_sql[$i].'"');
  82.             }
  83.             $columns[$j]['name'$matches[1];
  84.             $columns[$j]['type'strtolower($matches[2]);
  85.             if (isset($matches[4]&& strlen($matches[4])) {
  86.                 $columns[$j]['length'$matches[4];
  87.             }
  88.             if (isset($matches[6]&& strlen($matches[6])) {
  89.                 $columns[$j]['decimal'$matches[6];
  90.             }
  91.             if (isset($matches[7]&& strlen($matches[7])) {
  92.                 $columns[$j]['unsigned'= true;
  93.             }
  94.             if (isset($matches[8]&& strlen($matches[8])) {
  95.                 $columns[$j]['autoincrement'= true;
  96.             }
  97.             if (isset($matches[10]&& strlen($matches[10])) {
  98.                 $default $matches[10];
  99.                 if (strlen($default&& $default[0]=="'"{
  100.                     $default str_replace("''""'"substr($default1strlen($default)-2));
  101.                 }
  102.                 if ($default === 'NULL'{
  103.                     $default = null;
  104.                 }
  105.                 $columns[$j]['default'$default;
  106.             }
  107.             if (isset($matches[11]&& strlen($matches[11])) {
  108.                 $columns[$j]['notnull'= true;
  109.             }
  110.             ++$j;
  111.         }
  112.         return $columns;
  113.     }
  114.  
  115.     // {{{ getTableFieldDefinition()
  116.  
  117.     /**
  118.      * Get the stucture of a field into an array
  119.      *
  120.      * @param string    $table         name of table that should be used in method
  121.      * @param string    $field_name     name of field that should be used in method
  122.      * @return mixed data array on success, a MDB2 error on failure
  123.      * @access public
  124.      */
  125.     function getTableFieldDefinition($table$field_name)
  126.     {
  127.         $db =$this->getDBInstance();
  128.         if (PEAR::isError($db)) {
  129.             return $db;
  130.         }
  131.  
  132.         $result $db->loadModule('Datatype'nulltrue);
  133.         if (PEAR::isError($result)) {
  134.             return $result;
  135.         }
  136.         $table $db->quote($table'text');
  137.         $query "SELECT sql FROM sqlite_master WHERE type='table' AND ";
  138.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  139.             $query.= 'LOWER(name)='.strtolower($table);
  140.         else {
  141.             $query.= "name=$table";
  142.         }
  143.         $sql $db->queryOne($query);
  144.         if (PEAR::isError($sql)) {
  145.             return $sql;
  146.         }
  147.         $columns $this->_getTableColumns($sql);
  148.         foreach ($columns as $column{
  149.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  150.                 if ($db->options['field_case'== CASE_LOWER{
  151.                     $column['name'strtolower($column['name']);
  152.                 else {
  153.                     $column['name'strtoupper($column['name']);
  154.                 }
  155.             else {
  156.                 $column array_change_key_case($column$db->options['field_case']);
  157.             }
  158.             if ($field_name == $column['name']{
  159.                 list($types$length$unsigned$fixed$db->datatype->mapNativeDatatype($column);
  160.                 $notnull = false;
  161.                 if (array_key_exists('notnull'$column)) {
  162.                     $notnull $column['notnull'];
  163.                 }
  164.                 $default = false;
  165.                 if (array_key_exists('default'$column)) {
  166.                     $default $column['default'];
  167.                     if (is_null($default&& $notnull{
  168.                         $default '';
  169.                     }
  170.                 }
  171.                 $autoincrement = false;
  172.                 if (array_key_exists('autoincrement'$column&& $column['autoincrement']{
  173.                     $autoincrement = true;
  174.                 }
  175.                 $definition = array();
  176.                 foreach ($types as $key => $type{
  177.                     $definition[$key= array(
  178.                         'type' => $type,
  179.                         'notnull' => $notnull,
  180.                     );
  181.                     if ($length > 0{
  182.                         $definition[$key]['length'$length;
  183.                     }
  184.                     if (!is_null($unsigned)) {
  185.                         $definition[$key]['unsigned'$unsigned;
  186.                     }
  187.                     if (!is_null($fixed)) {
  188.                         $definition[$key]['fixed'$fixed;
  189.                     }
  190.                     if ($default !== false{
  191.                         $definition[$key]['default'$default;
  192.                     }
  193.                     if ($autoincrement !== false{
  194.                         $definition[$key]['autoincrement'$autoincrement;
  195.                     }
  196.                 }
  197.                 return $definition;
  198.             }
  199.         }
  200.  
  201.         return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  202.             'getTableFieldDefinition: it was not specified an existing table column');
  203.     }
  204.  
  205.     // }}}
  206.     // {{{ getTableIndexDefinition()
  207.  
  208.     /**
  209.      * Get the stucture of an index into an array
  210.      *
  211.      * @param string    $table      name of table that should be used in method
  212.      * @param string    $index_name name of index that should be used in method
  213.      * @return mixed data array on success, a MDB2 error on failure
  214.      * @access public
  215.      */
  216.     function getTableIndexDefinition($table$index_name)
  217.     {
  218.         $db =$this->getDBInstance();
  219.         if (PEAR::isError($db)) {
  220.             return $db;
  221.         }
  222.  
  223.         $index_name $db->getIndexName($index_name);
  224.         $index_name $db->quote($index_name'text');
  225.         $table $db->quote($table'text');
  226.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  227.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  228.             $query.= "LOWER(name)=".strtolower($index_name)." AND LOWER(tbl_name)=".strtolower($table);
  229.         else {
  230.             $query.= "name=$index_name AND tbl_name=$table";
  231.         }
  232.         $query.= " AND sql NOT NULL ORDER BY name";
  233.         $sql $db->queryOne($query'text');
  234.         if (PEAR::isError($sql)) {
  235.             return $sql;
  236.         }
  237.         if (!$sql{
  238.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  239.                 'getTableIndexDefinition: it was not specified an existing table index');
  240.         }
  241.  
  242.         $sql strtolower($sql);
  243.         $start_pos strpos($sql'(');
  244.         $end_pos strrpos($sql')');
  245.         $column_names substr($sql$start_pos+1$end_pos-$start_pos-1);
  246.         $column_names split(','$column_names);
  247.  
  248.         if (preg_match("/^create unique/"$sql)) {
  249.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  250.                 'getTableIndexDefinition: it was not specified an existing table index');
  251.         }
  252.  
  253.         $definition = array();
  254.         $count count($column_names);
  255.         for ($i=0; $i<$count; ++$i{
  256.             $column_name strtok($column_names[$i]," ");
  257.             $collation strtok(" ");
  258.             $definition['fields'][$column_name= array();
  259.             if (!empty($collation)) {
  260.                 $definition['fields'][$column_name]['sorting'=
  261.                     ($collation=='ASC' 'ascending' 'descending');
  262.             }
  263.         }
  264.  
  265.         if (!array_key_exists('fields'$definition)) {
  266.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  267.                 'getTableIndexDefinition: it was not specified an existing table index');
  268.         }
  269.         return $definition;
  270.     }
  271.  
  272.     // }}}
  273.     // {{{ getTableConstraintDefinition()
  274.  
  275.     /**
  276.      * Get the stucture of a constraint into an array
  277.      *
  278.      * @param string    $table      name of table that should be used in method
  279.      * @param string    $index_name name of index that should be used in method
  280.      * @return mixed data array on success, a MDB2 error on failure
  281.      * @access public
  282.      */
  283.     function getTableConstraintDefinition($table$index_name)
  284.     {
  285.         $db =$this->getDBInstance();
  286.         if (PEAR::isError($db)) {
  287.             return $db;
  288.         }
  289.  
  290.         $index_name $db->getIndexName($index_name);
  291.         $table $db->quote($table'text');
  292.         $index_name $db->quote($index_name'text');
  293.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  294.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  295.             $query.= 'LOWER(name)='.strtolower($index_name).' AND LOWER(tbl_name)='.strtolower($table);
  296.         else {
  297.             $query.= "name=$index_name AND tbl_name=$table";
  298.         }
  299.         $query.= " AND sql NOT NULL ORDER BY name";
  300.         $sql $db->queryOne($query'text');
  301.         if (PEAR::isError($sql)) {
  302.             return $sql;
  303.         }
  304.         if (!$sql{
  305.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  306.                 'getTableIndexDefinition: it was not specified an existing table index');
  307.         }
  308.  
  309.         $sql strtolower($sql);
  310.         $start_pos strpos($sql'(');
  311.         $end_pos strrpos($sql')');
  312.         $column_names substr($sql$start_pos+1$end_pos-$start_pos-1);
  313.         $column_names split(','$column_names);
  314.  
  315.         if (!preg_match("/^create unique/"$sql)) {
  316.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  317.                 'getTableConstraintDefinition: it was not specified an existing table constraint');
  318.         }
  319.  
  320.         $definition = array();
  321.         $definition['unique'= true;
  322.         $count count($column_names);
  323.         for ($i=0; $i<$count; ++$i{
  324.             $column_name strtok($column_names[$i]," ");
  325.             $collation strtok(" ");
  326.             $definition['fields'][$column_name= array();
  327.             if (!empty($collation)) {
  328.                 $definition['fields'][$column_name]['sorting'=
  329.                     ($collation=='ASC' 'ascending' 'descending');
  330.             }
  331.         }
  332.  
  333.         if (!array_key_exists('fields'$definition)) {
  334.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  335.                 'getTableConstraintDefinition: it was not specified an existing table constraint');
  336.         }
  337.         return $definition;
  338.     }
  339.  
  340.     // }}}
  341.     // {{{ tableInfo()
  342.  
  343.     /**
  344.      * Returns information about a table
  345.      *
  346.      * @param string         $result  a string containing the name of a table
  347.      * @param int            $mode    a valid tableInfo mode
  348.      *
  349.      * @return array  an associative array with the information requested.
  350.      *                  A MDB2_Error object on failure.
  351.      *
  352.      * @see MDB2_Driver_Common::tableInfo()
  353.      * @since Method available since Release 1.7.0
  354.      */
  355.     function tableInfo($result$mode = null)
  356.     {
  357.         $db =$this->getDBInstance();
  358.         if (PEAR::isError($db)) {
  359.             return $db;
  360.         }
  361.  
  362.         if (!is_string($result)) {
  363.             return $db->raiseError(MDB2_ERROR_NOT_CAPABLEnullnull,
  364.                 'This DBMS can not obtain tableInfo from result sets');
  365.         }
  366.  
  367.         /*
  368.          * Probably received a table name.
  369.          * Create a result resource identifier.
  370.          */
  371.         $id $db->queryAll('PRAGMA table_info('.$db->quote($result'text').');'nullMDB2_FETCHMODE_ASSOC);
  372.         $got_string = true;
  373.  
  374.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  375.             if ($db->options['field_case'== CASE_LOWER{
  376.                 $case_func 'strtolower';
  377.             else {
  378.                 $case_func 'strtoupper';
  379.             }
  380.         else {
  381.             $case_func 'strval';
  382.         }
  383.  
  384.         $count count($id);
  385.         $res   = array();
  386.  
  387.         if ($mode{
  388.             $res['num_fields'$count;
  389.         }
  390.  
  391.         $db->loadModule('Datatype'nulltrue);
  392.         for ($i = 0; $i $count$i++{
  393.             $id[$iarray_change_key_case($id[$i]CASE_LOWER);
  394.             if (strpos($id[$i]['type']'('!== false{
  395.                 $bits explode('('$id[$i]['type']);
  396.                 $type $bits[0];
  397.                 $len  rtrim($bits[1],')');
  398.             else {
  399.                 $type $id[$i]['type'];
  400.                 $len  = 0;
  401.             }
  402.  
  403.             $flags '';
  404.             if ($id[$i]['pk']{
  405.                 $flags.= 'primary_key ';
  406.             }
  407.             if ($id[$i]['notnull']{
  408.                 $flags.= 'not_null ';
  409.             }
  410.             if ($id[$i]['dflt_value'!== null{
  411.                 $flags.= 'default_' rawurlencode($id[$i]['dflt_value']);
  412.             }
  413.             $flags trim($flags);
  414.  
  415.             $res[$i= array(
  416.                 'table' => $case_func($result),
  417.                 'name'  => $case_func($id[$i]['name']),
  418.                 'type'  => $type,
  419.                 'length'   => $len,
  420.                 'flags' => $flags,
  421.             );
  422.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  423.             if (PEAR::isError($mdb2type_info)) {
  424.                return $mdb2type_info;
  425.             }
  426.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  427.  
  428.             if ($mode MDB2_TABLEINFO_ORDER{
  429.                 $res['order'][$res[$i]['name']] $i;
  430.             }
  431.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  432.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  433.             }
  434.         }
  435.  
  436.         return $res;
  437.     }
  438. }
  439.  
  440. ?>

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