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.30 2006/05/31 14:41: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 (!empty($sequence_definition['start'])) {
  182.                 $start $sequence_definition['start'];
  183.                 $buffer.= "  <start>$start</start>$eol";
  184.             }
  185.         }
  186.  
  187.         if (!empty($sequence_definition['on'])) {
  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 (!empty($arguments['output'])) {
  231.             if (!empty($arguments['output_mode']&& $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 = isset($arguments['end_of_line']$arguments['end_of_line'"\n";
  246.  
  247.         $sequences = array();
  248.         if (!empty($database_definition['sequences'])
  249.             && is_array($database_definition['sequences'])
  250.         {
  251.             foreach ($database_definition['sequences'as $sequence_name => $sequence{
  252.                 $table !empty($sequence['on']$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 (!empty($database_definition['tables']&& 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 (!empty($table['fields']&& is_array($table['fields'])) {
  274.                         foreach ($table['fields'as $field_name => $field{
  275.                             if (empty($field['type'])) {
  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 (!empty($field['unsigned'])) {
  287.                                 $buffer.= "    <unsigned>".$this->_dumpBoolean($field['unsigned'])."</unsigned>$eol";
  288.                             }
  289.                             if (!empty($field['length'])) {
  290.                                 $buffer.= '    <length>'.$field['length']."</length>$eol";
  291.                             }
  292.                             if (!empty($field['notnull'])) {
  293.                                 $buffer.= "    <notnull>".$this->_dumpBoolean($field['notnull'])."</notnull>$eol";
  294.                             else {
  295.                                 $buffer.= "    <notnull>false</notnull>$eol";
  296.                             }
  297.                             if (!empty($field['fixed']&& $field['type'=== 'text'{
  298.                                 $buffer.= "    <fixed>".$this->_dumpBoolean($field['fixed'])."</fixed>$eol";
  299.                             }
  300.                             if (array_key_exists('default'$field)
  301.                                 && $field['type'!== 'clob' && $field['type'!== 'blob'
  302.                             {
  303.                                 $buffer.= '    <default>'.$this->_escapeSpecialChars($field['default'])."</default>$eol";
  304.                             }
  305.                             if (!empty($field['autoincrement'])) {
  306.                                 $buffer.= "    <autoincrement>" $field['autoincrement'."</autoincrement>$eol";
  307.                             }
  308.                             $buffer.= "   </field>$eol";
  309.                         }
  310.                     }
  311.  
  312.                     if (!empty($table['indexes']&& is_array($table['indexes'])) {
  313.                         foreach ($table['indexes'as $index_name => $index{
  314.                             $buffer.= "$eol   <index>$eol    <name>$index_name</name>$eol";
  315.                             if (!empty($index['unique'])) {
  316.                                 $buffer.= "    <unique>".$this->_dumpBoolean($index['unique'])."</unique>$eol";
  317.                             }
  318.  
  319.                             if (!empty($index['primary'])) {
  320.                                 $buffer.= "    <primary>".$this->_dumpBoolean($index['primary'])."</primary>$eol";
  321.                             }
  322.  
  323.                             foreach ($index['fields'as $field_name => $field{
  324.                                 $buffer.= "    <field>$eol     <name>$field_name</name>$eol";
  325.                                 if (!empty($field&& is_array($field)) {
  326.                                     $buffer.= '     <sorting>'.$field['sorting']."</sorting>$eol";
  327.                                 }
  328.                                 $buffer.= "    </field>$eol";
  329.                             }
  330.                             $buffer.= "   </index>$eol";
  331.                         }
  332.                     }
  333.                     $buffer.= "$eol  </declaration>$eol";
  334.                 }
  335.  
  336.                 if ($output{
  337.                     call_user_func($output$buffer);
  338.                 else {
  339.                     fwrite($fp$buffer);
  340.                 }
  341.  
  342.                 $buffer '';
  343.                 if ($dump == MDB2_SCHEMA_DUMP_ALL || $dump == MDB2_SCHEMA_DUMP_CONTENT{
  344.                     if (!empty($table['initialization']&& is_array($table['initialization'])) {
  345.                         $buffer = "$eol  <initialization>$eol";
  346.                         foreach ($table['initialization'as $instruction{
  347.                             switch ($instruction['type']{
  348.                             case 'insert':
  349.                                 $buffer.= "$eol   <insert>$eol";
  350.                                 foreach ($instruction['fields'as $field_name => $field{
  351.                                     $buffer.= "$eol    <field>$eol     <name>$field_name</name>$eol     <value>";
  352.                                     $buffer.= $this->_escapeSpecialChars($field)."</value>$eol   </field>$eol";
  353.                                 }
  354.                                 $buffer.= "$eol   </insert>$eol";
  355.                                 break;
  356.                             }
  357.                         }
  358.                         $buffer.= "$eol  </initialization>$eol";
  359.                     }
  360.                 }
  361.                 $buffer.= "$eol </table>$eol";
  362.                 if ($output{
  363.                     call_user_func($output$buffer);
  364.                 else {
  365.                     fwrite($fp$buffer);
  366.                 }
  367.  
  368.                 if (isset($sequences[$table_name])) {
  369.                     foreach ($sequences[$table_nameas $sequence{
  370.                         $result $this->dumpSequence(
  371.                             $database_definition['sequences'][$sequence],
  372.                             $sequence$eol$dump
  373.                         );
  374.                         if (PEAR::isError($result)) {
  375.                             return $result;
  376.                         }
  377.  
  378.                         if ($output{
  379.                             call_user_func($output$result);
  380.                         else {
  381.                             fwrite($fp$result);
  382.                         }
  383.                     }
  384.                 }
  385.             }
  386.         }
  387.  
  388.         if (isset($sequences[''])) {
  389.             foreach ($sequences[''as $sequence{
  390.                 $result $this->dumpSequence(
  391.                     $database_definition['sequences'][$sequence],
  392.                     $sequence$eol$dump
  393.                 );
  394.                 if (PEAR::isError($result)) {
  395.                     return $result;
  396.                 }
  397.  
  398.                 if ($output{
  399.                     call_user_func($output$result);
  400.                 else {
  401.                     fwrite($fp$result);
  402.                 }
  403.             }
  404.         }
  405.  
  406.         $buffer = "$eol</database>$eol";
  407.         if ($output{
  408.             call_user_func($output$buffer);
  409.         else {
  410.             fwrite($fp$buffer);
  411.             fclose($fp);
  412.         }
  413.  
  414.         return MDB2_OK;
  415.     }
  416. }
  417. ?>

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