cvs: pear /MDB2_Schema/MDB2/Schema Parser.php Validate.php
| From: | Igor Feghali | Date: | Mon, 20 Aug 2007 03:19:21 +0000 |
| Subject: | cvs: pear /MDB2_Schema/MDB2/Schema Parser.php Validate.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-48471@lists.php.net to get a copy of this message | ||
ifeghali Mon Aug 20 03:19:21 2007 UTC
Modified files:
/pear/MDB2_Schema/MDB2/Schema Parser.php Validate.php
Log:
- validateConstraintReferencedField() API changed, method implemented
- improved foreign key validation
http://cvs.php.net/viewvc.cgi/pear/MDB2_Schema/MDB2/Schema/Parser.php?r1=1.63&r2=1.64&diff_format=u
Index: pear/MDB2_Schema/MDB2/Schema/Parser.php
diff -u pear/MDB2_Schema/MDB2/Schema/Parser.php:1.63 pear/MDB2_Schema/MDB2/Schema/Parser.php:1.64
--- pear/MDB2_Schema/MDB2/Schema/Parser.php:1.63 Fri Aug 17 01:02:28 2007
+++ pear/MDB2_Schema/MDB2/Schema/Parser.php Mon Aug 20 03:19:20 2007
@@ -42,7 +42,7 @@
// | Author: Christian Dickmann <dickmann@php.net> |
// +----------------------------------------------------------------------+
//
-// $Id: Parser.php,v 1.63 2007/08/17 01:02:28 ifeghali Exp $
+// $Id: Parser.php,v 1.64 2007/08/20 03:19:20 ifeghali Exp $
//
require_once 'XML/Parser.php';
@@ -427,7 +427,7 @@
}
break;
case 'database-table-declaration-foreign-references-field':
- $result =
$this->val->validateConstraintReferencedField($this->constraint['references']['fields'],
$this->field_name,
$this->database_definition['tables'],$this->constraint['references']['table']);
+ $result =
$this->val->validateConstraintReferencedField($this->constraint['references']['fields'],
$this->field_name);
if (PEAR::isError($result)) {
$this->raiseError($result->getUserinfo(), 0, $xp, $result->getCode());
} else {
http://cvs.php.net/viewvc.cgi/pear/MDB2_Schema/MDB2/Schema/Validate.php?r1=1.36&r2=1.37&diff_format=u
Index: pear/MDB2_Schema/MDB2/Schema/Validate.php
diff -u pear/MDB2_Schema/MDB2/Schema/Validate.php:1.36
pear/MDB2_Schema/MDB2/Schema/Validate.php:1.37
--- pear/MDB2_Schema/MDB2/Schema/Validate.php:1.36 Sun Aug 19 02:41:32 2007
+++ pear/MDB2_Schema/MDB2/Schema/Validate.php Mon Aug 20 03:19:20 2007
@@ -43,7 +43,7 @@
// | Author: Igor Feghali <ifeghali@php.net> |
// +----------------------------------------------------------------------+
//
-// $Id: Validate.php,v 1.36 2007/08/19 02:41:32 ifeghali Exp $
+// $Id: Validate.php,v 1.37 2007/08/20 03:19:20 ifeghali Exp $
//
/**
@@ -505,7 +505,19 @@
/* Have we got fields? */
if (empty($constraint['fields']) || !is_array($constraint['fields'])) {
return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
- 'foreign keys need one or more fields');
+ 'foreign key "'.$constraint_name.'" need one or more
fields');
+ }
+
+ /* Have we got referenced fields? */
+ if (empty($constraint['references']) ||
!is_array($constraint['references'])) {
+ return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
+ 'foreign key "'.$constraint_name.'" need to reference one
or more fields');
+ }
+
+ /* Have we got referenced table? */
+ if (empty($constraint['references']['table'])) {
+ return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
+ 'foreign key "'.$constraint_name.'" need to reference a
table');
}
if (empty($constraint['was'])) {
@@ -518,7 +530,7 @@
// {{{ validateConstraintField()
/**
- * Checks whether a parsed foreign-field is valid.
+ * Checks whether a foreign-field is valid.
*
* @param array multi dimensional array that contains the
* fields of current foreign key.
@@ -534,7 +546,7 @@
return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
'empty value for foreign-field');
}
- if (is_array($constraint_fields) && in_array($field_name, $constraint_fields)) {
+ if (is_array($constraint_fields) && isset($constraint_fields[$field_name])) {
return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
'foreign field "'.$field_name.'" already exists');
}
@@ -545,7 +557,7 @@
// {{{ validateConstraintReferencedField()
/**
- * Checks whether a referenced field by of a foreign key exists.
+ * Checks whether a foreign-referenced field is valid.
*
* @param array multi dimensional array that contains the
* fields of current foreign key.
@@ -555,9 +567,16 @@
*
* @access public
*/
- function validateConstraintReferencedField($referenced_fields, $field_name, $tables,
$table_name)
+ function validateConstraintReferencedField($referenced_fields, $field_name)
{
- // to be implemented yet
+ if (!$field_name) {
+ return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
+ 'empty value for referenced foreign-field');
+ }
+ if (is_array($referenced_fields) && isset($referenced_fields[$field_name])) {
+ return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
+ 'foreign field "'.$field_name.'" already
referenced');
+ }
return MDB2_OK;
}
@@ -710,10 +729,18 @@
if (!$primary) {
return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
'referenced table
"'.$referenced_table_name.'" has no primary key and no referenced field was
specified for foreign key "'.$constraint_name.'" of table
"'.$table_name.'"');
- } else {
-
$database['tables'][$table_name]['constraints'][$constraint_name]['references']['fields']
= $primary;
}
+
+ $constraint['references']['fields'] = $primary;
}
+
+ /* the same number of referencing and referenced fields ? */
+ if (count($constraint['fields']) !=
count($constraint['references']['fields'])) {
+ return $this->raiseError(MDB2_SCHEMA_ERROR_VALIDATE,
+ 'The number of fields in the referenced key must match those
of the foreign key "'.$constraint_name.'"');
+ }
+
+
$database['tables'][$table_name]['constraints'][$constraint_name]['references']['fields']
= $constraint['references']['fields'];
}
}
}