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

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