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.51 2006/06/15 08:07:22 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 (!empty($column['notnull'])) {
  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 (!empty($column['autoincrement'])) {
  173.                     $autoincrement = true;
  174.                 }
  175.  
  176.                 $definition[0= array('notnull' => $notnull);
  177.                 if ($length > 0{
  178.                     $definition[0]['length'$length;
  179.                 }
  180.                 if (!is_null($unsigned)) {
  181.                     $definition[0]['unsigned'$unsigned;
  182.                 }
  183.                 if (!is_null($fixed)) {
  184.                     $definition[0]['fixed'$fixed;
  185.                 }
  186.                 if ($default !== false{
  187.                     $definition[0]['default'$default;
  188.                 }
  189.                 if ($autoincrement !== false{
  190.                     $definition[0]['autoincrement'$autoincrement;
  191.                 }
  192.                 foreach ($types as $key => $type{
  193.                     $definition[$key$definition[0];
  194.                     $definition[$key]['type'$type;
  195.                 }
  196.                 return $definition;
  197.             }
  198.         }
  199.  
  200.         return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  201.             'getTableFieldDefinition: it was not specified an existing table column');
  202.     }
  203.  
  204.     // }}}
  205.     // {{{ getTableIndexDefinition()
  206.  
  207.     /**
  208.      * Get the stucture of an index into an array
  209.      *
  210.      * @param string    $table      name of table that should be used in method
  211.      * @param string    $index_name name of index that should be used in method
  212.      * @return mixed data array on success, a MDB2 error on failure
  213.      * @access public
  214.      */
  215.     function getTableIndexDefinition($table$index_name)
  216.     {
  217.         $db =$this->getDBInstance();
  218.         if (PEAR::isError($db)) {
  219.             return $db;
  220.         }
  221.  
  222.         $index_name $db->getIndexName($index_name);
  223.         $index_name $db->quote($index_name'text');
  224.         $table $db->quote($table'text');
  225.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  226.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  227.             $query.= "LOWER(name)=".strtolower($index_name)." AND LOWER(tbl_name)=".strtolower($table);
  228.         else {
  229.             $query.= "name=$index_name AND tbl_name=$table";
  230.         }
  231.         $query.= " AND sql NOT NULL ORDER BY name";
  232.         $sql $db->queryOne($query'text');
  233.         if (PEAR::isError($sql)) {
  234.             return $sql;
  235.         }
  236.         if (!$sql{
  237.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  238.                 'getTableIndexDefinition: it was not specified an existing table index');
  239.         }
  240.  
  241.         $sql strtolower($sql);
  242.         $start_pos strpos($sql'(');
  243.         $end_pos strrpos($sql')');
  244.         $column_names substr($sql$start_pos+1$end_pos-$start_pos-1);
  245.         $column_names split(','$column_names);
  246.  
  247.         if (preg_match("/^create unique/"$sql)) {
  248.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  249.                 'getTableIndexDefinition: it was not specified an existing table index');
  250.         }
  251.  
  252.         $definition = array();
  253.         $count count($column_names);
  254.         for ($i=0; $i<$count; ++$i{
  255.             $column_name strtok($column_names[$i]," ");
  256.             $collation strtok(" ");
  257.             $definition['fields'][$column_name= array();
  258.             if (!empty($collation)) {
  259.                 $definition['fields'][$column_name]['sorting'=
  260.                     ($collation=='ASC' 'ascending' 'descending');
  261.             }
  262.         }
  263.  
  264.         if (empty($definition['fields'])) {
  265.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  266.                 'getTableIndexDefinition: it was not specified an existing table index');
  267.         }
  268.         return $definition;
  269.     }
  270.  
  271.     // }}}
  272.     // {{{ getTableConstraintDefinition()
  273.  
  274.     /**
  275.      * Get the stucture of a constraint into an array
  276.      *
  277.      * @param string    $table      name of table that should be used in method
  278.      * @param string    $index_name name of index that should be used in method
  279.      * @return mixed data array on success, a MDB2 error on failure
  280.      * @access public
  281.      */
  282.     function getTableConstraintDefinition($table$index_name)
  283.     {
  284.         $db =$this->getDBInstance();
  285.         if (PEAR::isError($db)) {
  286.             return $db;
  287.         }
  288.  
  289.         $index_name $db->getIndexName($index_name);
  290.         $table $db->quote($table'text');
  291.         $index_name $db->quote($index_name'text');
  292.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  293.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  294.             $query.= 'LOWER(name)='.strtolower($index_name).' AND LOWER(tbl_name)='.strtolower($table);
  295.         else {
  296.             $query.= "name=$index_name AND tbl_name=$table";
  297.         }
  298.         $query.= " AND sql NOT NULL ORDER BY name";
  299.         $sql $db->queryOne($query'text');
  300.         if (PEAR::isError($sql)) {
  301.             return $sql;
  302.         }
  303.         if (!$sql{
  304.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  305.                 'getTableConstraintDefinition: it was not specified an existing table index');
  306.         }
  307.  
  308.         $sql strtolower($sql);
  309.         $start_pos strpos($sql'(');
  310.         $end_pos strrpos($sql')');
  311.         $column_names substr($sql$start_pos+1$end_pos-$start_pos-1);
  312.         $column_names split(','$column_names);
  313.  
  314.         if (!preg_match("/^create unique/"$sql)) {
  315.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  316.                 'getTableConstraintDefinition: it was not specified an existing table constraint');
  317.         }
  318.  
  319.         $definition = array();
  320.         $definition['unique'= true;
  321.         $count count($column_names);
  322.         for ($i=0; $i<$count; ++$i{
  323.             $column_name strtok($column_names[$i]," ");
  324.             $collation strtok(" ");
  325.             $definition['fields'][$column_name= array();
  326.             if (!empty($collation)) {
  327.                 $definition['fields'][$column_name]['sorting'=
  328.                     ($collation=='ASC' 'ascending' 'descending');
  329.             }
  330.         }
  331.  
  332.         if (empty($definition['fields'])) {
  333.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  334.                 'getTableConstraintDefinition: it was not specified an existing table constraint');
  335.         }
  336.         return $definition;
  337.     }
  338.  
  339.     // }}}
  340.     // {{{ tableInfo()
  341.  
  342.     /**
  343.      * Returns information about a table
  344.      *
  345.      * @param string         $result  a string containing the name of a table
  346.      * @param int            $mode    a valid tableInfo mode
  347.      *
  348.      * @return array  an associative array with the information requested.
  349.      *                  A MDB2_Error object on failure.
  350.      *
  351.      * @see MDB2_Driver_Common::tableInfo()
  352.      * @since Method available since Release 1.7.0
  353.      */
  354.     function tableInfo($result$mode = null)
  355.     {
  356.         $db =$this->getDBInstance();
  357.         if (PEAR::isError($db)) {
  358.             return $db;
  359.         }
  360.  
  361.         if (!is_string($result)) {
  362.             return $db->raiseError(MDB2_ERROR_NOT_CAPABLEnullnull,
  363.                 'This DBMS can not obtain tableInfo from result sets');
  364.         }
  365.  
  366.         /*
  367.          * Probably received a table name.
  368.          * Create a result resource identifier.
  369.          */
  370.         $id $db->queryAll('PRAGMA table_info('.$db->quote($result'text').');'nullMDB2_FETCHMODE_ASSOC);
  371.         $got_string = true;
  372.  
  373.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  374.             if ($db->options['field_case'== CASE_LOWER{
  375.                 $case_func 'strtolower';
  376.             else {
  377.                 $case_func 'strtoupper';
  378.             }
  379.         else {
  380.             $case_func 'strval';
  381.         }
  382.  
  383.         $count count($id);
  384.         $res   = array();
  385.  
  386.         if ($mode{
  387.             $res['num_fields'$count;
  388.         }
  389.  
  390.         $db->loadModule('Datatype'nulltrue);
  391.         for ($i = 0; $i $count$i++{
  392.             $id[$iarray_change_key_case($id[$i]CASE_LOWER);
  393.             if (strpos($id[$i]['type']'('!== false{
  394.                 $bits explode('('$id[$i]['type']);
  395.                 $type $bits[0];
  396.                 $len  rtrim($bits[1],')');
  397.             else {
  398.                 $type $id[$i]['type'];
  399.                 $len  = 0;
  400.             }
  401.  
  402.             $flags '';
  403.             if ($id[$i]['pk']{
  404.                 $flags.= 'primary_key ';
  405.             }
  406.             if ($id[$i]['notnull']{
  407.                 $flags.= 'not_null ';
  408.             }
  409.             if ($id[$i]['dflt_value'!== null{
  410.                 $flags.= 'default_' rawurlencode($id[$i]['dflt_value']);
  411.             }
  412.             $flags trim($flags);
  413.  
  414.             $res[$i= array(
  415.                 'table' => $case_func($result),
  416.                 'name'  => $case_func($id[$i]['name']),
  417.                 'type'  => $type,
  418.                 'length'   => $len,
  419.                 'flags' => $flags,
  420.             );
  421.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  422.             if (PEAR::isError($mdb2type_info)) {
  423.                return $mdb2type_info;
  424.             }
  425.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  426.  
  427.             if ($mode MDB2_TABLEINFO_ORDER{
  428.                 $res['order'][$res[$i]['name']] $i;
  429.             }
  430.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  431.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  432.             }
  433.         }
  434.  
  435.         return $res;
  436.     }
  437. }
  438.  
  439. ?>

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