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.31 2007/03/28 18:36:37 ifeghali 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.         // if it's a DECIMAL datatype, check if a 'scale' value is provided:
  326.         // <length>8,4</length> should be translated to DECIMAL(8,4)
  327.         if (is_float($this->valid_types[$field['type']])
  328.             && !empty($field['length'])
  329.             && strpos($field['length']','!== false
  330.         {
  331.             list($field['length']$field['scale']explode(','$field['length']);
  332.         }
  333.  
  334.         /* Was */
  335.         if (empty($field['was'])) {
  336.             $field['was'$field_name;
  337.         }
  338.  
  339.         /* Notnull */
  340.         if (empty($field['notnull'])) {
  341.             $field['notnull'= false;
  342.         }
  343.         if (!$this->isBoolean($field['notnull'])) {
  344.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  345.                 'field "notnull" has to be a boolean value');
  346.         }
  347.  
  348.         /* Default */
  349.         if ($this->force_defaults
  350.             && !array_key_exists('default'$field)
  351.             && $field['type'!= 'clob' && $field['type'!= 'blob'
  352.         {
  353.             $field['default'$this->valid_types[$field['type']];
  354.         }
  355.         if (array_key_exists('default'$field)) {
  356.             if ($field['type'== 'clob' || $field['type'== 'blob'{
  357.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  358.                     '"'.$field['type'].'"-fields are not allowed to have a default value');
  359.             }
  360.             if ($field['default'=== '' && !$field['notnull']{
  361.                 $field['default'= null;
  362.             }
  363.         }
  364.         if (isset($field['default'])
  365.             && PEAR::isError($result $this->validateDataFieldValue($field$field['default']$field_name))
  366.         {
  367.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  368.                 'default value of "'.$field_name.'" is incorrect: '.$result->getUserinfo());
  369.         }
  370.  
  371.         /* Autoincrement */
  372.         if (!empty($field['autoincrement'])) {
  373.             if (!$field['notnull']{
  374.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  375.                     'all autoincrement fields must be defined notnull');
  376.             }
  377.  
  378.             if (empty($field['default'])) {
  379.                 $field['default''0';
  380.             elseif ($field['default'!== '0' && $field['default'!== 0{
  381.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  382.                     'all autoincrement fields must be defined default "0"');
  383.             }
  384.         }
  385.         return true;
  386.     }
  387.  
  388.     // }}}
  389.     // {{{ validateIndex()
  390.  
  391.     /**
  392.      * Checks whether a parsed index is valid. Modify index definition when
  393.      * necessary.
  394.      *
  395.      * @param array  multi dimensional array that contains the
  396.      *                 indexes of current table.
  397.      * @param array  multi dimensional array that contains the
  398.      *                 structure of the parsed index.
  399.      * @param string  name of the parsed index
  400.      *
  401.      * @return bool|errorobject
  402.      *
  403.      * @access public
  404.      */
  405.     function validateIndex($table_indexes&$index$index_name)
  406.     {
  407.         if (!$index_name{
  408.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  409.                 'an index has to have a name');
  410.         }
  411.         if (is_array($table_indexes&& isset($table_indexes[$index_name])) {
  412.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  413.                 'index "'.$index_name.'" already exists');
  414.         }
  415.         if (array_key_exists('unique'$index&& !$this->isBoolean($index['unique'])) {
  416.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  417.                 'field "unique" has to be a boolean value');
  418.         }
  419.         if (array_key_exists('primary'$index&& !$this->isBoolean($index['primary'])) {
  420.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  421.                 'field "primary" has to be a boolean value');
  422.         }
  423.  
  424.         if (empty($index['was'])) {
  425.             $index['was'$index_name;
  426.         }
  427.         return true;
  428.     }
  429.  
  430.     // }}}
  431.     // {{{ validateIndexField()
  432.  
  433.     /**
  434.      * Checks whether a parsed index-field is valid. Modify its definition when
  435.      * necessary.
  436.      *
  437.      * @param array  multi dimensional array that contains the
  438.      *                 fields of current index.
  439.      * @param array  multi dimensional array that contains the
  440.      *                 structure of the parsed index-field.
  441.      * @param string  name of the parsed index-field
  442.      *
  443.      * @return bool|errorobject
  444.      *
  445.      * @access public
  446.      */
  447.     function validateIndexField($index_fields&$field$field_name)
  448.     {
  449.         if (is_array($index_fields&& isset($index_fields[$field_name])) {
  450.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  451.                 'index field "'.$field_name.'" already exists');
  452.         }
  453.         if (!$field_name{
  454.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  455.                 'the index-field-name is required');
  456.         }
  457.         if (empty($field['sorting'])) {
  458.             $field['sorting''ascending';
  459.         elseif($field['sorting'!== 'ascending' && $field['sorting'!== 'descending'{
  460.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  461.                 'sorting type unknown');
  462.         }
  463.         return true;
  464.     }
  465.  
  466.     // }}}
  467.     // {{{ validateSequence()
  468.  
  469.     /**
  470.      * Checks whether the definition of a parsed sequence is valid. Modify
  471.      * sequence definition when necessary.
  472.      *
  473.      * @param array  multi dimensional array that contains the
  474.      *                 sequences of current database.
  475.      * @param array  multi dimensional array that contains the
  476.      *                 structure of the parsed sequence.
  477.      * @param string  name of the parsed sequence
  478.      *
  479.      * @return bool|errorobject
  480.      *
  481.      * @access public
  482.      */
  483.     function validateSequence($sequences&$sequence$sequence_name)
  484.     {
  485.         if (!$sequence_name{
  486.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  487.                 'a sequence has to have a name');
  488.         }
  489.  
  490.         if (is_array($sequences&& isset($sequences[$sequence_name])) {
  491.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  492.                 'sequence "'.$sequence_name.'" already exists');
  493.         }
  494.  
  495.         if (is_array($this->fail_on_invalid_names)) {
  496.             $name strtoupper($sequence_name);
  497.             foreach ($this->fail_on_invalid_names as $rdbms{
  498.                 if (in_array($name$GLOBALS['_MDB2_Schema_Reserved'][$rdbms])) {
  499.                     return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  500.                         'sequence name "'.$sequence_name.'" is a reserved word in: '.$rdbms);
  501.                 }
  502.             }
  503.         }
  504.  
  505.         if (empty($sequence['was'])) {
  506.             $sequence['was'$sequence_name;
  507.         }
  508.  
  509.         if (!empty($sequence['on'])
  510.             && (empty($sequence['on']['table']|| empty($sequence['on']['field']))
  511.         {
  512.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  513.                 'sequence "'.$sequence_name.'" on a table was not properly defined');
  514.         }
  515.         return true;
  516.     }
  517.  
  518.     // }}}
  519.     // {{{ validateDatabase()
  520.  
  521.     /**
  522.      * Checks whether a parsed database is valid. Modify its structure and
  523.      * data when necessary.
  524.      *
  525.      * @param array  multi dimensional array that contains the
  526.      *                 structure and optional data of the database.
  527.      *
  528.      * @return bool|errorobject
  529.      *
  530.      * @access public
  531.      */
  532.     function validateDatabase(&$database)
  533.     {
  534.         /* Have we got a name? */
  535.         if (!is_array($database|| !isset($database['name']|| !$database['name']{
  536.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  537.                 'a database has to have a name');
  538.         }
  539.  
  540.         /* Database name reserved? */
  541.         if (is_array($this->fail_on_invalid_names)) {
  542.             $name strtoupper($database['name']);
  543.             foreach ($this->fail_on_invalid_names as $rdbms{
  544.                 if (in_array($name$GLOBALS['_MDB2_Schema_Reserved'][$rdbms])) {
  545.                     return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  546.                         'database name "'.$database['name'].'" is a reserved word in: '.$rdbms);
  547.                 }
  548.             }
  549.         }
  550.  
  551.         /* Create */
  552.         if (isset($database['create'])
  553.             && !$this->isBoolean($database['create'])
  554.         {
  555.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  556.                 'field "create" has to be a boolean value');
  557.         }
  558.  
  559.         /* Overwrite */
  560.         if (isset($database['overwrite'])
  561.             && $database['overwrite'!== ''
  562.             && !$this->isBoolean($database['overwrite'])
  563.         {
  564.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  565.                 'field "overwrite" has to be a boolean value');
  566.         }
  567.  
  568.         /*
  569.          * This have to be done here as we can't guarantee that all tables
  570.          * were already defined in the moment we are parsing indexes
  571.          */
  572.         if (isset($database['sequences'])) {
  573.             foreach ($database['sequences'as $seq_name => $seq{
  574.                 if (!empty($seq['on'])
  575.                     && empty($database['tables'][$seq['on']['table']]['fields'][$seq['on']['field']])
  576.                 {
  577.                     return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  578.                         'sequence "'.$seq_name.'" was assigned on unexisting field/table');
  579.                 }
  580.             }
  581.         }
  582.         return true;
  583.     }
  584.  
  585.     // }}}
  586.     // {{{ validateDataField()
  587.  
  588.     /* Data Manipulation */
  589.     /**
  590.      * Checks whether a parsed DML-field is valid. Modify its structure when
  591.      * necessary. This is called when validating INSERT and
  592.      * UPDATE.
  593.      *
  594.      * @param array  multi dimensional array that contains the
  595.      *                 definition for current table's fields.
  596.      * @param array  multi dimensional array that contains the
  597.      *                 parsed fields of the current DML instruction.
  598.      * @param string  array that contains the parsed instruction field
  599.      *
  600.      * @return bool|errorobject
  601.      *
  602.      * @access public
  603.      */
  604.     function validateDataField($table_fields$instruction_fields&$field)
  605.     {
  606.         if (!$field['name']{
  607.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  608.                 'field-name has to be specified');
  609.         }
  610.         if (is_array($instruction_fields&& isset($instruction_fields[$field['name']])) {
  611.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  612.                 'field "'.$field['name'].'" already initialized');
  613.         }
  614.         if (is_array($table_fields&& !isset($table_fields[$field['name']])) {
  615.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  616.                 '"'.$field['name'].'" is not defined');
  617.         }
  618.         if (!isset($field['group']['type'])) {
  619.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  620.                 '"'.$field['name'].'" has no initial value');
  621.         }
  622.         if (isset($field['group']['data'])
  623.             && $field['group']['type'== 'value'
  624.             && $field['group']['data'!== ''
  625.             && PEAR::isError($result $this->validateDataFieldValue($table_fields[$field['name']]$field['group']['data']$field['name']))
  626.         {
  627.             return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  628.                 'value of "'.$field['name'].'" is incorrect: '.$result->getUserinfo());
  629.         }
  630.         return true;
  631.     }
  632.  
  633.     // }}}
  634.     // {{{ validateDataFieldValue()
  635.  
  636.     /**
  637.      * Checks whether a given value is compatible with a table field. This is
  638.      * done when parsing a field for a INSERT or UPDATE instruction.
  639.      *
  640.      * @param array  multi dimensional array that contains the
  641.      *                 definition for current table's fields.
  642.      * @param string  value to fill the parsed field
  643.      * @param string  name of the parsed field
  644.      *
  645.      * @return bool|errorobject
  646.      *
  647.      * @access public
  648.      * @see MDB2_Schema_Validate::validateInsertField()
  649.      */
  650.     function validateDataFieldValue($field_def&$field_value$field_name)
  651.     {
  652.         switch ($field_def['type']{
  653.         case 'text':
  654.         case 'clob':
  655.             if (!empty($field_def['length']&& strlen($field_value$field_def['length']{
  656.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  657.                     '"'.$field_value.'" is larger than "'.$field_def['length'].'"');
  658.             }
  659.             break;
  660.         case 'blob':
  661.             $field_value pack('H*'$field_value);
  662.             if (!empty($field_def['length']&& strlen($field_value$field_def['length']{
  663.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  664.                     '"'.$field_value.'" is larger than "'.$field_def['type'].'"');
  665.             }
  666.             break;
  667.         case 'integer':
  668.             if ($field_value != ((int)$field_value)) {
  669.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  670.                     '"'.$field_value.'" is not of type "'.$field_def['type'].'"');
  671.             }
  672.             //$field_value = (int)$field_value;
  673.             if (!empty($field_def['unsigned']&& $field_def['unsigned'&& $field_value < 0{
  674.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  675.                     '"'.$field_value.'" signed instead of unsigned');
  676.             }
  677.             break;
  678.         case 'boolean':
  679.             if (!$this->isBoolean($field_value)) {
  680.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  681.                     '"'.$field_value.'" is not of type "'.$field_def['type'].'"');
  682.             }
  683.             break;
  684.         case 'date':
  685.             if (!preg_match('/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/'$field_value)
  686.                 && $field_value !== 'CURRENT_DATE'
  687.             {
  688.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  689.                     '"'.$field_value.'" is not of type "'.$field_def['type'].'"');
  690.             }
  691.             break;
  692.         case 'timestamp':
  693.             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)
  694.                 && $field_value !== 'CURRENT_TIMESTAMP'
  695.             {
  696.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  697.                     '"'.$field_value.'" is not of type "'.$field_def['type'].'"');
  698.             }
  699.             break;
  700.         case 'time':
  701.             if (!preg_match("/([0-9]{2}):([0-9]{2}):([0-9]{2})/"$field_value)
  702.                 && $field_value !== 'CURRENT_TIME'
  703.             {
  704.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  705.                     '"'.$field_value.'" is not of type "'.$field_def['type'].'"');
  706.             }
  707.             break;
  708.         case 'float':
  709.         case 'double':
  710.             if ($field_value != (double)$field_value{
  711.                 return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
  712.                     '"'.$field_value.'" is not of type "'.$field_def['type'].'"');
  713.             }
  714.             //$field_value = (double)$field_value;
  715.             break;
  716.         }
  717.         return true;
  718.     }
  719. }
  720.  
  721. ?>

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