Merge 'Parse booleans to integer literals in expressions' from Preston Thorpe

Adds tests for edgecases for the insane cases where someone might want
to name a table `true` :)

Closes #2874
This commit is contained in:
Preston Thorpe
2025-09-01 13:36:29 -04:00
committed by GitHub
3 changed files with 45 additions and 3 deletions

View File

@@ -15,6 +15,9 @@ use crate::lexer::{Lexer, Token};
use crate::token::TokenType::{self, *};
use crate::Result;
const TRUE_LIT: &str = "TRUE";
const FALSE_LIT: &str = "FALSE";
macro_rules! peek_expect {
( $parser:expr, $( $x:ident ),* $(,)?) => {
{
@@ -1545,7 +1548,18 @@ impl<'a> Parser<'a> {
Name::Ident(s) => Literal::String(s),
})))
} else {
Ok(Box::new(Expr::Id(name)))
match name {
Name::Ident(s) => match s.as_str() {
s if s.eq_ignore_ascii_case(TRUE_LIT) => {
return Ok(Box::new(Expr::Literal(Literal::Numeric("1".into()))))
}
s if s.eq_ignore_ascii_case(FALSE_LIT) => {
return Ok(Box::new(Expr::Literal(Literal::Numeric("0".into()))))
}
_ => return Ok(Box::new(Expr::Id(Name::Ident(s)))),
},
_ => Ok(Box::new(Expr::Id(name))),
}
}
}
}

View File

@@ -612,4 +612,24 @@ do_catchsql_test unknown-backtick-identifier-in-values-clause {
do_execsql_test_in_memory_error_content null-insert-in-nulltype-column-notnull-constraint {
CREATE TABLE test (id INTEGER,name NULL NOT NULL);
INSERT INTO test (id, name) VALUES (1, NULL);
} {NOT NULL constraint failed}
} {NOT NULL constraint failed}
do_execsql_test_on_specific_db {:memory:} returning-true-literal {
CREATE TABLE test (id INTEGER, value TEXT);
INSERT INTO test (id, value) VALUES (1, true) RETURNING id, value;
} {1|1}
do_execsql_test_on_specific_db {:memory:} returning-false-literal {
CREATE TABLE test (id INTEGER, value TEXT);
INSERT INTO test (id, value) VALUES (1, false) RETURNING id, value;
} {1|0}
do_execsql_test_on_specific_db {:memory:} boolean-literal-edgecase {
CREATE TABLE true (id INTEGER, value TEXT);
INSERT INTO true (id, value) VALUES (1, true) RETURNING id, value;
} {1|1}
do_execsql_test_on_specific_db {:memory:} boolean-literal-edgecase-false {
CREATE TABLE false (id INTEGER, true TEXT);
INSERT INTO false (id, true) VALUES (1, false) RETURNING id, false;
} {1|0}

View File

@@ -17,4 +17,12 @@ do_execsql_test notnull {
do_execsql_test not-null {
select null not null, 'hi' not null;
} {0|1}
} {0|1}
do_execsql_test sel-true {
select true;
} {1}
do_execsql_test sel-false {
select false;
} {0}