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

Source for file Writer.php

Documentation is available at Writer.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: Writer.php,v 1.27 2006/04/11 08:21:17 lsmith Exp $
  46. //
  47.  
  48. /**
  49.  * Writes an XML schema file
  50.  *
  51.  * @package MDB2_Schema
  52.  * @category Database
  53.  * @access protected
  54.  * @author  Lukas Smith <smith@pooteeweet.org>
  55.  */
  56. {
  57.     var $valid_types = array();
  58.  
  59.     function __construct($valid_types = array())
  60.     {
  61.         $this->valid_types = $valid_types;
  62.     }
  63.  
  64.     function MDB2_Schema_Writer($valid_types = array())
  65.     {
  66.         $this->__construct($valid_types);
  67.     }
  68.  
  69.     // }}}
  70.     // {{{ raiseError()
  71.  
  72.     /**
  73.      * This method is used to communicate an error and invoke error
  74.      * callbacks etc.  Basically a wrapper for PEAR::raiseError
  75.      * without the message string.
  76.      *
  77.      * @param int|PEAR_Error integer error code or and PEAR_Error instance
  78.      * @param int      error mode, see PEAR_Error docs
  79.      *
  80.      *                  error level (E_USER_NOTICE etc).  If error mode is
  81.      *                  PEAR_ERROR_CALLBACK, this is the callback function,
  82.      *                  either as a function name, or as an array of an
  83.      *                  object and method name.  For other error modes this
  84.      *                  parameter is ignored.
  85.      * @param string   Extra debug information.  Defaults to the last
  86.      *                  query and native error code.
  87.      * @return object  PEAR error object
  88.      * @access  public
  89.      * @see PEAR_Error
  90.      */
  91.     function &raiseError($code = null$mode = null$options = null$userinfo = null)
  92.     {
  93.         $error =MDB2_Schema::raiseError($code$mode$options$userinfo);
  94.         return $error;
  95.     }
  96.  
  97.     // }}}
  98.     // {{{ _escapeSpecialChars()
  99.  
  100.     /**
  101.      * add escapecharacters to all special characters in a string
  102.      *
  103.      * @param string string that should be escaped
  104.      * @return string escaped string
  105.      * @access protected
  106.      */
  107.     function _escapeSpecialChars($string)
  108.     {
  109.         if (!is_string($string)) {
  110.             $string strval($string);
  111.         }
  112.  
  113.         $escaped '';
  114.         for ($char = 0$count strlen($string)$char $count$char++{
  115.             switch ($string[$char]{
  116.             case '&':
  117.                 $escaped .= '&amp;';
  118.                 break;
  119.             case '>':
  120.                 $escaped .= '&gt;';
  121.                 break;
  122.             case '<':
  123.                 $escaped .= '&lt;';
  124.                 break;
  125.             case '"':
  126.                 $escaped .= '&quot;';
  127.                 break;
  128.             case '\'':
  129.                 $escaped .= '&apos;';
  130.                 break;
  131.             default:
  132.                 $code ord($string[$char]);
  133.                 if ($code < 32 || $code > 127{
  134.                     $escaped .= "&#$code;";
  135.                 else {
  136.                     $escaped .= $string[$char];
  137.                 }
  138.                 break;
  139.             }
  140.         }
  141.         return $escaped;
  142.     }
  143.  
  144.     // }}}
  145.     // {{{ _dumpBoolean()
  146.  
  147.     /**
  148.      * dump the structure of a sequence
  149.      *
  150.      * @param string boolean value or variable definition
  151.      * @return string with xml boolea definition
  152.      * @access private
  153.      */
  154.     function _dumpBoolean($boolean)
  155.     {
  156.         if (is_string($boolean)) {
  157.             if ($boolean !== 'true' || $boolean !== 'false'
  158.                 || preg_match('/<variable>.*</variable>/'$boolean)
  159.             {
  160.                 return $boolean;
  161.             }
  162.         }
  163.         return $boolean 'true' 'false';
  164.     }
  165.  
  166.     // }}}
  167.     // {{{ dumpSequence()
  168.  
  169.     /**
  170.      * dump the structure of a sequence
  171.      *
  172.      * @param string sequence name
  173.      * @param string end of line characters
  174.      * @return mixed string xml sequence definition on success, or a error object
  175.      * @access public
  176.      */
  177.     function dumpSequence($sequence_definition$sequence_name$eol$dump = MDB2_SCHEMA_DUMP_ALL)
  178.     {
  179.         $buffer = "$eol <sequence>$eol  <name>$sequence_name</name>$eol";
  180.         if ($dump == MDB2_SCHEMA_DUMP_ALL || $dump == MDB2_SCHEMA_DUMP_CONTENT{
  181.             if (array_key_exists('start'$sequence_definition)) {
  182.                 $start $sequence_definition['start'];
  183.                 $buffer.= "  <start>$start</start>$eol";
  184.             }
  185.         }
  186.  
  187.         if (array_key_exists('on'$sequence_definition)) {
  188.             $buffer.= "  <on>$eol";
  189.             $buffer.= "   <table>".$sequence_definition['on']['table'];
  190.             $buffer.= "</table>$eol   <field>".$sequence_definition['on']['field'];
  191.             $buffer.= "</field>$eol  </on>$eol";
  192.         }
  193.         $buffer.= " </sequence>$eol";
  194.  
  195.         return $buffer;
  196.     }
  197.  
  198.     // }}}
  199.     // {{{ dumpDatabase()
  200.  
  201.     /**
  202.      * Dump a previously parsed database structure in the Metabase schema
  203.      * XML based format suitable for the Metabase parser. This function
  204.      * may optionally dump the database definition with initialization
  205.      * commands that specify the data that is currently present in the tables.
  206.      *
  207.      * @param array associative array that takes pairs of tag
  208.      *               names and values that define dump options.
  209.      *                  array (
  210.      *                      'output_mode'    =>    String
  211.      *                          'file' :   dump into a file
  212.      *                          default:   dump using a function
  213.      *                      'output'        =>    String
  214.      *                          depending on the 'Output_Mode'
  215.      *                                   name of the file
  216.      *                                   name of the function
  217.      *                      'end_of_line'        =>    String
  218.      *                          end of line delimiter that should be used
  219.      *                          default: "\n"
  220.      *                  );
  221.      * @param integer determines what data to dump
  222.      *                       MDB2_SCHEMA_DUMP_ALL       : the entire db
  223.      *                       MDB2_SCHEMA_DUMP_STRUCTURE : only the structure of the db
  224.      *                       MDB2_SCHEMA_DUMP_CONTENT   : only the content of the db
  225.      * @return mixed MDB2_OK on success, or a error object
  226.      * @access public
  227.      */
  228.     function dumpDatabase($database_definition$arguments$dump = MDB2_SCHEMA_DUMP_ALL)
  229.     {
  230.         if (array_key_exists('output'$arguments)) {
  231.             if (array_key_exists('output_mode'$arguments&& $arguments['output_mode'== 'file'{
  232.                 $fp fopen($arguments['output']'w');
  233.                 $output = false;
  234.             elseif (is_callable($arguments['output'])) {
  235.                 $output $arguments['output'];
  236.             else {
  237.                 return $this->raiseError(MDB2_SCHEMA_ERRORnullnull,
  238.                     'no valid output function specified');
  239.             }
  240.         else {
  241.             return $this->raiseError(MDB2_SCHEMA_ERRORnullnull,
  242.                 'no output method specified');
  243.         }
  244.  
  245.         $eol array_key_exists('end_of_line'$arguments$arguments['end_of_line'"\n";
  246.  
  247.         $sequences = array();
  248.         if (array_key_exists('sequences'$database_definition)
  249.             && is_array($database_definition['sequences'])
  250.         {
  251.             foreach ($database_definition['sequences'as $sequence_name => $sequence{
  252.                 $table array_key_exists('on'$sequence$sequence['on']['table':'';
  253.                 $sequences[$table][$sequence_name;
  254.             }
  255.         }
  256.  
  257.         $buffer '<?xml version="1.0" encoding="ISO-8859-1" ?>'.$eol;
  258.         $buffer.= "<database>$eol$eol <name>".$database_definition['name']."</name>";
  259.         $buffer.= "$eol <create>".$this->_dumpBoolean($database_definition['create'])."</create>";
  260.         $buffer.= "$eol <overwrite>".$this->_dumpBoolean($database_definition['overwrite'])."</overwrite>$eol";
  261.  
  262.         if ($output{
  263.             call_user_func($output$buffer);
  264.         else {
  265.             fwrite($fp$buffer);
  266.         }
  267.  
  268.         if (array_key_exists('tables'$database_definition&& is_array($database_definition['tables'])) {
  269.             foreach ($database_definition['tables'as $table_name => $table{
  270.                 $buffer = "$eol <table>$eol$eol  <name>$table_name</name>$eol";
  271.                 if ($dump == MDB2_SCHEMA_DUMP_ALL || $dump == MDB2_SCHEMA_DUMP_STRUCTURE{
  272.                     $buffer.= "$eol  <declaration>$eol";
  273.                     if (array_key_exists('fields'$table&& is_array($table['fields'])) {
  274.                         foreach ($table['fields'as $field_name => $field{
  275.                             if (!array_key_exists('type'$field)) {
  276.                                 return $this->raiseError(MDB2_SCHEMA_ERRORnullnull,
  277.                                     'it was not specified the type of the field "'.
  278.                                     $field_name.'" of the table "'.$table_name);
  279.                             }
  280.                             if (!empty($this->valid_types&& !array_key_exists($field['type']$this->valid_types)) {
  281.                                 return $this->raiseError(MDB2_SCHEMA_ERROR_UNSUPPORTEDnullnull,
  282.                                     'type "'.$field['type'].'" is not yet supported');
  283.                             }
  284.                             $buffer.= "$eol   <field>$eol    <name>$field_name</name>$eol    <type>";
  285.                             $buffer.= $field['type']."</type>$eol";
  286.                             if (array_key_exists('unsigned'$field)) {
  287.                                 $buffer.= "    <unsigned>".$this->_dumpBoolean($field['unsigned'])."</unsigned>$eol";
  288.                             }
  289.                             if (array_key_exists('length'$field)) {
  290.                                 $buffer.= '    <length>'.$field['length']."</length>$eol";
  291.                             }
  292.                             if (array_key_exists('notnull'$field&& $field['notnull']{
  293.                                 $buffer.= "    <notnull>".$this->_dumpBoolean($field['notnull'])."</notnull>$eol";
  294.                             else {
  295.                                 $buffer.= "    <notnull>false</notnull>$eol";
  296.                             }
  297.                             if (array_key_exists('default'$field)) {
  298.                                 $buffer.= '    <default>'.$this->_escapeSpecialChars($field['default'])."</default>$eol";
  299.                             }
  300.                             if (array_key_exists('autoincrement'$field)) {
  301.                                 $buffer.= "    <autoincrement>" $field['autoincrement'."</autoincrement>$eol";
  302.                             }
  303.                             $buffer.= "   </field>$eol";
  304.                         }
  305.                     }
  306.  
  307.                     if (array_key_exists('indexes'$table&& is_array($table['indexes'])) {
  308.                         foreach ($table['indexes'as $index_name => $index{
  309.                             $buffer.= "$eol   <index>$eol    <name>$index_name</name>$eol";
  310.                             if (array_key_exists('unique'$index)) {
  311.                                 $buffer.= "    <unique>".$this->_dumpBoolean($index['unique'])."</unique>$eol";
  312.                             }
  313.  
  314.                             if (array_key_exists('primary'$index)) {
  315.                                 $buffer.= "    <primary>".$this->_dumpBoolean($index['primary'])."</primary>$eol";
  316.                             }
  317.  
  318.                             foreach ($index['fields'as $field_name => $field{
  319.                                 $buffer.= "    <field>$eol     <name>$field_name</name>$eol";
  320.                                 if (is_array($field&& array_key_exists('sorting'$field)) {
  321.                                     $buffer.= '     <sorting>'.$field['sorting']."</sorting>$eol";
  322.                                 }
  323.                                 $buffer.= "    </field>$eol";
  324.                             }
  325.                             $buffer.= "   </index>$eol";
  326.                         }
  327.                     }
  328.                     $buffer.= "$eol  </declaration>$eol";
  329.                 }
  330.  
  331.                 if ($output{
  332.                     call_user_func($output$buffer);
  333.                 else {
  334.                     fwrite($fp$buffer);
  335.                 }
  336.  
  337.                 $buffer '';
  338.                 if ($dump == MDB2_SCHEMA_DUMP_ALL || $dump == MDB2_SCHEMA_DUMP_CONTENT{
  339.                     if (array_key_exists('initialization'$table)
  340.                         && !empty($table['initialization'])
  341.                         && is_array($table['initialization'])
  342.                     {
  343.                         $buffer = "$eol  <initialization>$eol";
  344.                         foreach ($table['initialization'as $instruction{
  345.                             switch ($instruction['type']{
  346.                             case 'insert':
  347.                                 $buffer.= "$eol   <insert>$eol";
  348.                                 foreach ($instruction['fields'as $field_name => $field{
  349.                                     $buffer.= "$eol    <field>$eol     <name>$field_name</name>$eol     <value>";
  350.                                     $buffer.= $this->_escapeSpecialChars($field)."</value>$eol   </field>$eol";
  351.                                 }
  352.                                 $buffer.= "$eol   </insert>$eol";
  353.                                 break;
  354.                             }
  355.                         }
  356.                         $buffer.= "$eol  </initialization>$eol";
  357.                     }
  358.                 }
  359.                 $buffer.= "$eol </table>$eol";
  360.                 if ($output{
  361.                     call_user_func($output$buffer);
  362.                 else {
  363.                     fwrite($fp$buffer);
  364.                 }
  365.  
  366.                 if (isset($sequences[$table_name])) {
  367.                     foreach ($sequences[$table_nameas $sequence{
  368.                         $result $this->dumpSequence(
  369.                             $database_definition['sequences'][$sequence],
  370.                             $sequence$eol$dump
  371.                         );
  372.                         if (PEAR::isError($result)) {
  373.                             return $result;
  374.                         }
  375.  
  376.                         if ($output{
  377.                             call_user_func($output$result);
  378.                         else {
  379.                             fwrite($fp$result);
  380.                         }
  381.                     }
  382.                 }
  383.             }
  384.         }
  385.  
  386.         if (isset($sequences[''])) {
  387.             foreach ($sequences[''as $sequence{
  388.                 $result $this->dumpSequence(
  389.                     $database_definition['sequences'][$sequence],
  390.                     $sequence$eol$dump
  391.                 );
  392.                 if (PEAR::isError($result)) {
  393.                     return $result;
  394.                 }
  395.  
  396.                 if ($output{
  397.                     call_user_func($output$result);
  398.                 else {
  399.                     fwrite($fp$result);
  400.                 }
  401.             }
  402.         }
  403.  
  404.         $buffer = "$eol</database>$eol";
  405.         if ($output{
  406.             call_user_func($output$buffer);
  407.         else {
  408.             fwrite($fp$buffer);
  409.             fclose($fp);
  410.         }
  411.  
  412.         return MDB2_OK;
  413.     }
  414. }
  415. ?>

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