Skip to content

Commit 93ad5b0

Browse files
authored
Merge branch 'master' into feature/alter
2 parents 41b0728 + f80800c commit 93ad5b0

File tree

13 files changed

+226
-8
lines changed

13 files changed

+226
-8
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2024 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.expression;
11+
12+
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
13+
14+
import java.util.Objects;
15+
16+
/**
17+
* A boolean value true/false
18+
*/
19+
public final class BooleanValue extends ASTNodeAccessImpl implements Expression {
20+
21+
private boolean value = false;
22+
23+
public BooleanValue() {
24+
// empty constructor
25+
}
26+
27+
public BooleanValue(String value) {
28+
this(Boolean.parseBoolean(value));
29+
}
30+
31+
public BooleanValue(boolean bool) {
32+
value = bool;
33+
}
34+
35+
public boolean getValue() {
36+
return value;
37+
}
38+
39+
public void setValue(boolean bool) {
40+
value = bool;
41+
}
42+
43+
@Override
44+
public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
45+
return expressionVisitor.visit(this, context);
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return Boolean.toString(value);
51+
}
52+
53+
public BooleanValue withValue(boolean bool) {
54+
this.setValue(bool);
55+
return this;
56+
}
57+
58+
@Override
59+
public boolean equals(Object o) {
60+
if (this == o) {
61+
return true;
62+
}
63+
if (o == null || getClass() != o.getClass()) {
64+
return false;
65+
}
66+
BooleanValue that = (BooleanValue) o;
67+
return Objects.equals(value, that.value);
68+
}
69+
70+
@Override
71+
public int hashCode() {
72+
return Objects.hash(value);
73+
}
74+
}

src/main/java/net/sf/jsqlparser/expression/ExpressionVisitor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ default void visit(StringValue stringValue) {
147147
this.visit(stringValue, null);
148148
}
149149

150+
<S> T visit(BooleanValue booleanValue, S context);
151+
152+
default void visit(BooleanValue booleanValue) {
153+
this.visit(booleanValue, null);
154+
}
155+
150156
<S> T visit(Addition addition, S context);
151157

152158
default void visit(Addition addition) {

src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ public <S> T visit(StringValue stringValue, S context) {
156156
return visitExpression(stringValue, context);
157157
}
158158

159+
@Override
160+
public <S> T visit(BooleanValue booleanValue, S context) {
161+
return visitExpression(booleanValue, context);
162+
}
163+
159164
@Override
160165
public <S> T visit(Addition addition, S context) {
161166
return visitBinaryExpression(addition, context);

src/main/java/net/sf/jsqlparser/parser/ParserKeywordsUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class ParserKeywordsUtils {
6868
{"EXCEPT", RESTRICTED_SQL2016},
6969
{"EXCLUDES", RESTRICTED_JSQLPARSER},
7070
{"EXISTS", RESTRICTED_SQL2016},
71+
{"FALSE", RESTRICTED_SQL2016},
7172
{"FETCH", RESTRICTED_SQL2016},
7273
{"FINAL", RESTRICTED_JSQLPARSER},
7374
{"FOR", RESTRICTED_SQL2016},
@@ -131,6 +132,7 @@ public class ParserKeywordsUtils {
131132
{"TABLES", RESTRICTED_ALIAS},
132133
{"TOP", RESTRICTED_SQL2016},
133134
{"TRAILING", RESTRICTED_SQL2016},
135+
{"TRUE", RESTRICTED_SQL2016},
134136
{"UNBOUNDED", RESTRICTED_JSQLPARSER},
135137
{"UNION", RESTRICTED_SQL2016},
136138
{"UNIQUE", RESTRICTED_SQL2016},

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.sf.jsqlparser.expression.ArrayConstructor;
1717
import net.sf.jsqlparser.expression.ArrayExpression;
1818
import net.sf.jsqlparser.expression.BinaryExpression;
19+
import net.sf.jsqlparser.expression.BooleanValue;
1920
import net.sf.jsqlparser.expression.CaseExpression;
2021
import net.sf.jsqlparser.expression.CastExpression;
2122
import net.sf.jsqlparser.expression.CollateExpression;
@@ -626,6 +627,12 @@ public <S> Void visit(StringValue stringValue, S context) {
626627
return null;
627628
}
628629

630+
@Override
631+
public <S> Void visit(BooleanValue booleanValue, S context) {
632+
633+
return null;
634+
}
635+
629636
@Override
630637
public <S> Void visit(Subtraction subtraction, S context) {
631638
visitBinaryExpression(subtraction);

src/main/java/net/sf/jsqlparser/util/deparser/ExpressionDeParser.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.sf.jsqlparser.expression.ArrayConstructor;
1717
import net.sf.jsqlparser.expression.ArrayExpression;
1818
import net.sf.jsqlparser.expression.BinaryExpression;
19+
import net.sf.jsqlparser.expression.BooleanValue;
1920
import net.sf.jsqlparser.expression.CaseExpression;
2021
import net.sf.jsqlparser.expression.CastExpression;
2122
import net.sf.jsqlparser.expression.CollateExpression;
@@ -621,6 +622,13 @@ public <S> StringBuilder visit(StringValue stringValue, S context) {
621622
return buffer;
622623
}
623624

625+
@Override
626+
public <S> StringBuilder visit(BooleanValue booleanValue, S context) {
627+
buffer.append(booleanValue.getValue());
628+
629+
return buffer;
630+
}
631+
624632
@Override
625633
public <S> StringBuilder visit(Subtraction subtraction, S context) {
626634
deparse(subtraction, " - ", null);
@@ -749,6 +757,10 @@ public void visit(StringValue stringValue) {
749757
visit(stringValue, null);
750758
}
751759

760+
public void visit(BooleanValue booleanValue) {
761+
visit(booleanValue, null);
762+
}
763+
752764
public void visit(Subtraction subtraction) {
753765
visit(subtraction, null);
754766
}

src/main/java/net/sf/jsqlparser/util/validation/validator/ExpressionValidator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import net.sf.jsqlparser.expression.ArrayConstructor;
1616
import net.sf.jsqlparser.expression.ArrayExpression;
1717
import net.sf.jsqlparser.expression.BinaryExpression;
18+
import net.sf.jsqlparser.expression.BooleanValue;
1819
import net.sf.jsqlparser.expression.CaseExpression;
1920
import net.sf.jsqlparser.expression.CastExpression;
2021
import net.sf.jsqlparser.expression.CollateExpression;
@@ -486,6 +487,12 @@ public <S> Void visit(StringValue stringValue, S context) {
486487
return null;
487488
}
488489

490+
@Override
491+
public <S> Void visit(BooleanValue booleanValue, S context) {
492+
// nothing to validate
493+
return null;
494+
}
495+
489496
@Override
490497
public <S> Void visit(Subtraction subtraction, S context) {
491498
visitBinaryExpression(subtraction, " - ");
@@ -620,6 +627,10 @@ public void visit(StringValue stringValue) {
620627
visit(stringValue, null);
621628
}
622629

630+
public void visit(BooleanValue booleanValue) {
631+
visit(booleanValue, null);
632+
}
633+
623634
public void visit(Subtraction subtraction) {
624635
visit(subtraction, null);
625636
}

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,7 @@ MergeOperation MergeWhenNotMatched() : {
19711971
{ return mi; }
19721972
}
19731973

1974+
// table names seem to allow ":" delimiters, e.g. for Informix see #1134
19741975
ObjectNames RelObjectNames() : {
19751976
String token = null;
19761977
Token delimiter = null;
@@ -1986,16 +1987,31 @@ ObjectNames RelObjectNames() : {
19861987
{ return new ObjectNames(data, delimiters); }
19871988
}
19881989

1989-
// See: http://technet.microsoft.com/en-us/library/ms187879%28v=sql.105%29.aspx
1990+
// column names do not allow ":" delimeters as those represent JSON `GET` operators
1991+
ObjectNames ColumnIdentifier() : {
1992+
String token = null;
1993+
Token delimiter = null;
1994+
List<String> data = new ArrayList<String>();
1995+
List<String> delimiters = new ArrayList<String>();
1996+
} {
1997+
token = RelObjectNameExt() { data.add(token); }
1998+
(
1999+
LOOKAHEAD (2) ( delimiter = "." ) { delimiters.add(delimiter.image); } (( delimiter = "." ) { data.add(null); delimiters.add(delimiter.image); })*
2000+
token = RelObjectNameExt2() { data.add(token); }
2001+
) *
2002+
2003+
{ return new ObjectNames(data, delimiters); }
2004+
}
19902005

2006+
// See: http://technet.microsoft.com/en-us/library/ms187879%28v=sql.105%29.aspx
19912007
Column Column() #Column :
19922008
{
19932009
ObjectNames data = null;
19942010
ArrayConstructor arrayConstructor = null;
19952011
Token tk = null;
19962012
}
19972013
{
1998-
data = RelObjectNames()
2014+
data = ColumnIdentifier()
19992015
[ LOOKAHEAD(2) <K_COMMENT> tk=<S_CHAR_LITERAL> ]
20002016
// @todo: we better should return a SEQUENCE instead of a COLUMN
20012017
[ LOOKAHEAD(2) "." <K_NEXTVAL> { data.getNames().add("nextval"); } ]
@@ -4788,7 +4804,7 @@ Expression PrimaryExpression() #PrimaryExpression:
47884804

47894805
| LOOKAHEAD(2, {!interrupted}) retval=DateTimeLiteralExpression()
47904806

4791-
| LOOKAHEAD( 3 , {!interrupted}) retval=StructType()
4807+
| LOOKAHEAD(3 , {!interrupted}) retval=StructType()
47924808

47934809
| LOOKAHEAD(3, {!interrupted}) <K_ARRAY_LITERAL> retval=ArrayConstructor(true)
47944810

@@ -4802,6 +4818,8 @@ Expression PrimaryExpression() #PrimaryExpression:
48024818

48034819
| LOOKAHEAD(2, {!interrupted}) retval=Column()
48044820

4821+
| LOOKAHEAD(2, {!interrupted}) (token=<K_TRUE> | token=<K_FALSE>) { retval = new BooleanValue(token.image); }
4822+
48054823
| token=<S_CHAR_LITERAL> { retval = new StringValue(token.image); linkAST(retval,jjtThis); }
48064824

48074825
| "{d" token=<S_CHAR_LITERAL> "}" { retval = new DateValue(token.image); }
@@ -6752,7 +6770,7 @@ String AList():
67526770
"("
67536771

67546772
(
6755-
( (tk=<S_LONG> | tk=<S_DOUBLE> | tk=<S_CHAR_LITERAL>) { retval.append(tk.image); }
6773+
( (tk=<S_LONG> | tk=<S_DOUBLE> | tk=<S_CHAR_LITERAL> | tk=<K_TRUE> | tk=<K_FALSE>) { retval.append(tk.image); }
67566774
| (name=RelObjectNameWithoutValue()) { retval.append(name); })
67576775
[("," {retval.append(",");} | "=" {retval.append("=");})] )*
67586776

src/site/sphinx/keywords.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ The following Keywords are **restricted** in JSQLParser-|JSQLPARSER_VERSION| and
5353
+----------------------+-------------+-----------+
5454
| EXISTS | Yes | Yes |
5555
+----------------------+-------------+-----------+
56+
| FALSE | Yes | Yes |
57+
+----------------------+-------------+-----------+
5658
| FETCH | Yes | Yes |
5759
+----------------------+-------------+-----------+
5860
| FINAL | Yes | Yes |
@@ -179,6 +181,8 @@ The following Keywords are **restricted** in JSQLParser-|JSQLPARSER_VERSION| and
179181
+----------------------+-------------+-----------+
180182
| TRAILING | Yes | Yes |
181183
+----------------------+-------------+-----------+
184+
| TRUE | Yes | Yes |
185+
+----------------------+-------------+-----------+
182186
| UNBOUNDED | Yes | Yes |
183187
+----------------------+-------------+-----------+
184188
| UNION | Yes | Yes |
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2024 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.expression;
11+
12+
import org.junit.jupiter.api.Test;
13+
14+
import static org.junit.jupiter.api.Assertions.*;
15+
16+
/**
17+
*
18+
* @author tw
19+
*/
20+
public class BooleanValueTest {
21+
22+
@Test
23+
public void testTrueValue() {
24+
BooleanValue value = new BooleanValue("true");
25+
26+
assertTrue(value.getValue());
27+
}
28+
29+
@Test
30+
public void testFalseValue() {
31+
BooleanValue value = new BooleanValue("false");
32+
33+
assertFalse(value.getValue());
34+
}
35+
36+
@Test
37+
public void testWrongValueAsFalseLargeNumber() {
38+
BooleanValue value = new BooleanValue("test");
39+
40+
assertFalse(value.getValue());
41+
}
42+
43+
@Test
44+
public void testNullStringValue() {
45+
BooleanValue value = new BooleanValue(null);
46+
47+
assertFalse(value.getValue());
48+
}
49+
}

0 commit comments

Comments
 (0)