Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit 33e6114

Browse files
committed
Support "CONSTRAINT name" clause
1 parent 394bce2 commit 33e6114

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

tests/WP_SQLite_Driver_Tests.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4704,6 +4704,41 @@ public function testAlterTableDuplicateKeyNameWithUnique(): void {
47044704
$this->assertSame( '42S21', $exception->getCode() );
47054705
}
47064706

4707+
public function testConstraintName(): void {
4708+
$this->assertQuery(
4709+
'CREATE TABLE t ( id INT, CONSTRAINT cst_id UNIQUE (id) )'
4710+
);
4711+
4712+
$result = $this->assertQuery( 'SHOW INDEX FROM t' );
4713+
$this->assertCount( 1, $result );
4714+
$this->assertSame( 'cst_id', $result[0]->Key_name );
4715+
}
4716+
4717+
public function testIndexNamePrecedesConstraintName(): void {
4718+
$this->assertQuery(
4719+
'CREATE TABLE t ( id INT, CONSTRAINT cst_id UNIQUE idx_id (id) )'
4720+
);
4721+
4722+
$result = $this->assertQuery( 'SHOW INDEX FROM t' );
4723+
$this->assertCount( 1, $result );
4724+
$this->assertSame( 'idx_id', $result[0]->Key_name );
4725+
4726+
$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
4727+
$this->assertCount( 1, $result );
4728+
$this->assertSame(
4729+
implode(
4730+
"\n",
4731+
array(
4732+
'CREATE TABLE `t` (',
4733+
' `id` int DEFAULT NULL,',
4734+
' UNIQUE KEY `idx_id` (`id`)',
4735+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
4736+
)
4737+
),
4738+
$result[0]->{'Create Table'}
4739+
);
4740+
}
4741+
47074742
public function testNoBackslashEscapesSqlMode(): void {
47084743
$backslash = chr( 92 );
47094744

wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,19 @@ private function get_index_name( WP_Parser_Node $node ): string {
16571657
return 'PRIMARY';
16581658
}
16591659

1660+
/*
1661+
* Get index name.
1662+
*
1663+
* When both index and constraint name are defined, the index name will
1664+
* be used. E.g., in "CONSTRAINT c UNIQUE u (id)", the name will be "u".
1665+
*/
16601666
$name_node = $node->get_first_descendant_node( 'indexName' );
1667+
if ( null === $name_node && $node->has_child_node( 'constraintName' ) ) {
1668+
$name_node = $node
1669+
->get_first_child_node( 'constraintName' )
1670+
->get_first_child_node( 'identifier' );
1671+
}
1672+
16611673
if ( null === $name_node ) {
16621674
/*
16631675
* In MySQL, the default index name equals the first column name.

0 commit comments

Comments
 (0)