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. // | Authors: Lukas Smith <smith@pooteeweet.org>                          |
  43. // |          Lorenzo Alberton <l.alberton@quipo.it>                      |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: sqlite.php,v 1.62 2007/03/05 01:31:27 quipo Exp $
  47. //
  48.  
  49. require_once 'MDB2/Driver/Manager/Common.php';
  50.  
  51. /**
  52.  * MDB2 SQLite driver for the management modules
  53.  *
  54.  * @package MDB2
  55.  * @category Database
  56.  * @author  Lukas Smith <smith@pooteeweet.org>
  57.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  58.  */
  59. class MDB2_Driver_Manager_sqlite extends MDB2_Driver_Manager_Common
  60. {
  61.     // {{{ createDatabase()
  62.  
  63.     /**
  64.      * create a new database
  65.      *
  66.      * @param string $name name of the database that should be created
  67.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  68.      * @access public
  69.      */
  70.     function createDatabase($name)
  71.     {
  72.         $db =$this->getDBInstance();
  73.         if (PEAR::isError($db)) {
  74.             return $db;
  75.         }
  76.  
  77.         $database_file $db->_getDatabaseFile($name);
  78.         if (file_exists($database_file)) {
  79.             return $db->raiseError(MDB2_ERROR_ALREADY_EXISTSnullnull,
  80.                 'database already exists'__FUNCTION__);
  81.         }
  82.         $php_errormsg '';
  83.         $handle @sqlite_open($database_file$db->dsn['mode']$php_errormsg);
  84.         if (!$handle{
  85.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  86.                 (isset($php_errormsg$php_errormsg 'could not create the database file')__FUNCTION__);
  87.         }
  88.         @sqlite_close($handle);
  89.         return MDB2_OK;
  90.     }
  91.  
  92.     // }}}
  93.     // {{{ dropDatabase()
  94.  
  95.     /**
  96.      * drop an existing database
  97.      *
  98.      * @param string $name name of the database that should be dropped
  99.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  100.      * @access public
  101.      */
  102.     function dropDatabase($name)
  103.     {
  104.         $db =$this->getDBInstance();
  105.         if (PEAR::isError($db)) {
  106.             return $db;
  107.         }
  108.  
  109.         $database_file $db->_getDatabaseFile($name);
  110.         if (!@file_exists($database_file)) {
  111.             return $db->raiseError(MDB2_ERROR_CANNOT_DROPnullnull,
  112.                 'database does not exist'__FUNCTION__);
  113.         }
  114.         $result @unlink($database_file);
  115.         if (!$result{
  116.             return $db->raiseError(MDB2_ERROR_CANNOT_DROPnullnull,
  117.                 (isset($php_errormsg$php_errormsg 'could not remove the database file')__FUNCTION__);
  118.         }
  119.         return MDB2_OK;
  120.     }
  121.  
  122.     // }}}
  123.     // {{{ alterTable()
  124.  
  125.     /**
  126.      * alter an existing table
  127.      *
  128.      * @param string $name         name of the table that is intended to be changed.
  129.      * @param array $changes     associative array that contains the details of each type
  130.      *                              of change that is intended to be performed. The types of
  131.      *                              changes that are currently supported are defined as follows:
  132.      *
  133.      *                              name
  134.      *
  135.      *                                 New name for the table.
  136.      *
  137.      *                             add
  138.      *
  139.      *                                 Associative array with the names of fields to be added as
  140.      *                                  indexes of the array. The value of each entry of the array
  141.      *                                  should be set to another associative array with the properties
  142.      *                                  of the fields to be added. The properties of the fields should
  143.      *                                  be the same as defined by the MDB2 parser.
  144.      *
  145.      *
  146.      *                             remove
  147.      *
  148.      *                                 Associative array with the names of fields to be removed as indexes
  149.      *                                  of the array. Currently the values assigned to each entry are ignored.
  150.      *                                  An empty array should be used for future compatibility.
  151.      *
  152.      *                             rename
  153.      *
  154.      *                                 Associative array with the names of fields to be renamed as indexes
  155.      *                                  of the array. The value of each entry of the array should be set to
  156.      *                                  another associative array with the entry named name with the new
  157.      *                                  field name and the entry named Declaration that is expected to contain
  158.      *                                  the portion of the field declaration already in DBMS specific SQL code
  159.      *                                  as it is used in the CREATE TABLE statement.
  160.      *
  161.      *                             change
  162.      *
  163.      *                                 Associative array with the names of the fields to be changed as indexes
  164.      *                                  of the array. Keep in mind that if it is intended to change either the
  165.      *                                  name of a field and any other properties, the change array entries
  166.      *                                  should have the new names of the fields as array indexes.
  167.      *
  168.      *                                 The value of each entry of the array should be set to another associative
  169.      *                                  array with the properties of the fields to that are meant to be changed as
  170.      *                                  array entries. These entries should be assigned to the new values of the
  171.      *                                  respective properties. The properties of the fields should be the same
  172.      *                                  as defined by the MDB2 parser.
  173.      *
  174.      *                             Example
  175.      *                                 array(
  176.      *                                     'name' => 'userlist',
  177.      *                                     'add' => array(
  178.      *                                         'quota' => array(
  179.      *                                             'type' => 'integer',
  180.      *                                             'unsigned' => 1
  181.      *                                         )
  182.      *                                     ),
  183.      *                                     'remove' => array(
  184.      *                                         'file_limit' => array(),
  185.      *                                         'time_limit' => array()
  186.      *                                     ),
  187.      *                                     'change' => array(
  188.      *                                         'name' => array(
  189.      *                                             'length' => '20',
  190.      *                                             'definition' => array(
  191.      *                                                 'type' => 'text',
  192.      *                                                 'length' => 20,
  193.      *                                             ),
  194.      *                                         )
  195.      *                                     ),
  196.      *                                     'rename' => array(
  197.      *                                         'sex' => array(
  198.      *                                             'name' => 'gender',
  199.      *                                             'definition' => array(
  200.      *                                                 'type' => 'text',
  201.      *                                                 'length' => 1,
  202.      *                                                 'default' => 'M',
  203.      *                                             ),
  204.      *                                         )
  205.      *                                     )
  206.      *                                 )
  207.      *
  208.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  209.      *                              can perform the requested table alterations if the value is true or
  210.      *                              actually perform them otherwise.
  211.      * @access public
  212.      *
  213.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  214.      */
  215.     function alterTable($name$changes$check$options = array())
  216.     {
  217.         $db =$this->getDBInstance();
  218.         if (PEAR::isError($db)) {
  219.             return $db;
  220.         }
  221.  
  222.         foreach ($changes as $change_name => $change{
  223.             switch ($change_name{
  224.             case 'add':
  225.             case 'remove':
  226.             case 'change':
  227.             case 'name':
  228.             case 'rename':
  229.                 break;
  230.             default:
  231.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  232.                     'change type "'.$change_name.'" not yet supported'__FUNCTION__);
  233.             }
  234.         }
  235.  
  236.         if ($check{
  237.             return MDB2_OK;
  238.         }
  239.  
  240.         $db->loadModule('Reverse'nulltrue);
  241.  
  242.         // actually sqlite 2.x supports no ALTER TABLE at all .. so we emulate it
  243.         $fields $db->manager->listTableFields($name);
  244.         if (PEAR::isError($fields)) {
  245.             return $fields;
  246.         }
  247.  
  248.         $fields array_flip($fields);
  249.         foreach ($fields as $field => $value{
  250.             $definition $db->reverse->getTableFieldDefinition($name$field);
  251.             if (PEAR::isError($definition)) {
  252.                 return $definition;
  253.             }
  254.             $fields[$field$definition[0];
  255.         }
  256.  
  257.         $indexes $db->manager->listTableIndexes($name);
  258.         if (PEAR::isError($indexes)) {
  259.             return $indexes;
  260.         }
  261.  
  262.         $indexes array_flip($indexes);
  263.         foreach ($indexes as $index => $value{
  264.             $definition $db->reverse->getTableIndexDefinition($name$index);
  265.             if (PEAR::isError($definition)) {
  266.                 return $definition;
  267.             }
  268.             $indexes[$index$definition;
  269.         }
  270.  
  271.         $constraints $db->manager->listTableConstraints($name);
  272.         if (PEAR::isError($constraints)) {
  273.             return $constraints;
  274.         }
  275.  
  276.         $constraints array_flip($constraints);
  277.         foreach ($constraints as $constraint => $value{
  278.             if (!empty($definition['primary'])) {
  279.                 if (!array_key_exists('primary'$options)) {
  280.                     $options['primary'$definition['fields'];
  281.                 }
  282.             else {
  283.                 $definition $db->reverse->getTableConstraintDefinition($name$constraint);
  284.                 if (PEAR::isError($definition)) {
  285.                     return $definition;
  286.                 }
  287.                 $constraints[$constraint$definition;
  288.             }
  289.         }
  290.  
  291.         $name_new $name;
  292.         $create_order $select_fields array_keys($fields);
  293.         foreach ($changes as $change_name => $change{
  294.             switch ($change_name{
  295.             case 'add':
  296.                 foreach ($change as $field_name => $field{
  297.                     $fields[$field_name$field;
  298.                     $create_order[$field_name;
  299.                 }
  300.                 break;
  301.             case 'remove':
  302.                 foreach ($change as $field_name => $field{
  303.                     unset($fields[$field_name]);
  304.                     $select_fields array_diff($select_fieldsarray($field_name));
  305.                     $create_order array_diff($create_orderarray($field_name));
  306.                 }
  307.                 break;
  308.             case 'change':
  309.                 foreach ($change as $field_name => $field{
  310.                     $fields[$field_name$field['definition'];
  311.                 }
  312.                 break;
  313.             case 'name':
  314.                 $name_new $change;
  315.                 break;
  316.             case 'rename':
  317.                 foreach ($change as $field_name => $field{
  318.                     unset($fields[$field_name]);
  319.                     $fields[$field['name']] $field['definition'];
  320.                     $create_order[array_search($field_name$create_order)$field['name'];
  321.                 }
  322.                 break;
  323.             default:
  324.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  325.                     'change type "'.$change_name.'" not yet supported'__FUNCTION__);
  326.             }
  327.         }
  328.  
  329.         $data = null;
  330.         if (!empty($select_fields)) {
  331.             $query 'SELECT '.implode(', '$select_fields).' FROM '.$db->quoteIdentifier($nametrue);
  332.             $data $db->queryAll($querynullMDB2_FETCHMODE_ORDERED);
  333.         }
  334.  
  335.         $result $this->dropTable($name);
  336.         if (PEAR::isError($result)) {
  337.             return $result;
  338.         }
  339.  
  340.         $result $this->createTable($name_new$fields$options);
  341.         if (PEAR::isError($result)) {
  342.             return $result;
  343.         }
  344.  
  345.         foreach ($indexes as $index => $definition{
  346.             $this->createIndex($name_new$index$definition);
  347.         }
  348.  
  349.         foreach ($constraints as $constraint => $definition{
  350.             $this->createConstraint($name_new$constraint$definition);
  351.         }
  352.  
  353.         if (!empty($select_fields&& !empty($data)) {
  354.             $query 'INSERT INTO '.$db->quoteIdentifier($name_newtrue);
  355.             $query.= '('.implode(', 'array_slice(array_keys($fields)0count($select_fields))).')';
  356.             $query.=' VALUES (?'.str_repeat(', ?'(count($select_fields- 1)).')';
  357.             $stmt =$db->prepare($querynullMDB2_PREPARE_MANIP);
  358.             if (PEAR::isError($stmt)) {
  359.                 return $stmt;
  360.             }
  361.             foreach ($data as $row{
  362.                 $result $stmt->execute($row);
  363.                 if (PEAR::isError($result)) {
  364.                     return $result;
  365.                 }
  366.             }
  367.         }
  368.         return MDB2_OK;
  369.     }
  370.  
  371.     // }}}
  372.     // {{{ listDatabases()
  373.  
  374.     /**
  375.      * list all databases
  376.      *
  377.      * @return mixed array of database names on success, a MDB2 error on failure
  378.      * @access public
  379.      */
  380.     function listDatabases()
  381.     {
  382.         $db =$this->getDBInstance();
  383.         if (PEAR::isError($db)) {
  384.             return $db;
  385.         }
  386.  
  387.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  388.             'list databases is not supported'__FUNCTION__);
  389.     }
  390.  
  391.     // }}}
  392.     // {{{ listUsers()
  393.  
  394.     /**
  395.      * list all users
  396.      *
  397.      * @return mixed array of user names on success, a MDB2 error on failure
  398.      * @access public
  399.      */
  400.     function listUsers()
  401.     {
  402.         $db =$this->getDBInstance();
  403.         if (PEAR::isError($db)) {
  404.             return $db;
  405.         }
  406.  
  407.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  408.             'list databases is not supported'__FUNCTION__);
  409.     }
  410.  
  411.     // }}}
  412.     // {{{ listViews()
  413.  
  414.     /**
  415.      * list all views in the current database
  416.      *
  417.      * @return mixed array of view names on success, a MDB2 error on failure
  418.      * @access public
  419.      */
  420.     function listViews()
  421.     {
  422.         $db =$this->getDBInstance();
  423.         if (PEAR::isError($db)) {
  424.             return $db;
  425.         }
  426.  
  427.         $query "SELECT name FROM sqlite_master WHERE type='view' AND sql NOT NULL";
  428.         $result $db->queryCol($query);
  429.         if (PEAR::isError($result)) {
  430.             return $result;
  431.         }
  432.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  433.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  434.         }
  435.         return $result;
  436.     }
  437.  
  438.     // }}}
  439.     // {{{ listTableViews()
  440.  
  441.     /**
  442.      * list the views in the database that reference a given table
  443.      *
  444.      * @param string table for which all referenced views should be found
  445.      * @return mixed array of view names on success, a MDB2 error on failure
  446.      * @access public
  447.      */
  448.     function listTableViews($table)
  449.     {
  450.         $db =$this->getDBInstance();
  451.         if (PEAR::isError($db)) {
  452.             return $db;
  453.         }
  454.  
  455.         $query "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
  456.         $views $db->queryAll($queryarray('text''text')MDB2_FETCHMODE_ASSOC);
  457.         if (PEAR::isError($views)) {
  458.             return $views;
  459.         }
  460.         $result = array();
  461.         foreach ($views as $row{
  462.             if (preg_match("/^create view .* \bfrom\b\s+\b{$table}\b /i"$row['sql'])) {
  463.                 if (!empty($row['name'])) {
  464.                     $result[$row['name']] = true;
  465.                 }
  466.             }
  467.         }
  468.  
  469.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  470.             $result array_change_key_case($result$db->options['field_case']);
  471.         }
  472.         return array_keys($result);
  473.     }
  474.  
  475.     // }}}
  476.     // {{{ listTables()
  477.  
  478.     /**
  479.      * list all tables in the current database
  480.      *
  481.      * @return mixed array of table names on success, a MDB2 error on failure
  482.      * @access public
  483.      */
  484.     function listTables()
  485.     {
  486.         $db =$this->getDBInstance();
  487.         if (PEAR::isError($db)) {
  488.             return $db;
  489.         }
  490.  
  491.         $query "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
  492.         $table_names $db->queryCol($query);
  493.         if (PEAR::isError($table_names)) {
  494.             return $table_names;
  495.         }
  496.         $result = array();
  497.         foreach ($table_names as $table_name{
  498.             if (!$this->_fixSequenceName($table_nametrue)) {
  499.                 $result[$table_name;
  500.             }
  501.         }
  502.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  503.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  504.         }
  505.         return $result;
  506.     }
  507.  
  508.     // }}}
  509.     // {{{ listTableFields()
  510.  
  511.     /**
  512.      * list all fields in a table in the current database
  513.      *
  514.      * @param string $table name of table that should be used in method
  515.      * @return mixed array of field names on success, a MDB2 error on failure
  516.      * @access public
  517.      */
  518.     function listTableFields($table)
  519.     {
  520.         $db =$this->getDBInstance();
  521.         if (PEAR::isError($db)) {
  522.             return $db;
  523.         }
  524.  
  525.         $result $db->loadModule('Reverse'nulltrue);
  526.         if (PEAR::isError($result)) {
  527.             return $result;
  528.         }
  529.         $query "SELECT sql FROM sqlite_master WHERE type='table' AND ";
  530.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  531.             $query.= 'LOWER(name)='.$db->quote(strtolower($table)'text');
  532.         else {
  533.             $query.= 'name='.$db->quote($table'text');
  534.         }
  535.         $sql $db->queryOne($query);
  536.         if (PEAR::isError($sql)) {
  537.             return $sql;
  538.         }
  539.         $columns $db->reverse->_getTableColumns($sql);
  540.         $fields = array();
  541.         foreach ($columns as $column{
  542.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  543.                 if ($db->options['field_case'== CASE_LOWER{
  544.                     $column['name'strtolower($column['name']);
  545.                 else {
  546.                     $column['name'strtoupper($column['name']);
  547.                 }
  548.             else {
  549.                 $column array_change_key_case($column$db->options['field_case']);
  550.             }
  551.             $fields[$column['name'];
  552.         }
  553.         return $fields;
  554.     }
  555.  
  556.     // }}}
  557.     // {{{ listTableTriggers()
  558.  
  559.     /**
  560.      * list all triggers in the database that reference a given table
  561.      *
  562.      * @param string table for which all referenced triggers should be found
  563.      * @return mixed array of trigger names on success, a MDB2 error on failure
  564.      * @access public
  565.      */
  566.     function listTableTriggers($table = null)
  567.     {
  568.         $db =$this->getDBInstance();
  569.         if (PEAR::isError($db)) {
  570.             return $db;
  571.         }
  572.  
  573.         $query "SELECT name FROM sqlite_master WHERE type='trigger' AND sql NOT NULL";
  574.         if (!is_null($table)) {
  575.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  576.                 $query.= ' AND LOWER(tbl_name)='.$db->quote(strtolower($table)'text');
  577.             else {
  578.                 $query.= ' AND tbl_name='.$db->quote($table'text');
  579.             }
  580.         }
  581.         $result $db->queryCol($query);
  582.         if (PEAR::isError($result)) {
  583.             return $result;
  584.         }
  585.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  586.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  587.         }
  588.         return $result;
  589.     }
  590.  
  591.     // }}}
  592.     // {{{ createIndex()
  593.  
  594.     /**
  595.      * Get the stucture of a field into an array
  596.      *
  597.      * @param string    $table         name of the table on which the index is to be created
  598.      * @param string    $name         name of the index to be created
  599.      * @param array     $definition        associative array that defines properties of the index to be created.
  600.      *                                  Currently, only one property named FIELDS is supported. This property
  601.      *                                  is also an associative with the names of the index fields as array
  602.      *                                  indexes. Each entry of this array is set to another type of associative
  603.      *                                  array that specifies properties of the index that are specific to
  604.      *                                  each field.
  605.      *
  606.      *                                 Currently, only the sorting property is supported. It should be used
  607.      *                                  to define the sorting direction of the index. It may be set to either
  608.      *                                  ascending or descending.
  609.      *
  610.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  611.      *                                  drivers of those that do not support it ignore this property. Use the
  612.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  613.  
  614.      *                                  Example
  615.      *                                     array(
  616.      *                                         'fields' => array(
  617.      *                                             'user_name' => array(
  618.      *                                                 'sorting' => 'ascending'
  619.      *                                             ),
  620.      *                                             'last_login' => array()
  621.      *                                         )
  622.      *                                     )
  623.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  624.      * @access public
  625.      */
  626.     function createIndex($table$name$definition)
  627.     {
  628.         $db =$this->getDBInstance();
  629.         if (PEAR::isError($db)) {
  630.             return $db;
  631.         }
  632.  
  633.         $table $db->quoteIdentifier($tabletrue);
  634.         $name  $db->getIndexName($name);
  635.         $query = "CREATE INDEX $name ON $table";
  636.         $fields = array();
  637.         foreach ($definition['fields'as $field_name => $field{
  638.             $field_string $field_name;
  639.             if (!empty($field['sorting'])) {
  640.                 switch ($field['sorting']{
  641.                 case 'ascending':
  642.                     $field_string.= ' ASC';
  643.                     break;
  644.                 case 'descending':
  645.                     $field_string.= ' DESC';
  646.                     break;
  647.                 }
  648.             }
  649.             $fields[$field_string;
  650.         }
  651.         $query .= ' ('.implode(', '$fields')';
  652.         return $db->exec($query);
  653.     }
  654.  
  655.     // }}}
  656.     // {{{ dropIndex()
  657.  
  658.     /**
  659.      * drop existing index
  660.      *
  661.      * @param string    $table         name of table that should be used in method
  662.      * @param string    $name         name of the index to be dropped
  663.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  664.      * @access public
  665.      */
  666.     function dropIndex($table$name)
  667.     {
  668.         $db =$this->getDBInstance();
  669.         if (PEAR::isError($db)) {
  670.             return $db;
  671.         }
  672.  
  673.         $name $db->getIndexName($name);
  674.         return $db->exec("DROP INDEX $name");
  675.     }
  676.  
  677.     // }}}
  678.     // {{{ listTableIndexes()
  679.  
  680.     /**
  681.      * list all indexes in a table
  682.      *
  683.      * @param string $table name of table that should be used in method
  684.      * @return mixed array of index names on success, a MDB2 error on failure
  685.      * @access public
  686.      */
  687.     function listTableIndexes($table)
  688.     {
  689.         $db =$this->getDBInstance();
  690.         if (PEAR::isError($db)) {
  691.             return $db;
  692.         }
  693.  
  694.         $table $db->quote($table'text');
  695.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  696.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  697.             $query.= 'LOWER(tbl_name)='.strtolower($table);
  698.         else {
  699.             $query.= "tbl_name=$table";
  700.         }
  701.         $query.= " AND sql NOT NULL ORDER BY name";
  702.         $indexes $db->queryCol($query'text');
  703.         if (PEAR::isError($indexes)) {
  704.             return $indexes;
  705.         }
  706.  
  707.         $result = array();
  708.         foreach ($indexes as $sql{
  709.             if (preg_match("/^create index ([^ ]+) on /i"$sql$tmp)) {
  710.                 $index $this->_fixIndexName($tmp[1]);
  711.                 if (!empty($index)) {
  712.                     $result[$index= true;
  713.                 }
  714.             }
  715.         }
  716.  
  717.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  718.             $result array_change_key_case($result$db->options['field_case']);
  719.         }
  720.         return array_keys($result);
  721.     }
  722.  
  723.     // }}}
  724.     // {{{ createConstraint()
  725.  
  726.     /**
  727.      * create a constraint on a table
  728.      *
  729.      * @param string    $table         name of the table on which the constraint is to be created
  730.      * @param string    $name         name of the constraint to be created
  731.      * @param array     $definition        associative array that defines properties of the constraint to be created.
  732.      *                                  Currently, only one property named FIELDS is supported. This property
  733.      *                                  is also an associative with the names of the constraint fields as array
  734.      *                                  constraints. Each entry of this array is set to another type of associative
  735.      *                                  array that specifies properties of the constraint that are specific to
  736.      *                                  each field.
  737.      *
  738.      *                                  Example
  739.      *                                     array(
  740.      *                                         'fields' => array(
  741.      *                                             'user_name' => array(),
  742.      *                                             'last_login' => array()
  743.      *                                         )
  744.      *                                     )
  745.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  746.      * @access public
  747.      */
  748.     function createConstraint($table$name$definition)
  749.     {
  750.         $db =$this->getDBInstance();
  751.         if (PEAR::isError($db)) {
  752.             return $db;
  753.         }
  754.  
  755.         if (!empty($definition['primary'])) {
  756.             return $db->manager->alterTable($tablearray()falsearray('primary' => $definition['fields']));
  757.         }
  758.  
  759.         $table $db->quoteIdentifier($tabletrue);
  760.         $name  $db->getIndexName($name);
  761.         $query = "CREATE UNIQUE INDEX $name ON $table";
  762.         $fields = array();
  763.         foreach ($definition['fields'as $field_name => $field{
  764.             $field_string $field_name;
  765.             if (!empty($field['sorting'])) {
  766.                 switch ($field['sorting']{
  767.                 case 'ascending':
  768.                     $field_string.= ' ASC';
  769.                     break;
  770.                 case 'descending':
  771.                     $field_string.= ' DESC';
  772.                     break;
  773.                 }
  774.             }
  775.             $fields[$field_string;
  776.         }
  777.         $query .= ' ('.implode(', '$fields')';
  778.         return $db->exec($query);
  779.     }
  780.  
  781.     // }}}
  782.     // {{{ dropConstraint()
  783.  
  784.     /**
  785.      * drop existing constraint
  786.      *
  787.      * @param string    $table        name of table that should be used in method
  788.      * @param string    $name         name of the constraint to be dropped
  789.      * @param string    $primary      hint if the constraint is primary
  790.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  791.      * @access public
  792.      */
  793.     function dropConstraint($table$name$primary = false)
  794.     {
  795.         $db =$this->getDBInstance();
  796.         if (PEAR::isError($db)) {
  797.             return $db;
  798.         }
  799.  
  800.         if ($primary || $name == 'PRIMARY'{
  801.             return $db->manager->alterTable($tablearray()falsearray('primary' => null));
  802.         }
  803.  
  804.         $name $db->getIndexName($name);
  805.         return $db->exec("DROP INDEX $name");
  806.     }
  807.  
  808.     // }}}
  809.     // {{{ listTableConstraints()
  810.  
  811.     /**
  812.      * list all constraints in a table
  813.      *
  814.      * @param string $table name of table that should be used in method
  815.      * @return mixed array of constraint names on success, a MDB2 error on failure
  816.      * @access public
  817.      */
  818.     function listTableConstraints($table)
  819.     {
  820.         $db =$this->getDBInstance();
  821.         if (PEAR::isError($db)) {
  822.             return $db;
  823.         }
  824.  
  825.         $table $db->quote($table'text');
  826.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  827.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  828.             $query.= 'LOWER(tbl_name)='.strtolower($table);
  829.         else {
  830.             $query.= "tbl_name=$table";
  831.         }
  832.         $query.= " AND sql NOT NULL ORDER BY name";
  833.         $indexes $db->queryCol($query'text');
  834.         if (PEAR::isError($indexes)) {
  835.             return $indexes;
  836.         }
  837.  
  838.         $result = array();
  839.         foreach ($indexes as $sql{
  840.             if (preg_match("/^create unique index ([^ ]+) on /i"$sql$tmp)) {
  841.                 $index $this->_fixIndexName($tmp[1]);
  842.                 if (!empty($index)) {
  843.                     $result[$index= true;
  844.                 }
  845.             }
  846.         }
  847.  
  848.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  849.             $result array_change_key_case($result$db->options['field_case']);
  850.         }
  851.         return array_keys($result);
  852.     }
  853.  
  854.     // }}}
  855.     // {{{ createSequence()
  856.  
  857.     /**
  858.      * create sequence
  859.      *
  860.      * @param string    $seq_name     name of the sequence to be created
  861.      * @param string    $start         start value of the sequence; default is 1
  862.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  863.      * @access public
  864.      */
  865.     function createSequence($seq_name$start = 1)
  866.     {
  867.         $db =$this->getDBInstance();
  868.         if (PEAR::isError($db)) {
  869.             return $db;
  870.         }
  871.  
  872.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  873.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  874.         $query = "CREATE TABLE $sequence_name ($seqcol_name INTEGER PRIMARY KEY DEFAULT 0 NOT NULL)";
  875.         $res $db->exec($query);
  876.         if (PEAR::isError($res)) {
  877.             return $res;
  878.         }
  879.         if ($start == 1{
  880.             return MDB2_OK;
  881.         }
  882.         $res $db->exec("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  883.         if (!PEAR::isError($res)) {
  884.             return MDB2_OK;
  885.         }
  886.         // Handle error
  887.         $result $db->exec("DROP TABLE $sequence_name");
  888.         if (PEAR::isError($result)) {
  889.             return $db->raiseError($resultnullnull,
  890.                 'could not drop inconsistent sequence table'__FUNCTION__);
  891.         }
  892.         return $db->raiseError($resnullnull,
  893.             'could not create sequence table'__FUNCTION__);
  894.     }
  895.  
  896.     // }}}
  897.     // {{{ dropSequence()
  898.  
  899.     /**
  900.      * drop existing sequence
  901.      *
  902.      * @param string    $seq_name     name of the sequence to be dropped
  903.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  904.      * @access public
  905.      */
  906.     function dropSequence($seq_name)
  907.     {
  908.         $db =$this->getDBInstance();
  909.         if (PEAR::isError($db)) {
  910.             return $db;
  911.         }
  912.  
  913.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  914.         return $db->exec("DROP TABLE $sequence_name");
  915.     }
  916.  
  917.     // }}}
  918.     // {{{ listSequences()
  919.  
  920.     /**
  921.      * list all sequences in the current database
  922.      *
  923.      * @return mixed array of sequence names on success, a MDB2 error on failure
  924.      * @access public
  925.      */
  926.     function listSequences()
  927.     {
  928.         $db =$this->getDBInstance();
  929.         if (PEAR::isError($db)) {
  930.             return $db;
  931.         }
  932.  
  933.         $query "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
  934.         $table_names $db->queryCol($query);
  935.         if (PEAR::isError($table_names)) {
  936.             return $table_names;
  937.         }
  938.         $result = array();
  939.         foreach ($table_names as $table_name{
  940.             if ($sqn $this->_fixSequenceName($table_nametrue)) {
  941.                 $result[$sqn;
  942.             }
  943.         }
  944.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  945.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  946.         }
  947.         return $result;
  948.     }
  949.  
  950.     // }}}
  951. }
  952. ?>

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