Skip to content

Commit

Permalink
Added support to Schema::findUniqueIndexes;
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Sep 21, 2015
1 parent 91e1c57 commit a0b1cee
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,39 @@ protected function findTableNames($schema = '')
return $tables;
}

/**
* Returns all unique indexes for the given table.
* Each array element is of the following structure:
*
* ~~~
* [
* 'IndexName1' => ['col1' [, ...]],
* 'IndexName2' => ['col2' [, ...]],
* ]
* ~~~
*
* @param TableSchema $table the table metadata
* @return array all unique indexes for the given table.
* @since 2.0.4
*/
public function findUniqueIndexes($table)
{
$query = '
SELECT id.RDB$INDEX_NAME as index_name, ids.RDB$FIELD_NAME as column_name
FROM RDB$INDICES id
INNER JOIN RDB$INDEX_SEGMENTS ids ON ids.RDB$INDEX_NAME = id.RDB$INDEX_NAME
WHERE id.RDB$UNIQUE_FLAG = 1
AND id.RDB$SYSTEM_FLAG = 0
AND UPPER(id.RDB$RELATION_NAME) = UPPER(\'' . $table->name . '\')
ORDER BY id.RDB$RELATION_NAME, id.RDB$INDEX_NAME, ids.RDB$FIELD_POSITION';
$result = [];
$command = $this->db->createCommand($query);
foreach ($command->queryAll() as $row) {
$result[strtolower(rtrim($row['index_name']))][] = strtolower(rtrim($row['column_name']));
}
return $result;
}

/**
* Sets the isolation level of the current transaction.
* @param string $level The transaction isolation level to use for this transaction.
Expand Down
30 changes: 30 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,34 @@ public function testGetLastInsertID()
$this->assertEquals(2, $schema->getLastInsertID($schema->getTableSchema('animal')->sequenceName));
$this->assertEquals(2, $schema->getLastInsertID($schema->getTableSchema('profile')->sequenceName));
}

public function testFindUniqueIndexes()
{
/* @var $schema Schema */
$schema = $this->getConnection()->schema;

/* Test single primary key */
$table = $schema->getTableSchema('order');
$uniqueIndexes = $schema->findUniqueIndexes($table);

$this->assertTrue(count($uniqueIndexes) == 1);
$this->assertEquals(['id'], reset($uniqueIndexes));

/* Test composer primary key */
$table = $schema->getTableSchema('order_item');
$uniqueIndexes = $schema->findUniqueIndexes($table);

$this->assertTrue(count($uniqueIndexes) == 1);
$this->assertEquals(['order_id', 'item_id'], reset($uniqueIndexes));

/* Test without primary key */
$table = $schema->getTableSchema('unique_values');
$uniqueIndexes = $schema->findUniqueIndexes($table);

$this->assertTrue(count($uniqueIndexes) == 4);
$this->assertEquals(['a'], $uniqueIndexes['uniquea']);
$this->assertEquals(['b'], $uniqueIndexes['uniqueb']);
$this->assertEquals(['b', 'c'], $uniqueIndexes['uniquebc']);
$this->assertEquals(['a', 'b', 'c'], $uniqueIndexes['uniqueabc']);
}
}
21 changes: 21 additions & 0 deletions tests/data/source.sql
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,24 @@ BEGIN
INSERT INTO bit_values (id, val) VALUES (1, 0);
INSERT INTO bit_values (id, val) VALUES (2, 1);
END;
-- SQL
/* unique indexes test */
EXECUTE block AS
BEGIN
IF (EXISTS(SELECT 1 FROM rdb$relations WHERE LOWER(rdb$relation_name) = 'unique_values')) THEN
EXECUTE STATEMENT 'DROP TABLE unique_values;';
END;
-- SQL
CREATE TABLE unique_values (
a INTEGER NOT NULL,
b INTEGER NOT NULL,
c INTEGER NOT NULL
);
-- SQL
CREATE UNIQUE INDEX uniqueA ON unique_values (a);
-- SQL
CREATE UNIQUE INDEX uniqueB ON unique_values (b);
-- SQL
CREATE UNIQUE INDEX uniqueBC ON unique_values (b, c);
-- SQL
CREATE UNIQUE INDEX uniqueABC ON unique_values (a, b, c);

0 comments on commit a0b1cee

Please sign in to comment.