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@backendmedia.com>                         |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: sqlite.php,v 1.16 2005/04/04 08:09:59 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@backendmedia.com>
  56.  */
  57. {
  58.  
  59.     function _getTableColumns($query)
  60.     {
  61.         $start_pos strpos($query'(');
  62.         $end_pos strrpos($query')');
  63.         $column_def substr($query$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.                 $columns[$j]['default'$default;
  89.             }
  90.             if (isset($matches[10]&& strlen($matches[10])) {
  91.                 $columns[$j]['notnull'= true;
  92.             }
  93.             ++$j;
  94.         }
  95.         return $columns;
  96.     }
  97.  
  98.     // {{{ getTableFieldDefinition()
  99.  
  100.     /**
  101.      * get the stucture of a field into an array
  102.      *
  103.      * @param string    $table         name of table that should be used in method
  104.      * @param string    $field_name     name of field that should be used in method
  105.      * @return mixed data array on success, a MDB2 error on failure
  106.      * @access public
  107.      */
  108.     function getTableFieldDefinition($table$field_name)
  109.     {
  110.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  111.         $result $db->loadModule('Datatype');
  112.         if (PEAR::isError($result)) {
  113.             return $result;
  114.         }
  115.         $query = "SELECT sql FROM sqlite_master WHERE type='table' AND name='$table'";
  116.         $query $db->queryOne($query);
  117.         if (PEAR::isError($query)) {
  118.             return $query;
  119.         }
  120.         if (PEAR::isError($columns $this->_getTableColumns($query))) {
  121.             return $columns;
  122.         }
  123.         foreach ($columns as $column{
  124.             if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  125.                 $column['name'strtolower($column['name']);
  126.             else {
  127.                 $column array_change_key_case($columnCASE_LOWER);
  128.             }
  129.             if ($field_name == $column['name']{
  130.                 list($types$length$db->datatype->mapNativeDatatype($column);
  131.                 unset($notnull);
  132.                 if (isset($column['null']&& $column['null'!= 'YES'{
  133.                     $notnull = true;
  134.                 }
  135.                 unset($default);
  136.                 if (isset($column['default'])) {
  137.                     $default $column['default'];
  138.                 }
  139.                 $definition = array();
  140.                 foreach ($types as $key => $type{
  141.                     $definition[0][$key= array('type' => $type);
  142.                     if (isset($notnull)) {
  143.                         $definition[0][$key]['notnull'= true;
  144.                     }
  145.                     if (isset($default)) {
  146.                         $definition[0][$key]['default'$default;
  147.                     }
  148.                     if (isset($length)) {
  149.                         $definition[0][$key]['length'$length;
  150.                     }
  151.                 }
  152.                 // todo .. handle auto_inrement and primary keys
  153.                 return $definition;
  154.             }
  155.         }
  156.  
  157.         return $db->raiseError(MDB2_ERRORnullnull,
  158.             'getTableFieldDefinition: it was not specified an existing table column');
  159.     }
  160.  
  161.     // }}}
  162.     // {{{ getTableIndexDefinition()
  163.  
  164.     /**
  165.      * get the stucture of an index into an array
  166.      *
  167.      * @param string    $table      name of table that should be used in method
  168.      * @param string    $index_name name of index that should be used in method
  169.      * @return mixed data array on success, a MDB2 error on failure
  170.      * @access public
  171.      */
  172.     function getTableIndexDefinition($table$index_name)
  173.     {
  174.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  175.         if ($index_name == 'PRIMARY'{
  176.             return $db->raiseError(MDB2_ERRORnullnull,
  177.                 'getTableIndexDefinition: PRIMARY is an hidden index');
  178.         }
  179.         $query = "SELECT sql FROM sqlite_master WHERE type='index' AND name='$index_name' AND tbl_name='$table' AND sql NOT NULL ORDER BY name";
  180.         $result $db->query($query);
  181.         if (PEAR::isError($result)) {
  182.             return $result;
  183.         }
  184.         $columns $result->getColumnNames();
  185.         $column 'sql';
  186.         if (!isset($columns[$column])) {
  187.             $result->free();
  188.             return $db->raiseError('getTableIndexDefinition: show index does not return the table creation sql');
  189.         }
  190.  
  191.         $query strtolower($result->fetchOne());
  192.         $unique strstr($query' unique ');
  193.         $key_name $index_name;
  194.         $start_pos strpos($query'(');
  195.         $end_pos strrpos($query')');
  196.         $column_names substr($query$start_pos+1$end_pos-$start_pos-1);
  197.         $column_names split(','$column_names);
  198.  
  199.         $definition = array();
  200.         if ($unique{
  201.             $definition['unique'= true;
  202.         }
  203.         $count count($column_names);
  204.         for ($i=0; $i<$count; ++$i{
  205.             $column_name strtok($column_names[$i]," ");
  206.             $collation strtok(" ");
  207.             $definition['fields'][$column_name= array();
  208.             if (!empty($collation)) {
  209.                 $definition['fields'][$column_name]['sorting'($collation=='ASC' 'ascending' 'descending');
  210.             }
  211.         }
  212.  
  213.         $result->free();
  214.         if (!isset($definition['fields'])) {
  215.             return $db->raiseError(MDB2_ERRORnullnull,
  216.                 'getTableIndexDefinition: it was not specified an existing table index');
  217.         }
  218.         return $definition;
  219.     }
  220.  
  221.     // }}}
  222.     // {{{ tableInfo()
  223.  
  224.     /**
  225.      * Returns information about a table
  226.      *
  227.      * @param string         $result  a string containing the name of a table
  228.      * @param int            $mode    a valid tableInfo mode
  229.      *
  230.      * @return array  an associative array with the information requested.
  231.      *                  A MDB2_Error object on failure.
  232.      *
  233.      * @see MDB2_common::tableInfo()
  234.      * @since Method available since Release 1.7.0
  235.      */
  236.     function tableInfo($result$mode = null)
  237.     {
  238.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  239.         if (is_string($result)) {
  240.             /*
  241.              * Probably received a table name.
  242.              * Create a result resource identifier.
  243.              */
  244.             $id $db->queryAll("PRAGMA table_info('$result');"nullMDB2_FETCHMODE_ASSOC);
  245.             $got_string = true;
  246.         else {
  247.             return $db->raiseError(MDB2_ERROR_NOT_CAPABLEnullnull,
  248.                                      'This DBMS can not obtain tableInfo' .
  249.                                      ' from result sets');
  250.         }
  251.  
  252.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  253.             $case_func 'strtolower';
  254.         else {
  255.             $case_func 'strval';
  256.         }
  257.  
  258.         $count count($id);
  259.         $res   = array();
  260.  
  261.         if ($mode{
  262.             $res['num_fields'$count;
  263.         }
  264.  
  265.         for ($i = 0; $i $count$i++{
  266.             if (strpos($id[$i]['type']'('!== false{
  267.                 $bits explode('('$id[$i]['type']);
  268.                 $type $bits[0];
  269.                 $len  rtrim($bits[1],')');
  270.             else {
  271.                 $type $id[$i]['type'];
  272.                 $len  = 0;
  273.             }
  274.  
  275.             $flags '';
  276.             if ($id[$i]['pk']{
  277.                 $flags .= 'primary_key ';
  278.             }
  279.             if ($id[$i]['notnull']{
  280.                 $flags .= 'not_null ';
  281.             }
  282.             if ($id[$i]['dflt_value'!== null{
  283.                 $flags .= 'default_' rawurlencode($id[$i]['dflt_value']);
  284.             }
  285.             $flags trim($flags);
  286.  
  287.             $res[$i= array(
  288.                 'table' => $case_func($result),
  289.                 'name'  => $case_func($id[$i]['name']),
  290.                 'type'  => $type,
  291.                 'len'   => $len,
  292.                 'flags' => $flags,
  293.             );
  294.  
  295.             if ($mode MDB2_TABLEINFO_ORDER{
  296.                 $res['order'][$res[$i]['name']] $i;
  297.             }
  298.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  299.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  300.             }
  301.         }
  302.  
  303.         return $res;
  304.     }
  305. }
  306.  
  307. ?>

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