Add update and delete rules to foreign key methods
This commit is contained in:
parent
bfeaf76f9c
commit
d6c0fd23dc
@ -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
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
@ -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"
|
||||||
@ -252,12 +256,12 @@ SQL;
|
|||||||
"con"
|
"con"
|
||||||
JOIN "pg_attribute" "att" ON
|
JOIN "pg_attribute" "att" ON
|
||||||
"att"."attrelid" = "con"."confrelid"
|
"att"."attrelid" = "con"."confrelid"
|
||||||
AND "att"."attnum" = "con"."child"
|
AND "att"."attnum" = "con"."child"
|
||||||
JOIN "pg_class" "cl" ON
|
JOIN "pg_class" "cl" ON
|
||||||
"cl"."oid" = "con"."confrelid"
|
"cl"."oid" = "con"."confrelid"
|
||||||
JOIN "pg_attribute" "att2" ON
|
JOIN "pg_attribute" "att2" ON
|
||||||
"att2"."attrelid" = "con"."conrelid"
|
"att2"."attrelid" = "con"."conrelid"
|
||||||
AND "att2"."attnum" = "con"."parent"
|
AND "att2"."attnum" = "con"."parent"
|
||||||
SQL;
|
SQL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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');
|
||||||
|
Loading…
Reference in New Issue
Block a user