Skip to content

Commit

Permalink
Merge origin/master
Browse files Browse the repository at this point in the history
Conflicts:
	src/test/java/net/sf/jsqlparser/test/select/SelectTest.java
  • Loading branch information
wumpz committed Jan 5, 2017
2 parents cc35549 + 1899cbf commit 335286c
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Also I would like to know about needed examples or documentation stuff.

## Extensions in the latest SNAPSHOT version 0.9.7

* introduced NOT without parenthesis for column only conditions
* introduced more complex expressions within CASE - statements
* improved Postgresql JSON - support
* integrated some Postgresql create table contraints

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,6 @@ public interface ExpressionVisitor {

void visit(DateTimeLiteralExpression literal);

public void visit(NotExpression aThis);

}
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,10 @@ public void visit(Column column) {
@Override
public void visit(SubSelect subSelect) {
if (selectVisitor != null) {
for (WithItem item : subSelect.getWithItemsList()) {
item.accept(selectVisitor);
if (subSelect.getWithItemsList() != null) {
for (WithItem item : subSelect.getWithItemsList()) {
item.accept(selectVisitor);
}
}
subSelect.getSelectBody().accept(selectVisitor);
}
Expand Down Expand Up @@ -339,6 +341,11 @@ public void visit(MultiExpressionList multiExprList) {
}
}

@Override
public void visit(NotExpression notExpr) {
notExpr.getExpression().accept(this);
}

protected void visitBinaryExpression(BinaryExpression expr) {
expr.getLeftExpression().accept(this);
expr.getRightExpression().accept(this);
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/NotExpression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2017 JSQLParser
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package net.sf.jsqlparser.expression;

/**
* It represents a "-" or "+" before an expression
*/
public class NotExpression implements Expression {

private Expression expression;

public NotExpression(Expression expression) {
setExpression(expression);
}

public Expression getExpression() {
return expression;
}

public final void setExpression(Expression expression) {
this.expression = expression;
}

@Override
public void accept(ExpressionVisitor expressionVisitor) {
expressionVisitor.visit(this);
}

@Override
public String toString() {
return "NOT " + expression.toString();
}
}
5 changes: 5 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ public void visit(Subtraction subtraction) {
visitBinaryExpression(subtraction);
}

@Override
public void visit(NotExpression notExpr) {
notExpr.getExpression().accept(this);
}

public void visitBinaryExpression(BinaryExpression binaryExpression) {
binaryExpression.getLeftExpression().accept(this);
binaryExpression.getRightExpression().accept(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public void visit(HexValue hexValue) {
buffer.append(hexValue.toString());
}

@Override
public void visit(NotExpression notExpr) {
buffer.append(NOT);
notExpr.getExpression().accept(this);
}

public void visitOldOracleJoinBinaryExpression(OldOracleJoinBinaryExpression expression, String operator) {
if (expression.isNot()) {
buffer.append(NOT);
Expand Down
5 changes: 3 additions & 2 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,7 @@ Expression Condition():
(LOOKAHEAD(SQLCondition()) result=SQLCondition()
| LOOKAHEAD(RegularCondition()) result=RegularCondition()
| LOOKAHEAD(Function()) result=Function()
| <K_NOT> result=Column() { result = new NotExpression(result); }
| result=Column()
| LOOKAHEAD({ "0".equals(getToken(1).image) || "1".equals(getToken(1).image) }) token=<S_LONG> { result = new LongValue(token.image); }
)
Expand Down Expand Up @@ -2433,8 +2434,8 @@ Expression CaseWhenExpression() #CaseWhenExpression:
( clause=WhenThenSearchCondition() { whenClauses.add(clause); } )+
[<K_ELSE> elseExp=SimpleExpression()]
|
switchExp=PrimaryExpression()
( clause=WhenThenValue() { whenClauses.add(clause); } )*
(LOOKAHEAD(RegularCondition()) switchExp=RegularCondition() | switchExp=BitwiseAndOr())
( clause=WhenThenValue() { whenClauses.add(clause); } )+
[<K_ELSE> elseExp=SimpleExpression()]
)
<K_END>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectVisitorAdapter;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -169,4 +171,19 @@ public void visit(Column column) {
assertEquals(1, columnList.size());
assertEquals("bar", columnList.get(0));
}

@Test
public void testSubSelectExpressionProblem() throws JSQLParserException {
Select select = (Select) CCJSqlParserUtil.parse( "SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t2 WHERE t2.col2 = t1.col1)" );
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
Expression where = plainSelect.getWhere();
ExpressionVisitorAdapter adapter = new ExpressionVisitorAdapter();
adapter.setSelectVisitor(new SelectVisitorAdapter());
try {
where.accept(adapter);
} catch (NullPointerException npe){
fail();
}
}

}
18 changes: 17 additions & 1 deletion src/test/java/net/sf/jsqlparser/test/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,14 @@ public void testCase() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(statement);

}

public void testIssue371SimplifiedCase() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT CASE col + 4 WHEN 2 THEN 1 ELSE 0 END");
}

public void testIssue371SimplifiedCase2() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT CASE col > 4 WHEN true THEN 1 ELSE 0 END");
}

public void testReplaceAsFunction() throws JSQLParserException {
String statement = "SELECT REPLACE(a, 'b', c) FROM tab1";
Expand Down Expand Up @@ -2287,6 +2295,14 @@ public void testProblemIsIssue331() throws JSQLParserException {
}

public void testProblemIssue375() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("select n.nspname, c.relname, a.attname, a.atttypid, t.typname, a.attnum, a.attlen, a.atttypmod, a.attnotnull, c.relhasrules, c.relkind, c.oid, pg_get_expr(d.adbin, d.adrelid), case t.typtype when 'd' then t.typbasetype else 0 end, t.typtypmod, c.relhasoids from (((pg_catalog.pg_class c inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace and c.relname = 'business' and n.nspname = 'public') inner join pg_catalog.pg_attribute a on (not a.attisdropped) and a.attnum > 0 and a.attrelid = c.oid) inner join pg_catalog.pg_type t on t.oid = a.atttypid) left outer join pg_attrdef d on a.atthasdef and d.adrelid = a.attrelid and d.adnum = a.attnum order by n.nspname, c.relname, attnum");
assertSqlCanBeParsedAndDeparsed("select n.nspname, c.relname, a.attname, a.atttypid, t.typname, a.attnum, a.attlen, a.atttypmod, a.attnotnull, c.relhasrules, c.relkind, c.oid, pg_get_expr(d.adbin, d.adrelid), case t.typtype when 'd' then t.typbasetype else 0 end, t.typtypmod, c.relhasoids from (((pg_catalog.pg_class c inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace and c.relname = 'business' and n.nspname = 'public') inner join pg_catalog.pg_attribute a on (not a.attisdropped) and a.attnum > 0 and a.attrelid = c.oid) inner join pg_catalog.pg_type t on t.oid = a.atttypid) left outer join pg_attrdef d on a.atthasdef and d.adrelid = a.attrelid and d.adnum = a.attnum order by n.nspname, c.relname, attnum", true);
}

public void testProblemIssue375Simplified() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("select * from (((pg_catalog.pg_class c inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace and c.relname = 'business' and n.nspname = 'public') inner join pg_catalog.pg_attribute a on (not a.attisdropped) and a.attnum > 0 and a.attrelid = c.oid) inner join pg_catalog.pg_type t on t.oid = a.atttypid) left outer join pg_attrdef d on a.atthasdef and d.adrelid = a.attrelid and d.adnum = a.attnum order by n.nspname, c.relname, attnum", true);
}

public void testProblemIssue375Simplified2() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("select * from (pg_catalog.pg_class c inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace and c.relname = 'business' and n.nspname = 'public') inner join pg_catalog.pg_attribute a on (not a.attisdropped) and a.attnum > 0 and a.attrelid = c.oid", true);
}
}

0 comments on commit 335286c

Please sign in to comment.