Skip to content

Commit a0b1cee

Browse files
committed
Added support to Schema::findUniqueIndexes;
1 parent 91e1c57 commit a0b1cee

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

src/Schema.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,39 @@ protected function findTableNames($schema = '')
425425
return $tables;
426426
}
427427

428+
/**
429+
* Returns all unique indexes for the given table.
430+
* Each array element is of the following structure:
431+
*
432+
* ~~~
433+
* [
434+
* 'IndexName1' => ['col1' [, ...]],
435+
* 'IndexName2' => ['col2' [, ...]],
436+
* ]
437+
* ~~~
438+
*
439+
* @param TableSchema $table the table metadata
440+
* @return array all unique indexes for the given table.
441+
* @since 2.0.4
442+
*/
443+
public function findUniqueIndexes($table)
444+
{
445+
$query = '
446+
SELECT id.RDB$INDEX_NAME as index_name, ids.RDB$FIELD_NAME as column_name
447+
FROM RDB$INDICES id
448+
INNER JOIN RDB$INDEX_SEGMENTS ids ON ids.RDB$INDEX_NAME = id.RDB$INDEX_NAME
449+
WHERE id.RDB$UNIQUE_FLAG = 1
450+
AND id.RDB$SYSTEM_FLAG = 0
451+
AND UPPER(id.RDB$RELATION_NAME) = UPPER(\'' . $table->name . '\')
452+
ORDER BY id.RDB$RELATION_NAME, id.RDB$INDEX_NAME, ids.RDB$FIELD_POSITION';
453+
$result = [];
454+
$command = $this->db->createCommand($query);
455+
foreach ($command->queryAll() as $row) {
456+
$result[strtolower(rtrim($row['index_name']))][] = strtolower(rtrim($row['column_name']));
457+
}
458+
return $result;
459+
}
460+
428461
/**
429462
* Sets the isolation level of the current transaction.
430463
* @param string $level The transaction isolation level to use for this transaction.

tests/SchemaTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,34 @@ public function testGetLastInsertID()
128128
$this->assertEquals(2, $schema->getLastInsertID($schema->getTableSchema('animal')->sequenceName));
129129
$this->assertEquals(2, $schema->getLastInsertID($schema->getTableSchema('profile')->sequenceName));
130130
}
131+
132+
public function testFindUniqueIndexes()
133+
{
134+
/* @var $schema Schema */
135+
$schema = $this->getConnection()->schema;
136+
137+
/* Test single primary key */
138+
$table = $schema->getTableSchema('order');
139+
$uniqueIndexes = $schema->findUniqueIndexes($table);
140+
141+
$this->assertTrue(count($uniqueIndexes) == 1);
142+
$this->assertEquals(['id'], reset($uniqueIndexes));
143+
144+
/* Test composer primary key */
145+
$table = $schema->getTableSchema('order_item');
146+
$uniqueIndexes = $schema->findUniqueIndexes($table);
147+
148+
$this->assertTrue(count($uniqueIndexes) == 1);
149+
$this->assertEquals(['order_id', 'item_id'], reset($uniqueIndexes));
150+
151+
/* Test without primary key */
152+
$table = $schema->getTableSchema('unique_values');
153+
$uniqueIndexes = $schema->findUniqueIndexes($table);
154+
155+
$this->assertTrue(count($uniqueIndexes) == 4);
156+
$this->assertEquals(['a'], $uniqueIndexes['uniquea']);
157+
$this->assertEquals(['b'], $uniqueIndexes['uniqueb']);
158+
$this->assertEquals(['b', 'c'], $uniqueIndexes['uniquebc']);
159+
$this->assertEquals(['a', 'b', 'c'], $uniqueIndexes['uniqueabc']);
160+
}
131161
}

tests/data/source.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,24 @@ BEGIN
461461
INSERT INTO bit_values (id, val) VALUES (1, 0);
462462
INSERT INTO bit_values (id, val) VALUES (2, 1);
463463
END;
464+
-- SQL
465+
/* unique indexes test */
466+
EXECUTE block AS
467+
BEGIN
468+
IF (EXISTS(SELECT 1 FROM rdb$relations WHERE LOWER(rdb$relation_name) = 'unique_values')) THEN
469+
EXECUTE STATEMENT 'DROP TABLE unique_values;';
470+
END;
471+
-- SQL
472+
CREATE TABLE unique_values (
473+
a INTEGER NOT NULL,
474+
b INTEGER NOT NULL,
475+
c INTEGER NOT NULL
476+
);
477+
-- SQL
478+
CREATE UNIQUE INDEX uniqueA ON unique_values (a);
479+
-- SQL
480+
CREATE UNIQUE INDEX uniqueB ON unique_values (b);
481+
-- SQL
482+
CREATE UNIQUE INDEX uniqueBC ON unique_values (b, c);
483+
-- SQL
484+
CREATE UNIQUE INDEX uniqueABC ON unique_values (a, b, c);

0 commit comments

Comments
 (0)