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

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