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

Source for file Validate.php

Documentation is available at Validate.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: Christian Dickmann <dickmann@php.net>                        |
  43. // | Author: Igor Feghali <ifeghali@php.net>                              |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: Validate.php,v 1.30 2006/12/19 15:03:55 quipo Exp $
  47. //
  48.  
  49. /**
  50.  * Validates an XML schema file
  51.  *
  52.  * @package MDB2_Schema
  53.  * @category Database
  54.  * @access protected
  55.  * @author Igor Feghali <ifeghali@php.net>
  56.  */
  57. {
  58.     // {{{ properties
  59.  
  60.     var $fail_on_invalid_names = true;
  61.     var $valid_types = array();
  62.     var $force_defaults = true;
  63.  
  64.     // }}}
  65.     // {{{ constructor
  66.  
  67.     function __construct($fail_on_invalid_names = true$valid_types = array()$force_defaults = true)
  68.     {
  69.         if (is_array($fail_on_invalid_names)) {
  70.             $this->fail_on_invalid_names
  71.                 = array_intersect($fail_on_invalid_namesarray_keys($GLOBALS['_MDB2_Schema_Reserved']));
  72.         elseif ($this->fail_on_invalid_names === true{
  73.             $this->fail_on_invalid_names = array_keys($GLOBALS['_MDB2_Schema_Reserved']);
  74.         else {
  75.             $this->fail_on_invalid_names = array();
  76.         }
  77.         $this->valid_types = $valid_types;
  78.         $this->force_defaults = $force_defaults;
  79.     }
  80.  
  81.     function MDB2_Schema_Validate($fail_on_invalid_names = true$valid_types = array()$force_defaults = true)
  82.     {
  83.         $this->__construct($fail_on_invalid_names$valid_types$force_defaults);
  84.     }
  85.  
  86.     // }}}
  87.     // {{{ raiseError()
  88.  
  89.     function &raiseError($ecode$msg = null)
  90.     {
  91.         $error =MDB2_Schema::raiseError($ecodenullnull$msg);
  92.         return $error;
  93.     }
  94.  
  95.     // }}}
  96.     // {{{ isBoolean()
  97.  
  98.     /**
  99.      * Verifies if a given value can be considered boolean. If yes, set value
  100.      * to true or false according to its actual contents and return true. If
  101.      * not, keep its contents untouched and return false.
  102.      *
  103.      * @param mixed  value to be checked
  104.      *
  105.      * @return bool 
  106.      *
  107.      * @access public
  108.      * @static
  109.      */
  110.     function isBoolean(&$value)
  111.     {
  112.         if (is_bool($value)) {
  113.             return true;
  114.         }
  115.         if ($value === 0 || $value === 1 || $value === ''{
  116.             $value = (bool)$value;
  117.             return true;
  118.         }
  119.         if (!is_string($value)) {
  120.             return false;
  121.         }
  122.         switch ($value{
  123.         case '0':
  124.         case 'N':
  125.         case 'n':
  126.         case 'no':
  127.         case 'false':
  128.             $value = false;
  129.             break;
  130.         case '1':
  131.         case 'Y':
  132.         case 'y':
  133.         case 'yes':
  134.         case 'true':
  135.             $value = true;
  136.             break;
  137.         default:
  138.             return false;
  139.         }
  140.         return true;
  141.     }
  142.  
  143.     // }}}
  144.     // {{{ validateTable()
  145.  
  146.     /* Definition */
  147.     /**
  148.      * Checks whether the definition of a parsed table is valid. Modify table
  149.      * definition when necessary.
  150.      *
  151.      * @param array  multi dimensional array that contains the
  152.      *                 tables of current database.
  153.      * @param array  multi dimensional array that contains the
  154.      *                 structure and optional data of the table.
  155.      * @param string  name of the parsed table
  156.      *
  157.      * @return bool|errorobject
  158.      *
  159.      * @access public
  160.      */
  161.     function validateTable($tables&$table$table_name)
  162.     {
  163.         /* Have we got a name? */
  164.         if (!$table_name{
  165.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  166.                 'a table has to have a name');
  167.         }
  168.  
  169.         /* Table name duplicated? */
  170.         if (is_array($tables&& isset($tables[$table_name])) {
  171.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  172.                 'table "'.$table_name.'" already exists');
  173.         }
  174.  
  175.         /* Table name reserved? */
  176.         if (is_array($this->fail_on_invalid_names)) {
  177.             $name strtoupper($table_name);
  178.             foreach ($this->fail_on_invalid_names as $rdbms{
  179.                 if (in_array($name$GLOBALS['_MDB2_Schema_Reserved'][$rdbms])) {
  180.                     return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  181.                         'table name "'.$table_name.'" is a reserved word in: '.$rdbms);
  182.                 }
  183.             }
  184.         }
  185.  
  186.         /* Was */
  187.         if (empty($table['was'])) {
  188.             $table['was'$table_name;
  189.         }
  190.  
  191.         /* Have we got fields? */
  192.         if (empty($table['fields']|| !is_array($table['fields'])) {
  193.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  194.                 'tables need one or more fields');
  195.         }
  196.  
  197.         /* Autoincrement */
  198.         $autoinc $primary = false;
  199.         foreach ($table['fields'as $field_name => $field{
  200.             if (!empty($field['autoincrement'])) {
  201.                 if ($primary{
  202.                     return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  203.                         'there was already an autoincrement field in "'.$table_name.'" before "'.$field_name.'"');
  204.                 }
  205.                 $autoinc $primary = true;
  206.             }
  207.         }
  208.  
  209.         /*
  210.          * Checking Indexes
  211.          * this have to be done here as we can't
  212.          * guarantee that all table fields were already
  213.          * defined in the moment we are parssing indexes
  214.          */
  215.         if (!empty($table['indexes']&& is_array($table['indexes'])) {
  216.             foreach ($table['indexes'as $name => $index{
  217.                 $skip_index = false;
  218.                 if (!empty($index['primary'])) {
  219.                     /*
  220.                      * Lets see if we should skip this index since there is
  221.                      * already an auto increment on this field this implying
  222.                      * a primary key index.
  223.                      */
  224.                     if ($autoinc && count($index['fields']== '1'{
  225.                         $skip_index = true;
  226.                     elseif ($primary{
  227.                         return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  228.                             'there was already an primary index or autoincrement field in "'.$table_name.'" before "'.$name.'"');
  229.                     else {
  230.                         $primary = true;
  231.                     }
  232.                 }
  233.  
  234.                 if (!$skip_index && is_array($index['fields'])) {
  235.                     foreach ($index['fields'as $field_name => $field{
  236.                         if (!isset($table['fields'][$field_name])) {
  237.                             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  238.                                 'index field "'.$field_name.'" does not exist');
  239.                         }
  240.                         if (!empty($index['primary'])
  241.                             && !$table['fields'][$field_name]['notnull']
  242.                         {
  243.                             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  244.                                 'all primary key fields must be defined notnull in "'.$table_name.'"');
  245.                         }
  246.                     }
  247.                 else {
  248.                     unset($table['indexes'][$name]);
  249.                 }
  250.             }
  251.         }
  252.         return true;
  253.     }
  254.  
  255.     // }}}
  256.     // {{{ validateField()
  257.  
  258.     /**
  259.      * Checks whether the definition of a parsed field is valid. Modify field
  260.      * definition when necessary.
  261.      *
  262.      * @param array  multi dimensional array that contains the
  263.      *                 fields of current table.
  264.      * @param array  multi dimensional array that contains the
  265.      *                 structure of the parsed field.
  266.      * @param string  name of the parsed field
  267.      *
  268.      * @return bool|errorobject
  269.      *
  270.      * @access public
  271.      */
  272.     function validateField($fields&$field$field_name)
  273.     {
  274.         /* Have we got a name? */
  275.         if (!$field_name{
  276.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  277.                 'field name missing');
  278.         }
  279.  
  280.         /* Field name duplicated? */
  281.         if (is_array($fields&& isset($fields[$field_name])) {
  282.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  283.                 'field "'.$field_name.'" already exists');
  284.         }
  285.  
  286.         /* Field name reserverd? */
  287.         if (is_array($this->fail_on_invalid_names)) {
  288.             $name strtoupper($field_name);
  289.             foreach ($this->fail_on_invalid_names as $rdbms{
  290.                 if (in_array($name$GLOBALS['_MDB2_Schema_Reserved'][$rdbms])) {
  291.                     return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  292.                         'field name "'.$field_name.'" is a reserved word in: '.$rdbms);
  293.                 }
  294.             }
  295.         }
  296.  
  297.         /* Type check */
  298.         if (empty($field['type'])) {
  299.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  300.                 'no field type specified');
  301.         }
  302.         if (!empty($this->valid_types&& !array_key_exists($field['type']$this->valid_types)) {
  303.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  304.                 'no valid field type ("'.$field['type'].'") specified');
  305.         }
  306.  
  307.         /* Unsigned */
  308.         if (array_key_exists('unsigned'$field&& !$this->isBoolean($field['unsigned'])) {
  309.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  310.                 'unsigned has to be a boolean value');
  311.         }
  312.  
  313.         /* Fixed */
  314.         if (array_key_exists('fixed'$field&& !$this->isBoolean($field['fixed'])) {
  315.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  316.                 'fixed has to be a boolean value');
  317.         }
  318.  
  319.         /* Length */
  320.         if (array_key_exists('length'$field&& $field['length'<= 0{
  321.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  322.                 'length has to be an integer greater 0');
  323.         }
  324.  
  325.         /* Was */
  326.         if (empty($field['was'])) {
  327.             $field['was'$field_name;
  328.         }
  329.  
  330.         /* Notnull */
  331.         if (empty($field['notnull'])) {
  332.             $field['notnull'= false;
  333.         }
  334.         if (!$this->isBoolean($field['notnull'])) {
  335.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  336.                 'field "notnull" has to be a boolean value');
  337.         }
  338.  
  339.         /* Default */
  340.         if ($this->force_defaults
  341.             && !array_key_exists('default'$field)
  342.             && $field['type'!= 'clob' && $field['type'!= 'blob'
  343.         {
  344.             $field['default'$this->valid_types[$field['type']];
  345.         }
  346.         if (array_key_exists('default'$field)) {
  347.             if ($field['type'== 'clob' || $field['type'== 'blob'{
  348.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  349.                     '"'.$field['type'].'"-fields are not allowed to have a default value');
  350.             }
  351.             if ($field['default'=== '' && !$field['notnull']{
  352.                 $field['default'= null;
  353.             }
  354.         }
  355.         if (isset($field['default'])
  356.             && PEAR::isError($result $this->validateDataFieldValue($field$field['default']$field_name))
  357.         {
  358.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  359.                 'default value of "'.$field_name.'" is incorrect: '.$result->getUserinfo());
  360.         }
  361.  
  362.         /* Autoincrement */
  363.         if (!empty($field['autoincrement'])) {
  364.             if (!$field['notnull']{
  365.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  366.                     'all autoincrement fields must be defined notnull');
  367.             }
  368.  
  369.             if (empty($field['default'])) {
  370.                 $field['default''0';
  371.             elseif ($field['default'!== '0' && $field['default'!== 0{
  372.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  373.                     'all autoincrement fields must be defined default "0"');
  374.             }
  375.         }
  376.         return true;
  377.     }
  378.  
  379.     // }}}
  380.     // {{{ validateIndex()
  381.  
  382.     /**
  383.      * Checks whether a parsed index is valid. Modify index definition when
  384.      * necessary.
  385.      *
  386.      * @param array  multi dimensional array that contains the
  387.      *                 indexes of current table.
  388.      * @param array  multi dimensional array that contains the
  389.      *                 structure of the parsed index.
  390.      * @param string  name of the parsed index
  391.      *
  392.      * @return bool|errorobject
  393.      *
  394.      * @access public
  395.      */
  396.     function validateIndex($table_indexes&$index$index_name)
  397.     {
  398.         if (!$index_name{
  399.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  400.                 'an index has to have a name');
  401.         }
  402.         if (is_array($table_indexes&& isset($table_indexes[$index_name])) {
  403.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  404.                 'index "'.$index_name.'" already exists');
  405.         }
  406.         if (array_key_exists('unique'$index&& !$this->isBoolean($index['unique'])) {
  407.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  408.                 'field "unique" has to be a boolean value');
  409.         }
  410.         if (array_key_exists('primary'$index&& !$this->isBoolean($index['primary'])) {
  411.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  412.                 'field "primary" has to be a boolean value');
  413.         }
  414.  
  415.         if (empty($index['was'])) {
  416.             $index['was'$index_name;
  417.         }
  418.         return true;
  419.     }
  420.  
  421.     // }}}
  422.     // {{{ validateIndexField()
  423.  
  424.     /**
  425.      * Checks whether a parsed index-field is valid. Modify its definition when
  426.      * necessary.
  427.      *
  428.      * @param array  multi dimensional array that contains the
  429.      *                 fields of current index.
  430.      * @param array  multi dimensional array that contains the
  431.      *                 structure of the parsed index-field.
  432.      * @param string  name of the parsed index-field
  433.      *
  434.      * @return bool|errorobject
  435.      *
  436.      * @access public
  437.      */
  438.     function validateIndexField($index_fields&$field$field_name)
  439.     {
  440.         if (is_array($index_fields&& isset($index_fields[$field_name])) {
  441.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  442.                 'index field "'.$field_name.'" already exists');
  443.         }
  444.         if (!$field_name{
  445.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  446.                 'the index-field-name is required');
  447.         }
  448.         if (empty($field['sorting'])) {
  449.             $field['sorting''ascending';
  450.         elseif($field['sorting'!== 'ascending' && $field['sorting'!== 'descending'{
  451.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  452.                 'sorting type unknown');
  453.         }
  454.         return true;
  455.     }
  456.  
  457.     // }}}
  458.     // {{{ validateSequence()
  459.  
  460.     /**
  461.      * Checks whether the definition of a parsed sequence is valid. Modify
  462.      * sequence definition when necessary.
  463.      *
  464.      * @param array  multi dimensional array that contains the
  465.      *                 sequences of current database.
  466.      * @param array  multi dimensional array that contains the
  467.      *                 structure of the parsed sequence.
  468.      * @param string  name of the parsed sequence
  469.      *
  470.      * @return bool|errorobject
  471.      *
  472.      * @access public
  473.      */
  474.     function validateSequence($sequences&$sequence$sequence_name)
  475.     {
  476.         if (!$sequence_name{
  477.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  478.                 'a sequence has to have a name');
  479.         }
  480.  
  481.         if (is_array($sequences&& isset($sequences[$sequence_name])) {
  482.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  483.                 'sequence "'.$sequence_name.'" already exists');
  484.         }
  485.  
  486.         if (is_array($this->fail_on_invalid_names)) {
  487.             $name strtoupper($sequence_name);
  488.             foreach ($this->fail_on_invalid_names as $rdbms{
  489.                 if (in_array($name$GLOBALS['_MDB2_Schema_Reserved'][$rdbms])) {
  490.                     return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  491.                         'sequence name "'.$sequence_name.'" is a reserved word in: '.$rdbms);
  492.                 }
  493.             }
  494.         }
  495.  
  496.         if (empty($sequence['was'])) {
  497.             $sequence['was'$sequence_name;
  498.         }
  499.  
  500.         if (!empty($sequence['on'])
  501.             && (empty($sequence['on']['table']|| empty($sequence['on']['field']))
  502.         {
  503.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  504.                 'sequence "'.$sequence_name.'" on a table was not properly defined');
  505.         }
  506.         return true;
  507.     }
  508.  
  509.     // }}}
  510.     // {{{ validateDatabase()
  511.  
  512.     /**
  513.      * Checks whether a parsed database is valid. Modify its structure and
  514.      * data when necessary.
  515.      *
  516.      * @param array  multi dimensional array that contains the
  517.      *                 structure and optional data of the database.
  518.      *
  519.      * @return bool|errorobject
  520.      *
  521.      * @access public
  522.      */
  523.     function validateDatabase(&$database)
  524.     {
  525.         /* Have we got a name? */
  526.         if (!is_array($database|| !isset($database['name']|| !$database['name']{
  527.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  528.                 'a database has to have a name');
  529.         }
  530.  
  531.         /* Database name reserved? */
  532.         if (is_array($this->fail_on_invalid_names)) {
  533.             $name strtoupper($database['name']);
  534.             foreach ($this->fail_on_invalid_names as $rdbms{
  535.                 if (in_array($name$GLOBALS['_MDB2_Schema_Reserved'][$rdbms])) {
  536.                     return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  537.                         'database name "'.$database['name'].'" is a reserved word in: '.$rdbms);
  538.                 }
  539.             }
  540.         }
  541.  
  542.         /* Create */
  543.         if (isset($database['create'])
  544.             && !$this->isBoolean($database['create'])
  545.         {
  546.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  547.                 'field "create" has to be a boolean value');
  548.         }
  549.  
  550.         /* Overwrite */
  551.         if (isset($database['overwrite'])
  552.             && $database['overwrite'!== ''
  553.             && !$this->isBoolean($database['overwrite'])
  554.         {
  555.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  556.                 'field "overwrite" has to be a boolean value');
  557.         }
  558.  
  559.         /*
  560.          * This have to be done here as we can't guarantee that all tables
  561.          * were already defined in the moment we are parsing indexes
  562.          */
  563.         if (isset($database['sequences'])) {
  564.             foreach ($database['sequences'as $seq_name => $seq{
  565.                 if (!empty($seq['on'])
  566.                     && empty($database['tables'][$seq['on']['table']]['fields'][$seq['on']['field']])
  567.                 {
  568.                     return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  569.                         'sequence "'.$seq_name.'" was assigned on unexisting field/table');
  570.                 }
  571.             }
  572.         }
  573.         return true;
  574.     }
  575.  
  576.     // }}}
  577.     // {{{ validateDataField()
  578.  
  579.     /* Data Manipulation */
  580.     /**
  581.      * Checks whether a parsed DML-field is valid. Modify its structure when
  582.      * necessary. This is called when validating INSERT and
  583.      * UPDATE.
  584.      *
  585.      * @param array  multi dimensional array that contains the
  586.      *                 definition for current table's fields.
  587.      * @param array  multi dimensional array that contains the
  588.      *                 parsed fields of the current DML instruction.
  589.      * @param string  name of the parsed insert-field
  590.      * @param string  value to fill the parsed insert-field
  591.      *
  592.      * @return bool|errorobject
  593.      *
  594.      * @access public
  595.      */
  596.     function validateDataField($table_fields$instruction_fields&$field)
  597.     {
  598.         if (!$field['name']{
  599.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  600.                 'field-name has to be specified');
  601.         }
  602.         if (is_array($instruction_fields&& isset($instruction_fields[$field['name']])) {
  603.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  604.                 'field "'.$field['name'].'" already initialized');
  605.         }
  606.         if (is_array($table_fields&& !isset($table_fields[$field['name']])) {
  607.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  608.                 '"'.$field['name'].'" is not defined');
  609.         }
  610.         if (!isset($field['group']['type'])) {
  611.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  612.                 '"'.$field['name'].'" has no initial value');
  613.         }
  614.         if (isset($field['group']['data'])
  615.             && $field['group']['type'== 'value'
  616.             && $field['group']['data'!== ''
  617.             && PEAR::isError($result $this->validateDataFieldValue($table_fields[$field['name']]$field['group']['data']$field['name']))
  618.         {
  619.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  620.                 'value of "'.$field['name'].'" is incorrect: '.$result->getUserinfo());
  621.         }
  622.         return true;
  623.     }
  624.  
  625.     // }}}
  626.     // {{{ validateDataFieldValue()
  627.  
  628.     /**
  629.      * Checks whether a given value is compatible with a table field. This is
  630.      * done when parsing a field for a INSERT or UPDATE instruction.
  631.      *
  632.      * @param array  multi dimensional array that contains the
  633.      *                 definition for current table's fields.
  634.      * @param string  value to fill the parsed field
  635.      * @param string  name of the parsed field
  636.      *
  637.      * @return bool|errorobject
  638.      *
  639.      * @access public
  640.      * @see MDB2_Schema_Validate::validateInsertField()
  641.      */
  642.     function validateDataFieldValue($field_def&$field_value$field_name)
  643.     {
  644.         switch ($field_def['type']{
  645.         case 'text':
  646.         case 'clob':
  647.             if (!empty($field_def['length']&& strlen($field_value$field_def['length']{
  648.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  649.                     '"'.$field_value.'" is larger than "'.$field_def['length'].'"');
  650.             }
  651.             break;
  652.         case 'blob':
  653.             $field_value pack('H*'$field_value);
  654.             if (!empty($field_def['length']&& strlen($field_value$field_def['length']{
  655.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  656.                     '"'.$field_value.'" is larger than "'.$field_def['type'].'"');
  657.             }
  658.             break;
  659.         case 'integer':
  660.             if ($field_value != ((int)$field_value)) {
  661.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  662.                     '"'.$field_value.'" is not of type "'.$field_def['type'].'"');
  663.             }
  664.             //$field_value = (int)$field_value;
  665.             if (!empty($field_def['unsigned']&& $field_def['unsigned'&& $field_value < 0{
  666.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  667.                     '"'.$field_value.'" signed instead of unsigned');
  668.             }
  669.             break;
  670.         case 'boolean':
  671.             if (!$this->isBoolean($field_value)) {
  672.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  673.                     '"'.$field_value.'" is not of type "'.$field_def['type'].'"');
  674.             }
  675.             break;
  676.         case 'date':
  677.             if (!preg_match('/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/'$field_value)
  678.                 && $field_value !== 'CURRENT_DATE'
  679.             {
  680.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  681.                     '"'.$field_value.'" is not of type "'.$field_def['type'].'"');
  682.             }
  683.             break;
  684.         case 'timestamp':
  685.             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)
  686.                 && $field_value !== 'CURRENT_TIMESTAMP'
  687.             {
  688.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  689.                     '"'.$field_value.'" is not of type "'.$field_def['type'].'"');
  690.             }
  691.             break;
  692.         case 'time':
  693.             if (!preg_match("/([0-9]{2}):([0-9]{2}):([0-9]{2})/"$field_value)
  694.                 && $field_value !== 'CURRENT_TIME'
  695.             {
  696.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  697.                     '"'.$field_value.'" is not of type "'.$field_def['type'].'"');
  698.             }
  699.             break;
  700.         case 'float':
  701.         case 'double':
  702.             if ($field_value != (double)$field_value{
  703.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  704.                     '"'.$field_value.'" is not of type "'.$field_def['type'].'"');
  705.             }
  706.             //$field_value = (double)$field_value;
  707.             break;
  708.         }
  709.         return true;
  710.     }
  711. }
  712.  
  713. ?>

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