Skip to content

Commit 41e4b42

Browse files
committed
#26 Correct result set columns from SHOW COLUMNS FROM table.
1 parent f983b56 commit 41e4b42

2 files changed

Lines changed: 68 additions & 25 deletions

File tree

tests/WP_SQLite_Query_Tests.php

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function setUp(): void {
6464
$err->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
6565
$err_code = $err_data[1];
6666
$translator->rollback();
67-
$message = sprintf(
67+
$message = sprintf(
6868
'Error occurred while creating tables or indexes...<br />Query was: %s<br />',
6969
var_export( $query, true )
7070
);
@@ -112,37 +112,24 @@ public function setUp(): void {
112112
}
113113
}
114114

115-
private function assertQuery( $sql ) {
116-
$retval = $this->engine->query( $sql );
117-
$this->assertEquals(
118-
'',
119-
$this->engine->get_error_message()
120-
);
121-
$this->assertNotFalse(
122-
$retval
123-
);
124-
125-
return $retval;
126-
}
127-
128-
public function testGreatestLeast () {
115+
public function testGreatestLeast() {
129116
$q = <<<'QUERY'
130117
SELECT GREATEST('a', 'b') letter;
131118
QUERY;
132119

133120
$result = $this->assertQuery( $q );
134121
$actual = $this->engine->get_query_results();
135-
$this->assertEquals(1, count( $actual ));
136-
$this->assertEquals('b', $actual[0]->letter);
122+
$this->assertEquals( 1, count( $actual ) );
123+
$this->assertEquals( 'b', $actual[0]->letter );
137124

138125
$q = <<<'QUERY'
139126
SELECT LEAST('a', 'b') letter;
140127
QUERY;
141128

142129
$result = $this->assertQuery( $q );
143130
$actual = $this->engine->get_query_results();
144-
$this->assertEquals(1, count( $actual ));
145-
$this->assertEquals('a', $actual[0]->letter);
131+
$this->assertEquals( 1, count( $actual ) );
132+
$this->assertEquals( 'a', $actual[0]->letter );
146133

147134
$q = <<<'QUERY'
148135
SELECT GREATEST(2, 1.5) num;
@@ -224,8 +211,6 @@ public function testLikeEscapingParenAfterLike() {
224211
$this->assertEquals( 'visible_meta_key_40', $last );
225212
}
226213

227-
// https://github.com/WordPress/sqlite-database-integration/issues/19
228-
229214
public function testLikeEscapingWithConcatFunction() {
230215
$q = <<<'QUERY'
231216
SELECT DISTINCT meta_key FROM wp_postmeta WHERE meta_key NOT BETWEEN '_' AND '_z' AND meta_key NOT LIKE CONCAT('\_', '%') ORDER BY meta_key LIMIT 30
@@ -239,6 +224,8 @@ public function testLikeEscapingWithConcatFunction() {
239224
$this->assertEquals( 'visible_meta_key_30', $last );
240225
}
241226

227+
// https://github.com/WordPress/sqlite-database-integration/issues/19
228+
242229
public function testHavingWithoutGroupBy() {
243230

244231
$q = <<<'QUERY'
@@ -370,6 +357,7 @@ public function testExpiredTransients() {
370357
self::assertLessThan( time(), $row->option_timeout );
371358
}
372359
}
360+
373361
public function testDeleteExpiredNonSiteTransients() {
374362

375363
$now = time();
@@ -425,7 +413,7 @@ public function testDeleteExpiredNonSiteTransients() {
425413
$count_unexpired = 0;
426414
foreach ( $actual as $row ) {
427415
if ( str_starts_with( $row->option_name, '_transient' ) ) {
428-
$count_unexpired++;
416+
$count_unexpired ++;
429417
$this->assertGreaterThan( $now, $row->option_timeout );
430418
}
431419
}
@@ -491,7 +479,7 @@ public function testRecoverSerialized() {
491479
$this->assertEquals( $obj, $unserialized );
492480

493481
$obj ['two'] ++;
494-
$obj ['pi'] *= 2;
482+
$obj ['pi'] *= 2;
495483
$option_value = serialize( $obj );
496484
$option_value_escaped = $this->engine->get_pdo()->quote( $option_value );
497485
/* Note well: this is heredoc not nowdoc */
@@ -514,7 +502,36 @@ public function testRecoverSerialized() {
514502
$this->assertEquals( $option_value, $retrieved_string );
515503
$unserialized = unserialize( $retrieved_string );
516504
$this->assertEquals( $obj, $unserialized );
505+
}
506+
507+
public function testShowColumns() {
508+
509+
$query = 'SHOW COLUMNS FROM wp_posts';
510+
$this->assertQuery( $query );
511+
512+
$actual = $this->engine->get_query_results();
513+
foreach ( $actual as $row ) {
514+
$this->assertIsObject( $row );
515+
$this->assertTrue( property_exists( $row, 'Field' ) );
516+
$this->assertTrue( property_exists( $row, 'Type' ) );
517+
$this->assertTrue( property_exists( $row, 'Null' ) );
518+
$this->assertTrue( ( 'NO' === $row->Null ) || ( 'YES' === $row->Null ) );
519+
$this->assertTrue( property_exists( $row, 'Key' ) );
520+
$this->assertTrue( property_exists( $row, 'Default' ) );
521+
}
522+
}
523+
524+
private function assertQuery( $sql ) {
525+
$retval = $this->engine->query( $sql );
526+
$this->assertEquals(
527+
'',
528+
$this->engine->get_error_message()
529+
);
530+
$this->assertNotFalse(
531+
$retval
532+
);
517533

534+
return $retval;
518535
}
519536

520537
}

wp-includes/sqlite/class-wp-sqlite-translator.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,9 +2937,35 @@ private function execute_show() {
29372937
$stmt = $this->execute_sqlite_query(
29382938
"PRAGMA table_info(\"$table_name\");"
29392939
);
2940-
$this->set_results_from_fetched_data(
2941-
$stmt->fetchAll( $this->pdo_fetch_mode )
2940+
/* @todo we may need to add the Extra column if anybdy needs it. 'auto_increment' is the value */
2941+
$name_map = array(
2942+
'name' => 'Field',
2943+
'type' => 'Type',
2944+
'dflt_value' => 'Default',
2945+
'cid' => null,
2946+
'notnull' => null,
2947+
'pk' => null,
29422948
);
2949+
$columns = $stmt->fetchAll( $this->pdo_fetch_mode );
2950+
$columns = array_map( function ( $row ) use ( $name_map ) {
2951+
$new = array();
2952+
$is_object = is_object( $row );
2953+
$row = $is_object ? (array) $row : $row;
2954+
foreach ( $row as $k => $v ) {
2955+
$k = array_key_exists( $k, $name_map ) ? $name_map [ $k ] : $k;
2956+
if ( $k ) {
2957+
$new[ $k ] = $v;
2958+
}
2959+
}
2960+
if ( array_key_exists( 'notnull', $row ) ) {
2961+
$new['Null'] = ( '1' === $row ['notnull'] ) ? 'NO' : 'YES';
2962+
}
2963+
if ( array_key_exists( 'pk', $row ) ) {
2964+
$new['Key'] = ( '1' === $row ['pk'] ) ? 'PRI' : '';
2965+
}
2966+
return $is_object ? (object) $new : $new;
2967+
}, $columns );
2968+
$this->set_results_from_fetched_data( $columns );
29432969
return;
29442970

29452971
case 'INDEX FROM':

0 commit comments

Comments
 (0)