home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / MDB2 / Driver / Manager / fbsql.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  21.4 KB  |  581 lines

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