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

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