Skip to content

Commit

Permalink
Merge branch 'master' into feature/alter
Browse files Browse the repository at this point in the history
  • Loading branch information
minleejae authored Nov 23, 2024
2 parents 41b0728 + f80800c commit 93ad5b0
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 8 deletions.
74 changes: 74 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/BooleanValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2024 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.expression;

import net.sf.jsqlparser.parser.ASTNodeAccessImpl;

import java.util.Objects;

/**
* A boolean value true/false
*/
public final class BooleanValue extends ASTNodeAccessImpl implements Expression {

private boolean value = false;

public BooleanValue() {
// empty constructor
}

public BooleanValue(String value) {
this(Boolean.parseBoolean(value));
}

public BooleanValue(boolean bool) {
value = bool;
}

public boolean getValue() {
return value;
}

public void setValue(boolean bool) {
value = bool;
}

@Override
public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
return expressionVisitor.visit(this, context);
}

@Override
public String toString() {
return Boolean.toString(value);
}

public BooleanValue withValue(boolean bool) {
this.setValue(bool);
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BooleanValue that = (BooleanValue) o;
return Objects.equals(value, that.value);
}

@Override
public int hashCode() {
return Objects.hash(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ default void visit(StringValue stringValue) {
this.visit(stringValue, null);
}

<S> T visit(BooleanValue booleanValue, S context);

default void visit(BooleanValue booleanValue) {
this.visit(booleanValue, null);
}

<S> T visit(Addition addition, S context);

default void visit(Addition addition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public <S> T visit(StringValue stringValue, S context) {
return visitExpression(stringValue, context);
}

@Override
public <S> T visit(BooleanValue booleanValue, S context) {
return visitExpression(booleanValue, context);
}

@Override
public <S> T visit(Addition addition, S context) {
return visitBinaryExpression(addition, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class ParserKeywordsUtils {
{"EXCEPT", RESTRICTED_SQL2016},
{"EXCLUDES", RESTRICTED_JSQLPARSER},
{"EXISTS", RESTRICTED_SQL2016},
{"FALSE", RESTRICTED_SQL2016},
{"FETCH", RESTRICTED_SQL2016},
{"FINAL", RESTRICTED_JSQLPARSER},
{"FOR", RESTRICTED_SQL2016},
Expand Down Expand Up @@ -131,6 +132,7 @@ public class ParserKeywordsUtils {
{"TABLES", RESTRICTED_ALIAS},
{"TOP", RESTRICTED_SQL2016},
{"TRAILING", RESTRICTED_SQL2016},
{"TRUE", RESTRICTED_SQL2016},
{"UNBOUNDED", RESTRICTED_JSQLPARSER},
{"UNION", RESTRICTED_SQL2016},
{"UNIQUE", RESTRICTED_SQL2016},
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.sf.jsqlparser.expression.ArrayConstructor;
import net.sf.jsqlparser.expression.ArrayExpression;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.BooleanValue;
import net.sf.jsqlparser.expression.CaseExpression;
import net.sf.jsqlparser.expression.CastExpression;
import net.sf.jsqlparser.expression.CollateExpression;
Expand Down Expand Up @@ -626,6 +627,12 @@ public <S> Void visit(StringValue stringValue, S context) {
return null;
}

@Override
public <S> Void visit(BooleanValue booleanValue, S context) {

return null;
}

@Override
public <S> Void visit(Subtraction subtraction, S context) {
visitBinaryExpression(subtraction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.sf.jsqlparser.expression.ArrayConstructor;
import net.sf.jsqlparser.expression.ArrayExpression;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.BooleanValue;
import net.sf.jsqlparser.expression.CaseExpression;
import net.sf.jsqlparser.expression.CastExpression;
import net.sf.jsqlparser.expression.CollateExpression;
Expand Down Expand Up @@ -621,6 +622,13 @@ public <S> StringBuilder visit(StringValue stringValue, S context) {
return buffer;
}

@Override
public <S> StringBuilder visit(BooleanValue booleanValue, S context) {
buffer.append(booleanValue.getValue());

return buffer;
}

@Override
public <S> StringBuilder visit(Subtraction subtraction, S context) {
deparse(subtraction, " - ", null);
Expand Down Expand Up @@ -749,6 +757,10 @@ public void visit(StringValue stringValue) {
visit(stringValue, null);
}

public void visit(BooleanValue booleanValue) {
visit(booleanValue, null);
}

public void visit(Subtraction subtraction) {
visit(subtraction, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.sf.jsqlparser.expression.ArrayConstructor;
import net.sf.jsqlparser.expression.ArrayExpression;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.BooleanValue;
import net.sf.jsqlparser.expression.CaseExpression;
import net.sf.jsqlparser.expression.CastExpression;
import net.sf.jsqlparser.expression.CollateExpression;
Expand Down Expand Up @@ -486,6 +487,12 @@ public <S> Void visit(StringValue stringValue, S context) {
return null;
}

@Override
public <S> Void visit(BooleanValue booleanValue, S context) {
// nothing to validate
return null;
}

@Override
public <S> Void visit(Subtraction subtraction, S context) {
visitBinaryExpression(subtraction, " - ");
Expand Down Expand Up @@ -620,6 +627,10 @@ public void visit(StringValue stringValue) {
visit(stringValue, null);
}

public void visit(BooleanValue booleanValue) {
visit(booleanValue, null);
}

public void visit(Subtraction subtraction) {
visit(subtraction, null);
}
Expand Down
26 changes: 22 additions & 4 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -1971,6 +1971,7 @@ MergeOperation MergeWhenNotMatched() : {
{ return mi; }
}

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

// See: http://technet.microsoft.com/en-us/library/ms187879%28v=sql.105%29.aspx
// column names do not allow ":" delimeters as those represent JSON `GET` operators
ObjectNames ColumnIdentifier() : {
String token = null;
Token delimiter = null;
List<String> data = new ArrayList<String>();
List<String> delimiters = new ArrayList<String>();
} {
token = RelObjectNameExt() { data.add(token); }
(
LOOKAHEAD (2) ( delimiter = "." ) { delimiters.add(delimiter.image); } (( delimiter = "." ) { data.add(null); delimiters.add(delimiter.image); })*
token = RelObjectNameExt2() { data.add(token); }
) *

{ return new ObjectNames(data, delimiters); }
}

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

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

| LOOKAHEAD( 3 , {!interrupted}) retval=StructType()
| LOOKAHEAD(3 , {!interrupted}) retval=StructType()

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

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

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

| LOOKAHEAD(2, {!interrupted}) (token=<K_TRUE> | token=<K_FALSE>) { retval = new BooleanValue(token.image); }

| token=<S_CHAR_LITERAL> { retval = new StringValue(token.image); linkAST(retval,jjtThis); }

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

(
( (tk=<S_LONG> | tk=<S_DOUBLE> | tk=<S_CHAR_LITERAL>) { retval.append(tk.image); }
( (tk=<S_LONG> | tk=<S_DOUBLE> | tk=<S_CHAR_LITERAL> | tk=<K_TRUE> | tk=<K_FALSE>) { retval.append(tk.image); }
| (name=RelObjectNameWithoutValue()) { retval.append(name); })
[("," {retval.append(",");} | "=" {retval.append("=");})] )*

Expand Down
4 changes: 4 additions & 0 deletions src/site/sphinx/keywords.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ The following Keywords are **restricted** in JSQLParser-|JSQLPARSER_VERSION| and
+----------------------+-------------+-----------+
| EXISTS | Yes | Yes |
+----------------------+-------------+-----------+
| FALSE | Yes | Yes |
+----------------------+-------------+-----------+
| FETCH | Yes | Yes |
+----------------------+-------------+-----------+
| FINAL | Yes | Yes |
Expand Down Expand Up @@ -179,6 +181,8 @@ The following Keywords are **restricted** in JSQLParser-|JSQLPARSER_VERSION| and
+----------------------+-------------+-----------+
| TRAILING | Yes | Yes |
+----------------------+-------------+-----------+
| TRUE | Yes | Yes |
+----------------------+-------------+-----------+
| UNBOUNDED | Yes | Yes |
+----------------------+-------------+-----------+
| UNION | Yes | Yes |
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/net/sf/jsqlparser/expression/BooleanValueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2024 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.expression;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
*
* @author tw
*/
public class BooleanValueTest {

@Test
public void testTrueValue() {
BooleanValue value = new BooleanValue("true");

assertTrue(value.getValue());
}

@Test
public void testFalseValue() {
BooleanValue value = new BooleanValue("false");

assertFalse(value.getValue());
}

@Test
public void testWrongValueAsFalseLargeNumber() {
BooleanValue value = new BooleanValue("test");

assertFalse(value.getValue());
}

@Test
public void testNullStringValue() {
BooleanValue value = new BooleanValue(null);

assertFalse(value.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
package net.sf.jsqlparser.expression;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.statement.select.PlainSelect;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down Expand Up @@ -40,7 +42,15 @@ void testIssue1792() throws JSQLParserException {
@Test
void testSnowflakeGetOperator() throws JSQLParserException {
String sqlStr = "SELECT v:'attr[0].name' FROM vartab;";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
PlainSelect st = (PlainSelect) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
Assertions.assertInstanceOf(JsonExpression.class, st.getSelectItem(0).getExpression());
}

@Test
void testDataBricksExtractPathOperator() throws JSQLParserException {
String sqlStr = "SELECT C1:PRICE J FROM VALUES('{\"price\":5}')AS T(C1)";
PlainSelect st = (PlainSelect) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
Assertions.assertInstanceOf(JsonExpression.class, st.getSelectItem(0).getExpression());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
package net.sf.jsqlparser.statement.select;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.JsonExpression;
import net.sf.jsqlparser.schema.Column;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.List;
Expand All @@ -24,12 +27,13 @@ public class ExpressionDelimiterTest {
public void testColumnWithDifferentDelimiters() throws JSQLParserException {
String statement = "SELECT mytable.mycolumn:parent:child FROM mytable";
PlainSelect parsed = (PlainSelect) assertSqlCanBeParsedAndDeparsed(statement);
Column column = parsed.getSelectItem(0).getExpression(Column.class);
assertEquals(":", column.getTableDelimiter());
assertEquals(List.of(":", "."), column.getTable().getNamePartDelimiters());
Assertions.assertInstanceOf(JsonExpression.class, parsed.getSelectItem(0).getExpression());
}

// I don't know what kind of Operator ".:." shall present
// please rework
@Test
@Disabled
public void testColumnWithEmptyNameParts() throws JSQLParserException {
String statement = "SELECT mytable.:.child FROM mytable";
PlainSelect parsed = (PlainSelect) assertSqlCanBeParsedAndDeparsed(statement);
Expand Down
Loading

0 comments on commit 93ad5b0

Please sign in to comment.