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.53 2006/07/17 16:12:31 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'__FUNCTION__);
  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].'"'__FUNCTION__);
  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'nativetype' => $column['type']);
  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.                     $definition[$key]['mdb2type'$type;
  196.                 }
  197.                 return $definition;
  198.             }
  199.         }
  200.  
  201.         return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  202.             'it was not specified an existing table column'__FUNCTION__);
  203.     }
  204.  
  205.     // }}}
  206.     // {{{ getTableIndexDefinition()
  207.  
  208.     /**
  209.      * Get the stucture of an index into an array
  210.      *
  211.      * @param string    $table      name of table that should be used in method
  212.      * @param string    $index_name name of index that should be used in method
  213.      * @return mixed data array on success, a MDB2 error on failure
  214.      * @access public
  215.      */
  216.     function getTableIndexDefinition($table$index_name)
  217.     {
  218.         $db =$this->getDBInstance();
  219.         if (PEAR::isError($db)) {
  220.             return $db;
  221.         }
  222.  
  223.         $index_name $db->getIndexName($index_name);
  224.         $index_name $db->quote($index_name'text');
  225.         $table $db->quote($table'text');
  226.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  227.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  228.             $query.= "LOWER(name)=".strtolower($index_name)." AND LOWER(tbl_name)=".strtolower($table);
  229.         else {
  230.             $query.= "name=$index_name AND tbl_name=$table";
  231.         }
  232.         $query.= " AND sql NOT NULL ORDER BY name";
  233.         $sql $db->queryOne($query'text');
  234.         if (PEAR::isError($sql)) {
  235.             return $sql;
  236.         }
  237.         if (!$sql{
  238.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  239.                 'it was not specified an existing table index'__FUNCTION__);
  240.         }
  241.  
  242.         $sql strtolower($sql);
  243.         $start_pos strpos($sql'(');
  244.         $end_pos strrpos($sql')');
  245.         $column_names substr($sql$start_pos+1$end_pos-$start_pos-1);
  246.         $column_names split(','$column_names);
  247.  
  248.         if (preg_match("/^create unique/"$sql)) {
  249.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  250.                 'it was not specified an existing table index'__FUNCTION__);
  251.         }
  252.  
  253.         $definition = array();
  254.         $count count($column_names);
  255.         for ($i=0; $i<$count; ++$i{
  256.             $column_name strtok($column_names[$i]," ");
  257.             $collation strtok(" ");
  258.             $definition['fields'][$column_name= array();
  259.             if (!empty($collation)) {
  260.                 $definition['fields'][$column_name]['sorting'=
  261.                     ($collation=='ASC' 'ascending' 'descending');
  262.             }
  263.         }
  264.  
  265.         if (empty($definition['fields'])) {
  266.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  267.                 'it was not specified an existing table index'__FUNCTION__);
  268.         }
  269.         return $definition;
  270.     }
  271.  
  272.     // }}}
  273.     // {{{ getTableConstraintDefinition()
  274.  
  275.     /**
  276.      * Get the stucture of a constraint into an array
  277.      *
  278.      * @param string    $table      name of table that should be used in method
  279.      * @param string    $index_name name of index that should be used in method
  280.      * @return mixed data array on success, a MDB2 error on failure
  281.      * @access public
  282.      */
  283.     function getTableConstraintDefinition($table$index_name)
  284.     {
  285.         $db =$this->getDBInstance();
  286.         if (PEAR::isError($db)) {
  287.             return $db;
  288.         }
  289.  
  290.         $index_name $db->getIndexName($index_name);
  291.         $table $db->quote($table'text');
  292.         $index_name $db->quote($index_name'text');
  293.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  294.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  295.             $query.= 'LOWER(name)='.strtolower($index_name).' AND LOWER(tbl_name)='.strtolower($table);
  296.         else {
  297.             $query.= "name=$index_name AND tbl_name=$table";
  298.         }
  299.         $query.= " AND sql NOT NULL ORDER BY name";
  300.         $sql $db->queryOne($query'text');
  301.         if (PEAR::isError($sql)) {
  302.             return $sql;
  303.         }
  304.         if (!$sql{
  305.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  306.                 'it was not specified an existing table index'__FUNCTION__);
  307.         }
  308.  
  309.         $sql strtolower($sql);
  310.         $start_pos strpos($sql'(');
  311.         $end_pos strrpos($sql')');
  312.         $column_names substr($sql$start_pos+1$end_pos-$start_pos-1);
  313.         $column_names split(','$column_names);
  314.  
  315.         if (!preg_match("/^create unique/"$sql)) {
  316.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  317.                 'it was not specified an existing table constraint'__FUNCTION__);
  318.         }
  319.  
  320.         $definition = array();
  321.         $definition['unique'= true;
  322.         $count count($column_names);
  323.         for ($i=0; $i<$count; ++$i{
  324.             $column_name strtok($column_names[$i]," ");
  325.             $collation strtok(" ");
  326.             $definition['fields'][$column_name= array();
  327.             if (!empty($collation)) {
  328.                 $definition['fields'][$column_name]['sorting'=
  329.                     ($collation=='ASC' 'ascending' 'descending');
  330.             }
  331.         }
  332.  
  333.         if (empty($definition['fields'])) {
  334.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  335.                 'it was not specified an existing table constraint'__FUNCTION__);
  336.         }
  337.         return $definition;
  338.     }
  339.  
  340.     // }}}
  341.     // {{{ tableInfo()
  342.  
  343.     /**
  344.      * Returns information about a table
  345.      *
  346.      * @param string         $result  a string containing the name of a table
  347.      * @param int            $mode    a valid tableInfo mode
  348.      *
  349.      * @return array  an associative array with the information requested.
  350.      *                  A MDB2_Error object on failure.
  351.      *
  352.      * @see MDB2_Driver_Common::tableInfo()
  353.      * @since Method available since Release 1.7.0
  354.      */
  355.     function tableInfo($result$mode = null)
  356.     {
  357.         if (is_string($result)) {
  358.            return parent::tableInfo($result$mode);
  359.         }
  360.  
  361.         $db =$this->getDBInstance();
  362.         if (PEAR::isError($db)) {
  363.             return $db;
  364.         }
  365.  
  366.         return $db->raiseError(MDB2_ERROR_NOT_CAPABLEnullnull,
  367.            'This DBMS can not obtain tableInfo from result sets'__FUNCTION__);
  368.     }
  369. }
  370.  
  371. ?>

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