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

Source for file Parser.php

Documentation is available at Parser.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  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: Christian Dickmann <dickmann@php.net>                        |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: Parser.php,v 1.5 2004/04/09 10:39:08 lsmith Exp $
  46. //
  47.  
  48. require_once 'XML/Parser.php';
  49.  
  50. /**
  51.  * Parses an XML schema file
  52.  *
  53.  * @package MDB2
  54.  * @category Database
  55.  * @access private
  56.  * @author  Christian Dickmann <dickmann@php.net>
  57.  */
  58. class MDB2_Tools_Parser extends XML_Parser
  59. {
  60.     var $database_definition = array();
  61.     var $elements = array();
  62.     var $element '';
  63.     var $count = 0;
  64.     var $table = array();
  65.     var $table_name '';
  66.     var $field = array();
  67.     var $field_name '';
  68.     var $init = array();
  69.     var $init_name '';
  70.     var $init_value '';
  71.     var $index = array();
  72.     var $index_name '';
  73.     var $var_mode = false;
  74.     var $variables = array();
  75.     var $seq = array();
  76.     var $seq_name '';
  77.     var $error = null;
  78.  
  79.     var $invalid_names = array(
  80.         'user' => array(),
  81.         'is' => array(),
  82.         'file' => array(
  83.             'oci' => array(),
  84.             'oracle' => array()
  85.         ),
  86.         'notify' => array(
  87.             'pgsql' => array()
  88.         ),
  89.         'restrict' => array(
  90.             'mysql' => array()
  91.         ),
  92.         'password' => array(
  93.             'ibase' => array()
  94.         )
  95.     );
  96.     var $fail_on_invalid_names = true;
  97.  
  98.     function MDB2_Tools_Parser($variables$fail_on_invalid_names = true)
  99.     {
  100.         $this->XML_Parser();
  101.         $this->variables $variables;
  102.         $this->fail_on_invalid_names $fail_on_invalid_names;
  103.     }
  104.  
  105.     function startHandler($xp$element$attribs)
  106.     {
  107.         if (strtolower($element== 'variable'{
  108.             $this->var_mode = true;
  109.             return;
  110.         };
  111.  
  112.         $this->elements[$this->count++strtolower($element);
  113.         $this->element implode('-'$this->elements);
  114.  
  115.         switch ($this->element{
  116.         case 'database-table-initialization-insert':
  117.             $this->init = array('type' => 'insert');
  118.             break;
  119.         case 'database-table-initialization-insert-field':
  120.             $this->init_name '';
  121.             $this->init_value '';
  122.             break;
  123.         case 'database-table':
  124.             $this->table_name '';
  125.             $this->table = array();
  126.             break;
  127.         case 'database-table-declaration-field':
  128.             $this->field_name '';
  129.             $this->field = array();
  130.             break;
  131.         case 'database-table-declaration-field-default':
  132.             $this->field['default''';
  133.             break;
  134.         case 'database-table-declaration-index':
  135.             $this->index_name '';
  136.             $this->index = array();
  137.             break;
  138.         case 'database-sequence':
  139.             $this->seq_name '';
  140.             $this->seq = array();
  141.             break;
  142.         case 'database-table-declaration-index-field':
  143.             $this->field_name '';
  144.             $this->field = array();
  145.             break;
  146.         };
  147.     }
  148.  
  149.     function endHandler($xp$element)
  150.     {
  151.         if (strtolower($element== 'variable'{
  152.             $this->var_mode = false;
  153.             return;
  154.         };
  155.  
  156.         switch ($this->element{
  157.         /* Initialization */
  158.         case 'database-table-initialization-insert-field':
  159.             if (!$this->init_name{
  160.                 $this->raiseError('field-name has to be specified'$xp);
  161.             };
  162.             if (isset($this->init['fields'][$this->init_name])) {
  163.                 $this->raiseError('field "'.$this->init_name.'" already filled'$xp);
  164.             };
  165.             if (!isset($this->table['fields'][$this->init_name])) {
  166.                 $this->raiseError('unkown field "'.$this->init_name.'"'$xp);
  167.             };
  168.             if ($this->init_value !== ''
  169.                 && !$this->validateFieldValue($this->init_name$this->init_value$xp))
  170.             {
  171.                 $this->raiseError('field "'.$this->init_name.'" has wrong value'$xp);
  172.             };
  173.             $this->init['fields'][$this->init_name$this->init_value;
  174.             break;
  175.         case 'database-table-initialization-insert':
  176.             $this->table['initialization'][$this->init;
  177.             break;
  178.  
  179.         /* Table definition */
  180.         case 'database-table':
  181.             if (!isset($this->table['was'])) {
  182.                 $this->table['was'$this->table_name;
  183.             };
  184.             if (!$this->table_name{
  185.                 $this->raiseError('tables need names'$xp);
  186.             };
  187.             if (isset($this->database_definition['tables'][$this->table_name])) {
  188.                 $this->raiseError('table "'.$this->table_name.'" already exists'$xp);
  189.             };
  190.             if (!isset($this->table['fields'])) {
  191.                 $this->raiseError('tables need one or more fields'$xp);
  192.             };
  193.             if (isset($this->table['indexes'])) {
  194.                 foreach ($this->table['indexes'as $index_name => $index{
  195.                     foreach ($index['fields'as $field_name => $field{
  196.                         if (!isset($this->table['fields'][$field_name])) {
  197.                             $this->raiseError('index field "'.$field_name.'" does not exist'$xp);
  198.                         }
  199.                         if (!(isset($this->table['fields'][$field_name]['notnull'])
  200.                             && $this->table['fields'][$field_name]['notnull'== true))
  201.                         {
  202.                             $this->raiseError('index field "'.$field_name.
  203.                                 '" has to be "notnull"'$xp);
  204.                         }
  205.                     }
  206.                 }
  207.             };
  208.             $this->database_definition['tables'][$this->table_name$this->table;
  209.             break;
  210.  
  211.         /* Field declaration */
  212.         case 'database-table-declaration-field':
  213.             if (!$this->field_name || !isset($this->field['type'])) {
  214.                 $this->raiseError('field "'.$this->field_name.'" was not properly specified'$xp);
  215.             };
  216.             if (isset($this->table['fields'][$this->field_name])) {
  217.                 $this->raiseError('field "'.$this->field_name.'" already exists'$xp);
  218.             };
  219.             /* Invalidname check */
  220.             if ($this->fail_on_invalid_names && isset($this->invalid_names[$this->field_name])) {
  221.                 $this->raiseError('fieldname "'.$this->field_name.'" not allowed'$xp);
  222.             };
  223.             /* Type check */
  224.             switch ($this->field['type']{
  225.             case 'integer':
  226.                 if (isset($this->field['unsigned'])
  227.                     && $this->field['unsigned'!== '1' && $this->field['unsigned'!== '0')
  228.                 {
  229.                     $this->raiseError('unsigned has to be 1 or 0'$xp);
  230.                 };
  231.                 break;
  232.             case 'text':
  233.             case 'clob':
  234.             case 'blob':
  235.                 if (isset($this->field['length']&& ((int)$this->field['length']<= 0{
  236.                     $this->raiseError('length has to be an integer greater 0'$xp);
  237.                 };
  238.                 break;
  239.             case 'boolean':
  240.             case 'date':
  241.             case 'timestamp':
  242.             case 'time':
  243.             case 'float':
  244.             case 'decimal':
  245.                 break;
  246.             default:
  247.                 $this->raiseError('no valid field type ("'.$this->field['type'].'") specified'$xp);
  248.             };
  249.             if (!isset($this->field['was'])) {
  250.                 $this->field['was'$this->field_name;
  251.             };
  252.             if (isset($this->field['notnull']&& !$this->is_boolean($this->field['notnull'])) {
  253.                 $this->raiseError('field  "notnull" has to be 1 or 0'$xp);
  254.             };
  255.             if (isset($this->field['notnull']&& !isset($this->field['default'])) {
  256.                 $this->raiseError('if field is "notnull", it needs a default value'$xp);
  257.             };
  258.             if (isset($this->field['unsigned']&& !$this->is_boolean($this->field['unsigned'])) {
  259.                 $this->raiseError('field  "notnull" has to be 1 or 0'$xp);
  260.             };
  261.             $this->table['fields'][$this->field_name$this->field;
  262.             if (isset($this->field['default'])) {
  263.                 if ($this->field['type'== 'clob' || $this->field['type'== 'blob'{
  264.                     $this->raiseError('"'.$this->field['type'].
  265.                         '"-fields are not allowed to have a default value'$xp);
  266.                 };
  267.                 if ($this->field['default'!== ''
  268.                     && !$this->validateFieldValue($this->field_name$this->field['default']$xp))
  269.                 {
  270.                     $this->raiseError('default value of "'.$this->field_name.'" is of wrong type'$xp);
  271.                 };
  272.             };
  273.             break;
  274.  
  275.         /* Index declaration */
  276.         case 'database-table-declaration-index':
  277.             if (!$this->index_name{
  278.                 $this->raiseError('an index needs a name'$xp);
  279.             };
  280.             if (isset($this->table['indexes'][$this->index_name])) {
  281.                 $this->raiseError('index "'.$this->index_name.'" already exists'$xp);
  282.             };
  283.             if (isset($this->index['unique']&& !$this->is_boolean($this->index['unique'])) {
  284.                 $this->raiseError('field  "unique" has to be 1 or 0'$xp);
  285.             };
  286.             if (!isset($this->index['was'])) {
  287.                 $this->index['was'$this->index_name;
  288.             };
  289.             $this->table['indexes'][$this->index_name$this->index;
  290.             break;
  291.         case 'database-table-declaration-index-field':
  292.             if (!$this->field_name{
  293.                 $this->raiseError('the index-field-name is required'$xp);
  294.             };
  295.             if (isset($this->field['sorting'])
  296.                 && $this->field['sorting'!== 'ascending' && $this->field['sorting'!== 'descending'{
  297.                 $this->raiseError('sorting type unknown'$xp);
  298.             };
  299.             $this->index['fields'][$this->field_name$this->field;
  300.             break;
  301.  
  302.         /* Sequence declaration */
  303.         case 'database-sequence':
  304.             if (!$this->seq_name{
  305.                 $this->raiseError('a sequence has to have a name'$xp);
  306.             };
  307.             if (isset($this->database_definition['sequences'][$this->seq_name])) {
  308.                 $this->raiseError('sequence "'.$this->seq_name.'" already exists'$xp);
  309.             };
  310.             if (!isset($this->seq['was'])) {
  311.                 $this->seq['was'$this->seq_name;
  312.             };
  313.             if (isset($this->seq['on'])) {
  314.                 if ((!isset($this->seq['on']['table']|| !$this->seq['on']['table'])
  315.                     || (!isset($this->seq['on']['field']|| !$this->seq['on']['field']))
  316.                 {
  317.                     $this->raiseError('sequence "'.$this->seq_name.
  318.                         '" was not properly defined'$xp);
  319.                 };
  320.             };
  321.             $this->database_definition['sequences'][$this->seq_name$this->seq;
  322.             break;
  323.  
  324.         /* End of File */
  325.         case 'database':
  326.             if (isset($this->database_definition['create'])
  327.                 && !$this->is_boolean($this->database_definition['create']))
  328.             {
  329.                 $this->raiseError('field "create" has to be 1 or 0'$xp);
  330.             };
  331.             if (isset($this->database_definition['overwrite'])
  332.                 && !$this->is_boolean($this->database_definition['overwrite']))
  333.             {
  334.                 $this->raiseError('field "overwrite" has to be 1 or 0'$xp);
  335.             };
  336.             if (!isset($this->database_definition['name'])
  337.                 || !$this->database_definition['name']
  338.             {
  339.                 $this->raiseError('database needs a name'$xp);
  340.             };
  341.             if (isset($this->database_definition['sequences'])) {
  342.                 foreach ($this->database_definition['sequences'as $seq_name => $seq{
  343.                     if (isset($seq['on'])
  344.                         && !isset($this->database_definition['tables'][$seq['on']['table']]['fields'][$seq['on']['field']]))
  345.                     {
  346.                         $this->raiseError('sequence "'.$seq_name.
  347.                             '" was assigned on unexisting field/table'$xp);
  348.                     };
  349.                 };
  350.             };
  351.             if (MDB2::isError($this->error)) {
  352.                 $this->database_definition $this->error;
  353.             };
  354.             break;
  355.         }
  356.  
  357.         unset($this->elements[--$this->count]);
  358.         $this->element implode('-'$this->elements);
  359.     }
  360.  
  361.     function validateFieldValue($field_name&$field_value&$xp)
  362.     {
  363.         if (!isset($this->table['fields'][$field_name])) {
  364.             return;
  365.         };
  366.         $field_def $this->table['fields'][$field_name];
  367.         switch ($field_def['type']{
  368.         case 'text':
  369.         case 'clob':
  370.             if (isset($field_def['length']&& strlen($field_value$field_def['length']{
  371.                 return $this->raiseError('"'.$field_value.'" is not of type "'.
  372.                     $field_def['type'].'"'$xp);
  373.             };
  374.             break;
  375.         case 'blob':
  376.             /*
  377.             if (!preg_match('/^([0-9a-f]{2})*$/i', $field_value)) {
  378.                 return $this->raiseError('"'.$field_value.'" is not of type "'.
  379.                     $field_def['type'].'"', $xp);
  380.             }
  381.             */
  382.             $field_value pack('H*'$field_value);
  383.             if (isset($field_def['length']&& strlen($field_value$field_def['length']{
  384.                 return $this->raiseError('"'.$field_value.'" is not of type "'.
  385.                     $field_def['type'].'"'$xp);
  386.             };
  387.             break;
  388.         case 'integer':
  389.             if ($field_value != ((int)$field_value)) {
  390.                 return $this->raiseError('"'.$field_value.'" is not of type "'.
  391.                     $field_def['type'].'"'$xp);
  392.             };
  393.             $field_value = (int) $field_value;
  394.             if (isset($field_def['unsigned']&& $field_def['unsigned'&& $field_value < 0{
  395.                 return $this->raiseError('"'.$field_value.'" is not of type "'.
  396.                     $field_def['type'].'"'$xp);
  397.             };
  398.             break;
  399.         case 'boolean':
  400.             if (!$this->is_boolean($field_value)) {
  401.                 return $this->raiseError('"'.$field_value.'" is not of type "'.
  402.                     $field_def['type'].'"'$xp);
  403.             }
  404.             break;
  405.         case 'date':
  406.             if (!preg_match('/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/'$field_value)) {
  407.                 return $this->raiseError('"'.$field_value.'" is not of type "'.
  408.                     $field_def['type'].'"'$xp);
  409.             }
  410.             break;
  411.         case 'timestamp':
  412.             if (!preg_match('/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/'$field_value)) {
  413.                 return $this->raiseError('"'.$field_value.'" is not of type "'.
  414.                     $field_def['type'].'"'$xp);
  415.             }
  416.             break;
  417.         case 'time':
  418.             if (!preg_match("/([0-9]{2}):([0-9]{2}):([0-9]{2})/"$field_value)) {
  419.                 return $this->raiseError('"'.$field_value.'" is not of type "'.
  420.                     $field_def['type'].'"'$xp);
  421.             }
  422.             break;
  423.         case 'float':
  424.         case 'double':
  425.             if ($field_value != (double) $field_value{
  426.                 return $this->raiseError('"'.$field_value.'" is not of type "'.
  427.                     $field_def['type'].'"'$xp);
  428.             };
  429.             $field_value = (double) $field_value;
  430.             break;
  431.         }
  432.         return true;
  433.     }
  434.  
  435.     function raiseError($msg$xp = null)
  436.     {
  437.         if (is_null($this->error)) {
  438.             if (is_resource($msg)) {
  439.                 $error "Parser error: ";
  440.                 $xp $msg;
  441.             else {
  442.                 $error "Parser error: \"".$msg."\"\n";
  443.             }
  444.             if ($xp != null{
  445.                 $byte @xml_get_current_byte_index($xp);
  446.                 $line @xml_get_current_line_number($xp);
  447.                 $column @xml_get_current_column_number($xp);
  448.                 $error .= "Byte: $byte; Line: $line; Col: $column\n";
  449.             }
  450.             $this->error MDB2_Driver_Common::raiseError(MDB2_ERROR_MANAGER_PARSEnullnull);
  451.         };
  452.         return false;
  453.     }
  454.  
  455.     function is_boolean(&$value)
  456.     {
  457.         if (is_int($value&& ($value == 0 || $value == 1)) {
  458.             return true;
  459.         };
  460.         if ($value === '1' || $value === '0'{
  461.             $value = (int) $value;
  462.             return true;
  463.         };
  464.         switch ($value)
  465.         {
  466.         case 'N':
  467.         case 'n':
  468.         case 'no':
  469.         case 'false':
  470.             $value = 0;
  471.             break;
  472.         case 'Y':
  473.         case 'y':
  474.         case 'yes':
  475.         case 'true':
  476.             $value = 1;
  477.             break;
  478.         default:
  479.             return false;
  480.         };
  481.         return true;
  482.     }
  483.  
  484.     function cdataHandler($xp$data)
  485.     {
  486.         if ($this->var_mode == true{
  487.             if (!isset($this->variables[$data])) {
  488.                 $this->raiseError('variable "'.$data.'" not found'$xp);
  489.                 return;
  490.             };
  491.             $data $this->variables[$data];
  492.         };
  493.  
  494.         switch ($this->element{
  495.         /* Initialization */
  496.         case 'database-table-initialization-insert-field-name':
  497.             @$this->init_name .= $data;
  498.             break;
  499.         case 'database-table-initialization-insert-field-value':
  500.             @$this->init_value .= $data;
  501.             break;
  502.  
  503.         /* Database */
  504.         case 'database-name':
  505.             @$this->database_definition['name'.= $data;
  506.             break;
  507.         case 'database-create':
  508.             @$this->database_definition['create'.= $data;
  509.             break;
  510.         case 'database-overwrite':
  511.             @$this->database_definition['overwrite'.= $data;
  512.             break;
  513.         case 'database-table-name':
  514.             @$this->table_name .= $data;
  515.             break;
  516.         case 'database-table-was':
  517.             @$this->table['was'.= $data;
  518.             break;
  519.  
  520.         /* Field declaration */
  521.         case 'database-table-declaration-field-name':
  522.             @$this->field_name .= $data;
  523.             break;
  524.         case 'database-table-declaration-field-type':
  525.             @$this->field['type'.= $data;
  526.             break;
  527.         case 'database-table-declaration-field-was':
  528.             @$this->field['was'.= $data;
  529.             break;
  530.         case 'database-table-declaration-field-notnull':
  531.             @$this->field['notnull'.= $data;
  532.             break;
  533.         case 'database-table-declaration-field-unsigned':
  534.             @$this->field['unsigned'.= $data;
  535.             break;
  536.         case 'database-table-declaration-field-default':
  537.             @$this->field['default'.= $data;
  538.             break;
  539.         case 'database-table-declaration-field-length':
  540.             @$this->field['length'.= $data;
  541.             break;
  542.  
  543.         /* Index declaration */
  544.         case 'database-table-declaration-index-name':
  545.             @$this->index_name .= $data;
  546.             break;
  547.         case 'database-table-declaration-index-unique':
  548.             @$this->index['unique'.= $data;
  549.             break;
  550.         case 'database-table-declaration-index-was':
  551.             @$this->index['was'.= $data;
  552.             break;
  553.         case 'database-table-declaration-index-field-name':
  554.             @$this->field_name .= $data;
  555.             break;
  556.         case 'database-table-declaration-index-field-sorting':
  557.             @$this->field['sorting'.= $data;
  558.             break;
  559.  
  560.         /* Sequence declaration */
  561.         case 'database-sequence-name':
  562.             @$this->seq_name .= $data;
  563.             break;
  564.         case 'database-sequence-was':
  565.             @$this->seq['was'.= $data;
  566.             break;
  567.         case 'database-sequence-start':
  568.             @$this->seq['start'.= $data;
  569.             break;
  570.         case 'database-sequence-on-table':
  571.             @$this->seq['on']['table'.= $data;
  572.             break;
  573.         case 'database-sequence-on-field':
  574.             @$this->seq['on']['field'.= $data;
  575.             break;
  576.         };
  577.     }
  578. };
  579.  
  580. ?>

Documentation generated on Mon, 11 Mar 2019 10:15:53 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.