From 12ea7ba32d4730d213ef277eabeb8dda8b370eb8 Mon Sep 17 00:00:00 2001 From: MGatner Date: Mon, 9 Sep 2019 13:59:12 -0400 Subject: [PATCH 1/3] Add additional FK info --- system/Database/MySQLi/Connection.php | 16 +++++++++++----- system/Database/Postgre/Connection.php | 12 +++++++----- system/Database/SQLite3/Connection.php | 1 + 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/system/Database/MySQLi/Connection.php b/system/Database/MySQLi/Connection.php index 4a455ff267f3..d915d330e038 100644 --- a/system/Database/MySQLi/Connection.php +++ b/system/Database/MySQLi/Connection.php @@ -537,10 +537,14 @@ public function _foreignKeyData(string $table): array SELECT tc.CONSTRAINT_NAME, tc.TABLE_NAME, - rc.REFERENCED_TABLE_NAME + kcu.COLUMN_NAME, + rc.REFERENCED_TABLE_NAME, + kcu.REFERENCED_COLUMN_NAME FROM information_schema.TABLE_CONSTRAINTS AS tc INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS rc ON tc.CONSTRAINT_NAME = rc.CONSTRAINT_NAME + INNER JOIN information_schema.KEY_COLUMN_USAGE AS kcu + ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME WHERE tc.CONSTRAINT_TYPE = ' . $this->escape('FOREIGN KEY') . ' AND tc.TABLE_SCHEMA = ' . $this->escape($this->database) . ' AND @@ -555,10 +559,12 @@ public function _foreignKeyData(string $table): array $retVal = []; foreach ($query as $row) { - $obj = new \stdClass(); - $obj->constraint_name = $row->CONSTRAINT_NAME; - $obj->table_name = $row->TABLE_NAME; - $obj->foreign_table_name = $row->REFERENCED_TABLE_NAME; + $obj = new \stdClass(); + $obj->constraint_name = $row->CONSTRAINT_NAME; + $obj->table_name = $row->TABLE_NAME; + $obj->column_name = $row->COLUMN_NAME; + $obj->foreign_table_name = $row->REFERENCED_TABLE_NAME; + $obj->foreign_column_name = $row->REFERENCED_COLUMN_NAME; $retVal[] = $obj; } diff --git a/system/Database/Postgre/Connection.php b/system/Database/Postgre/Connection.php index 8becda2aef67..be9556a3f5ea 100644 --- a/system/Database/Postgre/Connection.php +++ b/system/Database/Postgre/Connection.php @@ -406,11 +406,13 @@ public function _foreignKeyData(string $table): array $retVal = []; foreach ($query as $row) { - $obj = new \stdClass(); - $obj->constraint_name = $row->constraint_name; - $obj->table_name = $row->table_name; - $obj->foreign_table_name = $row->foreign_table_name; - $retVal[] = $obj; + $obj = new \stdClass(); + $obj->constraint_name = $row->constraint_name; + $obj->table_name = $row->table_name; + $obj->column_name = $row->column_name; + $obj->foreign_table_name = $row->foreign_table_name; + $obj->foreign_column_name = $row->foreign_column_name; + $retVal[] = $obj; } return $retVal; diff --git a/system/Database/SQLite3/Connection.php b/system/Database/SQLite3/Connection.php index 576ebef9cbef..f7ad7da0ce58 100644 --- a/system/Database/SQLite3/Connection.php +++ b/system/Database/SQLite3/Connection.php @@ -407,6 +407,7 @@ public function _foreignKeyData(string $table): array $obj->constraint_name = $row->from . ' to ' . $row->table . '.' . $row->to; $obj->table_name = $table; $obj->foreign_table_name = $row->table; + $obj->sequence = $row->seq; $retVal[] = $obj; } From aeb0dd8edab772d3f6cec4f642ec8e12ed4bdd6e Mon Sep 17 00:00:00 2001 From: MGatner Date: Mon, 9 Sep 2019 14:01:31 -0400 Subject: [PATCH 2/3] Add test for additional FK data --- tests/system/Database/Live/ForgeTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/system/Database/Live/ForgeTest.php b/tests/system/Database/Live/ForgeTest.php index 2481363d95f0..bf21e3b07b12 100644 --- a/tests/system/Database/Live/ForgeTest.php +++ b/tests/system/Database/Live/ForgeTest.php @@ -323,10 +323,13 @@ public function testForeignKey() if ($this->db->DBDriver === 'SQLite3') { $this->assertEquals($foreignKeyData[0]->constraint_name, 'users_id to db_forge_test_users.id'); + $this->assertEquals($foreignKeyData[0]->sequence, 0); } else { $this->assertEquals($foreignKeyData[0]->constraint_name, $this->db->DBPrefix . 'forge_test_invoices_users_id_foreign'); + $this->assertEquals($foreignKeyData[0]->column_name, 'users_id'); + $this->assertEquals($foreignKeyData[0]->foreign_column_name, 'id'); } $this->assertEquals($foreignKeyData[0]->table_name, $this->db->DBPrefix . 'forge_test_invoices'); $this->assertEquals($foreignKeyData[0]->foreign_table_name, $this->db->DBPrefix . 'forge_test_users'); From 42b42ae24cbe1cf9e87511182d03ea98e0520d36 Mon Sep 17 00:00:00 2001 From: MGatner Date: Mon, 9 Sep 2019 14:05:36 -0400 Subject: [PATCH 3/3] Update User Guide --- user_guide_src/source/database/metadata.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/user_guide_src/source/database/metadata.rst b/user_guide_src/source/database/metadata.rst index d7a08b5ce6b9..7ec2eb6ffae0 100644 --- a/user_guide_src/source/database/metadata.rst +++ b/user_guide_src/source/database/metadata.rst @@ -163,6 +163,11 @@ Usage example:: { echo $key->constraint_name; echo $key->table_name; + echo $key->column_name; echo $key->foreign_table_name; + echo $key->foreign_column_name; } +The object fields may be unique to the database you are using. For instance, SQLite3 does +not return data on column names, but has the additional *sequence* field for compound +foreign key definitions.