From 2af3b0be9f713c382272e86359a9b97c28d5e93f Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 7 Apr 2014 16:49:49 -0400 Subject: [PATCH] Add method to retrieve foreign keys for a table to driver classes --- core/abstract/abstract_driver.php | 21 +++++++++++++ core/interfaces/sql_interface.php | 9 ++++++ drivers/firebird/firebird_sql.php | 30 +++++++++++++++++++ drivers/mysql/mysql_sql.php | 19 ++++++++++++ drivers/pgsql/pgsql_sql.php | 43 ++++++++++++++++++++++++++- drivers/sqlite/sqlite_sql.php | 14 +++++++++ tests/core/db_test.php | 8 +++++ tests/databases/sqlite/SqliteTest.php | 9 ++++++ 8 files changed, 152 insertions(+), 1 deletion(-) diff --git a/core/abstract/abstract_driver.php b/core/abstract/abstract_driver.php index 881cb6d..ca34e35 100644 --- a/core/abstract/abstract_driver.php +++ b/core/abstract/abstract_driver.php @@ -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 * diff --git a/core/interfaces/sql_interface.php b/core/interfaces/sql_interface.php index 54b3c64..fea8f3f 100644 --- a/core/interfaces/sql_interface.php +++ b/core/interfaces/sql_interface.php @@ -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 \ No newline at end of file diff --git a/drivers/firebird/firebird_sql.php b/drivers/firebird/firebird_sql.php index d477010..f9b68fd 100644 --- a/drivers/firebird/firebird_sql.php +++ b/drivers/firebird/firebird_sql.php @@ -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 << 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 \ No newline at end of file diff --git a/drivers/mysql/mysql_sql.php b/drivers/mysql/mysql_sql.php index dbdff41..08fd8e4 100644 --- a/drivers/mysql/mysql_sql.php +++ b/drivers/mysql/mysql_sql.php @@ -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 <<assertTrue(is_array($types)); } + // -------------------------------------------------------------------------- + + public function testGetFKs() + { + $keys = $this->db->get_fks('create_test'); + $this->assertTrue(is_array($keys)); + } + } // End of db_test.php \ No newline at end of file diff --git a/tests/databases/sqlite/SqliteTest.php b/tests/databases/sqlite/SqliteTest.php index 21a0902..6f38a00 100644 --- a/tests/databases/sqlite/SqliteTest.php +++ b/tests/databases/sqlite/SqliteTest.php @@ -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); + } } \ No newline at end of file