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

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