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

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