MDB2_Schema
[ class tree: MDB2_Schema ] [ index: MDB2_Schema ] [ all elements ]

Source for file Parser2.php

Documentation is available at Parser2.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: Igor Feghali <ifeghali@php.net>                              |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: Parser2.php,v 1.1 2007/02/07 19:03:01 ifeghali Exp $
  46. //
  47.  
  48. require_once 'XML/Unserializer.php';
  49. require_once 'MDB2/Schema/Validate.php';
  50.  
  51. if (empty($GLOBALS['_MDB2_Schema_Reserved'])) {
  52.     $GLOBALS['_MDB2_Schema_Reserved'= array();
  53. }
  54.  
  55. /**
  56.  * Parses an XML schema file
  57.  *
  58.  * @package MDB2_Schema
  59.  * @category Database
  60.  * @access protected
  61.  * @author Igor Feghali <ifeghali@php.net>
  62.  */
  63. class MDB2_Schema_Parser2 extends XML_Unserializer
  64. {
  65.     var $database_definition = array('tables' => array()'sequences' => array());
  66.     var $database_loaded = array();
  67.     var $variables = array();
  68.     var $error;
  69.     var $structure = false;
  70.     var $validator;
  71.     var $options = array();
  72.  
  73.     function __construct($variables$fail_on_invalid_names = true$structure = false$valid_types = array()$force_defaults = true)
  74.     {
  75.         // force ISO-8859-1 due to different defaults for PHP4 and PHP5
  76.         // todo: this probably needs to be investigated some more and cleaned up
  77.         $this->options['encoding''ISO-8859-1';
  78.         $this->options['XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE'= true;
  79.         $this->options['XML_UNSERIALIZER_OPTION_ATTRIBUTES_ARRAYKEY'= false;
  80.         $this->options['forceEnum'= array('table''field''index''insert''update''delete');
  81.         /*
  82.          * todo: find a way to force the following items not to be parsed as arrays
  83.          * as it cause problems in functions with multiple arguments
  84.          */
  85.         //$this->options['forceNEnum'] = array('value', 'column');
  86.         $this->variables = $variables;
  87.         $this->structure = $structure;
  88.         $this->validator =new MDB2_Schema_Validate($fail_on_invalid_names$valid_types$force_defaults);
  89.         parent::XML_Unserializer($this->options);
  90.     }
  91.  
  92.     function MDB2_Schema_Parser2($variables$fail_on_invalid_names = true$structure = false$valid_types = array()$force_defaults = true)
  93.     {
  94.         $this->__construct($variables$fail_on_invalid_names$structure$valid_types$force_defaults);
  95.     }
  96.  
  97.     function parse()
  98.     {
  99.         $result $this->unserialize($this->filenametrue);
  100.  
  101.         if (PEAR::isError($result)) {
  102.             return $result;
  103.         else {
  104.             $this->database_definition = $this->getUnserializedData();
  105.             $this->fixDatabaseKeys();
  106.  
  107.             if (!empty($this->database_definition['sequences']&& is_array($this->database_definition['sequences'])) {
  108.                 foreach ($this->database_definition['sequences'as $k => $v{
  109.                     $result $this->validator->validateSequence($this->database_definition['sequences']$v$v['name']);
  110.                     if (PEAR::isError($result)) {
  111.                         $this->raiseError($result->getUserinfo()0$xp$result->getCode());
  112.                     }
  113.                 }
  114.             }
  115.  
  116.             $result $this->validator->validateDatabase($this->database_definition);
  117.             if (PEAR::isError($result)) {
  118.                 $this->raiseError($result->getUserinfo()0$xp$result->getCode());
  119.             }
  120.         }
  121.  
  122.         return MDB2_OK;
  123.     }
  124.  
  125.     function setInputFile($filename)
  126.     {
  127.         $this->filename $filename;
  128.         return MDB2_OK;
  129.     }
  130.  
  131.     function renameKey(&$arr$oKey$nKey)
  132.     {
  133.         $arr[$nKey&$arr[$oKey];
  134.         unset($arr[$oKey]);
  135.     }
  136.  
  137.     function fixDatabaseKeys()
  138.     {
  139.         if (!empty($this->database_definition['table']&& is_array($this->database_definition['table'])) {
  140.             $this->renameKey($this->database_definition'table''tables');
  141.             foreach ($this->database_definition['tables'as $k => $v{
  142.                 $this->fixTableKeys($k);
  143.             }
  144.         }
  145.  
  146.         if (!empty($this->database_definition['sequence']&& is_array($this->database_definition['sequence'])) {
  147.             $this->renameKey($this->database_definition'sequece''sequences');
  148.             foreach ($this->database_definition['sequences'as $k => $v{
  149.                 $this->fixSequenceKeys($k);
  150.             }
  151.         }
  152.     }
  153.  
  154.     function fixTableKeys($k)
  155.     {
  156.         $table $this->database_definition['tables'][$k]['name'];
  157.         unset($this->database_definition['tables'][$k]['name']);
  158.         $this->renameKey($this->database_definition['tables']$k$table);
  159.  
  160.         $this->fixTableFieldKeys($table);
  161.         $this->fixTableIndexKeys($table);
  162.  
  163.         foreach ($this->database_definition['tables'][$table]['declaration'as $k => $v{
  164.             $this->database_definition['tables'][$table][$k$v;
  165.         }
  166.         unset($this->database_definition['tables'][$table]['declaration']);
  167.  
  168.         $this->fixTableInitializationKeys($table);
  169.     }
  170.  
  171.     function fixTableFieldKeys($table)
  172.     {
  173.         foreach ($this->database_definition['tables'][$table]['declaration']['field'as $k => $v{
  174.             $nKey $this->database_definition['tables'][$table]['declaration']['field'][$k]['name'];
  175.             unset($this->database_definition['tables'][$table]['declaration']['field'][$k]['name']);
  176.             $this->renameKey($this->database_definition['tables'][$table]['declaration']['field']$k$nKey);
  177.         }
  178.         $this->renameKey($this->database_definition['tables'][$table]['declaration']'field''fields');
  179.     }
  180.  
  181.     function fixTableIndexKeys($table)
  182.     {
  183.         if (!empty($this->database_definition['tables'][$table]['declaration']['index']&& is_array($this->database_definition['tables'][$table]['declaration']['index'])) {
  184.             foreach ($this->database_definition['tables'][$table]['declaration']['index'as $k => $v{
  185.                 $nKey $this->database_definition['tables'][$table]['declaration']['index'][$k]['name'];
  186.                 unset($this->database_definition['tables'][$table]['declaration']['index'][$k]['name']);
  187.                 $this->renameKey($this->database_definition['tables'][$table]['declaration']['index']$k$nKey);
  188.             }
  189.         }
  190.         $this->renameKey($this->database_definition['tables'][$table]['declaration']'index''indexes');
  191.     }
  192.  
  193.     function fixTableInitializationKeys($table)
  194.     {
  195.         $dml = array'insert''update''delete' );
  196.         $init = array();
  197.  
  198.         foreach ($dml as $type{
  199.             if (!empty($this->database_definition['tables'][$table]['initialization'][$type]&& is_array($this->database_definition['tables'][$table]['initialization'][$type])) {
  200.                 foreach ($this->database_definition['tables'][$table]['initialization'][$typeas $k => $v{
  201.                     $this->fixTableInitializationDataKeys($v);
  202.                     $init[= array'type' => $type'data' => $v);
  203.                 }
  204.             }
  205.         }
  206.         unset($this->database_definition['tables'][$table]['initialization']);
  207.         $this->database_definition['tables'][$table]['initialization'&$init;
  208.     }
  209.  
  210.     function fixTableInitializationDataKeys(&$element)
  211.     {
  212.         if (!empty($element['field']&& is_array($element['field'])) {
  213.             foreach ($element['field'as $k => $v{
  214.                 $name $v['name'];
  215.                 unset($v['name']);
  216.  
  217.                 $this->setExpression($v);
  218.                 $field = array'name' => $name'group' => $v );
  219.                 $element['field'][$k$field;
  220.             }
  221.         }
  222.         if (!empty($element['where']&& is_array($element['where'])) {
  223.             $this->setExpression($element['where']);
  224.         }
  225.     }
  226.  
  227.     function setExpression(&$arr)
  228.     {
  229.         $element each($arr);
  230.         $arr = array'type' => $element['key');
  231.         $element $element['value'];
  232.  
  233.         switch ($arr['type']{
  234.             case 'null':
  235.             case 'value':
  236.             case 'column':
  237.                 $arr['data'$element;
  238.             break;
  239.             case 'function':
  240.                 if (!empty($element)
  241.                     && is_array($element)
  242.                 {
  243.                     $arr['data'= array'name' => $element['name']'arguments' => array() );
  244.                     unset($element['name']);
  245.  
  246.                     foreach ($element as $k => $v{
  247.                         $argument = array$k => $v );
  248.                         $this->setExpression($argument);
  249.                         $arr['data']['arguments'][$argument;
  250.                     }
  251.                 }
  252.             break;
  253.             case 'expression':
  254.                 $arr['data'= array'operator' => $element['operator']'operants' => array() );
  255.                 unset($element['operator']);
  256.  
  257.                 foreach ($element as $k => $v{
  258.                     $argument = array$k => $v );
  259.                     $this->setExpression($argument);
  260.                     $arr['data']['operants'][$argument;
  261.                 }
  262.             break;
  263.         }
  264.     }
  265.  
  266.     function fixSequenceKeys($k)
  267.     {
  268.         $seq $this->database_definition['sequences'][$k]['name'];
  269.         unset($this->database_definition['sequences'][$k]['name']);
  270.         $this->renameKey($this->database_definition['sequences']$k$seq);
  271.     }
  272.  
  273.     /* blindly copied from original parser. to be implemented yet. */
  274.     function &raiseError($msg = null$xmlecode = 0$xp = null$ecode = MDB2_SCHEMA_ERROR_PARSE)
  275.     {
  276.         if (is_null($this->error)) {
  277.             $error '';
  278.             if (is_resource($msg)) {
  279.                 $error.= 'Parser error: '.xml_error_string(xml_get_error_code($msg));
  280.                 $xp $msg;
  281.             else {
  282.                 $error.= 'Parser error: '.$msg;
  283.                 if (!is_resource($xp)) {
  284.                     $xp $this->parser;
  285.                 }
  286.             }
  287.             if ($error_string xml_error_string($xmlecode)) {
  288.                 $error.= ' - '.$error_string;
  289.             }
  290.             if (is_resource($xp)) {
  291.                 $byte @xml_get_current_byte_index($xp);
  292.                 $line @xml_get_current_line_number($xp);
  293.                 $column @xml_get_current_column_number($xp);
  294.                 $error.= " - Byte: $byte; Line: $line; Col: $column";
  295.             }
  296.             $error.= "\n";
  297.             $this->error =MDB2_Schema::raiseError($ecodenullnull$error);
  298.         }
  299.         return $this->error;
  300.     }
  301. }
  302.  
  303. ?>

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