Add update and delete rules to foreign key methods

This commit is contained in:
Timothy Warren 2014-04-17 16:41:12 -04:00
parent bfeaf76f9c
commit d6c0fd23dc
6 changed files with 54 additions and 13 deletions

View File

@ -285,7 +285,9 @@ SQL;
SELECT DISTINCT SELECT DISTINCT
TRIM(d1.RDB\$FIELD_NAME) AS "child_column", TRIM(d1.RDB\$FIELD_NAME) AS "child_column",
TRIM(d2.RDB\$DEPENDED_ON_NAME) AS "parent_table", TRIM(d2.RDB\$DEPENDED_ON_NAME) AS "parent_table",
TRIM(d2.RDB\$FIELD_NAME) AS "parent_column" TRIM(d2.RDB\$FIELD_NAME) AS "parent_column",
TRIM(refc.RDB\$UPDATE_RULE) AS "update",
TRIM(refc.RDB\$DELETE_RULE) AS "delete"
FROM RDB\$RELATION_CONSTRAINTS AS rc FROM RDB\$RELATION_CONSTRAINTS AS rc
LEFT JOIN RDB\$REF_CONSTRAINTS refc ON rc.RDB\$CONSTRAINT_NAME = refc.RDB\$CONSTRAINT_NAME LEFT JOIN RDB\$REF_CONSTRAINTS refc ON rc.RDB\$CONSTRAINT_NAME = refc.RDB\$CONSTRAINT_NAME
LEFT JOIN RDB\$DEPENDENCIES d1 ON d1.RDB\$DEPENDED_ON_NAME = rc.RDB\$RELATION_NAME LEFT JOIN RDB\$DEPENDENCIES d1 ON d1.RDB\$DEPENDED_ON_NAME = rc.RDB\$RELATION_NAME

View File

@ -205,14 +205,16 @@ class MySQL_SQL extends Abstract_SQL {
return <<<SQL return <<<SQL
SELECT DISTINCT `kcu`.`COLUMN_NAME` as `child_column`, SELECT DISTINCT `kcu`.`COLUMN_NAME` as `child_column`,
`kcu`.`REFERENCED_TABLE_NAME` as `parent_table`, `kcu`.`REFERENCED_TABLE_NAME` as `parent_table`,
`kcu`.`REFERENCED_COLUMN_NAME` as `parent_column` `kcu`.`REFERENCED_COLUMN_NAME` as `parent_column`,
`rc`.`UPDATE_RULE` AS `update`,
`rc`.`DELETE_RULE` AS `delete`
FROM `INFORMATION_SCHEMA`.`TABLE_CONSTRAINTS` `tc` FROM `INFORMATION_SCHEMA`.`TABLE_CONSTRAINTS` `tc`
INNER JOIN `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE` `kcu` INNER JOIN `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE` `kcu`
ON `kcu`.`CONSTRAINT_NAME`=`tc`.`CONSTRAINT_NAME` ON `kcu`.`CONSTRAINT_NAME`=`tc`.`CONSTRAINT_NAME`
INNER JOIN `INFORMATION_SCHEMA`.`REFERENTIAL_CONSTRAINTS` `rc`
ON `rc`.`CONSTRAINT_NAME`=`tc`.`CONSTRAINT_NAME`
WHERE `tc`.`CONSTRAINT_TYPE`='FOREIGN KEY' WHERE `tc`.`CONSTRAINT_TYPE`='FOREIGN KEY'
AND `tc`.`TABLE_NAME`='{$table}' AND `tc`.`TABLE_NAME`='{$table}'
-- AND `parent_table` IS NOT NULL
-- AND `parent_column` IS NOT NULL
SQL; SQL;
} }

View File

@ -68,5 +68,35 @@ SQL;
return $this->driver_query($sql); return $this->driver_query($sql);
} }
// --------------------------------------------------------------------------
/**
* Retrieve foreign keys for the table
*
* @param string $table
* @return array
*/
public function get_fks($table)
{
$value_map = array(
'c' => 'CASCADE',
'r' => 'RESTRICT',
);
$keys = parent::get_fks($table);
foreach($keys as &$key)
{
foreach(array('update', 'delete') AS $type)
{
if ( ! isset($value_map[$key[$type]])) continue;
$key[$type] = $value_map[$key[$type]];
}
}
return $keys;
}
} }
//End of pgsql_driver.php //End of pgsql_driver.php

View File

@ -235,13 +235,17 @@ SQL;
SELECT SELECT
"att2"."attname" AS "child_column", "att2"."attname" AS "child_column",
"cl"."relname" AS "parent_table", "cl"."relname" AS "parent_table",
"att"."attname" AS "parent_column" "att"."attname" AS "parent_column",
"con"."update" AS "update",
"con"."update" AS "delete"
FROM FROM
(SELECT (SELECT
unnest(con1.conkey) AS "parent", unnest(con1.conkey) AS "parent",
unnest(con1.confkey) AS "child", unnest(con1.confkey) AS "child",
"con1"."confrelid", "con1"."confrelid",
"con1"."conrelid" "con1"."conrelid",
"con1"."confupdtype" as "update",
"con1"."confdeltype" as "delete"
FROM "pg_class" "cl" FROM "pg_class" "cl"
JOIN "pg_namespace" "ns" ON "cl"."relnamespace" = "ns"."oid" JOIN "pg_namespace" "ns" ON "cl"."relnamespace" = "ns"."oid"
JOIN "pg_constraint" "con1" ON "con1"."conrelid" = "cl"."oid" JOIN "pg_constraint" "con1" ON "con1"."conrelid" = "cl"."oid"

View File

@ -88,14 +88,15 @@ class SQLite extends Abstract_Driver {
public function get_fks($table) public function get_fks($table)
{ {
$return_rows = array(); $return_rows = array();
$rows = parent::get_fks($table);
foreach($rows as $row) foreach(parent::get_fks($table) as $row)
{ {
$return_rows[] = array( $return_rows[] = array(
'child_column' => $row['from'], 'child_column' => $row['from'],
'parent_table' => $row['table'], 'parent_table' => $row['table'],
'parent_column' => $row['to'] 'parent_column' => $row['to'],
'update' => $row['on_update'],
'delete' => $row['on_delete']
); );
} }

View File

@ -77,7 +77,9 @@ abstract class DBTest extends Query_TestCase {
$expected = array(array( $expected = array(array(
'child_column' => 'ext_id', 'child_column' => 'ext_id',
'parent_table' => 'testconstraints', 'parent_table' => 'testconstraints',
'parent_column' => 'someid' 'parent_column' => 'someid',
'update' => 'CASCADE',
'delete' => 'CASCADE'
)); ));
$keys = $this->db->get_fks('testconstraints2'); $keys = $this->db->get_fks('testconstraints2');