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.11 2005/03/04 12:37:58 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.     // {{{ getTableFieldDefinition()
  59.  
  60.     /**
  61.      * get the stucture of a field into an array
  62.      *
  63.      * @param string    $table         name of table that should be used in method
  64.      * @param string    $field_name     name of field that should be used in method
  65.      * @return mixed data array on success, a MDB2 error on failure
  66.      * @access public
  67.      */
  68.     function getTableFieldDefinition($table$field_name)
  69.     {
  70.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  71.         $query = "SELECT sql FROM sqlite_master WHERE type='table' AND name='$table'";
  72.         $result $db->query($query);
  73.         if (MDB2::isError($result)) {
  74.             return $result;
  75.         }
  76.         $columns $result->getColumnNames();
  77.         if (MDB2::isError($columns)) {
  78.             return $columns;
  79.         }
  80.         if (!isset($columns[$column 'sql'])) {
  81.             return $db->raiseError(MDB2_ERRORnullnull,
  82.                 'getTableFieldDefinition: show columns does not return the column '.$column);
  83.         }
  84.         $query $result->fetchOne();
  85.         if (MDB2::isError($columns $this->_getTableColumns($query))) {
  86.             return $columns;
  87.         }
  88.         $count count($columns);
  89.  
  90.         for ($i=0; $i<$count; ++$i{
  91.             if ($field_name == $columns[$i]['name']{
  92.                 $db_type $columns[$i]['type'];
  93.                 $type = array();
  94.                 switch ($db_type{
  95.                 case 'tinyint':
  96.                 case 'smallint':
  97.                 case 'mediumint':
  98.                 case 'int':
  99.                 case 'integer':
  100.                 case 'bigint':
  101.                     $type[0'integer';
  102.                     if ($columns[$i]['length'&& $columns[$i]['length'== '1'{
  103.                         $type[1'boolean';
  104.                         if (preg_match('/^[is|has]/'$field_name)) {
  105.                             $type array_reverse($type);
  106.                         }
  107.                     }
  108.                     break;
  109.                 case 'tinytext':
  110.                 case 'mediumtext':
  111.                 case 'longtext':
  112.                 case 'text':
  113.                 case 'char':
  114.                 case 'varchar':
  115.                 case "varchar2":
  116.                     $type[0'text';
  117.                     if (isset($columns[$i]['length']&& $columns[$i]['length'== '1'{
  118.                         $type[1'boolean';
  119.                         if (preg_match('/[is|has]/'$field_name)) {
  120.                             $type array_reverse($type);
  121.                         }
  122.                     elseif (strstr($db_type'text'))
  123.                         $type[1'clob';
  124.                     break;
  125.                 case 'enum':
  126.                     preg_match_all('/\'.+\'/U',$row[$type_column]$matches);
  127.                     $length = 0;
  128.                     if (is_array($matches)) {
  129.                         foreach ($matches[0as $value{
  130.                             $length max($lengthstrlen($value)-2);
  131.                         }
  132.                     }
  133.                     unset($decimal);
  134.                 case 'set':
  135.                     $type[0'text';
  136.                     $type[1'integer';
  137.                     break;
  138.                 case 'date':
  139.                     $type[0'date';
  140.                     break;
  141.                 case 'datetime':
  142.                 case 'timestamp':
  143.                     $type[0'timestamp';
  144.                     break;
  145.                 case 'time':
  146.                     $type[0'time';
  147.                     break;
  148.                 case 'float':
  149.                 case 'double':
  150.                 case 'real':
  151.                     $type[0'float';
  152.                     break;
  153.                 case 'decimal':
  154.                 case 'numeric':
  155.                     $type[0'decimal';
  156.                     break;
  157.                 case 'tinyblob':
  158.                 case 'mediumblob':
  159.                 case 'longblob':
  160.                 case 'blob':
  161.                     $type[0'text';
  162.                     break;
  163.                 case 'year':
  164.                     $type[0'integer';
  165.                     $type[1'date';
  166.                     break;
  167.                 default:
  168.                     return $db->raiseError(MDB2_ERRORnullnull,
  169.                         'getTableFieldDefinition: unknown database attribute type');
  170.                 }
  171.                 for ($field_choices = array()$datatype = 0;
  172.                     $datatype count($type);
  173.                     $datatype++
  174.                 {
  175.                     $field_choices[$datatype= array('type' => $type[$datatype]);
  176.                     if (isset($columns[$i]['notnull'])) {
  177.                         $field_choices[$datatype]['notnull'= true;
  178.                     }
  179.                     if (isset($columns[$i]['default'])) {
  180.                         $field_choices[$datatype]["default"]=$columns[$i]['default'];
  181.                     }
  182.                     if ($type[$datatype!= 'boolean'
  183.                         && $type[$datatype!= 'time'
  184.                         && $type[$datatype!= 'date'
  185.                         && $type[$datatype!= 'timestamp'
  186.                     {
  187.                         if (isset($columns[$i]['length'])) {
  188.                             $field_choices[$datatype]['length'$columns[$i]['length'];
  189.                         }
  190.                     }
  191.                 }
  192.                 $definition[0$field_choices;
  193. /*
  194.                 if (isset($columns['extra'])
  195.                     && isset($row[$columns['extra']])
  196.                     && $row[$columns['extra']] == 'auto_increment'
  197.                 ) {
  198.                     $implicit_sequence = array();
  199.                     $implicit_sequence['on'] = array();
  200.                     $implicit_sequence['on']['table'] = $table;
  201.                     $implicit_sequence['on']['field'] = $field_name;
  202.                     $definition[1]['name'] = $table.'_'.$field_name;
  203.                     $definition[1]['definition'] = $implicit_sequence;
  204.                 }
  205.                 if (isset($columns['key'])
  206.                     && isset($row[$columns['key']])
  207.                     && $row[$columns['key']] == 'PRI'
  208.                 ) {
  209.                     // check that its not just a unique field
  210.                     if (MDB2::isError($indexes = $db->query("SHOW INDEX FROM $table", null, MDB2_FETCHMODE_ASSOC))) {
  211.                         return $indexes;
  212.                     }
  213.                     $is_primary = false;
  214.                     foreach ($indexes as $index) {
  215.                         if ($index['key_name'] == 'PRIMARY' && $index['column_name'] == $field_name) {
  216.                             $is_primary = true;
  217.                             break;
  218.                         }
  219.                     }
  220.                     if ($is_primary) {
  221.                         $implicit_index = array();
  222.                         $implicit_index['unique'] = true;
  223.                         $implicit_index['fields'][$field_name] = '';
  224.                         $definition[2]['name'] = $field_name;
  225.                         $definition[2]['definition'] = $implicit_index;
  226.                     }
  227.                 }
  228. */
  229.                 return $definition;
  230.             }
  231.         }
  232.         $result->free();
  233.  
  234.         if (MDB2::isError($row)) {
  235.             return $row;
  236.         }
  237.         return $db->raiseError(MDB2_ERRORnullnull,
  238.             'getTableFieldDefinition: it was not specified an existing table column');
  239.     }
  240.  
  241.     // }}}
  242.     // {{{ getTableIndexDefinition()
  243.  
  244.     /**
  245.      * get the stucture of an index 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 getTableIndexDefinition($table$index_name)
  253.     {
  254.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  255.         if ($index_name == 'PRIMARY'{
  256.             return $db->raiseError(MDB2_ERRORnullnull,
  257.                 'getTableIndexDefinition: PRIMARY is an hidden index');
  258.         }
  259.         $query = "SELECT sql FROM sqlite_master WHERE type='index' AND name='$index' AND tbl_name='$table' AND sql NOT NULL ORDER BY name";
  260.         $result $db->query($query);
  261.         if (MDB2::isError($result)) {
  262.             return $result;
  263.         }
  264.         $columns $result->getColumnNames();
  265.         $column 'sql';
  266.         if (!isset($columns[$column])) {
  267.             $result->free();
  268.             return $db->raiseError('getTableIndexDefinition: show index does not return the table creation sql');
  269.         }
  270.  
  271.         $query strtolower($result->fetchOne());
  272.         $unique strstr($query' unique ');
  273.         $key_name $index;
  274.         $start_pos strpos($query'(');
  275.         $end_pos strrpos($query')');
  276.         $column_names substr($query$start_pos+1$end_pos-$start_pos-1);
  277.         $column_names split(','$column_names);
  278.  
  279.         $definition = array();
  280.         if ($unique{
  281.             $definition['unique'= true;
  282.         }
  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'($collation=='ASC' 'ascending' 'descending');
  290.             }
  291.         }
  292.  
  293.         $result->free();
  294.         if (!isset($definition['fields'])) {
  295.             return $db->raiseError(MDB2_ERRORnullnull,
  296.                 'getTableIndexDefinition: it was not specified an existing table index');
  297.         }
  298.         return $definition;
  299.     }
  300.  
  301.     // }}}
  302.     // {{{ tableInfo()
  303.  
  304.     /**
  305.      * Returns information about a table
  306.      *
  307.      * @param string         $result  a string containing the name of a table
  308.      * @param int            $mode    a valid tableInfo mode
  309.      *
  310.      * @return array  an associative array with the information requested.
  311.      *                  A MDB2_Error object on failure.
  312.      *
  313.      * @see MDB2_common::tableInfo()
  314.      * @since Method available since Release 1.7.0
  315.      */
  316.     function tableInfo($result$mode = null)
  317.     {
  318.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  319.         if (is_string($result)) {
  320.             /*
  321.              * Probably received a table name.
  322.              * Create a result resource identifier.
  323.              */
  324.             $id $db->queryAll("PRAGMA table_info('$result');"nullMDB2_FETCHMODE_ASSOC);
  325.             $got_string = true;
  326.         else {
  327.             return $db->raiseError(MDB2_ERROR_NOT_CAPABLEnullnull,
  328.                                      'This DBMS can not obtain tableInfo' .
  329.                                      ' from result sets');
  330.         }
  331.  
  332.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  333.             $case_func 'strtolower';
  334.         else {
  335.             $case_func 'strval';
  336.         }
  337.  
  338.         $count count($id);
  339.         $res   = array();
  340.  
  341.         if ($mode{
  342.             $res['num_fields'$count;
  343.         }
  344.  
  345.         for ($i = 0; $i $count$i++{
  346.             if (strpos($id[$i]['type']'('!== false{
  347.                 $bits explode('('$id[$i]['type']);
  348.                 $type $bits[0];
  349.                 $len  rtrim($bits[1],')');
  350.             else {
  351.                 $type $id[$i]['type'];
  352.                 $len  = 0;
  353.             }
  354.  
  355.             $flags '';
  356.             if ($id[$i]['pk']{
  357.                 $flags .= 'primary_key ';
  358.             }
  359.             if ($id[$i]['notnull']{
  360.                 $flags .= 'not_null ';
  361.             }
  362.             if ($id[$i]['dflt_value'!== null{
  363.                 $flags .= 'default_' rawurlencode($id[$i]['dflt_value']);
  364.             }
  365.             $flags trim($flags);
  366.  
  367.             $res[$i= array(
  368.                 'table' => $case_func($result),
  369.                 'name'  => $case_func($id[$i]['name']),
  370.                 'type'  => $type,
  371.                 'len'   => $len,
  372.                 'flags' => $flags,
  373.             );
  374.  
  375.             if ($mode MDB2_TABLEINFO_ORDER{
  376.                 $res['order'][$res[$i]['name']] $i;
  377.             }
  378.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  379.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  380.             }
  381.         }
  382.  
  383.         return $res;
  384.     }
  385. }
  386.  
  387. ?>

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