Skip to content

Commit 2b20845

Browse files
committed
Implement DROP INDEX statement
1 parent de42b1c commit 2b20845

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

tests/WP_SQLite_Driver_Tests.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5514,4 +5514,17 @@ public function testCreateIndexWithDuplicateName(): void {
55145514

55155515
$this->assertQuery( 'CREATE INDEX idx_value ON t (val2)' );
55165516
}
5517+
5518+
public function testDropIndex(): void {
5519+
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY, val_unique INT UNIQUE, val_index INT)' );
5520+
$this->assertQuery( 'CREATE INDEX idx_val_index ON t (val_index)' );
5521+
5522+
$result = $this->assertQuery( 'SHOW INDEX FROM t' );
5523+
$this->assertCount( 3, $result );
5524+
$this->assertEquals( 'PRIMARY', $result[0]->Key_name );
5525+
$this->assertEquals( 'val_unique', $result[1]->Key_name );
5526+
$this->assertEquals( 'idx_val_index', $result[2]->Key_name );
5527+
5528+
$this->assertQuery( 'DROP INDEX idx_val_index ON t' );
5529+
}
55175530
}

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,9 @@ private function execute_mysql_query( WP_Parser_Node $node ): void {
912912
case 'dropTable':
913913
$this->execute_drop_table_statement( $node );
914914
break;
915+
case 'dropIndex':
916+
$this->execute_drop_index_statement( $node );
917+
break;
915918
default:
916919
$query = $this->translate( $node );
917920
$this->execute_sqlite_query( $query );
@@ -1511,6 +1514,32 @@ private function execute_create_index_statement( WP_Parser_Node $node ): void {
15111514
);
15121515
}
15131516

1517+
/**
1518+
* Translate and execute a MySQL DROP INDEX statement in SQLite.
1519+
*
1520+
* @param WP_Parser_Node $node The "dropStatement" AST node with "dropIndex" child.
1521+
* @throws WP_SQLite_Driver_Exception When the query execution fails.
1522+
*/
1523+
private function execute_drop_index_statement( WP_Parser_Node $node ): void {
1524+
$this->information_schema_builder->record_drop_index( $node );
1525+
1526+
$drop_index = $node->get_first_child_node( 'dropIndex' );
1527+
$table_name = $this->unquote_sqlite_identifier(
1528+
$this->translate( $drop_index->get_first_child_node( 'tableRef' ) )
1529+
);
1530+
$index_name = $this->unquote_sqlite_identifier(
1531+
$this->translate( $drop_index->get_first_child_node( 'indexRef' ) )
1532+
);
1533+
1534+
$sqlite_index_name = $this->get_sqlite_index_name( $table_name, $index_name );
1535+
$this->execute_sqlite_query(
1536+
sprintf(
1537+
'DROP INDEX %s',
1538+
$this->quote_sqlite_identifier( $sqlite_index_name )
1539+
)
1540+
);
1541+
}
1542+
15141543
/**
15151544
* Translate and execute a MySQL SHOW statement in SQLite.
15161545
*

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ public function record_alter_table( WP_Parser_Node $node ): void {
584584
// DROP INDEX
585585
if ( $action->has_child_node( 'keyOrIndex' ) ) {
586586
$name = $this->get_value( $action->get_first_child_node( 'indexRef' ) );
587-
$this->record_drop_index( $table_is_temporary, $table_name, $name );
587+
$this->record_drop_index_data( $table_is_temporary, $table_name, $name );
588588
continue;
589589
}
590590
}
@@ -653,6 +653,19 @@ public function record_create_index( WP_Parser_Node $node ): void {
653653
$this->record_add_index( $table_is_temporary, $table_name, $create_index );
654654
}
655655

656+
/**
657+
* Analyze DROP INDEX definition and record data in the information schema.
658+
*
659+
* @param WP_Parser_Node $node The "dropStatement" AST node with "dropIndex" child.
660+
*/
661+
public function record_drop_index( WP_Parser_Node $node ): void {
662+
$drop_index = $node->get_first_child_node( 'dropIndex' );
663+
$table_name = $this->get_value( $drop_index->get_first_child_node( 'tableRef' ) );
664+
$index_name = $this->get_value( $drop_index->get_first_child_node( 'indexRef' ) );
665+
$table_is_temporary = $this->temporary_table_exists( $table_name );
666+
$this->record_drop_index_data( $table_is_temporary, $table_name, $index_name );
667+
}
668+
656669
/**
657670
* Analyze ADD COLUMN definition and record data in the information schema.
658671
*
@@ -1006,7 +1019,7 @@ private function record_add_index(
10061019
* @param string $table_name The table name.
10071020
* @param string $index_name The index name.
10081021
*/
1009-
private function record_drop_index(
1022+
private function record_drop_index_data(
10101023
bool $table_is_temporary,
10111024
string $table_name,
10121025
string $index_name

0 commit comments

Comments
 (0)