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

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