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-2006 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@pooteeweet.org>                           |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: fbsql.php 327310 2012-08-27 15:16:18Z danielc $
  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.      * @param array  $options array with charset, collation info
  66.      *
  67.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  68.      * @access public
  69.      */
  70.     function createDatabase($name$options = array())
  71.     {
  72.         $db =$this->getDBInstance();
  73.         if (MDB2::isError($db)) {
  74.             return $db;
  75.         }
  76.  
  77.         $name $db->quoteIdentifier($nametrue);
  78.         $query = "CREATE DATABASE $name";
  79.         return $db->standaloneQuery($querynulltrue);
  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 =$this->getDBInstance();
  95.         if (MDB2::isError($db)) {
  96.             return $db;
  97.         }
  98.  
  99.         $name $db->quoteIdentifier($nametrue);
  100.         $query = "DELETE DATABASE $name";
  101.         return $db->standaloneQuery($querynulltrue);
  102.     }
  103.  
  104.     // }}}
  105.     // {{{ dropTable()
  106.  
  107.     /**
  108.      * drop an existing table
  109.      *
  110.      * @param object    $dbs        database object that is extended by this class
  111.      * @param string $name name of the table that should be dropped
  112.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  113.      * @access public
  114.      */
  115.     function dropTable($name)
  116.     {
  117.         $db =$this->getDBInstance();
  118.         if (MDB2::isError($db)) {
  119.             return $db;
  120.         }
  121.  
  122.         $name $db->quoteIdentifier($nametrue);
  123.         $result $db->exec("DROP TABLE $name CASCADE");
  124.         if (MDB2::isError($result)) {
  125.             return $result;
  126.         }
  127.         return MDB2_OK;
  128.     }
  129.  
  130.     // }}}
  131.     // {{{ alterTable()
  132.  
  133.     /**
  134.      * alter an existing table
  135.      *
  136.      * @param string $name         name of the table that is intended to be changed.
  137.      * @param array $changes     associative array that contains the details of each type
  138.      *                              of change that is intended to be performed. The types of
  139.      *                              changes that are currently supported are defined as follows:
  140.      *
  141.      *                              name
  142.      *
  143.      *                                 New name for the table.
  144.      *
  145.      *                             add
  146.      *
  147.      *                                 Associative array with the names of fields to be added as
  148.      *                                  indexes of the array. The value of each entry of the array
  149.      *                                  should be set to another associative array with the properties
  150.      *                                  of the fields to be added. The properties of the fields should
  151.      *                                  be the same as defined by the MDB2 parser.
  152.      *
  153.      *
  154.      *                             remove
  155.      *
  156.      *                                 Associative array with the names of fields to be removed as indexes
  157.      *                                  of the array. Currently the values assigned to each entry are ignored.
  158.      *                                  An empty array should be used for future compatibility.
  159.      *
  160.      *                             rename
  161.      *
  162.      *                                 Associative array with the names of fields to be renamed as indexes
  163.      *                                  of the array. The value of each entry of the array should be set to
  164.      *                                  another associative array with the entry named name with the new
  165.      *                                  field name and the entry named Declaration that is expected to contain
  166.      *                                  the portion of the field declaration already in DBMS specific SQL code
  167.      *                                  as it is used in the CREATE TABLE statement.
  168.      *
  169.      *                             change
  170.      *
  171.      *                                 Associative array with the names of the fields to be changed as indexes
  172.      *                                  of the array. Keep in mind that if it is intended to change either the
  173.      *                                  name of a field and any other properties, the change array entries
  174.      *                                  should have the new names of the fields as array indexes.
  175.      *
  176.      *                                 The value of each entry of the array should be set to another associative
  177.      *                                  array with the properties of the fields to that are meant to be changed as
  178.      *                                  array entries. These entries should be assigned to the new values of the
  179.      *                                  respective properties. The properties of the fields should be the same
  180.      *                                  as defined by the MDB2 parser.
  181.      *
  182.      *                             Example
  183.      *                                 array(
  184.      *                                     'name' => 'userlist',
  185.      *                                     'add' => array(
  186.      *                                         'quota' => array(
  187.      *                                             'type' => 'integer',
  188.      *                                             'unsigned' => 1
  189.      *                                         )
  190.      *                                     ),
  191.      *                                     'remove' => array(
  192.      *                                         'file_limit' => array(),
  193.      *                                         'time_limit' => array()
  194.      *                                     ),
  195.      *                                     'change' => array(
  196.      *                                         'name' => array(
  197.      *                                             'length' => '20',
  198.      *                                             'definition' => array(
  199.      *                                                 'type' => 'text',
  200.      *                                                 'length' => 20,
  201.      *                                             ),
  202.      *                                         )
  203.      *                                     ),
  204.      *                                     'rename' => array(
  205.      *                                         'sex' => array(
  206.      *                                             'name' => 'gender',
  207.      *                                             'definition' => array(
  208.      *                                                 'type' => 'text',
  209.      *                                                 'length' => 1,
  210.      *                                                 'default' => 'M',
  211.      *                                             ),
  212.      *                                         )
  213.      *                                     )
  214.      *                                 )
  215.      *
  216.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  217.      *                              can perform the requested table alterations if the value is true or
  218.      *                              actually perform them otherwise.
  219.      * @access public
  220.      *
  221.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  222.      */
  223.     function alterTable($name$changes$check)
  224.     {
  225.         $db =$this->getDBInstance();
  226.         if (MDB2::isError($db)) {
  227.             return $db;
  228.         }
  229.  
  230.         foreach ($changes as $change_name => $change){
  231.             switch ($change_name{
  232.             case 'add':
  233.             case 'remove':
  234.             case 'change':
  235.             case 'rename':
  236.             case 'name':
  237.                 break;
  238.             default:
  239.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  240.                     'change type "'.$change_name.'" not yet supported'__FUNCTION__);
  241.             }
  242.         }
  243.  
  244.         if ($check{
  245.             return MDB2_OK;
  246.         }
  247.  
  248.         $query '';
  249.         if (!empty($changes['name'])) {
  250.             $change_name $db->quoteIdentifier($changes['name']true);
  251.             $query .= 'RENAME TO ' $change_name;
  252.         }
  253.  
  254.         if (!empty($changes['add']&& is_array($changes['add'])) {
  255.             foreach ($changes['add'as $field_name => $field{
  256.                 if ($query{
  257.                     $query.= ', ';
  258.                 }
  259.                 $query.= 'ADD ' $db->getDeclaration($field['type']$field_name$field);
  260.             }
  261.         }
  262.  
  263.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  264.             foreach ($changes['remove'as $field_name => $field{
  265.                 if ($query{
  266.                     $query.= ', ';
  267.                 }
  268.                 $field_name $db->quoteIdentifier($field_nametrue);
  269.                 $query.= 'DROP ' $field_name;
  270.             }
  271.         }
  272.  
  273.         $rename = array();
  274.         if (!empty($changes['rename']&& is_array($changes['rename'])) {
  275.             foreach ($changes['rename'as $field_name => $field{
  276.                 $rename[$field['name']] $field_name;
  277.             }
  278.         }
  279.  
  280.         if (!empty($changes['change']&& is_array($changes['change'])) {
  281.             foreach ($changes['change'as $field_name => $field{
  282.                 if ($query{
  283.                     $query.= ', ';
  284.                 }
  285.                 if (isset($rename[$field_name])) {
  286.                     $old_field_name $rename[$field_name];
  287.                     unset($rename[$field_name]);
  288.                 else {
  289.                     $old_field_name $field_name;
  290.                 }
  291.                 $old_field_name $db->quoteIdentifier($old_field_nametrue);
  292.                 $query.= "CHANGE $old_field_name " . $db->getDeclaration($field['definition']['type']$old_field_name$field['definition']);
  293.             }
  294.         }
  295.  
  296.         if (!empty($rename&& is_array($rename)) {
  297.             foreach ($rename as $renamed_field_name => $renamed_field{
  298.                 if ($query{
  299.                     $query.= ', ';
  300.                 }
  301.                 $old_field_name $rename[$renamed_field_name];
  302.                 $field $changes['rename'][$old_field_name];
  303.                 $query.= 'CHANGE ' $db->getDeclaration($field['definition']['type']$old_field_name$field['definition']);
  304.             }
  305.         }
  306.  
  307.         if (!$query{
  308.             return MDB2_OK;
  309.         }
  310.  
  311.         $name $db->quoteIdentifier($nametrue);
  312.         $result $db->exec("ALTER TABLE $name $query");
  313.         if (MDB2::isError($result)) {
  314.             return $result;
  315.         }
  316.         return MDB2_OK;
  317.     }
  318.  
  319.     // }}}
  320.     // {{{ listDatabases()
  321.  
  322.     /**
  323.      * list all databases
  324.      *
  325.      * @return mixed array of database names on success, a MDB2 error on failure
  326.      * @access public
  327.      */
  328.     function listDatabases()
  329.     {
  330.         $db =$this->getDBInstance();
  331.         if (MDB2::isError($db)) {
  332.             return $db;
  333.         }
  334.  
  335.         return $db->raiseError(MDB2_ERROR_NOT_CAPABLEnullnull,
  336.             'not capable'__FUNCTION__);
  337.     }
  338.  
  339.     // }}}
  340.     // {{{ listUsers()
  341.  
  342.     /**
  343.      * list all users
  344.      *
  345.      * @return mixed array of user names on success, a MDB2 error on failure
  346.      * @access public
  347.      */
  348.     function listUsers()
  349.     {
  350.         $db =$this->getDBInstance();
  351.         if (MDB2::isError($db)) {
  352.             return $db;
  353.         }
  354.  
  355.         return $db->queryCol('SELECT "user_name" FROM information_schema.users');
  356.     }
  357.  
  358.     // }}}
  359.     // {{{ listTables()
  360.  
  361.     /**
  362.      * list all tables in the current database
  363.      *
  364.      * @return mixed array of table names on success, a MDB2 error on failure
  365.      * @access public
  366.      */
  367.     function listTables()
  368.     {
  369.         $db =$this->getDBInstance();
  370.         if (MDB2::isError($db)) {
  371.             return $db;
  372.         }
  373.  
  374.         $table_names $db->queryCol('SELECT "table_name"'
  375.                 . ' FROM information_schema.tables'
  376.                 . ' t0, information_schema.schemata t1'
  377.                 . ' WHERE t0.schema_pk=t1.schema_pk AND'
  378.                 . ' "table_type" = \'BASE TABLE\''
  379.                 . ' AND "schema_name" = current_schema');
  380.         if (MDB2::isError($table_names)) {
  381.             return $table_names;
  382.         }
  383.         $result = array();
  384.         for ($i = 0$j count($table_names)$i $j; ++$i{
  385.             if (!$this->_fixSequenceName($table_names[$i]true)) {
  386.                 $result[$table_names[$i];
  387.             }
  388.         }
  389.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  390.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  391.         }
  392.         return $result;
  393.     }
  394.  
  395.     // }}}
  396.     // {{{ listTableFields()
  397.  
  398.     /**
  399.      * list all fields in a table in the current database
  400.      *
  401.      * @param string $table name of table that should be used in method
  402.      * @return mixed array of field names on success, a MDB2 error on failure
  403.      * @access public
  404.      */
  405.     function listTableFields($table)
  406.     {
  407.         $db =$this->getDBInstance();
  408.         if (MDB2::isError($db)) {
  409.             return $db;
  410.         }
  411.  
  412.         $table $db->quoteIdentifier($tabletrue);
  413.         $result $db->queryCol("SHOW COLUMNS FROM $table");
  414.         if (MDB2::isError($result)) {
  415.             return $result;
  416.         }
  417.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  418.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  419.         }
  420.         return $result;
  421.     }
  422.  
  423.     // }}}
  424.     // {{{ dropIndex()
  425.  
  426.     /**
  427.      * drop existing index
  428.      *
  429.      * @param string    $table         name of table that should be used in method
  430.      * @param string    $name         name of the index to be dropped
  431.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  432.      * @access public
  433.      */
  434.     function dropIndex($table$name)
  435.     {
  436.         $db =$this->getDBInstance();
  437.         if (MDB2::isError($db)) {
  438.             return $db;
  439.         }
  440.  
  441.         $table $db->quoteIdentifier($tabletrue);
  442.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  443.         $result $db->exec("ALTER TABLE $table DROP INDEX $name");
  444.         if (MDB2::isError($result)) {
  445.             return $result;
  446.         }
  447.         return MDB2_OK;
  448.     }
  449.  
  450.     // }}}
  451.     // {{{ listTableIndexes()
  452.  
  453.     /**
  454.      * list all indexes in a table
  455.      *
  456.      * @param string $table name of table that should be used in method
  457.      * @return mixed array of index names on success, a MDB2 error on failure
  458.      * @access public
  459.      */
  460.     function listTableIndexes($table)
  461.     {
  462.         $db =$this->getDBInstance();
  463.         if (MDB2::isError($db)) {
  464.             return $db;
  465.         }
  466.  
  467.         $key_name 'Key_name';
  468.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  469.             if ($db->options['field_case'== CASE_LOWER{
  470.                 $key_name strtolower($key_name);
  471.             else {
  472.                 $key_name strtoupper($key_name);
  473.             }
  474.         }
  475.  
  476.         $table $db->quoteIdentifier($tabletrue);
  477.         $query = "SHOW INDEX FROM $table";
  478.         $indexes $db->queryCol($query'text'$key_name);
  479.         if (MDB2::isError($indexes)) {
  480.             return $indexes;
  481.         }
  482.  
  483.         $result = array();
  484.         foreach ($indexes as $index{
  485.             if ($index != 'PRIMARY' && ($index $this->_fixIndexName($index))) {
  486.                 $result[$index= true;
  487.             }
  488.         }
  489.  
  490.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  491.             $result array_change_key_case($result$db->options['field_case']);
  492.         }
  493.         return array_keys($result);
  494.     }
  495.  
  496.     // }}}
  497.     // {{{ createSequence()
  498.  
  499.     /**
  500.      * create sequence
  501.      *
  502.      * @param string    $seq_name     name of the sequence to be created
  503.      * @param string    $start         start value of the sequence; default is 1
  504.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  505.      * @access public
  506.      */
  507.     function createSequence($seq_name$start = 1)
  508.     {
  509.         $db =$this->getDBInstance();
  510.         if (MDB2::isError($db)) {
  511.             return $db;
  512.         }
  513.  
  514.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  515.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  516.         $query = "CREATE TABLE $sequence_name ($seqcol_name INTEGER DEFAULT UNIQUE, PRIMARY KEY($seqcol_name))";
  517.         $res $db->exec($query);
  518.         $res $db->exec("SET UNIQUE = 1 FOR $sequence_name");
  519.         if (MDB2::isError($res)) {
  520.             return $res;
  521.         }
  522.         if ($start == 1{
  523.             return MDB2_OK;
  524.         }
  525.         $res $db->exec("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  526.         if (!MDB2::isError($res)) {
  527.             return MDB2_OK;
  528.         }
  529.         // Handle error
  530.         $result $db->exec("DROP TABLE $sequence_name");
  531.         if (MDB2::isError($result)) {
  532.             return $db->raiseError($resultnullnull,
  533.                 'could not drop inconsistent sequence table'__FUNCTION__);
  534.         }
  535.         return $db->raiseError($resnullnull,
  536.             'could not create sequence table'__FUNCTION__);
  537.     }
  538.  
  539.     // }}}
  540.     // {{{ dropSequence()
  541.  
  542.     /**
  543.      * drop existing sequence
  544.      *
  545.      * @param string    $seq_name     name of the sequence to be dropped
  546.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  547.      * @access public
  548.      */
  549.     function dropSequence($seq_name)
  550.     {
  551.         $db =$this->getDBInstance();
  552.         if (MDB2::isError($db)) {
  553.             return $db;
  554.         }
  555.  
  556.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  557.         $result $db->exec("DROP TABLE $sequence_name CASCADE");
  558.         if (MDB2::isError($result)) {
  559.             return $result;
  560.         }
  561.         return MDB2_OK;
  562.     }
  563.  
  564.     // }}}
  565.     // {{{ listSequences()
  566.  
  567.     /**
  568.      * list all sequences in the current database
  569.      *
  570.      * @return mixed array of sequence names on success, a MDB2 error on failure
  571.      * @access public
  572.      */
  573.     function listSequences()
  574.     {
  575.         $db =$this->getDBInstance();
  576.         if (MDB2::isError($db)) {
  577.             return $db;
  578.         }
  579.  
  580.         $table_names $db->queryCol('SHOW TABLES''text');
  581.         if (MDB2::isError($table_names)) {
  582.             return $table_names;
  583.         }
  584.         $result = array();
  585.         for ($i = 0$j count($table_names)$i $j; ++$i{
  586.             if ($sqn $this->_fixSequenceName($table_names[$i]true)) {
  587.                 $result[$sqn;
  588.             }
  589.         }
  590.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  591.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  592.         }
  593.         return $result;
  594.     }
  595.     // }}}
  596. }
  597. ?>

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