Skip to content

Commit

Permalink
drivers: unified code in getIndexes()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 10, 2022
1 parent 182f2bc commit 6cf3567
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
9 changes: 5 additions & 4 deletions src/Database/Drivers/MsSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ public function getIndexes(string $table): array
t.name, ind.name, ind.index_id, ic.index_column_id";

foreach ($this->connection->query($query) as $row) {
$indexes[$row['name_index']]['name'] = $row['name_index'];
$indexes[$row['name_index']]['unique'] = $row['is_unique'] !== 'False';
$indexes[$row['name_index']]['primary'] = $row['is_primary_key'] !== 'False';
$indexes[$row['name_index']]['columns'][$row['id_column'] - 1] = $row['name_column'];
$id = $row['name_index'];
$indexes[$id]['name'] = $id;
$indexes[$id]['unique'] = $row['is_unique'] !== 'False';
$indexes[$id]['primary'] = $row['is_primary_key'] !== 'False';
$indexes[$id]['columns'][$row['id_column'] - 1] = $row['name_column'];
}

return array_values($indexes);
Expand Down
10 changes: 5 additions & 5 deletions src/Database/Drivers/MySqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ public function getIndexes(string $table): array
{
$indexes = [];
foreach ($this->connection->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
$row = array_change_key_case((array) $row, CASE_LOWER);
$indexes[$row['key_name']]['name'] = $row['key_name'];
$indexes[$row['key_name']]['unique'] = !$row['non_unique'];
$indexes[$row['key_name']]['primary'] = $row['key_name'] === 'PRIMARY';
$indexes[$row['key_name']]['columns'][$row['seq_in_index'] - 1] = $row['column_name'];
$id = $row['Key_name'];
$indexes[$id]['name'] = $id;
$indexes[$id]['unique'] = !$row['Non_unique'];
$indexes[$id]['primary'] = $row['Key_name'] === 'PRIMARY';
$indexes[$id]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
}

return array_values($indexes);
Expand Down
9 changes: 5 additions & 4 deletions src/Database/Drivers/PgSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,11 @@ public function getIndexes(string $table): array
c1.relkind IN ('r', 'p')
AND c1.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
") as $row) {
$indexes[$row['name']]['name'] = $row['name'];
$indexes[$row['name']]['unique'] = $row['unique'];
$indexes[$row['name']]['primary'] = $row['primary'];
$indexes[$row['name']]['columns'][] = $row['column'];
$id = $row['name'];
$indexes[$id]['name'] = $id;
$indexes[$id]['unique'] = $row['unique'];
$indexes[$id]['primary'] = $row['primary'];
$indexes[$id]['columns'][] = $row['column'];
}

return array_values($indexes);
Expand Down
9 changes: 5 additions & 4 deletions src/Database/Drivers/SqliteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,16 @@ public function getIndexes(string $table): array
{
$indexes = [];
foreach ($this->connection->query("PRAGMA index_list({$this->delimite($table)})") as $row) {
$indexes[$row['name']]['name'] = $row['name'];
$indexes[$row['name']]['unique'] = (bool) $row['unique'];
$indexes[$row['name']]['primary'] = false;
$id = $row['name'];
$indexes[$id]['name'] = $id;
$indexes[$id]['unique'] = (bool) $row['unique'];
$indexes[$id]['primary'] = false;
}

foreach ($indexes as $index => $values) {
$res = $this->connection->query("PRAGMA index_info({$this->delimite($index)})");
while ($row = $res->fetch()) {
$indexes[$index]['columns'][$row['seqno']] = $row['name'];
$indexes[$index]['columns'][] = $row['name'];
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/Database/Drivers/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,11 @@ public function getIndexes(string $table): array
i.index_id,
ic.index_column_id
") as $row) {
$indexes[$row->name]['name'] = $row->name;
$indexes[$row->name]['unique'] = (bool) $row->unique;
$indexes[$row->name]['primary'] = (bool) $row->primary;
$indexes[$row->name]['columns'][] = $row->column;
$id = $row['name'];
$indexes[$id]['name'] = $id;
$indexes[$id]['unique'] = (bool) $row['unique'];
$indexes[$id]['primary'] = (bool) $row['primary'];
$indexes[$id]['columns'][] = $row['column'];
}

return array_values($indexes);
Expand Down

0 comments on commit 6cf3567

Please sign in to comment.