Skip to content

Commit

Permalink
drivers: updated getForeignKeys() [Closes #281]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 20, 2022
1 parent 287a273 commit 2a171e5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
9 changes: 4 additions & 5 deletions src/Database/Drivers/MySqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,10 @@ public function getForeignKeys(string $table): array
. 'WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ' . $this->connection->quote($table);

foreach ($this->connection->query($query) as $id => $row) {
$row = array_change_key_case((array) $row, CASE_LOWER);
$keys[$id]['name'] = $row['constraint_name']; // foreign key name
$keys[$id]['local'] = $row['column_name']; // local columns
$keys[$id]['table'] = $row['referenced_table_name']; // referenced table
$keys[$id]['foreign'] = $row['referenced_column_name']; // referenced columns
$keys[$id]['name'] = $row['CONSTRAINT_NAME'];
$keys[$id]['local'] = $row['COLUMN_NAME'];
$keys[$id]['table'] = $row['REFERENCED_TABLE_NAME'];
$keys[$id]['foreign'] = $row['REFERENCED_COLUMN_NAME'];
}

return array_values($keys);
Expand Down
13 changes: 5 additions & 8 deletions src/Database/Drivers/SqliteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,11 @@ public function getForeignKeys(string $table): array
{
$keys = [];
foreach ($this->connection->query("PRAGMA foreign_key_list({$this->delimite($table)})") as $row) {
$keys[$row['id']]['name'] = $row['id']; // foreign key name
$keys[$row['id']]['local'] = $row['from']; // local columns
$keys[$row['id']]['table'] = $row['table']; // referenced table
$keys[$row['id']]['foreign'] = $row['to']; // referenced columns

if ($keys[$row['id']]['foreign'][0] == null) {
$keys[$row['id']]['foreign'] = null;
}
$id = $row['id'];
$keys[$id]['name'] = $id;
$keys[$id]['local'] = $row['from'];
$keys[$id]['table'] = $row['table'];
$keys[$id]['foreign'] = $row['to'];
}

return array_values($keys);
Expand Down

0 comments on commit 2a171e5

Please sign in to comment.