From d6c0fd23dcee18c71c93f49f48a78388ae21147a Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 17 Apr 2014 16:41:12 -0400 Subject: [PATCH] Add update and delete rules to foreign key methods --- drivers/firebird/firebird_sql.php | 4 +++- drivers/mysql/mysql_sql.php | 8 +++++--- drivers/pgsql/pgsql_driver.php | 30 ++++++++++++++++++++++++++++++ drivers/pgsql/pgsql_sql.php | 14 +++++++++----- drivers/sqlite/sqlite_driver.php | 7 ++++--- tests/core/db_test.php | 4 +++- 6 files changed, 54 insertions(+), 13 deletions(-) diff --git a/drivers/firebird/firebird_sql.php b/drivers/firebird/firebird_sql.php index 05c8c66..a169b5d 100644 --- a/drivers/firebird/firebird_sql.php +++ b/drivers/firebird/firebird_sql.php @@ -285,7 +285,9 @@ SQL; SELECT DISTINCT TRIM(d1.RDB\$FIELD_NAME) AS "child_column", 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 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 diff --git a/drivers/mysql/mysql_sql.php b/drivers/mysql/mysql_sql.php index cb230ae..3a82b2d 100644 --- a/drivers/mysql/mysql_sql.php +++ b/drivers/mysql/mysql_sql.php @@ -205,14 +205,16 @@ class MySQL_SQL extends Abstract_SQL { return <<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 \ No newline at end of file diff --git a/drivers/pgsql/pgsql_sql.php b/drivers/pgsql/pgsql_sql.php index d1b044f..38eaa64 100644 --- a/drivers/pgsql/pgsql_sql.php +++ b/drivers/pgsql/pgsql_sql.php @@ -235,13 +235,17 @@ SQL; SELECT "att2"."attname" AS "child_column", "cl"."relname" AS "parent_table", - "att"."attname" AS "parent_column" + "att"."attname" AS "parent_column", + "con"."update" AS "update", + "con"."update" AS "delete" FROM (SELECT unnest(con1.conkey) AS "parent", unnest(con1.confkey) AS "child", "con1"."confrelid", - "con1"."conrelid" + "con1"."conrelid", + "con1"."confupdtype" as "update", + "con1"."confdeltype" as "delete" FROM "pg_class" "cl" JOIN "pg_namespace" "ns" ON "cl"."relnamespace" = "ns"."oid" JOIN "pg_constraint" "con1" ON "con1"."conrelid" = "cl"."oid" @@ -252,12 +256,12 @@ SQL; "con" JOIN "pg_attribute" "att" ON "att"."attrelid" = "con"."confrelid" - AND "att"."attnum" = "con"."child" + AND "att"."attnum" = "con"."child" JOIN "pg_class" "cl" ON "cl"."oid" = "con"."confrelid" JOIN "pg_attribute" "att2" ON - "att2"."attrelid" = "con"."conrelid" - AND "att2"."attnum" = "con"."parent" + "att2"."attrelid" = "con"."conrelid" + AND "att2"."attnum" = "con"."parent" SQL; } diff --git a/drivers/sqlite/sqlite_driver.php b/drivers/sqlite/sqlite_driver.php index 0043add..5368bff 100644 --- a/drivers/sqlite/sqlite_driver.php +++ b/drivers/sqlite/sqlite_driver.php @@ -88,14 +88,15 @@ class SQLite extends Abstract_Driver { public function get_fks($table) { $return_rows = array(); - $rows = parent::get_fks($table); - foreach($rows as $row) + foreach(parent::get_fks($table) as $row) { $return_rows[] = array( 'child_column' => $row['from'], 'parent_table' => $row['table'], - 'parent_column' => $row['to'] + 'parent_column' => $row['to'], + 'update' => $row['on_update'], + 'delete' => $row['on_delete'] ); } diff --git a/tests/core/db_test.php b/tests/core/db_test.php index dd8e6d7..f858652 100644 --- a/tests/core/db_test.php +++ b/tests/core/db_test.php @@ -77,7 +77,9 @@ abstract class DBTest extends Query_TestCase { $expected = array(array( 'child_column' => 'ext_id', 'parent_table' => 'testconstraints', - 'parent_column' => 'someid' + 'parent_column' => 'someid', + 'update' => 'CASCADE', + 'delete' => 'CASCADE' )); $keys = $this->db->get_fks('testconstraints2');