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

Source for file Schema.php

Documentation is available at Schema.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                                         |
  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: Schema.php,v 1.103 2006/10/21 15:00:24 ifeghali Exp $
  46. //
  47.  
  48. require_once 'MDB2.php';
  49.  
  50. define('MDB2_SCHEMA_DUMP_ALL',          0);
  51. define('MDB2_SCHEMA_DUMP_STRUCTURE',    1);
  52. define('MDB2_SCHEMA_DUMP_CONTENT',      2);
  53.  
  54. /**
  55.  * If you add an error code here, make sure you also add a textual
  56.  * version of it in MDB2_Schema::errorMessage().
  57.  */
  58.  
  59. define('MDB2_SCHEMA_ERROR',                                         -1);
  60. define('MDB2_SCHEMA_ERROR_PARSE',                                   -2);
  61. define('MDB2_SCHEMA_ERROR_VALIDATE',                                -3);
  62. define('MDB2_SCHEMA_ERROR_UNSUPPORTED',                             -4);    // Driver does not support this function
  63. define('MDB2_SCHEMA_ERROR_INVALID',                                 -5);    // Invalid attribute value
  64. define('MDB2_SCHEMA_ERROR_WRITER',                                  -6);
  65.  
  66. /**
  67.  * The database manager is a class that provides a set of database
  68.  * management services like installing, altering and dumping the data
  69.  * structures of databases.
  70.  *
  71.  * @package MDB2_Schema
  72.  * @category Database
  73.  * @author  Lukas Smith <smith@pooteeweet.org>
  74.  */
  75. class MDB2_Schema extends PEAR
  76. {
  77.     // {{{ properties
  78.  
  79.     var $db;
  80.  
  81.     var $warnings = array();
  82.  
  83.     var $options = array(
  84.         'fail_on_invalid_names' => true,
  85.         'dtd_file' => false,
  86.         'valid_types' => array(),
  87.         'force_defaults' => true,
  88.         'parser' => 'MDB2_Schema_Parser',
  89.         'writer' => 'MDB2_Schema_Writer',
  90.     );
  91.  
  92.     // }}}
  93.     // {{{ apiVersion()
  94.  
  95.     /**
  96.      * Return the MDB2 API version
  97.      *
  98.      * @return string  the MDB2 API version number
  99.      * @access public
  100.      */
  101.     function apiVersion()
  102.     {
  103.         return '0.4.3';
  104.     }
  105.  
  106.     // }}}
  107.     // {{{ arrayMergeClobber()
  108.  
  109.     /**
  110.      * Clobbers two arrays together
  111.      *
  112.      * @param  array        array that should be clobbered
  113.      * @param  array        array that should be clobbered
  114.      * @return array|false array on success and false on error
  115.      *
  116.      * @access public
  117.      * @author kc@hireability.com
  118.      */
  119.     function arrayMergeClobber($a1$a2)
  120.     {
  121.         if (!is_array($a1|| !is_array($a2)) {
  122.             return false;
  123.         }
  124.         foreach ($a2 as $key => $val{
  125.             if (is_array($val&& array_key_exists($key$a1&& is_array($a1[$key])) {
  126.                 $a1[$keyMDB2_Schema::arrayMergeClobber($a1[$key]$val);
  127.             else {
  128.                 $a1[$key$val;
  129.             }
  130.         }
  131.         return $a1;
  132.     }
  133.  
  134.     // }}}
  135.     // {{{ resetWarnings()
  136.  
  137.     /**
  138.      * reset the warning array
  139.      *
  140.      * @access public
  141.      */
  142.     function resetWarnings()
  143.     {
  144.         $this->warnings = array();
  145.     }
  146.  
  147.     // }}}
  148.     // {{{ getWarnings()
  149.  
  150.     /**
  151.      * Get all warnings in reverse order
  152.      *
  153.      * This means that the last warning is the first element in the array
  154.      *
  155.      * @return array with warnings
  156.      * @access public
  157.      * @see resetWarnings()
  158.      */
  159.     function getWarnings()
  160.     {
  161.         return array_reverse($this->warnings);
  162.     }
  163.  
  164.     // }}}
  165.     // {{{ setOption()
  166.  
  167.     /**
  168.      * Sets the option for the db class
  169.      *
  170.      * @param string option name
  171.      * @param mixed value for the option
  172.      * @return bool|MDB2_ErrorMDB2_OK or error object
  173.      * @access public
  174.      */
  175.     function setOption($option$value)
  176.     {
  177.         if (isset($this->options[$option])) {
  178.             if (is_null($value)) {
  179.                 return $this->raiseError(MDB2_SCHEMA_ERRORnullnull,
  180.                     'may not set an option to value null');
  181.             }
  182.             $this->options[$option$value;
  183.             return MDB2_OK;
  184.         }
  185.         return $this->raiseError(MDB2_SCHEMA_ERROR_UNSUPPORTEDnullnull,
  186.             "unknown option $option");
  187.     }
  188.  
  189.     // }}}
  190.     // {{{ getOption()
  191.  
  192.     /**
  193.      * returns the value of an option
  194.      *
  195.      * @param string option name
  196.      * @return mixed the option value or error object
  197.      * @access public
  198.      */
  199.     function getOption($option)
  200.     {
  201.         if (isset($this->options[$option])) {
  202.             return $this->options[$option];
  203.         }
  204.         return $this->raiseError(MDB2_SCHEMA_ERROR_UNSUPPORTED,
  205.             nullnull"unknown option $option");
  206.     }
  207.  
  208.     // }}}
  209.     // {{{ factory()
  210.  
  211.     /**
  212.      * Create a new MDB2 object for the specified database type
  213.      * type
  214.      *
  215.      * @param string|array|MDB2_Driver_Common  'data source name', see the
  216.      *              @see MDB2::parseDSN method for a description of the dsn format.
  217.      *               Can also be specified as an array of the
  218.      *               format returned by @see MDB2::parseDSN.
  219.      *               Finally you can also pass an existing db object to be used.
  220.      * @param array An associative array of option names and their values.
  221.      * @return bool|MDB2_ErrorMDB2_OK or error object
  222.      * @access public
  223.      * @see     MDB2::parseDSN
  224.      */
  225.     function &factory(&$db$options = array())
  226.     {
  227.         $obj =new MDB2_Schema();
  228.         $err $obj->connect($db$options);
  229.         if (PEAR::isError($err)) {
  230.             return $err;
  231.         }
  232.         return $obj;
  233.     }
  234.  
  235.     // }}}
  236.     // {{{ connect()
  237.  
  238.     /**
  239.      * Create a new MDB2 connection object and connect to the specified
  240.      * database
  241.      *
  242.      * @param string|array|MDB2_Driver_Common  'data source name', see the
  243.      *              @see MDB2::parseDSN method for a description of the dsn format.
  244.      *               Can also be specified as an array of the
  245.      *               format returned by @see MDB2::parseDSN.
  246.      *               Finally you can also pass an existing db object to be used.
  247.      * @param array An associative array of option names and their values.
  248.      * @return bool|MDB2_ErrorMDB2_OK or error object
  249.      * @access public
  250.      * @see    MDB2::parseDSN
  251.      */
  252.     function connect(&$db$options = array())
  253.     {
  254.         $db_options = array();
  255.         if (is_array($options)) {
  256.             foreach ($options as $option => $value{
  257.                 if (array_key_exists($option$this->options)) {
  258.                     $err $this->setOption($option$value);
  259.                     if (PEAR::isError($err)) {
  260.                         return $err;
  261.                     }
  262.                 else {
  263.                     $db_options[$option$value;
  264.                 }
  265.             }
  266.         }
  267.         $this->disconnect();
  268.         if (!MDB2::isConnection($db)) {
  269.             $db =MDB2::factory($db$db_options);
  270.         }
  271.         if (PEAR::isError($db)) {
  272.             return $db;
  273.         }
  274.  
  275.         $this->db =$db;
  276.         $this->db->loadModule('Datatype');
  277.         $this->db->loadModule('Manager');
  278.         $this->db->loadModule('Reverse');
  279.         $this->db->loadModule('Function');
  280.         if (empty($this->options['valid_types'])) {
  281.             $this->options['valid_types'$this->db->datatype->getValidTypes();
  282.         }
  283.  
  284.         return MDB2_OK;
  285.     }
  286.  
  287.     // }}}
  288.     // {{{ disconnect()
  289.  
  290.     /**
  291.      * Log out and disconnect from the database.
  292.      *
  293.      * @access public
  294.      */
  295.     function disconnect()
  296.     {
  297.         if (MDB2::isConnection($this->db)) {
  298.             $this->db->disconnect();
  299.             unset($this->db);
  300.         }
  301.     }
  302.  
  303.     // }}}
  304.     // {{{ parseDatabaseDefinition()
  305.  
  306.     /**
  307.      * Parse a database definition from a file or an array
  308.      *
  309.      * @param string|arraythe database schema array or file name
  310.      * @param bool if non readable files should be skipped
  311.      * @param array associative array that the defines the text string values
  312.      *               that are meant to be used to replace the variables that are
  313.      *               used in the schema description.
  314.      * @param bool make function fail on invalid names
  315.      * @param array database structure definition
  316.      * @access public
  317.      */
  318.     function parseDatabaseDefinition($schema$skip_unreadable = false$variables = array(),
  319.         $fail_on_invalid_names = true$structure = false)
  320.     {
  321.         $database_definition = false;
  322.         if (is_string($schema)) {
  323.             // if $schema is not readable then we just skip it
  324.             // and simply copy the $current_schema file to that file name
  325.             if (is_readable($schema)) {
  326.                 $database_definition $this->parseDatabaseDefinitionFile(
  327.                     $schema$variables$fail_on_invalid_names$structure
  328.                 );
  329.             }
  330.         elseif (is_array($schema)) {
  331.             $database_definition $schema;
  332.         }
  333.         if (!$database_definition && !$skip_unreadable{
  334.             $database_definition $this->raiseError(MDB2_SCHEMA_ERRORnullnull,
  335.                 'invalid data type of schema or unreadable data source');
  336.         }
  337.         return $database_definition;
  338.     }
  339.  
  340.     // }}}
  341.     // {{{ parseDatabaseDefinitionFile()
  342.  
  343.     /**
  344.      * Parse a database definition file by creating a schema format
  345.      * parser object and passing the file contents as parser input data stream.
  346.      *
  347.      * @param string the database schema file.
  348.      * @param array associative array that the defines the text string values
  349.      *               that are meant to be used to replace the variables that are
  350.      *               used in the schema description.
  351.      * @param bool make function fail on invalid names
  352.      * @param array database structure definition
  353.      * @access public
  354.      */
  355.     function parseDatabaseDefinitionFile($input_file$variables = array(),
  356.         $fail_on_invalid_names = true$structure = false)
  357.     {
  358.         $dtd_file $this->options['dtd_file'];
  359.         if ($dtd_file{
  360.             require_once 'XML/DTD/XmlValidator.php';
  361.             $dtd =new XML_DTD_XmlValidator;
  362.             if (!$dtd->isValid($dtd_file$input_file)) {
  363.                 return $this->raiseError(MDB2_SCHEMA_ERROR_PARSEnullnull$dtd->getMessage());
  364.             }
  365.         }
  366.  
  367.         $class_name $this->options['parser'];
  368.         $result = MDB2::loadClass($class_name$this->db->getOption('debug'));
  369.         if (PEAR::isError($result)) {
  370.             return $result;
  371.         }
  372.  
  373.         $parser =new $class_name($variables$fail_on_invalid_names$structure$this->options['valid_types']$this->options['force_defaults']);
  374.         $result $parser->setInputFile($input_file);
  375.         if (PEAR::isError($result)) {
  376.             return $result;
  377.         }
  378.  
  379.         $result $parser->parse();
  380.         if (PEAR::isError($result)) {
  381.             return $result;
  382.         }
  383.         if (PEAR::isError($parser->error)) {
  384.             return $parser->error;
  385.         }
  386.  
  387.         return $parser->database_definition;
  388.     }
  389.  
  390.     // }}}
  391.     // {{{ getDefinitionFromDatabase()
  392.  
  393.     /**
  394.      * Attempt to reverse engineer a schema structure from an existing MDB2
  395.      * This method can be used if no xml schema file exists yet.
  396.      * The resulting xml schema file may need some manual adjustments.
  397.      *
  398.      * @return array|MDB2_Errorarray with definition or error object
  399.      * @access public
  400.      */
  401.     function getDefinitionFromDatabase()
  402.     {
  403.         $database $this->db->database_name;
  404.         if (empty($database)) {
  405.             return $this->raiseError(MDB2_SCHEMA_ERROR_INVALIDnullnull,
  406.                 'it was not specified a valid database name');
  407.         }
  408.  
  409.         $database_definition = array(
  410.             'name' => $database,
  411.             'create' => true,
  412.             'overwrite' => false,
  413.             'tables' => array(),
  414.             'sequences' => array(),
  415.         );
  416.  
  417.         $tables $this->db->manager->listTables();
  418.         if (PEAR::isError($tables)) {
  419.             return $tables;
  420.         }
  421.  
  422.         foreach ($tables as $table_name{
  423.             $fields $this->db->manager->listTableFields($table_name);
  424.             if (PEAR::isError($fields)) {
  425.                 return $fields;
  426.             }
  427.  
  428.             $database_definition['tables'][$table_name= array('fields' => array());
  429.             $table_definition =$database_definition['tables'][$table_name];
  430.             foreach ($fields as $field_name{
  431.                 $definition $this->db->reverse->getTableFieldDefinition($table_name$field_name);
  432.                 if (PEAR::isError($definition)) {
  433.                     return $definition;
  434.                 }
  435.  
  436.                 if (!empty($definition[0]['autoincrement'])) {
  437.                     $definition[0]['default'= 0;
  438.                 }
  439.                 $table_definition['fields'][$field_name$definition[0];
  440.                 $field_choices count($definition);
  441.                 if ($field_choices > 1{
  442.                     $warning = "There are $field_choices type choices in the table $table_name field $field_name (#1 is the default): ";
  443.                     $field_choice_cnt = 1;
  444.                     $table_definition['fields'][$field_name]['choices'= array();
  445.                     foreach ($definition as $field_choice{
  446.                         $table_definition['fields'][$field_name]['choices'][$field_choice;
  447.                         $warning.= 'choice #'.($field_choice_cnt).': '.serialize($field_choice);
  448.                         $field_choice_cnt++;
  449.                     }
  450.                     $this->warnings[$warning;
  451.                 }
  452.             }
  453.             $index_definitions = array();
  454.             $indexes $this->db->manager->listTableIndexes($table_name);
  455.             if (PEAR::isError($indexes)) {
  456.                 return $indexes;
  457.             }
  458.  
  459.             if (is_array($indexes&& empty($table_definition['indexes'])) {
  460.                 foreach ($indexes as $index_name{
  461.                     $this->db->expectError(MDB2_ERROR_NOT_FOUND);
  462.                     $definition $this->db->reverse->getTableIndexDefinition($table_name$index_name);
  463.                     $this->db->popExpect();
  464.                     if (PEAR::isError($definition)) {
  465.                         if (PEAR::isError($definitionMDB2_ERROR_NOT_FOUND)) {
  466.                             continue;
  467.                         }
  468.                         return $definition;
  469.                     }
  470.                    $index_definitions[$index_name$definition;
  471.                 }
  472.             }
  473.  
  474.             $constraints $this->db->manager->listTableConstraints($table_name);
  475.             if (PEAR::isError($constraints)) {
  476.                 return $constraints;
  477.             }
  478.  
  479.             if (is_array($constraints&& empty($table_definition['indexes'])) {
  480.                 foreach ($constraints as $index_name{
  481.                     $this->db->expectError(MDB2_ERROR_NOT_FOUND);
  482.                     $definition $this->db->reverse->getTableConstraintDefinition($table_name$index_name);
  483.                     $this->db->popExpect();
  484.                     if (PEAR::isError($definition)) {
  485.                         if (PEAR::isError($definitionMDB2_ERROR_NOT_FOUND)) {
  486.                             continue;
  487.                         }
  488.                         return $definition;
  489.                     }
  490.                     $index_definitions[$index_name$definition;
  491.                 }
  492.             }
  493.             if (!empty($index_definitions)) {
  494.                 $table_definition['indexes'$index_definitions;
  495.             }
  496.         }
  497.  
  498.         $sequences $this->db->manager->listSequences();
  499.         if (PEAR::isError($sequences)) {
  500.             return $sequences;
  501.         }
  502.  
  503.         if (is_array($sequences)) {
  504.             foreach ($sequences as $sequence_name{
  505.                 $definition $this->db->reverse->getSequenceDefinition($sequence_name);
  506.                 if (PEAR::isError($definition)) {
  507.                     return $definition;
  508.                 }
  509.                 if (isset($database_definition['tables'][$sequence_name])
  510.                     && isset($database_definition['tables'][$sequence_name]['indexes'])
  511.                 {
  512.                     foreach ($database_definition['tables'][$sequence_name]['indexes'as $index{
  513.                         if (isset($index['primary']&& $index['primary']
  514.                             && count($index['fields'== 1)
  515.                         {
  516.                             $definition['on'= array(
  517.                                 'table' => $sequence_name,
  518.                                 'field' => key($index['fields']),
  519.                             );
  520.                             break;
  521.                         }
  522.                     }
  523.                 }
  524.                 $database_definition['sequences'][$sequence_name$definition;
  525.             }
  526.         }
  527.         return $database_definition;
  528.     }
  529.  
  530.     // }}}
  531.     // {{{ createTableIndexes()
  532.  
  533.     /**
  534.      * Create indexes on a table
  535.      *
  536.      * @param string  name of the table
  537.      * @param array   indexes to be created
  538.      * @param bool  if the table/index should be overwritten if it already exists
  539.      * @return bool|MDB2_ErrorMDB2_OK or error object
  540.      * @access public
  541.      */
  542.     function createTableIndexes($table_name$indexes$overwrite = false)
  543.     {
  544.         if (!$this->db->supports('indexes')) {
  545.             $this->db->debug('Indexes are not supported'__FUNCTION__);
  546.             return MDB2_OK;
  547.         }
  548.  
  549.         $supports_primary_key $this->db->supports('primary_key');
  550.         foreach ($indexes as $index_name => $index{
  551.             $errorcodes = array(MDB2_ERROR_UNSUPPORTEDMDB2_ERROR_NOT_CAPABLE);
  552.             $this->db->expectError($errorcodes);
  553.             if (!empty($index['primary']|| !empty($index['unique'])) {
  554.                 $indexes $this->db->manager->listTableConstraints($table_name);
  555.             else {
  556.                 $indexes $this->db->manager->listTableIndexes($table_name);
  557.             }
  558.             $this->db->popExpect();
  559.             if (PEAR::isError($indexes)) {
  560.                 if (!MDB2::isError($indexes$errorcodes)) {
  561.                     return $indexes;
  562.                 }
  563.             elseif (is_array($indexes&& in_array($index_name$indexes)) {
  564.                 if (!$overwrite{
  565.                     $this->db->debug('Index already exists: '.$index_name__FUNCTION__);
  566.                     return MDB2_OK;
  567.                 }
  568.                 if (!empty($index['primary']|| !empty($index['unique'])) {
  569.                     $result $this->db->manager->dropConstraint($table_name$index_name);
  570.                 else {
  571.                     $result $this->db->manager->dropIndex($table_name$index_name);
  572.                 }
  573.                 if (PEAR::isError($result)) {
  574.                     return $result;
  575.                 }
  576.                 $this->db->debug('Overwritting index: '.$index_name__FUNCTION__);
  577.             }
  578.  
  579.             // check if primary is being used and if it's supported
  580.             if (!empty($index['primary']&& !$supports_primary_key{
  581.                 /**
  582.                  * Primary not supported so we fallback to UNIQUE
  583.                  * and making the field NOT NULL
  584.                  */
  585.                 unset($index['primary']);
  586.                 $index['unique'= true;
  587.  
  588.                 $changes = array();
  589.  
  590.                 foreach ($index['fields'as $field => $empty{
  591.                     $field_info $this->db->reverse->getTableFieldDefinition($table_name$field);
  592.                     if (PEAR::isError($field_info)) {
  593.                         return $field_info;
  594.                     }
  595.  
  596.                     if (!$field_info[0]['notnull']{
  597.                         $changes['change'][$field$field_info[0];
  598.                         $changes['change'][$field]['notnull'= true;
  599.                     }
  600.                 }
  601.                 if (!empty($changes)) {
  602.                     $this->db->manager->alterTable($table_name$changesfalse);
  603.                 }
  604.             }
  605.  
  606.             if (!empty($index['primary']|| !empty($index['unique'])) {
  607.                 $result $this->db->manager->createConstraint($table_name$index_name$index);
  608.             else {
  609.                 $result $this->db->manager->createIndex($table_name$index_name$index);
  610.             }
  611.             if (PEAR::isError($result)) {
  612.                 return $result;
  613.             }
  614.         }
  615.         return MDB2_OK;
  616.     }
  617.  
  618.     // }}}
  619.     // {{{ createTable()
  620.  
  621.     /**
  622.      * Create a table and inititialize the table if data is available
  623.      *
  624.      * @param string name of the table to be created
  625.      * @param array  multi dimensional array that contains the
  626.      *                structure and optional data of the table
  627.      * @param bool   if the table/index should be overwritten if it already exists
  628.      * @return bool|MDB2_ErrorMDB2_OK or error object
  629.      * @access public
  630.      */
  631.     function createTable($table_name$table$overwrite = false)
  632.     {
  633.         $create = true;
  634.         $errorcodes = array(MDB2_ERROR_UNSUPPORTEDMDB2_ERROR_NOT_CAPABLE);
  635.         $this->db->expectError($errorcodes);
  636.         $tables $this->db->manager->listTables();
  637.         $this->db->popExpect();
  638.         if (PEAR::isError($tables)) {
  639.             if (!MDB2::isError($tables$errorcodes)) {
  640.                 return $tables;
  641.             }
  642.         elseif (is_array($tables&& in_array($table_name$tables)) {
  643.             if (!$overwrite{
  644.                 $create = false;
  645.                 $this->db->debug('Table already exists: '.$table_name__FUNCTION__);
  646.             else {
  647.                 $result $this->db->manager->dropTable($table_name);
  648.                 if (PEAR::isError($result)) {
  649.                     return $result;
  650.                 }
  651.                 $this->db->debug('Overwritting table: '.$table_name__FUNCTION__);
  652.             }
  653.         }
  654.  
  655.         if ($create{
  656.             $result $this->db->manager->createTable($table_name$table['fields']);
  657.             if (PEAR::isError($result)) {
  658.                 return $result;
  659.             }
  660.         }
  661.  
  662.         if (!empty($table['initialization']&& is_array($table['initialization'])) {
  663.             $result $this->initializeTable($table_name$table);
  664.             if (PEAR::isError($result)) {
  665.                 return $result;
  666.             }
  667.         }
  668.  
  669.         if (!empty($table['indexes']&& is_array($table['indexes'])) {
  670.             $result $this->createTableIndexes($table_name$table['indexes']$overwrite);
  671.             if (PEAR::isError($result)) {
  672.                 return $result;
  673.             }
  674.         }
  675.  
  676.         return MDB2_OK;
  677.     }
  678.  
  679.     // }}}
  680.     // {{{ initializeTable()
  681.  
  682.     /**
  683.      * Inititialize the table with data
  684.      *
  685.      * @param string name of the table
  686.      * @param array  multi dimensional array that contains the
  687.      *                structure and optional data of the table
  688.      * @return bool|MDB2_ErrorMDB2_OK or error object
  689.      * @access public
  690.      */
  691.     function initializeTable($table_name$table)
  692.     {
  693.         $query_insert 'INSERT INTO %s (%s) VALUES (%s)';
  694.         $query_update 'UPDATE %s SET %s %s';
  695.         $query_delete 'DELETE FROM %s %s';
  696.  
  697.         $table_name $this->db->quoteIdentifier($table_nametrue);
  698.  
  699.         $result = MDB2_OK;
  700.  
  701.         foreach ($table['initialization'as $instruction{
  702.             $query '';
  703.             switch ($instruction['type']{
  704.             case 'insert':
  705.                 $data $this->getInstructionFields($instruction$table['fields']);
  706.                 if (!empty($data)) {
  707.                     $fields implode(', 'array_keys($data));
  708.                     $values implode(', 'array_values($data));
  709.  
  710.                     $query sprintf($query_insert$table_name$fields$values);
  711.                 }
  712.                 break;
  713.             case 'update':
  714.                 $data $this->getInstructionFields($instruction$table['fields']);
  715.                 $where $this->getInstructionWhere($instruction$table['fields']);
  716.                 if (!empty($data)) {
  717.                     array_walk($dataarray($this'buildFieldValue'));
  718.                     $fields_values implode(', '$data);
  719.  
  720.                     $query sprintf($query_update$table_name$fields_values$where);
  721.                 }
  722.                 break;
  723.             case 'delete':
  724.                 $where $this->getInstructionWhere($instruction$table['fields']);
  725.                 $query sprintf($query_delete$table_name$where);
  726.                 break;
  727.             }
  728.             if ($query{
  729.                 $result $this->db->exec($query);
  730.                 if (PEAR::isError($result)) {
  731.                     return $result;
  732.                 }
  733.             }
  734.         }
  735.         return $result;
  736.     }
  737.  
  738.     // }}}
  739.     // {{{ buildFieldValue()
  740.  
  741.     /**
  742.      * Appends the contents of second argument + '=' to the beginning of first
  743.      * argument.
  744.      *
  745.      * Used with array_walk() in initializeTable() for UPDATEs.
  746.      *
  747.      * @param string  value of array's element
  748.      * @param string  key of array's element
  749.      *
  750.      * @return void 
  751.      *
  752.      * @access public
  753.      * @see MDB2_Schema::initializeTable()
  754.      */
  755.     function buildFieldValue(&$element$key)
  756.     {
  757.        $element $key."=$element";
  758.     }
  759.  
  760.     // }}}
  761.     // {{{ getExpression()
  762.  
  763.     /**
  764.      * Generates a string that represents a value that would be associated
  765.      * with a column in a DML instruction.
  766.      *
  767.      * @param array  multi dimensional array that represents the parsed field
  768.      *                 of a DML instruction.
  769.      * @param array  multi dimensional array that contains the
  770.      *                 definition for current table's fields.
  771.      * @param string  type of given field
  772.      *
  773.      * @return string 
  774.      *
  775.      * @access public
  776.      * @see MDB2_Schema::getInstructionFields(), MDB2_Schema::getInstructionWhere()
  777.      */
  778.     function getExpression($element$fields_definition = array()$type = null)
  779.     {
  780.         $str '';
  781.         switch ($element['type']{
  782.             case 'null':
  783.                 $str.= 'NULL';
  784.             break;
  785.             case 'value':
  786.                 $str.= $this->db->quote($element['data']$type);
  787.             break;
  788.             case 'column':
  789.                 $str.= $this->db->quoteIdentifier($element['data']true);
  790.             break;
  791.             case 'function':
  792.                 $arguments = array();
  793.                 if (!empty($element['data']['arguments'])
  794.                     && is_array($element['data']['arguments'])
  795.                 {
  796.                     foreach ($element['data']['arguments'as $v{
  797.                         $arguments[$this->getExpression($v$fields_definition);
  798.                     }
  799.                 }
  800.                 if (method_exists($this->db->function$element['data']['name'])) {
  801.                     $str.= call_user_func_array(
  802.                         array(&$this->db->function$element['data']['name']),
  803.                         $arguments
  804.                     );
  805.                 else {
  806.                     $str.= $element['data']['name'].'(';
  807.                     $str.= implode(', '$arguments);
  808.                     $str.= ')';
  809.                 }
  810.             break;
  811.             case 'expression':
  812.                 $type0 $type1 = null;
  813.                 if ($element['data']['operants'][0]['type'== 'column'
  814.                     && array_key_exists($element['data']['operants'][0]['data']$fields_definition)
  815.                 {
  816.                     $type0 $fields_definition[$element['data']['operants'][0]['data']]['type'];
  817.                 }
  818.                 if ($element['data']['operants'][1]['type'== 'column'
  819.                     && array_key_exists($element['data']['operants'][1]['data']$fields_definition)
  820.                 {
  821.                     $type1 $fields_definition[$element['data']['operants'][1]['data']]['type'];
  822.                 }
  823.                 $str.= '(';
  824.                 $str.= $this->getExpression($element['data']['operants'][0]$fields_definition$type1);
  825.                 $str.= $this->getOperator($element['data']['operator']);
  826.                 $str.= $this->getExpression($element['data']['operants'][1]$fields_definition$type0);
  827.                 $str.= ')';
  828.             break;
  829.         }
  830.         return $str;
  831.     }
  832.  
  833.     // }}}
  834.     // {{{ getOperator()
  835.  
  836.     /**
  837.      * Returns the matching SQL operator
  838.      *
  839.      * @param string parsed descriptive operator
  840.      *
  841.      * @return string matching SQL operator
  842.      *
  843.      * @access public
  844.      * @static
  845.      * @see MDB2_Schema::getExpression()
  846.      */
  847.     function getOperator($op)
  848.     {
  849.         switch ($op{
  850.         case 'PLUS':
  851.             return ' + ';
  852.         case 'MINUS':
  853.             return ' - ';
  854.         case 'TIMES':
  855.             return ' * ';
  856.         case 'DIVIDED':
  857.             return ' / ';
  858.         case 'EQUAL':
  859.             return ' = ';
  860.         case 'NOT EQUAL':
  861.             return ' != ';
  862.         case 'LESS THAN':
  863.             return ' < ';
  864.         case 'GREATER THAN':
  865.             return ' > ';
  866.         case 'LESS THAN OR EQUAL':
  867.             return ' <= ';
  868.         case 'GREATER THAN OR EQUAL':
  869.             return ' >= ';
  870.         default:
  871.             return ' '.$op.' ';
  872.         }
  873.     }
  874.  
  875.     // }}}
  876.     // {{{ getInstructionFields()
  877.  
  878.     /**
  879.      * Walks the parsed DML instruction array, field by field,
  880.      * storing them and their processed values inside a new array.
  881.      *
  882.      * @param array  multi dimensional array that contains the parsed
  883.      *                 DML instruction to be processed.
  884.      * @param array  multi dimensional array that contains the
  885.      *                 definition for current table's fields.
  886.      *
  887.      * @return array  array of strings in the form 'field_name' => 'value'
  888.      *
  889.      * @access public
  890.      * @static
  891.      * @see MDB2_Schema::initializeTable()
  892.      */
  893.     function getInstructionFields($instruction$fields_definition = array())
  894.     {
  895.         $fields = array();
  896.         if (!empty($instruction['data']['field']&& is_array($instruction['data']['field'])) {
  897.             foreach ($instruction['data']['field'as $field{
  898.                 $fields[$field['name']] $this->getExpression($field['group']$fields_definition);
  899.             }
  900.         }
  901.         return $fields;
  902.     }
  903.  
  904.     // }}}
  905.     // {{{ getInstructionWhere()
  906.  
  907.     /**
  908.      * Translates the parsed WHERE expression of a DML instruction
  909.      * (array structure) to a SQL WHERE clause (string).
  910.      *
  911.      * @param array  multi dimensional array that contains the
  912.      *                 structure of the current DML instruction.
  913.      * @param array  multi dimensional array that contains the
  914.      *                 definition for current table's fields.
  915.      *
  916.      * @return string SQL WHERE clause
  917.      *
  918.      * @access public
  919.      * @static
  920.      * @see MDB2_Schema::initializeTable()
  921.      */
  922.     function getInstructionWhere($instruction$fields_definition = array())
  923.     {
  924.         $where '';
  925.         if (!empty($instruction['data']['where'])) {
  926.             $where 'WHERE '.$this->getExpression($instruction['data']['where']$fields_definition);
  927.         }
  928.         return $where;
  929.     }
  930.  
  931.     // }}}
  932.     // {{{ createSequence()
  933.  
  934.     /**
  935.      * Create a sequence
  936.      *
  937.      * @param string name of the sequence to be created
  938.      * @param array  multi dimensional array that contains the
  939.      *                structure and optional data of the table
  940.      * @param bool  if the sequence should be overwritten if it already exists
  941.      * @return bool|MDB2_ErrorMDB2_OK or error object
  942.      * @access public
  943.      */
  944.     function createSequence($sequence_name$sequence$overwrite = false)
  945.     {
  946.         if (!$this->db->supports('sequences')) {
  947.             $this->db->debug('Sequences are not supported'__FUNCTION__);
  948.             return MDB2_OK;
  949.         }
  950.  
  951.         $errorcodes = array(MDB2_ERROR_UNSUPPORTEDMDB2_ERROR_NOT_CAPABLE);
  952.         $this->db->expectError($errorcodes);
  953.         $sequences $this->db->manager->listSequences();
  954.         $this->db->popExpect();
  955.         if (PEAR::isError($sequences)) {
  956.             if (!MDB2::isError($sequences$errorcodes)) {
  957.                 return $sequences;
  958.             }
  959.         elseif (is_array($sequence&& in_array($sequence_name$sequences)) {
  960.             if (!$overwrite{
  961.                 $this->db->debug('Sequence already exists: '.$sequence_name__FUNCTION__);
  962.                 return MDB2_OK;
  963.             }
  964.  
  965.             $result $this->db->manager->dropSequence($sequence_name);
  966.             if (PEAR::isError($result)) {
  967.                 return $result;
  968.             }
  969.             $this->db->debug('Overwritting sequence: '.$sequence_name__FUNCTION__);
  970.         }
  971.  
  972.         $start = 1;
  973.         $field '';
  974.         if (!empty($sequence['on'])) {
  975.             $table $sequence['on']['table'];
  976.             $field $sequence['on']['field'];
  977.  
  978.             $errorcodes = array(MDB2_ERROR_UNSUPPORTEDMDB2_ERROR_NOT_CAPABLE);
  979.             $this->db->expectError($errorcodes);
  980.             $tables $this->db->manager->listTables();
  981.             $this->db->popExpect();
  982.             if (PEAR::isError($tables&& !MDB2::isError($tables$errorcodes)) {
  983.                  return $tables;
  984.             }
  985.  
  986.             if (!PEAR::isError($tables&& is_array($tables&& in_array($table$tables)) {
  987.                 if ($this->db->supports('summary_functions')) {
  988.                     $query = "SELECT MAX($field) FROM ".$this->quoteIdentifier($tabletrue);
  989.                 else {
  990.                     $query = "SELECT $field FROM ".$this->quoteIdentifier($tabletrue)." ORDER BY $field DESC";
  991.                 }
  992.                 $start $this->db->queryOne($query'integer');
  993.                 if (PEAR::isError($start)) {
  994.                     return $start;
  995.                 }
  996.                 ++$start;
  997.             else {
  998.                 $this->warnings['Could not sync sequence: '.$sequence_name;
  999.             }
  1000.         elseif (!empty($sequence['start']&& is_numeric($sequence['start'])) {
  1001.             $start $sequence['start'];
  1002.             $table '';
  1003.         }
  1004.  
  1005.         $result $this->db->manager->createSequence($sequence_name$start);
  1006.         if (PEAR::isError($result)) {
  1007.             return $result;
  1008.         }
  1009.  
  1010.         return MDB2_OK;
  1011.     }
  1012.  
  1013.     // }}}
  1014.     // {{{ createDatabase()
  1015.  
  1016.     /**
  1017.      * Create a database space within which may be created database objects
  1018.      * like tables, indexes and sequences. The implementation of this function
  1019.      * is highly DBMS specific and may require special permissions to run
  1020.      * successfully. Consult the documentation or the DBMS drivers that you
  1021.      * use to be aware of eventual configuration requirements.
  1022.      *
  1023.      * @param array multi dimensional array that contains the current definition
  1024.      * @return bool|MDB2_ErrorMDB2_OK or error object
  1025.      * @access public
  1026.      */
  1027.     function createDatabase($database_definition)
  1028.     {
  1029.         if (!isset($database_definition['name']|| !$database_definition['name']{
  1030.             return $this->raiseError(MDB2_SCHEMA_ERROR_INVALIDnullnull,
  1031.                 'no valid database name specified');
  1032.         }
  1033.         $create (isset($database_definition['create']&& $database_definition['create']);
  1034.         $overwrite (isset($database_definition['overwrite']&& $database_definition['overwrite']);
  1035.         if ($create{
  1036.             $errorcodes = array(MDB2_ERROR_UNSUPPORTEDMDB2_ERROR_NOT_CAPABLE);
  1037.             $this->db->expectError($errorcodes);
  1038.  
  1039.             /**
  1040.              *
  1041.              * We need to clean up database name before any query to prevent
  1042.              * database driver from using a inexistent database
  1043.              *
  1044.              */
  1045.             $this->db->setDatabase("");
  1046.             $databases $this->db->manager->listDatabases();
  1047.  
  1048.             // Lower / Upper case the db name if the portability deems so.
  1049.             if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1050.                 $func $this->db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper';
  1051.                 $db_name $func($database_definition['name']);
  1052.             }
  1053.  
  1054.             $this->db->popExpect();
  1055.             if (PEAR::isError($databases)) {
  1056.                 if (!MDB2::isError($databases$errorcodes)) {
  1057.                     return $databases;
  1058.                 }
  1059.             elseif (is_array($databases&& isset($db_name&& in_array($db_name$databases)) {
  1060.                 if (!$overwrite{
  1061.                     $this->db->debug('Database already exists: ' $database_definition['name']__FUNCTION__);
  1062.                     $create = false;
  1063.                 else {
  1064.                     $result $this->db->manager->dropDatabase($database_definition['name']);
  1065.                     if (PEAR::isError($result)) {
  1066.                         return $result;
  1067.                     }
  1068.                     $this->db->debug('Overwritting database: '.$database_definition['name']__FUNCTION__);
  1069.                 }
  1070.             }
  1071.             if ($create{
  1072.                 $this->db->expectError(MDB2_ERROR_ALREADY_EXISTS);
  1073.                 $result $this->db->manager->createDatabase($database_definition['name']);
  1074.                 $this->db->popExpect();
  1075.                 if (PEAR::isError($result&& !MDB2::isError($resultMDB2_ERROR_ALREADY_EXISTS)) {
  1076.                     return $result;
  1077.                 }
  1078.             }
  1079.         }
  1080.         $previous_database_name $this->db->setDatabase($database_definition['name']);
  1081.         if (($support_transactions $this->db->supports('transactions'))
  1082.             && PEAR::isError($result $this->db->beginNestedTransaction())
  1083.         {
  1084.             return $result;
  1085.         }
  1086.  
  1087.         $created_objects = 0;
  1088.         if (isset($database_definition['tables'])
  1089.             && is_array($database_definition['tables'])
  1090.         {
  1091.             foreach ($database_definition['tables'as $table_name => $table{
  1092.                 $result $this->createTable($table_name$table$overwrite);
  1093.                 if (PEAR::isError($result)) {
  1094.                     break;
  1095.                 }
  1096.                 $created_objects++;
  1097.             }
  1098.         }
  1099.         if (!PEAR::isError($result)
  1100.             && isset($database_definition['sequences'])
  1101.             && is_array($database_definition['sequences'])
  1102.         {
  1103.             foreach ($database_definition['sequences'as $sequence_name => $sequence{
  1104.                 $result $this->createSequence($sequence_name$sequencefalse$overwrite);
  1105.  
  1106.                 if (PEAR::isError($result)) {
  1107.                     break;
  1108.                 }
  1109.                 $created_objects++;
  1110.             }
  1111.         }
  1112.  
  1113.         if ($support_transactions{
  1114.             $res $this->db->completeNestedTransaction();
  1115.             if (PEAR::isError($res)) {
  1116.                 $result $this->raiseError(MDB2_SCHEMA_ERRORnullnull,
  1117.                     'Could not end transaction ('.
  1118.                     $res->getMessage().' ('.$res->getUserinfo().'))');
  1119.             }
  1120.         elseif (PEAR::isError($result&& $created_objects{
  1121.             $result $this->raiseError(MDB2_SCHEMA_ERRORnullnull,
  1122.                 'the database was only partially created ('.
  1123.                 $result->getMessage().' ('.$result->getUserinfo().'))');
  1124.         }
  1125.  
  1126.         $this->db->setDatabase($previous_database_name);
  1127.  
  1128.         if (PEAR::isError($result&& $create
  1129.             && PEAR::isError($result2 $this->db->manager->dropDatabase($database_definition['name']))
  1130.         {
  1131.             return $this->raiseError(MDB2_SCHEMA_ERRORnullnull,
  1132.                 'Could not drop the created database after unsuccessful creation attempt ('.
  1133.                 $result2->getMessage().' ('.$result2->getUserinfo().'))');
  1134.         }
  1135.  
  1136.         return $result;
  1137.     }
  1138.  
  1139.     // }}}
  1140.     // {{{ compareDefinitions()
  1141.  
  1142.     /**
  1143.      * Compare a previous definition with the currently parsed definition
  1144.      *
  1145.      * @param array multi dimensional array that contains the current definition
  1146.      * @param array multi dimensional array that contains the previous definition
  1147.      * @return array|MDB2_Errorarray of changes on success, or a error object
  1148.      * @access public
  1149.      */
  1150.     function compareDefinitions($current_definition$previous_definition)
  1151.     {
  1152.         $changes = array();
  1153.  
  1154.         if (!empty($current_definition['tables']&& is_array($current_definition['tables'])) {
  1155.             $changes['tables'$defined_tables = array();
  1156.             foreach ($current_definition['tables'as $table_name => $table{
  1157.                 $previous_tables = array();
  1158.                 if (!empty($previous_definition&& is_array($previous_definition)) {
  1159.                     $previous_tables $previous_definition['tables'];
  1160.                 }
  1161.                 $change $this->compareTableDefinitions($table_name$table$previous_tables$defined_tables);
  1162.                 if (PEAR::isError($change)) {
  1163.                     return $change;
  1164.                 }
  1165.                 if (!empty($change)) {
  1166.                     $changes['tables'MDB2_Schema::arrayMergeClobber($changes['tables']$change);
  1167.                 }
  1168.             }
  1169.             if (!empty($previous_definition['tables']&& is_array($previous_definition['tables'])) {
  1170.                 foreach ($previous_definition['tables'as $table_name => $table{
  1171.                     if (empty($defined_tables[$table_name])) {
  1172.                         $changes['remove'][$table_name= true;
  1173.                     }
  1174.                 }
  1175.             }
  1176.         }
  1177.         if (!empty($current_definition['sequences']&& is_array($current_definition['sequences'])) {
  1178.             $changes['sequences'$defined_sequences = array();
  1179.             foreach ($current_definition['sequences'as $sequence_name => $sequence{
  1180.                 $previous_sequences = array();
  1181.                 if (!empty($previous_definition&& is_array($previous_definition)) {
  1182.                     $previous_sequences $previous_definition['sequences'];
  1183.                 }
  1184.                 $change $this->compareSequenceDefinitions(
  1185.                     $sequence_name,
  1186.                     $sequence,
  1187.                     $previous_sequences,
  1188.                     $defined_sequences
  1189.                 );
  1190.                 if (PEAR::isError($change)) {
  1191.                     return $change;
  1192.                 }
  1193.                 if (!empty($change)) {
  1194.                     $changes['sequences'MDB2_Schema::arrayMergeClobber($changes['sequences']$change);
  1195.                 }
  1196.             }
  1197.             if (!empty($previous_definition['sequences']&& is_array($previous_definition['sequences'])) {
  1198.                 foreach ($previous_definition['sequences'as $sequence_name => $sequence{
  1199.                     if (empty($defined_sequences[$sequence_name])) {
  1200.                         $changes['remove'][$sequence_name= true;
  1201.                     }
  1202.                 }
  1203.             }
  1204.         }
  1205.         return $changes;
  1206.     }
  1207.  
  1208.     // }}}
  1209.     // {{{ compareTableFieldsDefinitions()
  1210.  
  1211.     /**
  1212.      * Compare a previous definition with the currently parsed definition
  1213.      *
  1214.      * @param string name of the table
  1215.      * @param array multi dimensional array that contains the current definition
  1216.      * @param array multi dimensional array that contains the previous definition
  1217.      * @return array|MDB2_Errorarray of changes on success, or a error object
  1218.      * @access public
  1219.      */
  1220.     function compareTableFieldsDefinitions($table_name$current_definition,
  1221.         $previous_definition)
  1222.     {
  1223.         $changes $defined_fields = array();
  1224.  
  1225.         if (is_array($current_definition)) {
  1226.             foreach ($current_definition as $field_name => $field{
  1227.                 $was_field_name $field['was'];
  1228.                 if (!empty($previous_definition[$field_name])
  1229.                     && isset($previous_definition[$field_name]['was'])
  1230.                     && $previous_definition[$field_name]['was'== $was_field_name
  1231.                 {
  1232.                     $was_field_name $field_name;
  1233.                 }
  1234.                 if (!empty($previous_definition[$was_field_name])) {
  1235.                     if ($was_field_name != $field_name{
  1236.                         $changes['rename'][$was_field_name= array('name' => $field_name'definition' => $field);
  1237.                     }
  1238.                     if (!empty($defined_fields[$was_field_name])) {
  1239.                         return $this->raiseError(MDB2_SCHEMA_ERROR_INVALIDnullnull,
  1240.                             'the field "'.$was_field_name.
  1241.                             '" was specified for more than one field of table');
  1242.                     }
  1243.                     $defined_fields[$was_field_name= true;
  1244.                     $change $this->db->compareDefinition($field$previous_definition[$was_field_name]);
  1245.                     if (PEAR::isError($change)) {
  1246.                         return $change;
  1247.                     }
  1248.                     if (!empty($change)) {
  1249.                         $change['definition'$field;
  1250.                         $changes['change'][$field_name$change;
  1251.                     }
  1252.                 else {
  1253.                     if ($field_name != $was_field_name{
  1254.                         return $this->raiseError(MDB2_SCHEMA_ERROR_INVALIDnullnull,
  1255.                             'it was specified a previous field name ("'.
  1256.                             $was_field_name.'") for field "'.$field_name.'" of table "'.
  1257.                             $table_name.'" that does not exist');
  1258.                     }
  1259.                     $changes['add'][$field_name$field;
  1260.                 }
  1261.             }
  1262.         }
  1263.         if (isset($previous_definition&& is_array($previous_definition)) {
  1264.             foreach ($previous_definition as $field_previous_name => $field_previous{
  1265.                 if (empty($defined_fields[$field_previous_name])) {
  1266.                     $changes['remove'][$field_previous_name= true;
  1267.                 }
  1268.             }
  1269.         }
  1270.         return $changes;
  1271.     }
  1272.  
  1273.     // }}}
  1274.     // {{{ compareTableIndexesDefinitions()
  1275.  
  1276.     /**
  1277.      * Compare a previous definition with the currently parsed definition
  1278.      *
  1279.      * @param string name of the table
  1280.      * @param array multi dimensional array that contains the current definition
  1281.      * @return array|MDB2_Errorarray of changes on success, or a error object
  1282.      * @access public
  1283.      */
  1284.     function compareTableIndexesDefinitions($table_name$current_definition,
  1285.         $previous_definition)
  1286.     {
  1287.         $changes $defined_indexes = array();
  1288.  
  1289.         if (is_array($current_definition)) {
  1290.             foreach ($current_definition as $index_name => $index{
  1291.                 $was_index_name $index['was'];
  1292.                 if (!empty($previous_definition[$index_name])
  1293.                     && isset($previous_definition[$index_name]['was'])
  1294.                     && $previous_definition[$index_name]['was'== $was_index_name
  1295.                 {
  1296.                     $was_index_name $index_name;
  1297.                 }
  1298.                 if (!empty($previous_definition[$was_index_name])) {
  1299.                     $change = array();
  1300.                     if ($was_index_name != $index_name{
  1301.                         $change['name'$was_index_name;
  1302.                     }
  1303.                     if (!empty($defined_indexes[$was_index_name])) {
  1304.                         return $this->raiseError(MDB2_SCHEMA_ERROR_INVALIDnullnull,
  1305.                             'the index "'.$was_index_name.'" was specified for'.
  1306.                             ' more than one index of table "'.$table_name.'"');
  1307.                     }
  1308.                     $defined_indexes[$was_index_name= true;
  1309.  
  1310.                     $previous_unique array_key_exists('unique'$previous_definition[$was_index_name])
  1311.                         ? $previous_definition[$was_index_name]['unique': false;
  1312.                     $unique array_key_exists('unique'$index$index['unique': false;
  1313.                     if ($previous_unique != $unique{
  1314.                         $change['unique'$unique;
  1315.                     }
  1316.                     $previous_primary array_key_exists('primary'$previous_definition[$was_index_name])
  1317.                         ? $previous_definition[$was_index_name]['primary': false;
  1318.                     $primary array_key_exists('primary'$index$index['primary': false;
  1319.                     if ($previous_primary != $primary{
  1320.                         $change['primary'$primary;
  1321.                     }
  1322.                     $defined_fields = array();
  1323.                     $previous_fields $previous_definition[$was_index_name]['fields'];
  1324.                     if (!empty($index['fields']&& is_array($index['fields'])) {
  1325.                         foreach ($index['fields'as $field_name => $field{
  1326.                             if (!empty($previous_fields[$field_name])) {
  1327.                                 $defined_fields[$field_name= true;
  1328.                                 $previous_sorting array_key_exists('sorting'$previous_fields[$field_name])
  1329.                                     ? $previous_fields[$field_name]['sorting''';
  1330.                                 $sorting array_key_exists('sorting'$field$field['sorting''';
  1331.                                 if ($previous_sorting != $sorting{
  1332.                                     $change['change'= true;
  1333.                                 }
  1334.                             else {
  1335.                                 $change['change'= true;
  1336.                             }
  1337.                         }
  1338.                     }
  1339.                     if (isset($previous_fields&& is_array($previous_fields)) {
  1340.                         foreach ($previous_fields as $field_name => $field{
  1341.                             if (empty($defined_fields[$field_name])) {
  1342.                                 $change['change'= true;
  1343.                             }
  1344.                         }
  1345.                     }
  1346.                     if (!empty($change)) {
  1347.                         $changes['change'][$index_name$current_definition[$index_name];
  1348.                     }
  1349.                 else {
  1350.                     if ($index_name != $was_index_name{
  1351.                         return $this->raiseError(MDB2_SCHEMA_ERROR_INVALIDnullnull,
  1352.                             'it was specified a previous index name ("'.$was_index_name.
  1353.                             ') for index "'.$index_name.'" of table "'.$table_name.'" that does not exist');
  1354.                     }
  1355.                     $changes['add'][$index_name$current_definition[$index_name];
  1356.                 }
  1357.             }
  1358.         }
  1359.         foreach ($previous_definition as $index_previous_name => $index_previous{
  1360.             if (empty($defined_indexes[$index_previous_name])) {
  1361.                 $changes['remove'][$index_previous_name= true;
  1362.             }
  1363.         }
  1364.         return $changes;
  1365.     }
  1366.  
  1367.     // }}}
  1368.     // {{{ compareTableDefinitions()
  1369.  
  1370.     /**
  1371.      * Compare a previous definition with the currently parsed definition
  1372.      *
  1373.      * @param string name of the table
  1374.      * @param array multi dimensional array that contains the current definition
  1375.      * @param array multi dimensional array that contains the previous definition
  1376.      * @param array table names in the schema
  1377.      * @return array|MDB2_Errorarray of changes on success, or a error object
  1378.      * @access public
  1379.      */
  1380.     function compareTableDefinitions($table_name$current_definition,
  1381.         $previous_definition&$defined_tables)
  1382.     {
  1383.         $changes = array();
  1384.  
  1385.         if (is_array($current_definition)) {
  1386.             $was_table_name $table_name;
  1387.             if (!empty($current_definition['was'])) {
  1388.                 $was_table_name $current_definition['was'];
  1389.             }
  1390.             if (!empty($previous_definition[$was_table_name])) {
  1391.                 $changes['change'][$was_table_name= array();
  1392.                 if ($was_table_name != $table_name{
  1393.                     $changes['change'][$was_table_name= array('name' => $table_name);
  1394.                 }
  1395.                 if (!empty($defined_tables[$was_table_name])) {
  1396.                     return $this->raiseError(MDB2_SCHEMA_ERROR_INVALIDnullnull,
  1397.                         'the table "'.$was_table_name.
  1398.                         '" was specified for more than one table of the database');
  1399.                 }
  1400.                 $defined_tables[$was_table_name= true;
  1401.                 if (!empty($current_definition['fields']&& is_array($current_definition['fields'])) {
  1402.                     $previous_fields = array();
  1403.                     if (isset($previous_definition[$was_table_name]['fields'])
  1404.                         && is_array($previous_definition[$was_table_name]['fields'])
  1405.                     {
  1406.                         $previous_fields $previous_definition[$was_table_name]['fields'];
  1407.                     }
  1408.                     $change $this->compareTableFieldsDefinitions(
  1409.                         $table_name,
  1410.                         $current_definition['fields'],
  1411.                         $previous_fields
  1412.                     );
  1413.                     if (PEAR::isError($change)) {
  1414.                         return $change;
  1415.                     }
  1416.                     if (!empty($change)) {
  1417.                         $changes['change'][$was_table_name=
  1418.                             MDB2_Schema::arrayMergeClobber($changes['change'][$was_table_name]$change);
  1419.                     }
  1420.                 }
  1421.                 if (!empty($current_definition['indexes']&& is_array($current_definition['indexes'])) {
  1422.                     $previous_indexes = array();
  1423.                     if (isset($previous_definition[$was_table_name]['indexes'])
  1424.                         && is_array($previous_definition[$was_table_name]['indexes'])
  1425.                     {
  1426.                         $previous_indexes $previous_definition[$was_table_name]['indexes'];
  1427.                     }
  1428.                     $change $this->compareTableIndexesDefinitions(
  1429.                         $table_name,
  1430.                         $current_definition['indexes'],
  1431.                         $previous_indexes
  1432.                     );
  1433.                     if (PEAR::isError($change)) {
  1434.                         return $change;
  1435.                     }
  1436.                     if (!empty($change)) {
  1437.                         $changes['change'][$was_table_name]['indexes'$change;
  1438.                     }
  1439.                 }
  1440.                 if (empty($changes['change'][$was_table_name])) {
  1441.                     unset($changes['change'][$was_table_name]);
  1442.                 }
  1443.                 if (empty($changes['change'])) {
  1444.                     unset($changes['change']);
  1445.                 }
  1446.             else {
  1447.                 if ($table_name != $was_table_name{
  1448.                     return $this->raiseError(MDB2_SCHEMA_ERROR_INVALIDnullnull,
  1449.                         'it was specified a previous table name ("'.$was_table_name.
  1450.                         '") for table "'.$table_name.'" that does not exist');
  1451.                 }
  1452.                 $changes['add'][$table_name= true;
  1453.             }
  1454.         }
  1455.  
  1456.         return $changes;
  1457.     }
  1458.  
  1459.     // }}}
  1460.     // {{{ compareSequenceDefinitions()
  1461.  
  1462.     /**
  1463.      * Compare a previous definition with the currently parsed definition
  1464.      *
  1465.      * @param string name of the sequence
  1466.      * @param array multi dimensional array that contains the current definition
  1467.      * @param array multi dimensional array that contains the previous definition
  1468.      * @param array sequence names in the schema
  1469.      * @return array|MDB2_Errorarray of changes on success, or a error object
  1470.      * @access public
  1471.      */
  1472.     function compareSequenceDefinitions($sequence_name$current_definition,
  1473.         $previous_definition&$defined_sequences)
  1474.     {
  1475.         $changes = array();
  1476.  
  1477.         if (is_array($current_definition)) {
  1478.             $was_sequence_name $sequence_name;
  1479.             if (!empty($previous_definition[$sequence_name])
  1480.                 && isset($previous_definition[$sequence_name]['was'])
  1481.                 && $previous_definition[$sequence_name]['was'== $was_sequence_name
  1482.             {
  1483.                 $was_sequence_name $sequence_name;
  1484.             elseif (!empty($current_definition['was'])) {
  1485.                 $was_sequence_name $current_definition['was'];
  1486.             }
  1487.             if (!empty($previous_definition[$was_sequence_name])) {
  1488.                 if ($was_sequence_name != $sequence_name{
  1489.                     $changes['change'][$was_sequence_name]['name'$sequence_name;
  1490.                 }
  1491.                 if (!empty($defined_sequences[$was_sequence_name])) {
  1492.                     return $this->raiseError(MDB2_SCHEMA_ERROR_INVALIDnullnull,
  1493.                         'the sequence "'.$was_sequence_name.'" was specified as base'.
  1494.                         ' of more than of sequence of the database');
  1495.                 }
  1496.                 $defined_sequences[$was_sequence_name= true;
  1497.                 $change = array();
  1498.                 if (!empty($current_definition['start'])
  1499.                     && isset($previous_definition[$was_sequence_name]['start'])
  1500.                     && $current_definition['start'!= $previous_definition[$was_sequence_name]['start']
  1501.                 {
  1502.                     $change['start'$previous_definition[$sequence_name]['start'];
  1503.                 }
  1504.                 if (isset($current_definition['on']['table'])
  1505.                     && isset($previous_definition[$was_sequence_name]['on']['table'])
  1506.                     && $current_definition['on']['table'!= $previous_definition[$was_sequence_name]['on']['table']
  1507.                     && isset($current_definition['on']['field'])
  1508.                     && isset($previous_definition[$was_sequence_name]['on']['field'])
  1509.                     && $current_definition['on']['field'!= $previous_definition[$was_sequence_name]['on']['field']
  1510.                 {
  1511.                     $change['on'$current_definition['on'];
  1512.                 }
  1513.                 if (!empty($change)) {
  1514.                     $changes['change'][$was_sequence_name][$sequence_name$change;
  1515.                 }
  1516.             else {
  1517.                 if ($sequence_name != $was_sequence_name{
  1518.                     return $this->raiseError(MDB2_SCHEMA_ERROR_INVALIDnullnull,
  1519.                         'it was specified a previous sequence name ("'.$was_sequence_name.
  1520.                         '") for sequence "'.$sequence_name.'" that does not exist');
  1521.                 }
  1522.                 $changes['add'][$sequence_name= true;
  1523.             }
  1524.         }
  1525.         return $changes;
  1526.     }
  1527.     // }}}
  1528.     // {{{ verifyAlterDatabase()
  1529.  
  1530.     /**
  1531.      * Verify that the changes requested are supported
  1532.      *
  1533.      * @param array associative array that contains the definition of the changes
  1534.      *               that are meant to be applied to the database structure.
  1535.      * @return bool|MDB2_ErrorMDB2_OK or error object
  1536.      * @access public
  1537.      */
  1538.     function verifyAlterDatabase($changes)
  1539.     {
  1540.         if (!empty($changes['tables']['change']&& is_array($changes['tables']['change'])) {
  1541.             foreach ($changes['tables']['change'as $table_name => $table{
  1542.                 if (!empty($table['indexes']&& is_array($table['indexes'])) {
  1543.                     if (!$this->db->supports('indexes')) {
  1544.                         return $this->raiseError(MDB2_SCHEMA_ERROR_UNSUPPORTEDnullnull,
  1545.                             'indexes are not supported');
  1546.                     }
  1547.                     $table_changes count($table['indexes']);
  1548.                     if (!empty($table['indexes']['add'])) {
  1549.                         $table_changes--;
  1550.                     }
  1551.                     if (!empty($table['indexes']['remove'])) {
  1552.                         $table_changes--;
  1553.                     }
  1554.                     if (!empty($table['indexes']['change'])) {
  1555.                         $table_changes--;
  1556.                     }
  1557.                     if ($table_changes{
  1558.                         return $this->raiseError(MDB2_SCHEMA_ERROR_UNSUPPORTEDnullnull,
  1559.                             'index alteration not yet supported: '.implode(', 'array_keys($table['indexes'])));
  1560.                     }
  1561.                 }
  1562.                 unset($table['indexes']);
  1563.                 $result $this->db->manager->alterTable($table_name$tabletrue);
  1564.                 if (PEAR::isError($result)) {
  1565.                     return $result;
  1566.                 }
  1567.             }
  1568.         }
  1569.         if (!empty($changes['sequences']&& is_array($changes['sequences'])) {
  1570.             if (!$this->db->supports('sequences')) {
  1571.                 return $this->raiseError(MDB2_SCHEMA_ERROR_UNSUPPORTEDnullnull,
  1572.                     'sequences are not supported');
  1573.             }
  1574.             $sequence_changes count($changes['sequences']);
  1575.             if (!empty($changes['sequences']['add'])) {
  1576.                 $sequence_changes--;
  1577.             }
  1578.             if (!empty($changes['sequences']['remove'])) {
  1579.                 $sequence_changes--;
  1580.             }
  1581.             if (!empty($changes['sequences']['change'])) {
  1582.                 $sequence_changes--;
  1583.             }
  1584.             if ($sequence_changes{
  1585.                 return $this->raiseError(MDB2_SCHEMA_ERROR_UNSUPPORTEDnullnull,
  1586.                     'sequence alteration not yet supported: '.implode(', 'array_keys($changes['sequences'])));
  1587.             }
  1588.         }
  1589.         return MDB2_OK;
  1590.     }
  1591.  
  1592.     // }}}
  1593.     // {{{ alterDatabaseIndexes()
  1594.  
  1595.     /**
  1596.      * Execute the necessary actions to implement the requested changes
  1597.      * in the indexes inside a database structure.
  1598.      *
  1599.      * @param string name of the table
  1600.      * @param array associative array that contains the definition of the changes
  1601.      *               that are meant to be applied to the database structure.
  1602.      * @return bool|MDB2_ErrorMDB2_OK or error object
  1603.      * @access public
  1604.      */
  1605.     function alterDatabaseIndexes($table_name$changes)
  1606.     {
  1607.         $alterations = 0;
  1608.         if (empty($changes)) {
  1609.             return $alterations;
  1610.         }
  1611.  
  1612.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  1613.             foreach ($changes['remove'as $index_name => $index{
  1614.                 if (!empty($index['primary']|| !empty($index['unique'])) {
  1615.                     $result $this->db->manager->dropConstraint($table_name$index_name!empty($index['primary']));
  1616.                 else {
  1617.                     $result $this->db->manager->dropIndex($table_name$index_name);
  1618.                 }
  1619.                 if (PEAR::isError($result)) {
  1620.                     return $result;
  1621.                 }
  1622.                 $alterations++;
  1623.             }
  1624.         }
  1625.         if (!empty($changes['change']&& is_array($changes['change'])) {
  1626.             foreach ($changes['change'as $index_name => $index{
  1627.                 if (!empty($index['primary']|| !empty($index['unique'])) {
  1628.                     $result $this->db->manager->dropConstraint($table_name$index_name!empty($index['primary']));
  1629.                     if (PEAR::isError($result)) {
  1630.                         return $result;
  1631.                     }
  1632.                     $result $this->db->manager->createConstraint($table_name$index_name$index);
  1633.                 else {
  1634.                     $result $this->db->manager->dropIndex($table_name$index_name);
  1635.                     if (PEAR::isError($result)) {
  1636.                         return $result;
  1637.                     }
  1638.                     $result $this->db->manager->createIndex($table_name$index_name$index);
  1639.                 }
  1640.                 if (PEAR::isError($result)) {
  1641.                     return $result;
  1642.                 }
  1643.                 $alterations++;
  1644.             }
  1645.         }
  1646.         if (!empty($changes['add']&& is_array($changes['add'])) {
  1647.             foreach ($changes['add'as $index_name => $index{
  1648.                 if (!empty($index['primary']|| !empty($index['unique'])) {
  1649.                     $result $this->db->manager->createConstraint($table_name$index_name$index);
  1650.                 else {
  1651.                     $result $this->db->manager->createIndex($table_name$index_name$index);
  1652.                 }
  1653.                 if (PEAR::isError($result)) {
  1654.                     return $result;
  1655.                 }
  1656.                 $alterations++;
  1657.             }
  1658.         }
  1659.  
  1660.         return $alterations;
  1661.     }
  1662.  
  1663.     // }}}
  1664.     // {{{ alterDatabaseTables()
  1665.  
  1666.     /**
  1667.      * Execute the necessary actions to implement the requested changes
  1668.      * in the tables inside a database structure.
  1669.      *
  1670.      * @param array multi dimensional array that contains the current definition
  1671.      * @param array multi dimensional array that contains the previous definition
  1672.      * @param array associative array that contains the definition of the changes
  1673.      *               that are meant to be applied to the database structure.
  1674.      * @return bool|MDB2_ErrorMDB2_OK or error object
  1675.      * @access public
  1676.      */
  1677.     function alterDatabaseTables($current_definition$previous_definition$changes)
  1678.     {
  1679.         $alterations = 0;
  1680.         if (empty($changes)) {
  1681.             return $alterations;
  1682.         }
  1683.  
  1684.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  1685.             foreach ($changes['remove'as $table_name => $table{
  1686.                 $result $this->db->manager->dropTable($table_name);
  1687.                 if (PEAR::isError($result)) {
  1688.                     return $result;
  1689.                 }
  1690.                 $alterations++;
  1691.             }
  1692.         }
  1693.  
  1694.         if (!empty($changes['add']&& is_array($changes['add'])) {
  1695.             foreach ($changes['add'as $table_name => $table{
  1696.                 $result $this->createTable($table_name$current_definition[$table_name]);
  1697.                 if (PEAR::isError($result)) {
  1698.                     return $result;
  1699.                 }
  1700.                 $alterations++;
  1701.             }
  1702.         }
  1703.  
  1704.         if (!empty($changes['change']&& is_array($changes['change'])) {
  1705.             foreach ($changes['change'as $table_name => $table{
  1706.                 $indexes = array();
  1707.                 if (!empty($table['indexes'])) {
  1708.                     $indexes $table['indexes'];
  1709.                     unset($table['indexes']);
  1710.                 }
  1711.                 if (!empty($indexes['remove'])) {
  1712.                     $result $this->alterDatabaseIndexes($table_namearray('remove' => $indexes['remove']));
  1713.                     if (PEAR::isError($result)) {
  1714.                         return $result;
  1715.                     }
  1716.                     unset($indexes['remove']);
  1717.                     $alterations += $result;
  1718.                 }
  1719.                 $result $this->db->manager->alterTable($table_name$tablefalse);
  1720.                 if (PEAR::isError($result)) {
  1721.                     return $result;
  1722.                 }
  1723.                 $alterations++;
  1724.                 if (!empty($indexes)) {
  1725.                     $result $this->alterDatabaseIndexes($table_name$indexes);
  1726.                     if (PEAR::isError($result)) {
  1727.                         return $result;
  1728.                     }
  1729.                     $alterations += $result;
  1730.                 }
  1731.             }
  1732.         }
  1733.  
  1734.         return $alterations;
  1735.     }
  1736.  
  1737.     // }}}
  1738.     // {{{ alterDatabaseSequences()
  1739.  
  1740.     /**
  1741.      * Execute the necessary actions to implement the requested changes
  1742.      * in the sequences inside a database structure.
  1743.      *
  1744.      * @param array multi dimensional array that contains the current definition
  1745.      * @param array multi dimensional array that contains the previous definition
  1746.      * @param array associative array that contains the definition of the changes
  1747.      *               that are meant to be applied to the database structure.
  1748.      * @return bool|MDB2_ErrorMDB2_OK or error object
  1749.      * @access public
  1750.      */
  1751.     function alterDatabaseSequences($current_definition$previous_definition$changes)
  1752.     {
  1753.         $alterations = 0;
  1754.         if (empty($changes)) {
  1755.             return $alterations;
  1756.         }
  1757.  
  1758.         if (!empty($changes['add']&& is_array($changes['add'])) {
  1759.             foreach ($changes['add'as $sequence_name => $sequence{
  1760.                 $result $this->createSequence($sequence_name$current_definition[$sequence_name]);
  1761.                 if (PEAR::isError($result)) {
  1762.                     return $result;
  1763.                 }
  1764.                 $alterations++;
  1765.             }
  1766.         }
  1767.  
  1768.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  1769.             foreach ($changes['remove'as $sequence_name => $sequence{
  1770.                 $result $this->db->manager->dropSequence($sequence_name);
  1771.                 if (PEAR::isError($result)) {
  1772.                     return $result;
  1773.                 }
  1774.                 $alterations++;
  1775.             }
  1776.         }
  1777.  
  1778.         if (!empty($changes['change']&& is_array($changes['change'])) {
  1779.             foreach ($changes['change'as $sequence_name => $sequence{
  1780.                 $result $this->db->manager->dropSequence($previous_definition[$sequence_name]['was']);
  1781.                 if (PEAR::isError($result)) {
  1782.                     return $result;
  1783.                 }
  1784.                 $result $this->createSequence($sequence_name$sequence);
  1785.                 if (PEAR::isError($result)) {
  1786.                     return $result;
  1787.                 }
  1788.                 $alterations++;
  1789.             }
  1790.         }
  1791.  
  1792.         return $alterations;
  1793.     }
  1794.  
  1795.     // }}}
  1796.     // {{{ alterDatabase()
  1797.  
  1798.     /**
  1799.      * Execute the necessary actions to implement the requested changes
  1800.      * in a database structure.
  1801.      *
  1802.      * @param array multi dimensional array that contains the current definition
  1803.      * @param array multi dimensional array that contains the previous definition
  1804.      * @param array associative array that contains the definition of the changes
  1805.      *               that are meant to be applied to the database structure.
  1806.      * @return bool|MDB2_ErrorMDB2_OK or error object
  1807.      * @access public
  1808.      */
  1809.     function alterDatabase($current_definition$previous_definition$changes)
  1810.     {
  1811.         $alterations = 0;
  1812.         if (empty($changes)) {
  1813.             return $alterations;
  1814.         }
  1815.  
  1816.         $result $this->verifyAlterDatabase($changes);
  1817.         if (PEAR::isError($result)) {
  1818.             return $result;
  1819.         }
  1820.  
  1821.         if (!empty($current_definition['name'])) {
  1822.             $previous_database_name $this->db->setDatabase($current_definition['name']);
  1823.         }
  1824.  
  1825.         if (($support_transactions $this->db->supports('transactions'))
  1826.             && PEAR::isError($result $this->db->beginNestedTransaction())
  1827.         {
  1828.             return $result;
  1829.         }
  1830.  
  1831.         if (!empty($changes['tables']&& !empty($current_definition['tables'])) {
  1832.             $current_tables = isset($current_definition['tables']$current_definition['tables': array();
  1833.             $previous_tables = isset($previous_definition['tables']$previous_definition['tables': array();
  1834.             $result $this->alterDatabaseTables($current_tables$previous_tables$changes['tables']);
  1835.             if (is_numeric($result)) {
  1836.                 $alterations += $result;
  1837.             }
  1838.         }
  1839.  
  1840.         if (!PEAR::isError($result&& !empty($changes['sequences'])) {
  1841.             $current_sequences = isset($current_definition['sequences']$current_definition['sequences': array();
  1842.             $previous_sequences = isset($previous_definition['sequences']$previous_definition['sequences': array();
  1843.             $result $this->alterDatabaseSequences($current_sequences$previous_sequences$changes['sequences']);
  1844.             if (is_numeric($result)) {
  1845.                 $alterations += $result;
  1846.             }
  1847.         }
  1848.  
  1849.         if ($support_transactions{
  1850.             $res $this->db->completeNestedTransaction();
  1851.             if (PEAR::isError($res)) {
  1852.                 $result $this->raiseError(MDB2_SCHEMA_ERRORnullnull,
  1853.                     'Could not end transaction ('.
  1854.                     $res->getMessage().' ('.$res->getUserinfo().'))');
  1855.             }
  1856.         elseif (PEAR::isError($result&& $alterations{
  1857.             $result $this->raiseError(MDB2_SCHEMA_ERRORnullnull,
  1858.                 'the requested database alterations were only partially implemented ('.
  1859.                 $result->getMessage().' ('.$result->getUserinfo().'))');
  1860.         }
  1861.  
  1862.         if (isset($previous_database_name)) {
  1863.             $this->db->setDatabase($previous_database_name);
  1864.         }
  1865.         return $result;
  1866.     }
  1867.  
  1868.     // }}}
  1869.     // {{{ dumpDatabaseChanges()
  1870.  
  1871.     /**
  1872.      * Dump the changes between two database definitions.
  1873.      *
  1874.      * @param array associative array that specifies the list of database
  1875.      *               definitions changes as returned by the _compareDefinitions
  1876.      *               manager class function.
  1877.      * @return bool|MDB2_ErrorMDB2_OK or error object
  1878.      * @access public
  1879.      */
  1880.     function dumpDatabaseChanges($changes)
  1881.     {
  1882.         if (!empty($changes['tables'])) {
  1883.             if (!empty($changes['tables']['add']&& is_array($changes['tables']['add'])) {
  1884.                 foreach ($changes['tables']['add'as $table_name => $table{
  1885.                     $this->db->debug("$table_name:"__FUNCTION__);
  1886.                     $this->db->debug("\tAdded table '$table_name'"__FUNCTION__);
  1887.                 }
  1888.             }
  1889.             if (!empty($changes['tables']['remove']&& is_array($changes['tables']['remove'])) {
  1890.                 foreach ($changes['tables']['remove'as $table_name => $table{
  1891.                     $this->db->debug("$table_name:"__FUNCTION__);
  1892.                     $this->db->debug("\tRemoved table '$table_name'"__FUNCTION__);
  1893.                 }
  1894.             }
  1895.             if (!empty($changes['tables']['change']&& is_array($changes['tables']['change'])) {
  1896.                 foreach ($changes['tables']['change'as $table_name => $table{
  1897.                     if (array_key_exists('name'$table)) {
  1898.                         $this->db->debug("\tRenamed table '$table_name' to '".$table['name']."'"__FUNCTION__);
  1899.                     }
  1900.                     if (!empty($table['add']&& is_array($table['add'])) {
  1901.                         foreach ($table['add'as $field_name => $field{
  1902.                             $this->db->debug("\tAdded field '".$field_name."'"__FUNCTION__);
  1903.                         }
  1904.                     }
  1905.                     if (!empty($table['remove']&& is_array($table['remove'])) {
  1906.                         foreach ($table['remove'as $field_name => $field{
  1907.                             $this->db->debug("\tRemoved field '".$field_name."'"__FUNCTION__);
  1908.                         }
  1909.                     }
  1910.                     if (!empty($table['rename']&& is_array($table['rename'])) {
  1911.                         foreach ($table['rename'as $field_name => $field{
  1912.                             $this->db->debug("\tRenamed field '".$field_name."' to '".$field['name']."'"__FUNCTION__);
  1913.                         }
  1914.                     }
  1915.                     if (!empty($table['change']&& is_array($table['change'])) {
  1916.                         foreach ($table['change'as $field_name => $field{
  1917.                             $field $field['definition'];
  1918.                             if (array_key_exists('type'$field)) {
  1919.                                 $this->db->debug(
  1920.                                     "\tChanged field '$field_name' type to '".$field['type']."'"__FUNCTION__);
  1921.                             }
  1922.                             if (array_key_exists('unsigned'$field)) {
  1923.                                 $this->db->debug(
  1924.                                     "\tChanged field '$field_name' type to '".
  1925.                                     (!empty($field['unsigned']&& $field['unsigned''' 'not ')."unsigned'",
  1926.                                     __FUNCTION__);
  1927.                             }
  1928.                             if (array_key_exists('length'$field)) {
  1929.                                 $this->db->debug(
  1930.                                     "\tChanged field '$field_name' length to '".
  1931.                                     (!empty($field['length']$field['length']'no length')."'"__FUNCTION__);
  1932.                             }
  1933.                             if (array_key_exists('default'$field)) {
  1934.                                 $this->db->debug(
  1935.                                     "\tChanged field '$field_name' default to ".
  1936.                                     (isset($field['default']"'".$field['default']."'" 'NULL')__FUNCTION__);
  1937.                             }
  1938.                             if (array_key_exists('notnull'$field)) {
  1939.                                 $this->db->debug(
  1940.                                    "\tChanged field '$field_name' notnull to ".
  1941.                                     (!empty($field['notnull']&& $field['notnull''true' 'false'),
  1942.                                     __FUNCTION__
  1943.                                 );
  1944.                             }
  1945.                         }
  1946.                     }
  1947.                     if (!empty($table['indexes']&& is_array($table['indexes'])) {
  1948.                         if (!empty($table['indexes']['add']&& is_array($table['indexes']['add'])) {
  1949.                             foreach ($table['indexes']['add'as $index_name => $index{
  1950.                                 $this->db->debug("\tAdded index '".$index_name.
  1951.                                     "' of table '$table_name'"__FUNCTION__);
  1952.                             }
  1953.                         }
  1954.                         if (!empty($table['indexes']['remove']&& is_array($table['indexes']['remove'])) {
  1955.                             foreach ($table['indexes']['remove'as $index_name => $index{
  1956.                                 $this->db->debug("\tRemoved index '".$index_name.
  1957.                                     "' of table '$table_name'"__FUNCTION__);
  1958.                             }
  1959.                         }
  1960.                         if (!empty($table['indexes']['change']&& is_array($table['indexes']['change'])) {
  1961.                             foreach ($table['indexes']['change'as $index_name => $index{
  1962.                                 if (array_key_exists('name'$index)) {
  1963.                                     $this->db->debug(
  1964.                                         "\tRenamed index '".$index_name."' to '".$index['name'].
  1965.                                         "' on table '$table_name'"__FUNCTION__);
  1966.                                 }
  1967.                                 if (array_key_exists('unique'$index)) {
  1968.                                     $this->db->debug(
  1969.                                         "\tChanged index '".$index_name."' unique to '".
  1970.                                         !empty($index['unique'])."' on table '$table_name'"__FUNCTION__);
  1971.                                 }
  1972.                                 if (array_key_exists('primary'$index)) {
  1973.                                     $this->db->debug(
  1974.                                         "\tChanged index '".$index_name."' primary to '".
  1975.                                         !empty($index['primary'])."' on table '$table_name'"__FUNCTION__);
  1976.                                 }
  1977.                                 if (array_key_exists('change'$index)) {
  1978.                                     $this->db->debug("\tChanged index '".$index_name.
  1979.                                         "' on table '$table_name'"__FUNCTION__);
  1980.                                 }
  1981.                             }
  1982.                         }
  1983.                     }
  1984.                 }
  1985.             }
  1986.         }
  1987.         if (!empty($changes['sequences'])) {
  1988.             if (!empty($changes['sequences']['add']&& is_array($changes['sequences']['add'])) {
  1989.                 foreach ($changes['sequences']['add'as $sequence_name => $sequence{
  1990.                     $this->db->debug("$sequence_name:"__FUNCTION__);
  1991.                     $this->db->debug("\tAdded sequence '$sequence_name'"__FUNCTION__);
  1992.                 }
  1993.             }
  1994.             if (!empty($changes['sequences']['remove']&& is_array($changes['sequences']['remove'])) {
  1995.                 foreach ($changes['sequences']['remove'as $sequence_name => $sequence{
  1996.                     $this->db->debug("$sequence_name:"__FUNCTION__);
  1997.                     $this->db->debug("\tAdded sequence '$sequence_name'"__FUNCTION__);
  1998.                 }
  1999.             }
  2000.             if (!empty($changes['sequences']['change']&& is_array($changes['sequences']['change'])) {
  2001.                 foreach ($changes['sequences']['change'as $sequence_name => $sequence{
  2002.                     if (array_key_exists('name'$sequence)) {
  2003.                         $this->db->debug(
  2004.                             "\tRenamed sequence '$sequence_name' to '".
  2005.                             $sequence['name']."'"__FUNCTION__);
  2006.                     }
  2007.                     if (!empty($sequence['change']&& is_array($sequence['change'])) {
  2008.                         foreach ($sequence['change'as $sequence_name => $sequence{
  2009.                             if (array_key_exists('start'$sequence)) {
  2010.                                 $this->db->debug(
  2011.                                     "\tChanged sequence '$sequence_name' start to '".
  2012.                                     $sequence['start']."'"__FUNCTION__);
  2013.                             }
  2014.                         }
  2015.                     }
  2016.                 }
  2017.             }
  2018.         }
  2019.         return MDB2_OK;
  2020.     }
  2021.  
  2022.     // }}}
  2023.     // {{{ dumpDatabase()
  2024.  
  2025.     /**
  2026.      * Dump a previously parsed database structure in the Metabase schema
  2027.      * XML based format suitable for the Metabase parser. This function
  2028.      * may optionally dump the database definition with initialization
  2029.      * commands that specify the data that is currently present in the tables.
  2030.      *
  2031.      * @param array multi dimensional array that contains the current definition
  2032.      * @param array associative array that takes pairs of tag
  2033.      *  names and values that define dump options.
  2034.      *                  <pre>array (
  2035.      *                      'output_mode'    =>    String
  2036.      *                          'file' :   dump into a file
  2037.      *                          default:   dump using a function
  2038.      *                      'output'        =>    String
  2039.      *                          depending on the 'Output_Mode'
  2040.      *                                   name of the file
  2041.      *                                   name of the function
  2042.      *                      'end_of_line'        =>    String
  2043.      *                          end of line delimiter that should be used
  2044.      *                          default: "\n"
  2045.      *                  );</pre>
  2046.      * @param int that determines what data to dump
  2047.      *               + MDB2_SCHEMA_DUMP_ALL       : the entire db
  2048.      *               + MDB2_SCHEMA_DUMP_STRUCTURE : only the structure of the db
  2049.      *               + MDB2_SCHEMA_DUMP_CONTENT   : only the content of the db
  2050.      * @return bool|MDB2_ErrorMDB2_OK or error object
  2051.      * @access public
  2052.      */
  2053.     function dumpDatabase($database_definition$arguments$dump = MDB2_SCHEMA_DUMP_ALL)
  2054.     {
  2055.         $class_name $this->options['writer'];
  2056.         $result = MDB2::loadClass($class_name$this->db->getOption('debug'));
  2057.         if (PEAR::isError($result)) {
  2058.             return $result;
  2059.         }
  2060.  
  2061.         // get initialization data
  2062.         if (isset($database_definition['tables']&& is_array($database_definition['tables'])
  2063.             && $dump == MDB2_SCHEMA_DUMP_ALL || $dump == MDB2_SCHEMA_DUMP_CONTENT
  2064.         {
  2065.             foreach ($database_definition['tables'as $table_name => $table{
  2066.                 $fields = array();
  2067.                 foreach ($table['fields'as $field_name => $field{
  2068.                     $fields[$field_name$field['type'];
  2069.                 }
  2070.                 $query 'SELECT '.implode(', 'array_keys($fields)).' FROM ';
  2071.                 $query.= $this->db->quoteIdentifier($table_nametrue);
  2072.                 $data $this->db->queryAll($query$fieldsMDB2_FETCHMODE_ASSOC);
  2073.                 if (PEAR::isError($data)) {
  2074.                     return $data;
  2075.                 }
  2076.                 if (!empty($data)) {
  2077.                     $initialization = array();
  2078.                     $lob_buffer_length $this->db->getOption('lob_buffer_length');
  2079.                     foreach ($data as $row{
  2080.                         $rows = array();
  2081.                         foreach($row as $key => $lob{
  2082.                             if (is_resource($lob)) {
  2083.                                 $value '';
  2084.                                 while (!feof($lob)) {
  2085.                                     $value.= fread($lob$lob_buffer_length);
  2086.                                 }
  2087.                                 $row[$key$value;
  2088.                             }
  2089.                             $rows[= array('name' => $key'group' => array('type' => 'value''data' => $row[$key]));
  2090.                         }
  2091.                         $initialization[= array('type' => 'insert''data' => array('field' => $rows));
  2092.                     }
  2093.                     $database_definition['tables'][$table_name]['initialization'$initialization;
  2094.                 }
  2095.             }
  2096.         }
  2097.  
  2098.         $writer =new $class_name($this->options['valid_types']);
  2099.         return $writer->dumpDatabase($database_definition$arguments$dump);
  2100.     }
  2101.  
  2102.     // }}}
  2103.     // {{{ writeInitialization()
  2104.  
  2105.     /**
  2106.      * Write initialization and sequences
  2107.      *
  2108.      * @param string|array data file or data array
  2109.      * @param string|array structure file or array
  2110.      * @param array associative array that is passed to the argument
  2111.      *  of the same name to the parseDatabaseDefinitionFile function. (there third
  2112.      *  param)
  2113.      * @return bool|MDB2_ErrorMDB2_OK or error object
  2114.      * @access public
  2115.      */
  2116.     function writeInitialization($data$structure = false$variables = array())
  2117.     {
  2118.         if ($structure{
  2119.             $structure $this->parseDatabaseDefinition($structurefalse$variables);
  2120.             if (PEAR::isError($structure)) {
  2121.                 return $structure;
  2122.             }
  2123.         }
  2124.  
  2125.         $data $this->parseDatabaseDefinition($datafalse$variablesfalse$structure);
  2126.         if (PEAR::isError($data)) {
  2127.             return $data;
  2128.         }
  2129.  
  2130.         $previous_database_name = null;
  2131.         if (!empty($data['name'])) {
  2132.             $previous_database_name $this->db->setDatabase($data['name']);
  2133.         elseif(!empty($structure['name'])) {
  2134.             $previous_database_name $this->db->setDatabase($structure['name']);
  2135.         }
  2136.  
  2137.         if (!empty($data['tables']&& is_array($data['tables'])) {
  2138.             foreach ($data['tables'as $table_name => $table{
  2139.                 if (empty($table['initialization'])) {
  2140.                     continue;
  2141.                 }
  2142.                 $result $this->initializeTable($table_name$table);
  2143.                 if (PEAR::isError($result)) {
  2144.                     return $result;
  2145.                 }
  2146.             }
  2147.         }
  2148.  
  2149.         if (!empty($structure['sequences']&& is_array($structure['sequences'])) {
  2150.             foreach ($structure['sequences'as $sequence_name => $sequence{
  2151.                 if (isset($data['sequences'][$sequence_name])
  2152.                     || !isset($sequence['on']['table'])
  2153.                     || !isset($data['tables'][$sequence['on']['table']])
  2154.                 {
  2155.                     continue;
  2156.                 }
  2157.                 $result $this->createSequence($sequence_name$sequencetrue);
  2158.                 if (PEAR::isError($result)) {
  2159.                     return $result;
  2160.                 }
  2161.             }
  2162.         }
  2163.         if (!empty($data['sequences']&& is_array($data['sequences'])) {
  2164.             foreach ($data['sequences'as $sequence_name => $sequence{
  2165.                 $result $this->createSequence($sequence_name$sequencetrue);
  2166.                 if (PEAR::isError($result)) {
  2167.                     return $result;
  2168.                 }
  2169.             }
  2170.         }
  2171.  
  2172.         if (isset($previous_database_name)) {
  2173.             $this->db->setDatabase($previous_database_name);
  2174.         }
  2175.  
  2176.         return MDB2_OK;
  2177.     }
  2178.  
  2179.     // }}}
  2180.     // {{{ updateDatabase()
  2181.  
  2182.     /**
  2183.      * Compare the correspondent files of two versions of a database schema
  2184.      * definition: the previously installed and the one that defines the schema
  2185.      * that is meant to update the database.
  2186.      * If the specified previous definition file does not exist, this function
  2187.      * will create the database from the definition specified in the current
  2188.      * schema file.
  2189.      * If both files exist, the function assumes that the database was previously
  2190.      * installed based on the previous schema file and will update it by just
  2191.      * applying the changes.
  2192.      * If this function succeeds, the contents of the current schema file are
  2193.      * copied to replace the previous schema file contents. Any subsequent schema
  2194.      * changes should only be done on the file specified by the $current_schema_file
  2195.      * to let this function make a consistent evaluation of the exact changes that
  2196.      * need to be applied.
  2197.      *
  2198.      * @param string|arrayfilename or array of the updated database schema definition.
  2199.      * @param string|arrayfilename or array of the previously installed database schema definition.
  2200.      * @param array associative array that is passed to the argument of the same
  2201.      *               name to the parseDatabaseDefinitionFile function. (there third param)
  2202.      * @param bool determines if the disable_query option should be set to true
  2203.      *               for the alterDatabase() or createDatabase() call
  2204.      * @return bool|MDB2_ErrorMDB2_OK or error object
  2205.      * @access public
  2206.      */
  2207.     function updateDatabase($current_schema$previous_schema = false
  2208.         $variables = array()$disable_query = false)
  2209.     {
  2210.         $current_definition $this->parseDatabaseDefinition(
  2211.             $current_schemafalse$variables$this->options['fail_on_invalid_names']
  2212.         );
  2213.         if (PEAR::isError($current_definition)) {
  2214.             return $current_definition;
  2215.         }
  2216.  
  2217.         $previous_definition = false;
  2218.         if ($previous_schema{
  2219.             $previous_definition $this->parseDatabaseDefinition(
  2220.                 $previous_schematrue$variables$this->options['fail_on_invalid_names']
  2221.             );
  2222.             if (PEAR::isError($previous_definition)) {
  2223.                 return $previous_definition;
  2224.             }
  2225.         }
  2226.  
  2227.         if ($previous_definition{
  2228.             $errorcodes = array(MDB2_ERROR_UNSUPPORTEDMDB2_ERROR_NOT_CAPABLE);
  2229.             $this->db->expectError($errorcodes);
  2230.             $databases $this->db->manager->listDatabases();
  2231.             $this->db->popExpect();
  2232.             if (PEAR::isError($databases)) {
  2233.                 if (!MDB2::isError($databases$errorcodes)) {
  2234.                     return $databases;
  2235.                 }
  2236.             elseif (!is_array($databases||
  2237.                 !in_array($current_definition['name']$databases)
  2238.             {
  2239.                 return $this->raiseError(MDB2_SCHEMA_ERRORnullnull,
  2240.                     'database to update does not exist: '.$current_definition['name']);
  2241.             }
  2242.  
  2243.             $changes $this->compareDefinitions($current_definition$previous_definition);
  2244.             if (PEAR::isError($changes)) {
  2245.                 return $changes;
  2246.             }
  2247.  
  2248.             if (is_array($changes)) {
  2249.                 $this->db->setOption('disable_query'$disable_query);
  2250.                 $result $this->alterDatabase($current_definition$previous_definition$changes);
  2251.                 $this->db->setOption('disable_query'false);
  2252.                 if (PEAR::isError($result)) {
  2253.                     return $result;
  2254.                 }
  2255.                 $copy = true;
  2256.                 if ($this->db->options['debug']{
  2257.                     $result $this->dumpDatabaseChanges($changes);
  2258.                     if (PEAR::isError($result)) {
  2259.                         return $result;
  2260.                     }
  2261.                 }
  2262.             }
  2263.         else {
  2264.             $this->db->setOption('disable_query'$disable_query);
  2265.             $result $this->createDatabase($current_definition);
  2266.             $this->db->setOption('disable_query'false);
  2267.             if (PEAR::isError($result)) {
  2268.                 return $result;
  2269.             }
  2270.         }
  2271.  
  2272.         if (!$disable_query
  2273.             && is_string($previous_schema&& is_string($current_schema)
  2274.             && !copy($current_schema$previous_schema)
  2275.         {
  2276.             return $this->raiseError(MDB2_SCHEMA_ERRORnullnull,
  2277.                 'Could not copy the new database definition file to the current file');
  2278.         }
  2279.  
  2280.         return MDB2_OK;
  2281.     }
  2282.     // }}}
  2283.     // {{{ errorMessage()
  2284.  
  2285.     /**
  2286.      * Return a textual error message for a MDB2 error code
  2287.      *
  2288.      * @param   int|arrayinteger error code,
  2289.      *                      <code>null</code> to get the current error code-message map,
  2290.      *                     or an array with a new error code-message map
  2291.      * @return  string  error message, or false if the error code was not recognized
  2292.      * @access public
  2293.      */
  2294.     function errorMessage($value = null)
  2295.     {
  2296.         static $errorMessages;
  2297.         if (is_array($value)) {
  2298.             $errorMessages $value;
  2299.             return MDB2_OK;
  2300.         elseif (!isset($errorMessages)) {
  2301.             $errorMessages = array(
  2302.                 MDB2_SCHEMA_ERROR              => 'unknown error',
  2303.                 MDB2_SCHEMA_ERROR_PARSE        => 'schema parse error',
  2304.                 MDB2_SCHEMA_ERROR_VALIDATE     => 'schema validation error',
  2305.                 MDB2_SCHEMA_ERROR_INVALID      => 'invalid',
  2306.                 MDB2_SCHEMA_ERROR_UNSUPPORTED  => 'not supported',
  2307.                 MDB2_SCHEMA_ERROR_WRITER       => 'schema writer error',
  2308.             );
  2309.         }
  2310.  
  2311.         if (is_null($value)) {
  2312.             return $errorMessages;
  2313.         }
  2314.  
  2315.         if (PEAR::isError($value)) {
  2316.             $value $value->getCode();
  2317.         }
  2318.  
  2319.         return !empty($errorMessages[$value]?
  2320.            $errorMessages[$value$errorMessages[MDB2_SCHEMA_ERROR];
  2321.     }
  2322.  
  2323.     // }}}
  2324.     // {{{ raiseError()
  2325.  
  2326.     /**
  2327.      * This method is used to communicate an error and invoke error
  2328.      * callbacks etc.  Basically a wrapper for PEAR::raiseError
  2329.      * without the message string.
  2330.      *
  2331.      * @param int|PEAR_Error integer error code or and PEAR_Error instance
  2332.      * @param int      error mode, see PEAR_Error docs
  2333.      *
  2334.      *                  error level (E_USER_NOTICE etc).  If error mode is
  2335.      *                  PEAR_ERROR_CALLBACK, this is the callback function,
  2336.      *                  either as a function name, or as an array of an
  2337.      *                  object and method name.  For other error modes this
  2338.      *                  parameter is ignored.
  2339.      * @param array    Options, depending on the mode, @see PEAR::setErrorHandling
  2340.      * @param string   Extra debug information.  Defaults to the last
  2341.      *                  query and native error code.
  2342.      * @return object  PEAR error object
  2343.      * @access  public
  2344.      * @see PEAR_Error
  2345.      */
  2346.     function &raiseError($code = null$mode = null$options = null$userinfo = null)
  2347.     {
  2348.         $err =PEAR::raiseError(null$code$mode$options$userinfo'MDB2_Schema_Error'true);
  2349.         return $err;
  2350.     }
  2351.  
  2352.     // }}}
  2353.     // {{{ isError()
  2354.  
  2355.     /**
  2356.      * Tell whether a value is an MDB2_Schema error.
  2357.      *
  2358.      * @param   mixed the value to test
  2359.      * @param   int   if $data is an error object, return true only if $code is
  2360.                       a string and $db->getMessage() == $code or
  2361.      *                 $code is an integer and $db->getCode() == $code
  2362.      * @return  bool  true if parameter is an error
  2363.      * @access  public
  2364.      */
  2365.     function isError($data$code = null)
  2366.     {
  2367.         if (is_a($data'MDB2_Schema_Error')) {
  2368.             if (is_null($code)) {
  2369.                 return true;
  2370.             elseif (is_string($code)) {
  2371.                 return $data->getMessage(=== $code;
  2372.             else {
  2373.                 $code = (array)$code;
  2374.                 return in_array($data->getCode()$code);
  2375.             }
  2376.         }
  2377.         return false;
  2378.     }
  2379.  
  2380.     // }}}
  2381. }
  2382.  
  2383. /**
  2384.  * MDB2_Schema_Error implements a class for reporting portable database error
  2385.  * messages.
  2386.  *
  2387.  * @package MDB2_Schema
  2388.  * @category Database
  2389.  * @author  Stig Bakken <ssb@fast.no>
  2390.  */
  2391. class MDB2_Schema_Error extends PEAR_Error
  2392. {
  2393.     /**
  2394.      * MDB2_Schema_Error constructor.
  2395.      *
  2396.      * @param mixed     error code, or string with error message.
  2397.      * @param int       what 'error mode' to operate in
  2398.      * @param int       what error level to use for $mode & PEAR_ERROR_TRIGGER
  2399.      * @param mixed     additional debug info, such as the last query
  2400.      * @access  public
  2401.      */
  2402.     function MDB2_Schema_Error($code = MDB2_SCHEMA_ERROR$mode = PEAR_ERROR_RETURN,
  2403.               $level = E_USER_NOTICE$debuginfo = null)
  2404.     {
  2405.         $this->PEAR_Error('MDB2_Schema Error: ' MDB2_Schema::errorMessage($code)$code,
  2406.             $mode$level$debuginfo);
  2407.     }
  2408. }
  2409. ?>

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