Skip to content

Commit 7df37d0

Browse files
committed
Implement index part order (ASC/DESC), improve tests
1 parent 7b17a19 commit 7df37d0

3 files changed

Lines changed: 148 additions & 2 deletions

File tree

tests/WP_SQLite_Driver_Tests.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,7 @@ public function testAlterTableAddIndex() {
17951795
$result = $this->assertQuery( 'ALTER TABLE _tmp_table ADD INDEX name (name);' );
17961796
$this->assertNull( $result );
17971797

1798+
// Verify that the index was created in the information schema.
17981799
$this->assertQuery( 'SHOW INDEX FROM _tmp_table;' );
17991800
$results = $this->engine->get_query_results();
18001801
$this->assertEquals(
@@ -1819,6 +1820,22 @@ public function testAlterTableAddIndex() {
18191820
),
18201821
$results
18211822
);
1823+
1824+
// Verify that the index is defined in the SQLite.
1825+
$result = $this->engine
1826+
->execute_sqlite_query( "PRAGMA index_list('_tmp_table')" )
1827+
->fetchAll( PDO::FETCH_ASSOC );
1828+
$this->assertCount( 1, $result );
1829+
$this->assertEquals(
1830+
array(
1831+
'seq' => '0',
1832+
'name' => '_tmp_table__name',
1833+
'unique' => '0',
1834+
'origin' => 'c',
1835+
'partial' => '0',
1836+
),
1837+
$result[0]
1838+
);
18221839
}
18231840

18241841
public function testAlterTableAddUniqueIndex() {
@@ -1831,6 +1848,7 @@ public function testAlterTableAddUniqueIndex() {
18311848
$result = $this->assertQuery( 'ALTER TABLE _tmp_table ADD UNIQUE INDEX name (name(20));' );
18321849
$this->assertNull( $result );
18331850

1851+
// Verify that the index was created in the information schema.
18341852
$this->assertQuery( 'SHOW INDEX FROM _tmp_table;' );
18351853
$results = $this->engine->get_query_results();
18361854
$this->assertEquals(
@@ -1855,6 +1873,22 @@ public function testAlterTableAddUniqueIndex() {
18551873
),
18561874
$results
18571875
);
1876+
1877+
// Verify that the index is defined in the SQLite.
1878+
$result = $this->engine
1879+
->execute_sqlite_query( "PRAGMA index_list('_tmp_table')" )
1880+
->fetchAll( PDO::FETCH_ASSOC );
1881+
$this->assertCount( 1, $result );
1882+
$this->assertEquals(
1883+
array(
1884+
'seq' => '0',
1885+
'name' => '_tmp_table__name',
1886+
'unique' => '1',
1887+
'origin' => 'c',
1888+
'partial' => '0',
1889+
),
1890+
$result[0]
1891+
);
18581892
}
18591893

18601894
public function testAlterTableAddFulltextIndex() {
@@ -1867,6 +1901,7 @@ public function testAlterTableAddFulltextIndex() {
18671901
$result = $this->assertQuery( 'ALTER TABLE _tmp_table ADD FULLTEXT INDEX name (name);' );
18681902
$this->assertNull( $result );
18691903

1904+
// Verify that the index was created in the information schema.
18701905
$this->assertQuery( 'SHOW INDEX FROM _tmp_table;' );
18711906
$results = $this->engine->get_query_results();
18721907
$this->assertEquals(
@@ -1891,6 +1926,66 @@ public function testAlterTableAddFulltextIndex() {
18911926
),
18921927
$results
18931928
);
1929+
1930+
// Verify that the index is defined in the SQLite.
1931+
$result = $this->engine
1932+
->execute_sqlite_query( "PRAGMA index_list('_tmp_table')" )
1933+
->fetchAll( PDO::FETCH_ASSOC );
1934+
$this->assertCount( 1, $result );
1935+
$this->assertEquals(
1936+
array(
1937+
'seq' => '0',
1938+
'name' => '_tmp_table__name',
1939+
'unique' => '0',
1940+
'origin' => 'c',
1941+
'partial' => '0',
1942+
),
1943+
$result[0]
1944+
);
1945+
}
1946+
1947+
public function testAlterTableAddIndexWithOrder(): void {
1948+
$this->assertQuery( 'CREATE TABLE t (id INT, value VARCHAR(255))' );
1949+
$this->assertQuery( 'ALTER TABLE t ADD INDEX idx_value (value DESC)' );
1950+
1951+
// Verify that the order was saved in the information schema.
1952+
$result = $this->assertQuery( 'SHOW INDEX FROM t' );
1953+
$this->assertCount( 1, $result );
1954+
$this->assertEquals( 'D', $result[0]->Collation );
1955+
1956+
// Verify that the order is included in the CREATE TABLE statement.
1957+
$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
1958+
$this->assertCount( 1, $result );
1959+
$this->assertSame(
1960+
implode(
1961+
"\n",
1962+
array(
1963+
'CREATE TABLE `t` (',
1964+
' `id` int DEFAULT NULL,',
1965+
' `value` varchar(255) DEFAULT NULL,',
1966+
' KEY `idx_value` (`value` DESC)',
1967+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
1968+
)
1969+
),
1970+
$result[0]->{'Create Table'}
1971+
);
1972+
1973+
// Verify that the order is defined in the SQLite index.
1974+
$result = $this->engine
1975+
->execute_sqlite_query( "SELECT * FROM pragma_index_xinfo('t__idx_value') WHERE cid != -1" )
1976+
->fetchAll( PDO::FETCH_ASSOC );
1977+
$this->assertCount( 1, $result );
1978+
$this->assertEquals(
1979+
array(
1980+
'seqno' => '0',
1981+
'cid' => '1',
1982+
'name' => 'value',
1983+
'desc' => '1',
1984+
'coll' => 'NOCASE',
1985+
'key' => '1',
1986+
),
1987+
$result[0]
1988+
);
18941989
}
18951990

18961991
public function testAlterTableModifyColumn() {
@@ -5496,6 +5591,50 @@ public function testCreateSpatialIndex(): void {
54965591
$this->assertEquals( 'SPATIAL', $result[0]->Index_type );
54975592
}
54985593

5594+
public function testCreateIndexWithOrder(): void {
5595+
$this->assertQuery( 'CREATE TABLE t (id INT, value VARCHAR(255))' );
5596+
$this->assertQuery( 'CREATE INDEX idx_value ON t (value DESC)' );
5597+
5598+
// Verify that the order was saved in the information schema.
5599+
$result = $this->assertQuery( 'SHOW INDEX FROM t' );
5600+
$this->assertCount( 1, $result );
5601+
$this->assertEquals( 'D', $result[0]->Collation );
5602+
5603+
// Verify that the order is included in the CREATE TABLE statement.
5604+
$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
5605+
$this->assertCount( 1, $result );
5606+
$this->assertSame(
5607+
implode(
5608+
"\n",
5609+
array(
5610+
'CREATE TABLE `t` (',
5611+
' `id` int DEFAULT NULL,',
5612+
' `value` varchar(255) DEFAULT NULL,',
5613+
' KEY `idx_value` (`value` DESC)',
5614+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
5615+
)
5616+
),
5617+
$result[0]->{'Create Table'}
5618+
);
5619+
5620+
// Verify that the order is defined in the SQLite index.
5621+
$result = $this->engine
5622+
->execute_sqlite_query( "SELECT * FROM pragma_index_xinfo('t__idx_value') WHERE cid != -1" )
5623+
->fetchAll( PDO::FETCH_ASSOC );
5624+
$this->assertCount( 1, $result );
5625+
$this->assertEquals(
5626+
array(
5627+
'seqno' => '0',
5628+
'cid' => '1',
5629+
'name' => 'value',
5630+
'desc' => '1',
5631+
'coll' => 'NOCASE',
5632+
'key' => '1',
5633+
),
5634+
$result[0]
5635+
);
5636+
}
5637+
54995638
public function testCreateIndexWithComment(): void {
55005639
$this->assertQuery( 'CREATE TABLE t (id INT, value INT)' );
55015640
$this->assertQuery( 'CREATE INDEX idx_value ON t (value) COMMENT "Test comment"' );

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3522,7 +3522,11 @@ function ( $column ) {
35223522
', ',
35233523
array_map(
35243524
function ( $column ) {
3525-
return $this->quote_sqlite_identifier( $column['COLUMN_NAME'] );
3525+
$fragment = $this->quote_sqlite_identifier( $column['COLUMN_NAME'] );
3526+
if ( 'D' === $column['COLLATION'] ) {
3527+
$fragment .= ' DESC';
3528+
}
3529+
return $fragment;
35263530
},
35273531
$constraint
35283532
)
@@ -3695,6 +3699,9 @@ function ( $column ) {
36953699
if ( null !== $column['SUB_PART'] ) {
36963700
$definition .= sprintf( '(%d)', $column['SUB_PART'] );
36973701
}
3702+
if ( 'D' === $column['COLLATION'] ) {
3703+
$definition .= ' DESC';
3704+
}
36983705
return $definition;
36993706
},
37003707
$constraint

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2228,7 +2228,7 @@ private function get_index_column_collation( WP_Parser_Node $node, string $index
22282228
return null;
22292229
}
22302230

2231-
$collate_node = $node->get_first_descendant_node( 'collationName' );
2231+
$collate_node = $node->get_first_descendant_node( 'direction' );
22322232
if ( null === $collate_node ) {
22332233
return 'A';
22342234
}

0 commit comments

Comments
 (0)