Add method to retrieve foreign keys for a table to driver classes

This commit is contained in:
Timothy Warren 2014-04-07 16:49:49 -04:00
parent 87754e6f2c
commit 2af3b0be9f
8 changed files with 152 additions and 1 deletions

View File

@ -49,6 +49,12 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
*/
public $util;
/**
* Reference to table_builder class
* @var \Query\Table\Table_Builder
*/
public $table;
/**
* Last query executed
* @var string
@ -81,6 +87,8 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
$class = get_class($this) . "_{$sub}";
$this->$sub = new $class($this);
}
$this->table = new \Query\Table\Table_Builder('', array(), $this);
}
// --------------------------------------------------------------------------
@ -394,6 +402,19 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
// --------------------------------------------------------------------------
/**
* Retrieve foreign keys for the table
*
* @param string $table
* @return array
*/
public function get_fks($table)
{
return $this->driver_query($this->sql->fk_list($table), FALSE);
}
// --------------------------------------------------------------------------
/**
* Retrieve list of data types for the database
*

View File

@ -122,5 +122,14 @@ interface SQL_Interface {
*/
public function column_list($table);
/**
* Get the list of foreign keys for the current
* table
*
* @parma string $table
* @return string
*/
public function fk_list($table);
}
// End of sql_interface.php

View File

@ -269,5 +269,35 @@ SQL;
SQL;
}
// --------------------------------------------------------------------------
/**
* Get the list of foreign keys for the current
* table
*
* @parma string $table
* @return string
*/
public function fk_list($table)
{
return <<<SQL
SELECT DISTINCT
rc.RDB\$CONSTRAINT_NAME AS "constraint_name",
rc.RDB\$RELATION_NAME AS "on table",
d1.RDB\$FIELD_NAME AS "on field",
d2.RDB\$DEPENDED_ON_NAME AS "references table",
d2.RDB\$FIELD_NAME AS "references field",
refc.RDB\$UPDATE_RULE AS "on update",
refc.RDB\$DELETE_RULE AS "on 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
LEFT JOIN RDB\$DEPENDENCIES d2 ON d1.RDB\$DEPENDENT_NAME = d2.RDB\$DEPENDENT_NAME
WHERE rc.RDB\$CONSTRAINT_TYPE = 'FOREIGN KEY'
AND d1.RDB\$DEPENDED_ON_NAME <> d2.RDB\$DEPENDED_ON_NAME
AND d1.RDB\$FIELD_NAME <> d2.RDB\$FIELD_NAME
AND rc.RDB\$RELATION_NAME = '{$table}' -- table name
SQL;
}
}
//End of firebird_sql.php

View File

@ -190,5 +190,24 @@ class MySQL_SQL extends Abstract_SQL {
{
return "SHOW FULL COLUMNS FROM {$table}";
}
// --------------------------------------------------------------------------
/**
* Get the list of foreign keys for the current
* table
*
* @parma string $table
* @return string
*/
public function fk_list($table)
{
return <<<SQL
SELECT `TABLE_NAME`,`COLUMN_NAME`,`CONSTRAINT_NAME`,
`REFERENCED_TABLE_NAME`,`REFERENCED_COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE`
WHERE `REFERENCED_TABLE_NAME` = '{$table}';
SQL;
}
}
//End of mysql_sql.php

View File

@ -217,7 +217,48 @@ SQL;
WHERE "typname" !~ '^pg_|_'
AND "typtype" = 'b'
ORDER BY "typname"
SQL;
}
// --------------------------------------------------------------------------
/**
* Get the list of foreign keys for the current
* table
*
* @parma string $table
* @return string
*/
public function fk_list($table)
{
return <<<SQL
SELECT
"att2"."attname" AS "child_column",
"cl"."relname" AS "parent_table",
"att"."attname" AS "parent_column"
FROM
(SELECT
unnest(con1.conkey) AS "parent",
unnest(con1.confkey) AS "child",
"con1"."confrelid",
"con1"."conrelid"
FROM "pg_class" "cl"
JOIN "pg_namespace" "ns" ON "cl"."relnamespace" = "ns"."oid"
JOIN "pg_constraint" "con1" ON "con1"."conrelid" = "cl"."oid"
WHERE "cl"."relname" = 'child_table'
AND "ns"."nspname" = 'child_schema'
AND "con1"."contype" = 'f'
)
"con"
JOIN "pg_attribute" "att" ON
"att"."attrelid" = "con"."confrelid"
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"
SQL;
}
}
//End of pgsql_manip.php
//End of pgsql_sql.php

View File

@ -174,5 +174,19 @@ SQL;
return 'PRAGMA table_info("'.$table.'")';
}
// --------------------------------------------------------------------------
/**
* Get the list of foreign keys for the current
* table
*
* @parma string $table
* @return string
*/
public function fk_list($table)
{
return NULL;
}
}
//End of sqlite_sql.php

View File

@ -75,5 +75,13 @@ abstract class DBTest extends Query_TestCase {
$this->assertTrue(is_array($types));
}
// --------------------------------------------------------------------------
public function testGetFKs()
{
$keys = $this->db->get_fks('create_test');
$this->assertTrue(is_array($keys));
}
}
// End of db_test.php

View File

@ -261,7 +261,16 @@ SQL;
$sql = $this->db->sql->sequence_list();
$this->assertEqual(NULL, $sql);
$sql = $this->db->sql->fk_list('create_test');
$this->assertEqual(NULL, $sql);
}
// --------------------------------------------------------------------------
public function testGetFKs()
{
$keys = $this->db->get_fks('create_test');
$this->assertNull($keys);
}
}