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@backendmedia.com>                         |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: Writer.php,v 1.7 2005/04/21 16:59:10 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@backendmedia.com>
  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.         return MDB2::raiseError($code$mode$options$userinfo);
  84.     }
  85.  
  86.     // }}}
  87.     // {{{ _escapeSpecialChars()
  88.  
  89.     /**
  90.      * add escapecharacters to all special characters in a string
  91.      *
  92.      * @param string $string string that should be escaped
  93.      * @return string escaped string
  94.      * @access protected
  95.      */
  96.     function _escapeSpecialChars($string)
  97.     {
  98.         if (!is_string($string)) {
  99.             $string strval($string);
  100.         }
  101.         $escaped '';
  102.         for ($char = 0$count strlen($string)$char $count$char++{
  103.             switch ($string[$char]{
  104.             case '&':
  105.                 $escaped .= '&amp;';
  106.                 break;
  107.             case '>':
  108.                 $escaped .= '&gt;';
  109.                 break;
  110.             case '<':
  111.                 $escaped .= '&lt;';
  112.                 break;
  113.             case '"':
  114.                 $escaped .= '&quot;';
  115.                 break;
  116.             case '\'':
  117.                 $escaped .= '&apos;';
  118.                 break;
  119.             default:
  120.                 $code ord($string[$char]);
  121.                 if ($code < 32 || $code > 127{
  122.                     $escaped .= "&#$code;";
  123.                 else {
  124.                     $escaped .= $string[$char];
  125.                 }
  126.                 break;
  127.             }
  128.         }
  129.         return $escaped;
  130.     }
  131.  
  132.     // }}}
  133.     // {{{ dumpSequence()
  134.  
  135.     /**
  136.      * dump the structure of a sequence
  137.      *
  138.      * @param string  $sequence_name 
  139.      * @param string  $eol 
  140.      * @return mixed string with xml seqeunce definition on success, or a MDB2 error object
  141.      * @access public
  142.      */
  143.     function dumpSequence($sequence_definition$sequence_name$eol$dump = MDB2_MANAGER_DUMP_ALL)
  144.     {
  145.         $buffer = "$eol <sequence>$eol  <name>$sequence_name</name>$eol";
  146.         if ($dump == MDB2_MANAGER_DUMP_ALL || $dump == MDB2_MANAGER_DUMP_CONTENT{
  147.             if (isset($sequence_definition['start'])) {
  148.                 $start $sequence_definition['start'];
  149.                 $buffer .= "  <start>$start</start>$eol";
  150.             }
  151.         }
  152.         if (isset($sequence_definition['on'])) {
  153.             $buffer .= "  <on>$eol   <table>".$sequence_definition['on']['table'].
  154.                 "</table>$eol   <field>".$sequence_definition['on']['field'].
  155.                 "</field>$eol  </on>$eol";
  156.         }
  157.         $buffer .= " </sequence>$eol";
  158.         return $buffer;
  159.     }
  160.  
  161.     // }}}
  162.     // {{{ dumpDatabase()
  163.  
  164.     /**
  165.      * Dump a previously parsed database structure in the Metabase schema
  166.      * XML based format suitable for the Metabase parser. This function
  167.      * may optionally dump the database definition with initialization
  168.      * commands that specify the data that is currently present in the tables.
  169.      *
  170.      * @param array $arguments an associative array that takes pairs of tag
  171.      *  names and values that define dump options.
  172.      *                  array (
  173.      *                      'output_mode'    =>    String
  174.      *                          'file' :   dump into a file
  175.      *                          default:   dump using a function
  176.      *                      'output'        =>    String
  177.      *                          depending on the 'Output_Mode'
  178.      *                                   name of the file
  179.      *                                   name of the function
  180.      *                      'end_of_line'        =>    String
  181.      *                          end of line delimiter that should be used
  182.      *                          default: "\n"
  183.      *                  );
  184.      * @param integer $dump constant that determines what data to dump
  185.      *                       MDB2_MANAGER_DUMP_ALL       : the entire db
  186.      *                       MDB2_MANAGER_DUMP_STRUCTURE : only the structure of the db
  187.      *                       MDB2_MANAGER_DUMP_CONTENT   : only the content of the db
  188.      * @return mixed MDB2_OK on success, or a MDB2 error object
  189.      * @access public
  190.      */
  191.     function dumpDatabase($database_definition$arguments$dump = MDB2_MANAGER_DUMP_ALL)
  192.     {
  193.         if (isset($arguments['definition']&& $arguments['definition']{
  194.             $database_definition $arguments['definition'];
  195.         }
  196.         if (isset($arguments['output'])) {
  197.             if (isset($arguments['output_mode']&& $arguments['output_mode'== 'file'{
  198.                 $fp fopen($arguments['output']'w');
  199.                 $output = false;
  200.             elseif (function_exists($arguments['output'])) {
  201.                 $output $arguments['output'];
  202.             else {
  203.                 return $this->raiseError(MDB2_ERROR_MANAGERnullnull,
  204.                     'no valid output function specified');
  205.             }
  206.         else {
  207.             return $this->raiseError(MDB2_ERROR_MANAGERnullnull,
  208.                 'no output method specified');
  209.         }
  210.         if (isset($arguments['end_of_line'])) {
  211.             $eol $arguments['end_of_line'];
  212.         else {
  213.             $eol "\n";
  214.         }
  215.  
  216.         $sequences = array();
  217.         if (isset($database_definition['sequences'])
  218.             && is_array($database_definition['sequences'])
  219.         {
  220.             foreach ($database_definition['sequences'as $sequence_name => $sequence{
  221.                 if (isset($sequence['on'])) {
  222.                     $table $sequence['on']['table'];
  223.                 else {
  224.                     $table '';
  225.                 }
  226.                 $sequences[$table][$sequence_name;
  227.             }
  228.         }
  229.  
  230.         $buffer ('<?xml version="1.0" encoding="ISO-8859-1" ?>'.$eol);
  231.         $buffer .= ("<database>$eol$eol <name>".$database_definition['name']."</name>$eol <create>"
  232.             .$database_definition['create']."</create>$eol");
  233.  
  234.         if ($output{
  235.             $output($buffer);
  236.         else {
  237.             fwrite($fp$buffer);
  238.         }
  239.         $buffer '';
  240.         if (isset($database_definition['tables']&& is_array($database_definition['tables'])) {
  241.             foreach ($database_definition['tables'as $table_name => $table{
  242.                 $buffer ("$eol <table>$eol$eol  <name>$table_name</name>$eol");
  243.                 if ($dump == MDB2_MANAGER_DUMP_ALL || $dump == MDB2_MANAGER_DUMP_STRUCTURE{
  244.                     $buffer .= ("$eol  <declaration>$eol");
  245.                     if (isset($table['fields']&& is_array($table['fields'])) {
  246.                         foreach ($table['fields'as $field_name => $field{
  247.                             if (!isset($field['type'])) {
  248.                                 return $this->raiseError(MDB2_ERROR_MANAGERnullnull,
  249.                                     'it was not specified the type of the field "'.
  250.                                     $field_name.'" of the table "'.$table_name);
  251.                             }
  252.                             $buffer .=("$eol   <field>$eol    <name>$field_name</name>$eol    <type>"
  253.                                 .$field['type']."</type>$eol");
  254.                             switch ($field['type']{
  255.                             case 'integer':
  256.                                 if (isset($field['unsigned'])) {
  257.                                     $buffer .=("    <unsigned>1</unsigned>$eol");
  258.                                 }
  259.                                 break;
  260.                             case 'text':
  261.                             case 'clob':
  262.                             case 'blob':
  263.                                 if (isset($field['length'])) {
  264.                                     $buffer .=('    <length>'.$field['length']."</length>$eol");
  265.                                 }
  266.                                 break;
  267.                             case 'boolean':
  268.                             case 'date':
  269.                             case 'timestamp':
  270.                             case 'time':
  271.                             case 'float':
  272.                             case 'decimal':
  273.                                 break;
  274.                             default:
  275.                                 return $this->raiseError('type "'.$field['type'].
  276.                                     '" is not yet supported');
  277.                             }
  278.                             if (isset($field['notnull']&& $field['notnull']{
  279.                                 $buffer .=("    <notnull>1</notnull>$eol");
  280.                             }
  281.                             if (isset($field['default'])) {
  282.                                 $buffer .=('    <default>'.$this->_escapeSpecialChars($field['default'])
  283.                                     ."</default>$eol");
  284.                             }
  285.                             $buffer .=("   </field>$eol");
  286.                         }
  287.                     }
  288.                     if (isset($table['indexes']&& is_array($table['indexes'])) {
  289.                         foreach ($table['indexes'as $index_name => $index{
  290.                             $buffer .=("$eol   <index>$eol    <name>$index_name</name>$eol");
  291.                             if (isset($index['unique'])) {
  292.                                 $buffer .=("    <unique>1</unique>$eol");
  293.                             }
  294.                             foreach ($index['fields'as $field_name => $field{
  295.                                 $buffer .=("    <field>$eol     <name>$field_name</name>$eol");
  296.                                 if (is_array($field&& isset($field['sorting'])) {
  297.                                     $buffer .=('     <sorting>'.$field['sorting']."</sorting>$eol");
  298.                                 }
  299.                                 $buffer .=("    </field>$eol");
  300.                             }
  301.                             $buffer .=("   </index>$eol");
  302.                         }
  303.                     }
  304.                     $buffer .= ("$eol  </declaration>$eol");
  305.                 }
  306.                 if ($output{
  307.                     $output($buffer);
  308.                 else {
  309.                     fwrite($fp$buffer);
  310.                 }
  311.                 $buffer '';
  312.                 if ($dump == MDB2_MANAGER_DUMP_ALL || $dump == MDB2_MANAGER_DUMP_CONTENT{
  313.                     if (isset($table['initialization']&& !empty($table['initialization']&& is_array($table['initialization'])) {
  314.                         $buffer ("$eol  <initialization>$eol");
  315.                         foreach ($table['initialization'as $instruction{
  316.                             switch ($instruction['type']{
  317.                             case 'insert':
  318.                                 $buffer .= ("$eol   <insert>$eol");
  319.                                 foreach ($instruction['fields'as $field_name => $field{
  320.                                     $buffer .= ("$eol    <field>$eol     <name>$field_name</name>$eol     <value>"
  321.                                         .$this->_escapeSpecialChars($field)."</value>$eol   </field>$eol");
  322.                                 }
  323.                                 $buffer .= ("$eol   </insert>$eol");
  324.                                 break;
  325.                             }
  326.                         }
  327.                         $buffer .= ("$eol  </initialization>$eol");
  328.                     }
  329.                 }
  330.                 $buffer .= ("$eol </table>$eol");
  331.                 if ($output{
  332.                     $output($buffer);
  333.                 else {
  334.                     fwrite($fp$buffer);
  335.                 }
  336.                 if (isset($sequences[$table_name])) {
  337.                     foreach ($sequences[$table_nameas $sequence{
  338.                         $result $this->dumpSequence(
  339.                             $database_definition['sequences'][$sequence],
  340.                             $sequence,
  341.                             $eol,
  342.                             $dump
  343.                         );
  344.                         if (PEAR::isError($result)) {
  345.                             return $result;
  346.                         }
  347.                         if ($output{
  348.                             $output($result);
  349.                         else {
  350.                             fwrite($fp$result);
  351.                         }
  352.                     }
  353.                 }
  354.             }
  355.         }
  356.         if (isset($sequences[''])) {
  357.             foreach ($sequences[''as $sequence{
  358.                 $result $this->dumpSequence(
  359.                     $database_definition['sequences'][$sequence],
  360.                     $sequence,
  361.                     $eol,
  362.                     $dump
  363.                 );
  364.                 if (PEAR::isError($result)) {
  365.                     return $result;
  366.                 }
  367.                 if ($output{
  368.                     $output($result);
  369.                 else {
  370.                     fwrite($fp$result);
  371.                 }
  372.             }
  373.         }
  374.  
  375.         $buffer ("$eol</database>$eol");
  376.         if ($output{
  377.             $output($buffer);
  378.         else {
  379.             fwrite($fp$buffer);
  380.             fclose($fp);
  381.         }
  382.  
  383.         return MDB2_OK;
  384.     }
  385. }
  386. ?>

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