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

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