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

Source for file fbsql.php

Documentation is available at fbsql.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, Frank M. Kromann                       |
  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: fbsql.php,v 1.21 2005/04/16 22:27:48 lsmith Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Manager/Common.php';
  49.  
  50. /**
  51.  * MDB2 FrontBase driver for the management modules
  52.  *
  53.  * @package MDB2
  54.  * @category Database
  55.  * @author  Frank M. Kromann <frank@kromann.info>
  56.  */
  57. class MDB2_Driver_Manager_fbsql extends MDB2_Driver_Manager_Common
  58. {
  59.     // {{{ createDatabase()
  60.  
  61.     /**
  62.      * create a new database
  63.      *
  64.      * @param string $name name of the database that should be created
  65.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  66.      * @access public
  67.      */
  68.     function createDatabase($name)
  69.     {
  70.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  71.         if (PEAR::isError($result $db->connect())) {
  72.             return $result;
  73.         }
  74.         $query = "CREATE DATABASE $name";
  75.         if (PEAR::isError($result $db->query($query))) {
  76.             return $result;
  77.         }
  78.  
  79.         return MDB2_OK;
  80.     }
  81.  
  82.     // }}}
  83.     // {{{ dropDatabase()
  84.  
  85.     /**
  86.      * drop an existing database
  87.      *
  88.      * @param string $name name of the database that should be dropped
  89.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  90.      * @access public
  91.      */
  92.     function dropDatabase($name)
  93.     {
  94.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  95.         if (PEAR::isError($result $db->connect())) {
  96.             return $result;
  97.         }
  98.         $query = "DELETE DATABASE $name";
  99.         if (PEAR::isError($result $db->query($query))) {
  100.             return $result;
  101.         }
  102.  
  103.         return MDB2_OK;
  104.     }
  105.  
  106.     // }}}
  107.     // {{{ dropTable()
  108.  
  109.     /**
  110.      * drop an existing table
  111.      *
  112.      * @param object    $dbs        database object that is extended by this class
  113.      * @param string $name name of the table that should be dropped
  114.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  115.      * @access public
  116.      */
  117.     function dropTable($name)
  118.     {
  119.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  120.         return $db->query("DROP TABLE $name CASCADE");
  121.     }
  122.  
  123.     // }}}
  124.     // {{{ alterTable()
  125.  
  126.     /**
  127.      * alter an existing table
  128.      *
  129.      * @param string $name         name of the table that is intended to be changed.
  130.      * @param array $changes     associative array that contains the details of each type
  131.      *                              of change that is intended to be performed. The types of
  132.      *                              changes that are currently supported are defined as follows:
  133.      *
  134.      *                              name
  135.      *
  136.      *                                 New name for the table.
  137.      *
  138.      *                             added_fields
  139.      *
  140.      *                                 Associative array with the names of fields to be added as
  141.      *                                  indexes of the array. The value of each entry of the array
  142.      *                                  should be set to another associative array with the properties
  143.      *                                  of the fields to be added. The properties of the fields should
  144.      *                                  be the same as defined by the Metabase parser.
  145.      *
  146.      *                                 Additionally, there should be an entry named Declaration that
  147.      *                                  is expected to contain the portion of the field declaration already
  148.      *                                  in DBMS specific SQL code as it is used in the CREATE TABLE statement.
  149.      *
  150.      *                             removed_fields
  151.      *
  152.      *                                 Associative array with the names of fields to be removed as indexes
  153.      *                                  of the array. Currently the values assigned to each entry are ignored.
  154.      *                                  An empty array should be used for future compatibility.
  155.      *
  156.      *                             renamed_fields
  157.      *
  158.      *                                 Associative array with the names of fields to be renamed as indexes
  159.      *                                  of the array. The value of each entry of the array should be set to
  160.      *                                  another associative array with the entry named name with the new
  161.      *                                  field name and the entry named Declaration that is expected to contain
  162.      *                                  the portion of the field declaration already in DBMS specific SQL code
  163.      *                                  as it is used in the CREATE TABLE statement.
  164.      *
  165.      *                             changed_fields
  166.      *
  167.      *                                 Associative array with the names of the fields to be changed as indexes
  168.      *                                  of the array. Keep in mind that if it is intended to change either the
  169.      *                                  name of a field and any other properties, the changed_fields array entries
  170.      *                                  should have the new names of the fields as array indexes.
  171.      *
  172.      *                                 The value of each entry of the array should be set to another associative
  173.      *                                  array with the properties of the fields to that are meant to be changed as
  174.      *                                  array entries. These entries should be assigned to the new values of the
  175.      *                                  respective properties. The properties of the fields should be the same
  176.      *                                  as defined by the Metabase parser.
  177.      *
  178.      *                                 If the default property is meant to be added, removed or changed, there
  179.      *                                  should also be an entry with index ChangedDefault assigned to 1. Similarly,
  180.      *                                  if the notnull constraint is to be added or removed, there should also be
  181.      *                                  an entry with index ChangedNotNull assigned to 1.
  182.      *
  183.      *                                 Additionally, there should be an entry named Declaration that is expected
  184.      *                                  to contain the portion of the field changed declaration already in DBMS
  185.      *                                  specific SQL code as it is used in the CREATE TABLE statement.
  186.      *                             Example
  187.      *                                 array(
  188.      *                                     'name' => 'userlist',
  189.      *                                     'added_fields' => array(
  190.      *                                         'quota' => array(
  191.      *                                             'type' => 'integer',
  192.      *                                             'unsigned' => 1
  193.      *                                             'declaration' => 'quota INT'
  194.      *                                         )
  195.      *                                     ),
  196.      *                                     'removed_fields' => array(
  197.      *                                         'file_limit' => array(),
  198.      *                                         'time_limit' => array()
  199.      *                                         ),
  200.      *                                     'changed_fields' => array(
  201.      *                                         'gender' => array(
  202.      *                                             'default' => 'M',
  203.      *                                             'change_default' => 1,
  204.      *                                             'declaration' => "gender CHAR(1) DEFAULT 'M'"
  205.      *                                         )
  206.      *                                     ),
  207.      *                                     'renamed_fields' => array(
  208.      *                                         'sex' => array(
  209.      *                                             'name' => 'gender',
  210.      *                                             'declaration' => "gender CHAR(1) DEFAULT 'M'"
  211.      *                                         )
  212.      *                                     )
  213.      *                                 )
  214.      *
  215.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  216.      *                              can perform the requested table alterations if the value is true or
  217.      *                              actually perform them otherwise.
  218.      * @access public
  219.      *
  220.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  221.      */
  222.     function alterTable($name$changes$check)
  223.     {
  224.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  225.         foreach ($changes as $change_name => $change){
  226.             switch ($change_name{
  227.             case 'added_fields':
  228.             case 'removed_fields':
  229.             case 'changed_fields':
  230.             case 'renamed_fields':
  231.             case 'name':
  232.                 break;
  233.             default:
  234.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  235.                     'alterTable: change type "'.$change_name.'" not yet supported');
  236.             }
  237.         }
  238.         if ($check{
  239.             return MDB2_OK;
  240.         }
  241.         $query (isset($changes['name']'RENAME AS '.$changes['name''');
  242.         if (isset($changes['added_fields'])) {
  243.             $fields $changes['added_fields'];
  244.             foreach ($fields as $field_name => $field{
  245.                 if ($query{
  246.                     $query .= ',';
  247.                 }
  248.                 $query .= 'ADD '.$field['declaration'];
  249.             }
  250.         }
  251.         if (isset($changes['removed_fields'])) {
  252.             $fields $changes['removed_fields'];
  253.             foreach ($fields as $field_name => $field{
  254.                 if ($query{
  255.                     $query .= ',';
  256.                 }
  257.                 $query .= 'DROP '.$field_name;
  258.             }
  259.         }
  260.         $renamed_fields = array();
  261.         if (isset($changes['renamed_fields'])) {
  262.             $fields $changes['renamed_fields'];
  263.             foreach ($fields as $field_name => $field{
  264.                 $renamed_fields[$field['name']] $field_name;
  265.             }
  266.         }
  267.         if (isset($changes['changed_fields'])) {
  268.             $fields $changes['changed_fields'];
  269.             foreach ($fields as $field_name => $field{
  270.                 if ($query{
  271.                     $query .= ',';
  272.                 }
  273.                 if (isset($renamed_fields[$field_name])) {
  274.                     $old_field_name $renamed_fields[$field_name];
  275.                     unset($renamed_fields[$field_name]);
  276.                 else {
  277.                     $old_field_name $field_name;
  278.                 }
  279.                 $query .= "CHANGE $old_field_name ".$field['declaration'];
  280.             }
  281.         }
  282.         if (count($renamed_fields)) {
  283.             foreach ($renamed_fields as $renamed_field_name => $renamed_field{
  284.                 if ($query{
  285.                     $query .= ',';
  286.                 }
  287.                 $old_field_name $renamed_fields[$renamed_field_name];
  288.                 $query .= "CHANGE $old_field_name ".$changes['renamed_fields'][$old_field_name]['declaration'];
  289.             }
  290.         }
  291.         if (!$query{
  292.             return MDB2_OK;
  293.         }
  294.         return $db->query("ALTER TABLE $name $query");
  295.     }
  296.  
  297.     // }}}
  298.     // {{{ listDatabases()
  299.  
  300.     /**
  301.      * list all databases
  302.      *
  303.      * @return mixed data array on success, a MDB2 error on failure
  304.      * @access public
  305.      */
  306.     function listDatabases()
  307.     {
  308.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  309.         return $db->raiseError(MDB2_ERROR_NOT_CAPABLE);
  310.     }
  311.  
  312.     // }}}
  313.     // {{{ listUsers()
  314.  
  315.     /**
  316.      * list all users
  317.      *
  318.      * @return mixed data array on success, a MDB2 error on failure
  319.      * @access public
  320.      */
  321.     function listUsers()
  322.     {
  323.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  324.         return $db->queryCol('SELECT "user_name" FROM'
  325.                              . ' information_schema.users');
  326.     }
  327.  
  328.     // }}}
  329.     // {{{ listTables()
  330.  
  331.     /**
  332.      * list all tables in the current database
  333.      *
  334.      * @return mixed data array on success, a MDB2 error on failure
  335.      * @access public
  336.      */
  337.     function listTables()
  338.     {
  339.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  340.         $table_names $db->queryCol('SELECT "table_name"'
  341.                 . ' FROM information_schema.tables'
  342.                 . ' t0, information_schema.schemata t1'
  343.                 . ' WHERE t0.schema_pk=t1.schema_pk AND'
  344.                 . ' "table_type" = \'BASE TABLE\''
  345.                 . ' AND "schema_name" = current_schema');
  346.         if (PEAR::isError($table_names)) {
  347.             return $table_names;
  348.         }
  349.         for ($i = 0$j count($table_names)$tables = array()$i $j; ++$i{
  350.             if (!$this->_isSequenceName($table_names[$i]))
  351.                 $tables[$table_names[$i];
  352.         }
  353.         return $tables;
  354.     }
  355.  
  356.     // }}}
  357.     // {{{ listTableFields()
  358.  
  359.     /**
  360.      * list all fields in a tables in the current database
  361.      *
  362.      * @param string $table name of table that should be used in method
  363.      * @return mixed data array on success, a MDB2 error on failure
  364.      * @access public
  365.      */
  366.     function listTableFields($table)
  367.     {
  368.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  369.         $fields $db->queryCol("SHOW COLUMNS FROM $table");
  370.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  371.             $fields array_flip(array_change_key_case(array_flip($fields)CASE_LOWER));
  372.         }
  373.         return $fields;
  374.     }
  375.  
  376.     // }}}
  377.     // {{{ createIndex()
  378.  
  379.     /**
  380.      * get the stucture of a field into an array
  381.      *
  382.      * @param string    $table         name of the table on which the index is to be created
  383.      * @param string    $name         name of the index to be created
  384.      * @param array     $definition        associative array that defines properties of the index to be created.
  385.      *                                  Currently, only one property named FIELDS is supported. This property
  386.      *                                  is also an associative with the names of the index fields as array
  387.      *                                  indexes. Each entry of this array is set to another type of associative
  388.      *                                  array that specifies properties of the index that are specific to
  389.      *                                  each field.
  390.      *
  391.      *                                 Currently, only the sorting property is supported. It should be used
  392.      *                                  to define the sorting direction of the index. It may be set to either
  393.      *                                  ascending or descending.
  394.      *
  395.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  396.      *                                  drivers of those that do not support it ignore this property. Use the
  397.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  398.  
  399.      *                                  Example
  400.      *                                     array(
  401.      *                                         'fields' => array(
  402.      *                                             'user_name' => array(
  403.      *                                                 'sorting' => 'ascending'
  404.      *                                             ),
  405.      *                                             'last_login' => array()
  406.      *                                         )
  407.      *                                     )
  408.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  409.      * @access public
  410.      */
  411.     function createIndex($table$name$definition)
  412.     {
  413.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  414.         $query "CREATE ".(isset($definition['unique']'UNIQUE INDEX' 'INDEX')." $name on $table (";
  415.         $skipped_first = false;
  416.         foreach ($definition['fields'as $field_name => $field{
  417.             if ($skipped_first{
  418.                 $query .= ',';
  419.             }
  420.             $skipped_first = true;
  421.             $query .= $field_name;
  422.         }
  423.         $query .= ')';
  424.         return $db->query($query);
  425.     }
  426.  
  427.     // }}}
  428.     // {{{ dropIndex()
  429.  
  430.     /**
  431.      * drop existing index
  432.      *
  433.      * @param string    $table         name of table that should be used in method
  434.      * @param string    $name         name of the index to be dropped
  435.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  436.      * @access public
  437.      */
  438.     function dropIndex($table$name)
  439.     {
  440.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  441.         return $db->query("ALTER TABLE $table DROP INDEX $name");
  442.     }
  443.  
  444.     // }}}
  445.     // {{{ listTableIndexes()
  446.  
  447.     /**
  448.      * list all indexes in a table
  449.      *
  450.      * @param string    $table      name of table that should be used in method
  451.      * @return mixed data array on success, a MDB2 error on failure
  452.      * @access public
  453.      */
  454.     function listTableIndexes($table)
  455.     {
  456.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  457.         $indexes_all $db->queryCol("SHOW INDEX FROM $table"'text''key_name');
  458.         if (PEAR::isError($indexes_all)) {
  459.             return $indexes_all;
  460.         }
  461.  
  462.         $found $indexes = array();
  463.         foreach ($indexes_all as $index_name{
  464.             if ($indexes_all[$index!= 'PRIMARY' && !isset($found[$index_name])) {
  465.                 $indexes[$index_name;
  466.                 $found[$index_name= true;
  467.             }
  468.         }
  469.         return $indexes;
  470.     }
  471.  
  472.     // }}}
  473.     // {{{ createSequence()
  474.  
  475.     /**
  476.      * create sequence
  477.      *
  478.      * @param string    $seq_name     name of the sequence to be created
  479.      * @param string    $start         start value of the sequence; default is 1
  480.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  481.      * @access public
  482.      */
  483.     function createSequence($seq_name$start = 1)
  484.     {
  485.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  486.         $sequence_name $db->getSequenceName($seq_name);
  487.         $seqcol_name $db->options['seqcol_name'];
  488.         $query = "CREATE TABLE $sequence_name ($seqcol_name INTEGER DEFAULT UNIQUE, PRIMARY KEY($seqcol_name))";
  489.         $res $db->query($query);
  490.         $res $db->query("SET UNIQUE = 1 FOR $sequence_name");
  491.         if (PEAR::isError($res)) {
  492.             return $res;
  493.         }
  494.         if ($start == 1{
  495.             return MDB2_OK;
  496.         }
  497.         $res $db->query("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  498.         if (!PEAR::isError($res)) {
  499.             return MDB2_OK;
  500.         }
  501.         // Handle error
  502.         $result $db->query("DROP TABLE $sequence_name");
  503.         if (PEAR::isError($result)) {
  504.             return $db->raiseError(MDB2_ERRORnullnull,
  505.                 'createSequence: could not drop inconsistent sequence table ('.
  506.                 $result->getMessage().' ('.$result->getUserinfo().'))');
  507.         }
  508.         return $db->raiseError(MDB2_ERRORnullnull,
  509.             'createSequence: could not create sequence table ('.
  510.             $res->getMessage().' ('.$res->getUserinfo().'))');
  511.     }
  512.  
  513.     // }}}
  514.     // {{{ dropSequence()
  515.  
  516.     /**
  517.      * drop existing sequence
  518.      *
  519.      * @param string    $seq_name     name of the sequence to be dropped
  520.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  521.      * @access public
  522.      */
  523.     function dropSequence($seq_name)
  524.     {
  525.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  526.         $sequence_name $db->getSequenceName($seq_name);
  527.         return $db->query("DROP TABLE $sequence_name CASCADE");
  528.     }
  529.  
  530.     // }}}
  531.     // {{{ listSequences()
  532.  
  533.     /**
  534.      * list all sequences in the current database
  535.      *
  536.      * @return mixed data array on success, a MDB2 error on failure
  537.      * @access public
  538.      */
  539.     function listSequences()
  540.     {
  541.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  542.         $table_names $db->queryCol('SHOW TABLES''text');
  543.         if (PEAR::isError($table_names)) {
  544.             return $table_names;
  545.         }
  546.         $sequences = array();
  547.         for ($i = 0$j count($table_names)$i $j; ++$i{
  548.             if ($sqn $this->_isSequenceName($table_names[$i]))
  549.                 $sequences[$sqn;
  550.         }
  551.         return $sequences;
  552.     }
  553.     // }}}
  554. }
  555.  
  556. ?>

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