MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file sqlite.php

Documentation is available at sqlite.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  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.4 2004/04/09 10:41:21 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.     // {{{ constructor
  60.  
  61.     /**
  62.      * Constructor
  63.      */
  64.     function MDB2_Driver_Reverse_sqlite($db_index)
  65.     {
  66.         $this->MDB2_Driver_Reverse_Common($db_index);
  67.     }
  68.  
  69.     // }}}
  70.     // {{{ getTableFieldDefinition()
  71.  
  72.     /**
  73.      * get the stucture of a field into an array
  74.      *
  75.      * @param string    $table         name of table that should be used in method
  76.      * @param string    $field_name     name of field that should be used in method
  77.      * @return mixed data array on success, a MDB2 error on failure
  78.      * @access public
  79.      */
  80.     function getTableFieldDefinition($table$field_name)
  81.     {
  82.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  83.         $query = "SELECT sql FROM sqlite_master WHERE type='table' AND name='$table'";
  84.         $result $db->query($query);
  85.         if (MDB2::isError($result)) {
  86.             return $result;
  87.         }
  88.         $columns $result->getColumnNames();
  89.         if (MDB2::isError($columns)) {
  90.             return $columns;
  91.         }
  92.         if (!isset($columns[$column 'sql'])) {
  93.             return $db->raiseError(MDB2_ERRORnullnull,
  94.                 'getTableFieldDefinition: show columns does not return the column '.$column);
  95.         }
  96.         $sql=$result->fetch();
  97.         if (MDB2::isError($columns $this->_getTableColumns($sql))) {
  98.             return $columns;
  99.         }
  100.         $count count($columns);
  101.  
  102.         for ($i=0; $i<$count; ++$i{
  103.             if ($field_name == $columns[$i]['name']{
  104.                 $db_type $columns[$i]['type'];
  105.                 $type = array();
  106.                 switch ($db_type{
  107.                     case 'tinyint':
  108.                     case 'smallint':
  109.                     case 'mediumint':
  110.                     case 'int':
  111.                     case 'integer':
  112.                     case 'bigint':
  113.                         $type[0'integer';
  114.                         if ($columns[$i]['length'&& $columns[$i]['length'== '1'{
  115.                             $type[1'boolean';
  116.                             if (preg_match('/^[is|has]/'$field_name)) {
  117.                                 $type array_reverse($type);
  118.                             }
  119.                         }
  120.                         break;
  121.                     case 'tinytext':
  122.                     case 'mediumtext':
  123.                     case 'longtext':
  124.                     case 'text':
  125.                     case 'char':
  126.                     case 'varchar':
  127.                     case "varchar2":
  128.                         $type[0'text';
  129.                         if (isset($columns[$i]['length']&& $columns[$i]['length'== '1'{
  130.                             $type[1'boolean';
  131.                             if (preg_match('/[is|has]/'$field_name)) {
  132.                                 $type array_reverse($type);
  133.                             }
  134.                         elseif (strstr($db_type'text'))
  135.                             $type[1'clob';
  136.                         break;
  137.                     case 'enum':
  138.                         preg_match_all('/\'.+\'/U',$row[$type_column]$matches);
  139.                         $length = 0;
  140.                         if (is_array($matches)) {
  141.                             foreach ($matches[0as $value{
  142.                                 $length max($lengthstrlen($value)-2);
  143.                             }
  144.                         }
  145.                         unset($decimal);
  146.                     case 'set':
  147.                         $type[0'text';
  148.                         $type[1'integer';
  149.                         break;
  150.                     case 'date':
  151.                         $type[0'date';
  152.                         break;
  153.                     case 'datetime':
  154.                     case 'timestamp':
  155.                         $type[0'timestamp';
  156.                         break;
  157.                     case 'time':
  158.                         $type[0'time';
  159.                         break;
  160.                     case 'float':
  161.                     case 'double':
  162.                     case 'real':
  163.                         $type[0'float';
  164.                         break;
  165.                     case 'decimal':
  166.                     case 'numeric':
  167.                         $type[0'decimal';
  168.                         break;
  169.                     case 'tinyblob':
  170.                     case 'mediumblob':
  171.                     case 'longblob':
  172.                     case 'blob':
  173.                         $type[0'text';
  174.                         break;
  175.                     case 'year':
  176.                         $type[0'integer';
  177.                         $type[1'date';
  178.                         break;
  179.                     default:
  180.                         return $db->raiseError(MDB2_ERRORnullnull,
  181.                             'getTableFieldDefinition: unknown database attribute type');
  182.                 }
  183.                 for ($field_choices = array()$datatype = 0;
  184.                     $datatype count($type);
  185.                     $datatype++
  186.                 {
  187.                     $field_choices[$datatype= array('type' => $type[$datatype]);
  188.                     if (isset($columns[$i]['notnull'])) {
  189.                         $field_choices[$datatype]['notnull'= true;
  190.                     }
  191.                     if (isset($columns[$i]['default'])) {
  192.                         $field_choices[$datatype]["default"]=$columns[$i]['default'];
  193.                     }
  194.                     if ($type[$datatype!= 'boolean'
  195.                         && $type[$datatype!= 'time'
  196.                         && $type[$datatype!= 'date'
  197.                         && $type[$datatype!= 'timestamp')
  198.                     {
  199.                         if (isset($columns[$i]['length'])) {
  200.                             $field_choices[$datatype]['length'$columns[$i]['length'];
  201.                         }
  202.                     }
  203.                 }
  204.                 $definition[0$field_choices;
  205. /*
  206.                 if (isset($columns['extra'])
  207.                     && isset($row[$columns['extra']])
  208.                     && $row[$columns['extra']] == 'auto_increment')
  209.                 {
  210.                     $implicit_sequence = array();
  211.                     $implicit_sequence['on'] = array();
  212.                     $implicit_sequence['on']['table'] = $table;
  213.                     $implicit_sequence['on']['field'] = $field_name;
  214.                     $definition[1]['name'] = $table.'_'.$field_name;
  215.                     $definition[1]['definition'] = $implicit_sequence;
  216.                 }
  217.                 if (isset($columns['key'])
  218.                     && isset($row[$columns['key']])
  219.                     && $row[$columns['key']] == 'PRI')
  220.                 {
  221.                     // check that its not just a unique field
  222.                     if (MDB2::isError($indexes = $db->query("SHOW INDEX FROM $table", null, MDB2_FETCHMODE_ASSOC))) {
  223.                         return $indexes;
  224.                     }
  225.                     $is_primary = false;
  226.                     foreach ($indexes as $index) {
  227.                         if ($index['key_name'] == 'PRIMARY' && $index['column_name'] == $field_name) {
  228.                             $is_primary = true;
  229.                             break;
  230.                         }
  231.                     }
  232.                     if ($is_primary) {
  233.                         $implicit_index = array();
  234.                         $implicit_index['unique'] = true;
  235.                         $implicit_index['fields'][$field_name] = '';
  236.                         $definition[2]['name'] = $field_name;
  237.                         $definition[2]['definition'] = $implicit_index;
  238.                     }
  239.                 }
  240. */
  241.                 return $definition;
  242.             }
  243.         }
  244.         $result->free();
  245.  
  246.         if (MDB2::isError($row)) {
  247.             return $row;
  248.         }
  249.         return $db->raiseError(MDB2_ERRORnullnull,
  250.             'getTableFieldDefinition: it was not specified an existing table column');
  251.     }
  252.  
  253.     // }}}
  254.     // {{{ getTableIndexDefinition()
  255.  
  256.     /**
  257.      * get the stucture of an index into an array
  258.      *
  259.      * @param string    $table      name of table that should be used in method
  260.      * @param string    $index_name name of index that should be used in method
  261.      * @return mixed data array on success, a MDB2 error on failure
  262.      * @access public
  263.      */
  264.     function getTableIndexDefinition($table$index_name)
  265.     {
  266.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  267.         if ($index_name == 'PRIMARY'{
  268.             return $db->raiseError(MDB2_ERRORnullnull,
  269.                 'getTableIndexDefinition: PRIMARY is an hidden index');
  270.         }
  271.         $query = "SELECT sql FROM sqlite_master WHERE type='index' AND name='$index' AND tbl_name='$table' AND sql NOT NULL ORDER BY name";
  272.         $result $db->query($query);
  273.         if (MDB2::isError($result)) {
  274.             return $result;
  275.         }
  276.         $columns $result->getColumnNames();
  277.         $column 'sql';
  278.         if (!isset($columns[$column])) {
  279.             $result->free();
  280.             return $db->raiseError('getTableIndexDefinition: show index does not return the table creation sql');
  281.         }
  282.  
  283.         $sql strtolower($result->fetch());
  284.         $unique strstr($sql" unique ");
  285.         $key_name $index;
  286.         $start_pos strpos($sql"(");
  287.         $end_pos strrpos($sql")");
  288.         $column_names substr($sql$start_pos+1$end_pos-$start_pos-1);
  289.         $column_names split(","$column_names);
  290.  
  291.         $definition = array();
  292.         if ($unique{
  293.             $definition['unique'= true;
  294.         }
  295.         $count count($column_names);
  296.         for ($i=0; $i<$count; ++$i{
  297.             $column_name strtok($column_names[$i]," ");
  298.             $collation strtok(" ");
  299.             $definition['fields'][$column_name= array();
  300.             if (!empty($collation)) {
  301.                 $definition['fields'][$column_name]['sorting'($collation=='ASC' 'ascending' 'descending');
  302.             }
  303.         }
  304.  
  305.         $result->free();
  306.         if (!isset($definition['fields'])) {
  307.             return $db->raiseError(MDB2_ERRORnullnull,
  308.                 'getTableIndexDefinition: it was not specified an existing table index');
  309.         }
  310.         return $definition;
  311.     }
  312. }
  313.  
  314. ?>

Documentation generated on Mon, 11 Mar 2019 10:15:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.