Skip to content

Commit ccf0c80

Browse files
committed
Fix handling of DEFAULT TRUE and DEFAULT FALSE columns
1 parent 89ece22 commit ccf0c80

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

tests/WP_SQLite_Driver_Tests.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,8 @@ public function testShowCreateTableWithDefaultValues(): void {
499499
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
500500
no_default VARCHAR(255),
501501
default_zero INT DEFAULT 0,
502+
default_true INT DEFAULT TRUE,
503+
default_false INT DEFAULT FALSE,
502504
default_empty_string VARCHAR(255) DEFAULT '',
503505
special_chars_1 TEXT NOT NULL COMMENT '\'',
504506
special_chars_2 TEXT NOT NULL COMMENT '''',
@@ -528,6 +530,8 @@ public function testShowCreateTableWithDefaultValues(): void {
528530
' `ID` bigint NOT NULL AUTO_INCREMENT,',
529531
' `no_default` varchar(255) DEFAULT NULL,',
530532
" `default_zero` int DEFAULT '0',",
533+
" `default_true` int DEFAULT '1',",
534+
" `default_false` int DEFAULT '0',",
531535
" `default_empty_string` varchar(255) DEFAULT '',",
532536
" `special_chars_1` text NOT NULL COMMENT '''',",
533537
" `special_chars_2` text NOT NULL COMMENT '''',",

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,15 +2053,35 @@ private function get_column_default( WP_Parser_Node $node ): ?string {
20532053
return null;
20542054
}
20552055

2056+
/*
2057+
* [GRAMMAR]
2058+
* DEFAULT_SYMBOL (
2059+
* signedLiteral
2060+
* | NOW_SYMBOL timeFunctionParameters?
2061+
* | {serverVersion >= 80013}? exprWithParentheses
2062+
* )
2063+
*/
2064+
2065+
// DEFAULT NOW()
20562066
if ( $default_attr->has_child_token( WP_MySQL_Lexer::NOW_SYMBOL ) ) {
20572067
return 'CURRENT_TIMESTAMP';
20582068
}
20592069

2060-
if (
2061-
$default_attr->has_child_node( 'signedLiteral' )
2062-
&& null !== $default_attr->get_first_descendant_node( 'nullLiteral' )
2063-
) {
2064-
return null;
2070+
// DEFAULT signedLiteral
2071+
$signed_literal = $default_attr->get_first_child_node( 'signedLiteral' );
2072+
if ( $signed_literal ) {
2073+
$literal = $signed_literal->get_first_child_node( 'literal' );
2074+
2075+
// DEFAULT NULL
2076+
if ( $literal->has_child_node( 'nullLiteral' ) ) {
2077+
return null;
2078+
}
2079+
2080+
// DEFAULT TRUE or DEFAULT FALSE
2081+
if ( $literal->has_child_node( 'boolLiteral' ) ) {
2082+
$bool_literal = $literal->get_first_child_node( 'boolLiteral' );
2083+
return $bool_literal->has_child_token( WP_MySQL_Lexer::TRUE_SYMBOL ) ? '1' : '0';
2084+
}
20652085
}
20662086

20672087
// @TODO: MySQL seems to normalize default values for numeric

0 commit comments

Comments
 (0)