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. {
  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 (PEAR::isError($result $db->connect())) {
  71.             return $result;
  72.         }
  73.         $query = "CREATE DATABASE $name";
  74.         if (PEAR::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 (PEAR::isError($result $db->connect())) {
  95.             return $result;
  96.         }
  97.         $query = "DELETE DATABASE $name";
  98.         if (PEAR::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.         foreach ($changes as $change_name => $change){
  225.             switch ($change_name{
  226.             case 'added_fields':
  227.             case 'removed_fields':
  228.             case 'changed_fields':
  229.             case 'renamed_fields':
  230.             case 'name':
  231.                 break;
  232.             default:
  233.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  234.                     'alterTable: change type "'.$change_name.'" not yet supported');
  235.             }
  236.         }
  237.         if ($check{
  238.             return MDB2_OK;
  239.         }
  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.         if (!$query{
  291.             return MDB2_OK;
  292.         }
  293.         return $db->query("ALTER TABLE $name $query");
  294.     }
  295.  
  296.     // }}}
  297.     // {{{ listDatabases()
  298.  
  299.     /**
  300.      * list all databases
  301.      *
  302.      * @return mixed data array on success, a MDB2 error on failure
  303.      * @access public
  304.      */
  305.     function listDatabases()
  306.     {
  307.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  308.         return $db->raiseError(MDB2_ERROR_NOT_CAPABLE);
  309.     }
  310.  
  311.     // }}}
  312.     // {{{ listUsers()
  313.  
  314.     /**
  315.      * list all users
  316.      *
  317.      * @return mixed data array on success, a MDB2 error on failure
  318.      * @access public
  319.      */
  320.     function listUsers()
  321.     {
  322.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  323.         return $db->queryCol('SELECT "user_name" FROM'
  324.                              . ' information_schema.users');
  325.     }
  326.  
  327.     // }}}
  328.     // {{{ listTables()
  329.  
  330.     /**
  331.      * list all tables in the current database
  332.      *
  333.      * @return mixed data array on success, a MDB2 error on failure
  334.      * @access public
  335.      */
  336.     function listTables()
  337.     {
  338.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  339.         $table_names $db->queryCol('SELECT "table_name"'
  340.                 . ' FROM information_schema.tables'
  341.                 . ' t0, information_schema.schemata t1'
  342.                 . ' WHERE t0.schema_pk=t1.schema_pk AND'
  343.                 . ' "table_type" = \'BASE TABLE\''
  344.                 . ' AND "schema_name" = current_schema');
  345.         if (PEAR::isError($table_names)) {
  346.             return $table_names;
  347.         }
  348.         for ($i = 0$j count($table_names)$tables = array()$i $j; ++$i{
  349.             if (!$this->_isSequenceName($table_names[$i]))
  350.                 $tables[$table_names[$i];
  351.         }
  352.         return $tables;
  353.     }
  354.  
  355.     // }}}
  356.     // {{{ listTableFields()
  357.  
  358.     /**
  359.      * list all fields in a tables in the current database
  360.      *
  361.      * @param string $table name of table that should be used in method
  362.      * @return mixed data array on success, a MDB2 error on failure
  363.      * @access public
  364.      */
  365.     function listTableFields($table)
  366.     {
  367.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  368.         $fields $db->queryCol("SHOW COLUMNS FROM $table");
  369.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  370.             $fields array_flip(array_change_key_case(array_flip($fields)CASE_LOWER));
  371.         }
  372.         return $fields;
  373.     }
  374.  
  375.     // }}}
  376.     // {{{ createIndex()
  377.  
  378.     /**
  379.      * get the stucture of a field into an array
  380.      *
  381.      * @param string    $table         name of the table on which the index is to be created
  382.      * @param string    $name         name of the index to be created
  383.      * @param array     $definition        associative array that defines properties of the index to be created.
  384.      *                                  Currently, only one property named FIELDS is supported. This property
  385.      *                                  is also an associative with the names of the index fields as array
  386.      *                                  indexes. Each entry of this array is set to another type of associative
  387.      *                                  array that specifies properties of the index that are specific to
  388.      *                                  each field.
  389.      *
  390.      *                                 Currently, only the sorting property is supported. It should be used
  391.      *                                  to define the sorting direction of the index. It may be set to either
  392.      *                                  ascending or descending.
  393.      *
  394.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  395.      *                                  drivers of those that do not support it ignore this property. Use the
  396.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  397.  
  398.      *                                  Example
  399.      *                                     array(
  400.      *                                         'fields' => array(
  401.      *                                             'user_name' => array(
  402.      *                                                 'sorting' => 'ascending'
  403.      *                                             ),
  404.      *                                             'last_login' => array()
  405.      *                                         )
  406.      *                                     )
  407.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  408.      * @access public
  409.      */
  410.     function createIndex($table$name$definition)
  411.     {
  412.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  413.         $query "CREATE ".(isset($definition['unique']'UNIQUE INDEX' 'INDEX')." $name on $table (";
  414.         $skipped_first = false;
  415.         foreach ($definition['fields'as $field_name => $field{
  416.             if ($skipped_first{
  417.                 $query .= ',';
  418.             }
  419.             $skipped_first = true;
  420.             $query .= $field_name;
  421.         }
  422.         $query .= ')';
  423.         return $db->query($query);
  424.     }
  425.  
  426.     // }}}
  427.     // {{{ dropIndex()
  428.  
  429.     /**
  430.      * drop existing index
  431.      *
  432.      * @param string    $table         name of table that should be used in method
  433.      * @param string    $name         name of the index to be dropped
  434.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  435.      * @access public
  436.      */
  437.     function dropIndex($table$name)
  438.     {
  439.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  440.         return $db->query("ALTER TABLE $table DROP INDEX $name");
  441.     }
  442.  
  443.     // }}}
  444.     // {{{ listTableIndexes()
  445.  
  446.     /**
  447.      * list all indexes in a table
  448.      *
  449.      * @param string    $table      name of table that should be used in method
  450.      * @return mixed data array on success, a MDB2 error on failure
  451.      * @access public
  452.      */
  453.     function listTableIndexes($table)
  454.     {
  455.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  456.         $indexes_all $db->queryCol("SHOW INDEX FROM $table"'text''key_name');
  457.         if (PEAR::isError($indexes_all)) {
  458.             return $indexes_all;
  459.         }
  460.  
  461.         $found $indexes = array();
  462.         foreach ($indexes_all as $index_name{
  463.             if ($indexes_all[$index!= 'PRIMARY' && !isset($found[$index_name])) {
  464.                 $indexes[$index_name;
  465.                 $found[$index_name= true;
  466.             }
  467.         }
  468.         return $indexes;
  469.     }
  470.  
  471.     // }}}
  472.     // {{{ createSequence()
  473.  
  474.     /**
  475.      * create sequence
  476.      *
  477.      * @param string    $seq_name     name of the sequence to be created
  478.      * @param string    $start         start value of the sequence; default is 1
  479.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  480.      * @access public
  481.      */
  482.     function createSequence($seq_name$start = 1)
  483.     {
  484.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  485.         $sequence_name $db->getSequenceName($seq_name);
  486.         $seqcol_name $db->options['seqcol_name'];
  487.         $query = "CREATE TABLE $sequence_name ($seqcol_name INTEGER DEFAULT UNIQUE, PRIMARY KEY($seqcol_name))";
  488.         $res $db->query($query);
  489.         $res $db->query("SET UNIQUE = 1 FOR $sequence_name");
  490.         if (PEAR::isError($res)) {
  491.             return $res;
  492.         }
  493.         if ($start == 1{
  494.             return MDB2_OK;
  495.         }
  496.         $res $db->query("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  497.         if (!PEAR::isError($res)) {
  498.             return MDB2_OK;
  499.         }
  500.         // Handle error
  501.         $result $db->query("DROP TABLE $sequence_name");
  502.         if (PEAR::isError($result)) {
  503.             return $db->raiseError(MDB2_ERRORnullnull,
  504.                 'createSequence: could not drop inconsistent sequence table ('.
  505.                 $result->getMessage().' ('.$result->getUserinfo().'))');
  506.         }
  507.         return $db->raiseError(MDB2_ERRORnullnull,
  508.             'createSequence: could not create sequence table ('.
  509.             $res->getMessage().' ('.$res->getUserinfo().'))');
  510.     }
  511.  
  512.     // }}}
  513.     // {{{ dropSequence()
  514.  
  515.     /**
  516.      * drop existing sequence
  517.      *
  518.      * @param string    $seq_name     name of the sequence to be dropped
  519.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  520.      * @access public
  521.      */
  522.     function dropSequence($seq_name)
  523.     {
  524.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  525.         $sequence_name $db->getSequenceName($seq_name);
  526.         return $db->query("DROP TABLE $sequence_name CASCADE");
  527.     }
  528.  
  529.     // }}}
  530.     // {{{ listSequences()
  531.  
  532.     /**
  533.      * list all sequences in the current database
  534.      *
  535.      * @return mixed data array on success, a MDB2 error on failure
  536.      * @access public
  537.      */
  538.     function listSequences()
  539.     {
  540.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  541.         $table_names $db->queryCol('SHOW TABLES''text');
  542.         if (PEAR::isError($table_names)) {
  543.             return $table_names;
  544.         }
  545.         $sequences = array();
  546.         for ($i = 0$j count($table_names)$i $j; ++$i{
  547.             if ($sqn $this->_isSequenceName($table_names[$i]))
  548.                 $sequences[$sqn;
  549.         }
  550.         return $sequences;
  551.     }
  552.     // }}}
  553. }
  554.  
  555. ?>

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