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.34 2007/03/22 22:07:53 ifeghali 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.     // {{{ properties
  58.  
  59.     var $valid_types = array();
  60.  
  61.     // }}}
  62.     // {{{ constructor
  63.  
  64.     function __construct($valid_types = array())
  65.     {
  66.         $this->valid_types = $valid_types;
  67.     }
  68.  
  69.     function MDB2_Schema_Writer($valid_types = array())
  70.     {
  71.         $this->__construct($valid_types);
  72.     }
  73.  
  74.     // }}}
  75.     // {{{ raiseError()
  76.  
  77.     /**
  78.      * This method is used to communicate an error and invoke error
  79.      * callbacks etc.  Basically a wrapper for PEAR::raiseError
  80.      * without the message string.
  81.      *
  82.      * @param int|PEAR_Error integer error code or and PEAR_Error instance
  83.      * @param int      error mode, see PEAR_Error docs
  84.      *
  85.      *                  error level (E_USER_NOTICE etc).  If error mode is
  86.      *                  PEAR_ERROR_CALLBACK, this is the callback function,
  87.      *                  either as a function name, or as an array of an
  88.      *                  object and method name.  For other error modes this
  89.      *                  parameter is ignored.
  90.      * @param string   Extra debug information.  Defaults to the last
  91.      *                  query and native error code.
  92.      * @return object  PEAR error object
  93.      * @access  public
  94.      * @see PEAR_Error
  95.      */
  96.     function &raiseError($code = null$mode = null$options = null$userinfo = null)
  97.     {
  98.         $error =MDB2_Schema::raiseError($code$mode$options$userinfo);
  99.         return $error;
  100.     }
  101.  
  102.     // }}}
  103.     // {{{ _escapeSpecialChars()
  104.  
  105.     /**
  106.      * add escapecharacters to all special characters in a string
  107.      *
  108.      * @param string string that should be escaped
  109.      * @return string escaped string
  110.      * @access protected
  111.      */
  112.     function _escapeSpecialChars($string)
  113.     {
  114.         if (!is_string($string)) {
  115.             $string strval($string);
  116.         }
  117.  
  118.         $escaped '';
  119.         for ($char = 0$count strlen($string)$char $count$char++{
  120.             switch ($string[$char]{
  121.             case '&':
  122.                 $escaped.= '&amp;';
  123.                 break;
  124.             case '>':
  125.                 $escaped.= '&gt;';
  126.                 break;
  127.             case '<':
  128.                 $escaped.= '&lt;';
  129.                 break;
  130.             case '"':
  131.                 $escaped.= '&quot;';
  132.                 break;
  133.             case '\'':
  134.                 $escaped.= '&apos;';
  135.                 break;
  136.             default:
  137.                 $code ord($string[$char]);
  138.                 if ($code < 32 || $code > 127{
  139.                     $escaped.= "&#$code;";
  140.                 else {
  141.                     $escaped.= $string[$char];
  142.                 }
  143.                 break;
  144.             }
  145.         }
  146.         return $escaped;
  147.     }
  148.  
  149.     // }}}
  150.     // {{{ _dumpBoolean()
  151.  
  152.     /**
  153.      * dump the structure of a sequence
  154.      *
  155.      * @param string boolean value or variable definition
  156.      * @return string with xml boolea definition
  157.      * @access private
  158.      */
  159.     function _dumpBoolean($boolean)
  160.     {
  161.         if (is_string($boolean)) {
  162.             if ($boolean !== 'true' || $boolean !== 'false'
  163.                 || preg_match('/<variable>.*</variable>/'$boolean)
  164.             {
  165.                 return $boolean;
  166.             }
  167.         }
  168.         return $boolean 'true' 'false';
  169.     }
  170.  
  171.     // }}}
  172.     // {{{ dumpSequence()
  173.  
  174.     /**
  175.      * dump the structure of a sequence
  176.      *
  177.      * @param string sequence name
  178.      * @param string end of line characters
  179.      * @return mixed string xml sequence definition on success, or a error object
  180.      * @access public
  181.      */
  182.     function dumpSequence($sequence_definition$sequence_name$eol$dump = MDB2_SCHEMA_DUMP_ALL)
  183.     {
  184.         $buffer = "$eol <sequence>$eol  <name>$sequence_name</name>$eol";
  185.         if ($dump == MDB2_SCHEMA_DUMP_ALL || $dump == MDB2_SCHEMA_DUMP_CONTENT{
  186.             if (!empty($sequence_definition['start'])) {
  187.                 $start $sequence_definition['start'];
  188.                 $buffer.= "  <start>$start</start>$eol";
  189.             }
  190.         }
  191.  
  192.         if (!empty($sequence_definition['on'])) {
  193.             $buffer.= "  <on>$eol";
  194.             $buffer.= "   <table>".$sequence_definition['on']['table'];
  195.             $buffer.= "</table>$eol   <field>".$sequence_definition['on']['field'];
  196.             $buffer.= "</field>$eol  </on>$eol";
  197.         }
  198.         $buffer.= " </sequence>$eol";
  199.  
  200.         return $buffer;
  201.     }
  202.  
  203.     // }}}
  204.     // {{{ dumpDatabase()
  205.  
  206.     /**
  207.      * Dump a previously parsed database structure in the Metabase schema
  208.      * XML based format suitable for the Metabase parser. This function
  209.      * may optionally dump the database definition with initialization
  210.      * commands that specify the data that is currently present in the tables.
  211.      *
  212.      * @param array associative array that takes pairs of tag
  213.      *               names and values that define dump options.
  214.      *                  array (
  215.      *                      'output_mode'    =>    String
  216.      *                          'file' :   dump into a file
  217.      *                          default:   dump using a function
  218.      *                      'output'        =>    String
  219.      *                          depending on the 'Output_Mode'
  220.      *                                   name of the file
  221.      *                                   name of the function
  222.      *                      'end_of_line'        =>    String
  223.      *                          end of line delimiter that should be used
  224.      *                          default: "\n"
  225.      *                  );
  226.      * @param integer determines what data to dump
  227.      *                       MDB2_SCHEMA_DUMP_ALL       : the entire db
  228.      *                       MDB2_SCHEMA_DUMP_STRUCTURE : only the structure of the db
  229.      *                       MDB2_SCHEMA_DUMP_CONTENT   : only the content of the db
  230.      * @return mixed MDB2_OK on success, or a error object
  231.      * @access public
  232.      */
  233.     function dumpDatabase($database_definition$arguments$dump = MDB2_SCHEMA_DUMP_ALL)
  234.     {
  235.         if (!empty($arguments['output'])) {
  236.             if (!empty($arguments['output_mode']&& $arguments['output_mode'== 'file'{
  237.                 $fp fopen($arguments['output']'w');
  238.                 if ($fp === false{
  239.                     return $this->raiseError(MDB2_SCHEMA_ERROR_WRITERnullnull,
  240.                         'it was not possible to open output file');
  241.                 }
  242.  
  243.                 $output = false;
  244.             elseif (is_callable($arguments['output'])) {
  245.                 $output $arguments['output'];
  246.             else {
  247.                 return $this->raiseError(MDB2_SCHEMA_ERROR_WRITERnullnull,
  248.                     'no valid output function specified');
  249.             }
  250.         else {
  251.             return $this->raiseError(MDB2_SCHEMA_ERROR_WRITERnullnull,
  252.                 'no output method specified');
  253.         }
  254.  
  255.         $eol = isset($arguments['end_of_line']$arguments['end_of_line'"\n";
  256.  
  257.         $sequences = array();
  258.         if (!empty($database_definition['sequences'])
  259.             && is_array($database_definition['sequences'])
  260.         {
  261.             foreach ($database_definition['sequences'as $sequence_name => $sequence{
  262.                 $table !empty($sequence['on']$sequence['on']['table':'';
  263.                 $sequences[$table][$sequence_name;
  264.             }
  265.         }
  266.  
  267.         $buffer '<?xml version="1.0" encoding="ISO-8859-1" ?>'.$eol;
  268.         $buffer.= "<database>$eol$eol <name>".$database_definition['name']."</name>";
  269.         $buffer.= "$eol <create>".$this->_dumpBoolean($database_definition['create'])."</create>";
  270.         $buffer.= "$eol <overwrite>".$this->_dumpBoolean($database_definition['overwrite'])."</overwrite>$eol";
  271.  
  272.         if ($output{
  273.             call_user_func($output$buffer);
  274.         else {
  275.             fwrite($fp$buffer);
  276.         }
  277.  
  278.         if (!empty($database_definition['tables']&& is_array($database_definition['tables'])) {
  279.             foreach ($database_definition['tables'as $table_name => $table{
  280.                 $buffer = "$eol <table>$eol$eol  <name>$table_name</name>$eol";
  281.                 if ($dump == MDB2_SCHEMA_DUMP_ALL || $dump == MDB2_SCHEMA_DUMP_STRUCTURE{
  282.                     $buffer.= "$eol  <declaration>$eol";
  283.                     if (!empty($table['fields']&& is_array($table['fields'])) {
  284.                         foreach ($table['fields'as $field_name => $field{
  285.                             if (empty($field['type'])) {
  286.                                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATEnullnull,
  287.                                     'it was not specified the type of the field "'.
  288.                                     $field_name.'" of the table "'.$table_name);
  289.                             }
  290.                             if (!empty($this->valid_types&& !array_key_exists($field['type']$this->valid_types)) {
  291.                                 return $this->raiseError(MDB2_SCHEMA_ERROR_UNSUPPORTEDnullnull,
  292.                                     'type "'.$field['type'].'" is not yet supported');
  293.                             }
  294.                             $buffer.= "$eol   <field>$eol    <name>$field_name</name>$eol    <type>";
  295.                             $buffer.= $field['type']."</type>$eol";
  296.                             if (!empty($field['unsigned'])) {
  297.                                 $buffer.= "    <unsigned>".$this->_dumpBoolean($field['unsigned'])."</unsigned>$eol";
  298.                             }
  299.                             if (!empty($field['length'])) {
  300.                                 $buffer.= '    <length>'.$field['length']."</length>$eol";
  301.                             }
  302.                             if (!empty($field['notnull'])) {
  303.                                 $buffer.= "    <notnull>".$this->_dumpBoolean($field['notnull'])."</notnull>$eol";
  304.                             else {
  305.                                 $buffer.= "    <notnull>false</notnull>$eol";
  306.                             }
  307.                             if (!empty($field['fixed']&& $field['type'=== 'text'{
  308.                                 $buffer.= "    <fixed>".$this->_dumpBoolean($field['fixed'])."</fixed>$eol";
  309.                             }
  310.                             if (array_key_exists('default'$field)
  311.                                 && $field['type'!== 'clob' && $field['type'!== 'blob'
  312.                             {
  313.                                 $buffer.= '    <default>'.$this->_escapeSpecialChars($field['default'])."</default>$eol";
  314.                             }
  315.                             if (!empty($field['autoincrement'])) {
  316.                                 $buffer.= "    <autoincrement>" $field['autoincrement'."</autoincrement>$eol";
  317.                             }
  318.                             $buffer.= "   </field>$eol";
  319.                         }
  320.                     }
  321.  
  322.                     if (!empty($table['indexes']&& is_array($table['indexes'])) {
  323.                         foreach ($table['indexes'as $index_name => $index{
  324.                             if (strtolower($index_name=== 'primary'{
  325.                                 $index_name $table_name '_pKey';
  326.                             }
  327.                             $buffer.= "$eol   <index>$eol    <name>$index_name</name>$eol";
  328.                             if (!empty($index['unique'])) {
  329.                                 $buffer.= "    <unique>".$this->_dumpBoolean($index['unique'])."</unique>$eol";
  330.                             }
  331.  
  332.                             if (!empty($index['primary'])) {
  333.                                 $buffer.= "    <primary>".$this->_dumpBoolean($index['primary'])."</primary>$eol";
  334.                             }
  335.  
  336.                             foreach ($index['fields'as $field_name => $field{
  337.                                 $buffer.= "    <field>$eol     <name>$field_name</name>$eol";
  338.                                 if (!empty($field&& is_array($field)) {
  339.                                     $buffer.= '     <sorting>'.$field['sorting']."</sorting>$eol";
  340.                                 }
  341.                                 $buffer.= "    </field>$eol";
  342.                             }
  343.                             $buffer.= "   </index>$eol";
  344.                         }
  345.                     }
  346.                     $buffer.= "$eol  </declaration>$eol";
  347.                 }
  348.  
  349.                 if ($output{
  350.                     call_user_func($output$buffer);
  351.                 else {
  352.                     fwrite($fp$buffer);
  353.                 }
  354.  
  355.                 $buffer '';
  356.                 if ($dump == MDB2_SCHEMA_DUMP_ALL || $dump == MDB2_SCHEMA_DUMP_CONTENT{
  357.                     if (!empty($table['initialization']&& is_array($table['initialization'])) {
  358.                         $buffer = "$eol  <initialization>$eol";
  359.                         foreach ($table['initialization'as $instruction{
  360.                             switch ($instruction['type']{
  361.                             case 'insert':
  362.                                 $buffer.= "$eol   <insert>$eol";
  363.                                 foreach ($instruction['data']['field'as $field{
  364.                                     $field_name $field['name'];
  365.                                     $buffer.= "$eol    <field>$eol     <name>$field_name</name>$eol";
  366.                                     $buffer.= $this->writeExpression($field['group']5$arguments);
  367.                                     $buffer.= "    </field>$eol";
  368.                                 }
  369.                                 $buffer.= "$eol   </insert>$eol";
  370.                                 break;
  371.                             case 'update':
  372.                                 $buffer.= "$eol   <update>$eol";
  373.                                 foreach ($instruction['data']['field'as $field{
  374.                                     $field_name $field['name'];
  375.                                     $buffer.= "$eol    <field>$eol     <name>$field_name</name>$eol";
  376.                                     $buffer.= $this->writeExpression($field['group']5$arguments);
  377.                                     $buffer.= "    </field>$eol";
  378.                                 }
  379.  
  380.                                 if (!empty($instruction['data']['where'])
  381.                                     && is_array($instruction['data']['where'])
  382.                                 {
  383.                                     $buffer.= "    <where>$eol";
  384.                                     $buffer.= $this->writeExpression($instruction['data']['where']5$arguments);
  385.                                     $buffer.= "    </where>$eol";
  386.                                 }
  387.  
  388.                                 $buffer.= "$eol   </update>$eol";
  389.                                 break;
  390.                             case 'delete':
  391.                                 $buffer.= "$eol   <delete>$eol$eol";
  392.                                 if (!empty($instruction['data']['where'])
  393.                                     && is_array($instruction['data']['where'])
  394.                                 {
  395.                                     $buffer.= "    <where>$eol";
  396.                                     $buffer.= $this->writeExpression($instruction['data']['where']5$arguments);
  397.                                     $buffer.= "    </where>$eol";
  398.                                 }
  399.                                 $buffer.= "$eol   </delete>$eol";
  400.                                 break;
  401.                             }
  402.                         }
  403.                         $buffer.= "$eol  </initialization>$eol";
  404.                     }
  405.                 }
  406.                 $buffer.= "$eol </table>$eol";
  407.                 if ($output{
  408.                     call_user_func($output$buffer);
  409.                 else {
  410.                     fwrite($fp$buffer);
  411.                 }
  412.  
  413.                 if (isset($sequences[$table_name])) {
  414.                     foreach ($sequences[$table_nameas $sequence{
  415.                         $result $this->dumpSequence(
  416.                             $database_definition['sequences'][$sequence],
  417.                             $sequence$eol$dump
  418.                         );
  419.                         if (PEAR::isError($result)) {
  420.                             return $result;
  421.                         }
  422.  
  423.                         if ($output{
  424.                             call_user_func($output$result);
  425.                         else {
  426.                             fwrite($fp$result);
  427.                         }
  428.                     }
  429.                 }
  430.             }
  431.         }
  432.  
  433.         if (isset($sequences[''])) {
  434.             foreach ($sequences[''as $sequence{
  435.                 $result $this->dumpSequence(
  436.                     $database_definition['sequences'][$sequence],
  437.                     $sequence$eol$dump
  438.                 );
  439.                 if (PEAR::isError($result)) {
  440.                     return $result;
  441.                 }
  442.  
  443.                 if ($output{
  444.                     call_user_func($output$result);
  445.                 else {
  446.                     fwrite($fp$result);
  447.                 }
  448.             }
  449.         }
  450.  
  451.         $buffer = "$eol</database>$eol";
  452.         if ($output{
  453.             call_user_func($output$buffer);
  454.         else {
  455.             fwrite($fp$buffer);
  456.             fclose($fp);
  457.         }
  458.  
  459.         return MDB2_OK;
  460.     }
  461.  
  462.     // }}}
  463.     // {{{ writeExpression()
  464.  
  465.     /**
  466.      * Dumps the structure of an element. Elements can be value, column,
  467.      * function or expression.
  468.      *
  469.      * @param array  multi dimensional array that represents the parsed element
  470.      *                 of a DML instruction.
  471.      * @param integer  base indentation width
  472.      * @param array  associative array that takes pairs of tag
  473.      *                 names and values that define dump options.
  474.      *
  475.      * @return string 
  476.      *
  477.      * @access public
  478.      * @see MDB2_Schema_Writer::dumpDatabase()
  479.      */
  480.     function writeExpression($element$offset = 0$arguments = null)
  481.     {
  482.         $eol = isset($arguments['end_of_line']$arguments['end_of_line'"\n";
  483.         $str '';
  484.         $indent str_repeat(' '$offset);
  485.         $noffset $offset + 1;
  486.  
  487.         switch ($element['type']{
  488.             case 'value':
  489.                 $str.= "$indent<value>".$this->_escapeSpecialChars($element['data'])."</value>$eol";
  490.             break;
  491.             case 'column':
  492.                 $str.= "$indent<column>".$this->_escapeSpecialChars($element['data'])."</column>$eol";
  493.             break;
  494.             case 'function':
  495.                 $str.= "$indent<function>$eol$indent <name>".$this->_escapeSpecialChars($element['data']['name'])."</name>$eol";
  496.  
  497.                 if (!empty($element['data']['arguments'])
  498.                     && is_array($element['data']['arguments'])
  499.                 {
  500.                     foreach ($element['data']['arguments'as $v{
  501.                         $str.= $this->writeExpression($v$noffset$arguments);
  502.                     }
  503.                 }
  504.  
  505.                 $str.= "$indent</function>$eol";
  506.             break;
  507.             case 'expression':
  508.                 $str.= "$indent<expression>$eol";
  509.                 $str.= $this->writeExpression($element['data']['operants'][0]$noffset$arguments);
  510.                 $str.= "$indent <operator>".$element['data']['operator']."</operator>$eol";
  511.                 $str.= $this->writeExpression($element['data']['operants'][1]$noffset$arguments);
  512.                 $str.= "$indent</expression>$eol";
  513.             break;
  514.         }
  515.         return $str;
  516.     }
  517.  
  518.     // }}}
  519. }
  520. ?>

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