Skip to content

Commit

Permalink
Merge origin/master
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Aug 14, 2019
2 parents 30619d8 + c481ce0 commit 22c6eb0
Show file tree
Hide file tree
Showing 18 changed files with 272 additions and 54 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Please provide feedback on:
* Is there any need for a Java 7 JSqlParser build, or can we move on to at least Java 8? (https://github.com/JSQLParser/JSqlParser/issues/814)

## News
* The array parsing is the default behaviour. Square bracket quotation has to be enabled using
a parser flag (**CCJSqlParser.withSquareBracketQuotation**).
* due to an API change the version will be 3.0
* JSqlParser uses now Java 8 at the minimum
* Released version **2.1** of JSqlParser
* Released version **2.0** of JSqlParser
Expand Down Expand Up @@ -60,8 +63,11 @@ To help JSqlParser's development you are encouraged to provide

Also I would like to know about needed examples or documentation stuff.

## Extensions in the latest SNAPSHOT version 2.2
## Extensions in the latest SNAPSHOT version 3.0

* support for array constructs using square brackets. This collides with square bracket
quotation of SqlServer. The parser has now a flag to enable this quotation again (**CCJSqlParser.withSquareBracketQuotation**).
* support for **update table1 inner join table2 ...** (API change)
* support for **declare** statement
* allow empty double quotes
* allow **year**, **month** ... as column data type for **create table**
Expand All @@ -88,7 +94,7 @@ As the project is a Maven project, building is rather simple by running:

The project requires the following to build:
- Maven
- JDK 1.7 or later. The jar will target JDK 1.6, but the version of the maven-compiler-plugin that JsqlParser uses requires JDK 1.7+
- JDK 8 or later. The jar will target JDK 8, but the version of the maven-compiler-plugin that JsqlParser uses requires JDK 8+

This will produce the jsqlparser-VERSION.jar file in the target/ directory.

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>2.2-SNAPSHOT</version>
<version>3.0-SNAPSHOT</version>
<name>JSQLParser library</name>
<inceptionYear>2004</inceptionYear>
<organization>
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/ArrayExpression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 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;

public class ArrayExpression extends ASTNodeAccessImpl implements Expression {

private Expression objExpression;
private Expression indexExpression;

public ArrayExpression(Expression objExpression, Expression indexExpression) {
this.objExpression = objExpression;
this.indexExpression = indexExpression;
}

public Expression getObjExpression() {
return objExpression;
}

public void setObjExpression(Expression objExpression) {
this.objExpression = objExpression;
}

public Expression getIndexExpression() {
return indexExpression;
}

public void setIndexExpression(Expression indexExpression) {
this.indexExpression = indexExpression;
}

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

@Override
public String toString() {
return objExpression.toString() + "[" + indexExpression.toString() + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,6 @@ public interface ExpressionVisitor {

public void visit(SimilarToExpression aThis);

public void visit(ArrayExpression aThis);

}
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,10 @@ public void visit(CollateExpression col) {
public void visit(SimilarToExpression expr) {
visitBinaryExpression(expr);
}

@Override
public void visit(ArrayExpression array) {
array.getObjExpression().accept(this);
array.getIndexExpression().accept(this);
}
}
11 changes: 10 additions & 1 deletion src/main/java/net/sf/jsqlparser/parser/CCJSqlParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.InputStream;
import java.io.Reader;
import java.util.function.Consumer;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.statement.Statement;
Expand All @@ -36,7 +37,14 @@ public static Statement parse(Reader statementReader) throws JSQLParserException
}

public static Statement parse(String sql) throws JSQLParserException {
return parse(sql, null);
}

public static Statement parse(String sql, Consumer<CCJSqlParser> consumer) throws JSQLParserException {
CCJSqlParser parser = new CCJSqlParser(new StringProvider(sql));
if (consumer != null) {
consumer.accept(parser);
}
try {
return parser.Statement();
} catch (Exception ex) {
Expand Down Expand Up @@ -96,7 +104,8 @@ public static Expression parseCondExpression(String condExpr) throws JSQLParserE
}

/**
* Parse an conditional expression. This is the expression after a where clause.
* Parse an conditional expression. This is the expression after a where
* clause.
*
* @param condExpr
* @param allowPartialParse false: needs the whole string to be processed.
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/net/sf/jsqlparser/parser/SimpleCharStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,22 @@ public char[] GetSuffix(int len) {

char[] ret = new char[len];

if ((bufpos + 1) >= len) {
System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
if (isStringProvider) {
String str = ((StringProvider) inputStream)._string;
if ((bufpos + 1) >= len) {
str.getChars(bufpos - len + 1, bufpos - len + 1 + len, ret, 0);
} else {
str.getChars(bufsize - (len - bufpos - 1), bufsize - (len - bufpos - 1) + len - bufpos - 1, ret, 0);
str.getChars(0, bufpos + 1, ret, len - bufpos - 1);
}
} else {
System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
len - bufpos - 1);
System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
if ((bufpos + 1) >= len) {
System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
} else {
System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
len - bufpos - 1);
System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
}
}

return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class CreateView implements Statement {
private boolean materialized = false;
private ForceOption force = ForceOption.NONE;
private TemporaryOption temp = TemporaryOption.NONE;
private boolean withReadOnly = false;

@Override
public void accept(StatementVisitor statementVisitor) {
Expand Down Expand Up @@ -90,6 +91,14 @@ public void setTemporary(TemporaryOption temp) {
this.temp = temp;
}

public boolean isWithReadOnly() {
return withReadOnly;
}

public void setWithReadOnly(boolean withReadOnly) {
this.withReadOnly = withReadOnly;
}

@Override
public String toString() {
StringBuilder sql = new StringBuilder("CREATE ");
Expand All @@ -104,11 +113,11 @@ public String toString() {
sql.append("NO FORCE ");
break;
}

if (temp != TemporaryOption.NONE) {
sql.append(temp.name()).append(" ");
}

if (isMaterialized()) {
sql.append("MATERIALIZED ");
}
Expand All @@ -118,6 +127,9 @@ public String toString() {
sql.append(PlainSelect.getStringList(columnNames, true, true));
}
sql.append(" AS ").append(select);
if (isWithReadOnly()) {
sql.append(" WITH READ ONLY");
}
return sql.toString();
}
}
31 changes: 25 additions & 6 deletions src/main/java/net/sf/jsqlparser/statement/update/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@

public class Update implements Statement {

private List<Table> tables;
private Table table;
private Expression where;
private List<Column> columns;
private List<Expression> expressions;
private FromItem fromItem;
private List<Join> joins;
private List<Join> startJoins;
private Select select;
private boolean useColumnsBrackets = true;
private boolean useSelect = false;
Expand All @@ -45,16 +46,16 @@ public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
}

public List<Table> getTables() {
return tables;
public Table getTable() {
return table;
}

public Expression getWhere() {
return where;
}

public void setTables(List<Table> list) {
tables = list;
public void setTable(Table table) {
this.table = table;
}

public void setWhere(Expression expression) {
Expand Down Expand Up @@ -93,6 +94,14 @@ public void setJoins(List<Join> joins) {
this.joins = joins;
}

public List<Join> getStartJoins() {
return startJoins;
}

public void setStartJoins(List<Join> startJoins) {
this.startJoins = startJoins;
}

public Select getSelect() {
return select;
}
Expand Down Expand Up @@ -152,7 +161,17 @@ public void setReturningExpressionList(List<SelectExpressionItem> returningExpre
@Override
public String toString() {
StringBuilder b = new StringBuilder("UPDATE ");
b.append(PlainSelect.getStringList(getTables(), true, false)).append(" SET ");
b.append(table);
if (startJoins != null) {
for (Join join : startJoins) {
if (join.isSimple()) {
b.append(", ").append(join);
} else {
b.append(" ").append(join);
}
}
}
b.append(" SET ");

if (!useSelect) {
for (int i = 0; i < getColumns().size(); i++) {
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.sf.jsqlparser.expression.AllComparisonExpression;
import net.sf.jsqlparser.expression.AnalyticExpression;
import net.sf.jsqlparser.expression.AnyComparisonExpression;
import net.sf.jsqlparser.expression.ArrayExpression;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.CaseExpression;
import net.sf.jsqlparser.expression.CastExpression;
Expand Down Expand Up @@ -623,8 +624,11 @@ public void visit(Delete delete) {

@Override
public void visit(Update update) {
for (Table table : update.getTables()) {
visit(table);
visit(update.getTable());
if (update.getStartJoins() != null) {
for (Join join : update.getStartJoins()) {
join.getRightItem().accept(this);
}
}
if (update.getExpressions() != null) {
for (Expression expression : update.getExpressions()) {
Expand Down Expand Up @@ -851,4 +855,10 @@ public void visit(SimilarToExpression expr) {
@Override
public void visit(DeclareStatement aThis) {
}

@Override
public void visit(ArrayExpression array) {
array.getObjExpression().accept(this);
array.getIndexExpression().accept(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public void deParse(CreateView createView) {
buffer.append(" ");
}
createView.getSelect().getSelectBody().accept(selectVisitor);
if (createView.isWithReadOnly()) {
buffer.append(" WITH READ ONLY");
}
}

public StringBuilder getBuffer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.sf.jsqlparser.expression.AllComparisonExpression;
import net.sf.jsqlparser.expression.AnalyticExpression;
import net.sf.jsqlparser.expression.AnyComparisonExpression;
import net.sf.jsqlparser.expression.ArrayExpression;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.CaseExpression;
import net.sf.jsqlparser.expression.CastExpression;
Expand Down Expand Up @@ -808,4 +809,11 @@ public void visit(SimilarToExpression expr) {
visitBinaryExpression(expr, (expr.isNot() ? " NOT" : "") + " SIMILAR TO ");
}

@Override
public void visit(ArrayExpression array) {
array.getObjExpression().accept(this);
buffer.append("[");
array.getIndexExpression().accept(this);
buffer.append("]");
}
}
14 changes: 11 additions & 3 deletions src/main/java/net/sf/jsqlparser/util/deparser/UpdateDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectVisitor;
Expand Down Expand Up @@ -49,8 +48,17 @@ public void setBuffer(StringBuilder buffer) {
}

public void deParse(Update update) {
buffer.append("UPDATE ").append(PlainSelect.getStringList(update.getTables(), true, false)).
append(" SET ");
buffer.append("UPDATE ").append(update.getTable());
if (update.getStartJoins() != null) {
for (Join join : update.getStartJoins()) {
if (join.isSimple()) {
buffer.append(", ").append(join);
} else {
buffer.append(" ").append(join);
}
}
}
buffer.append(" SET ");

if (!update.isUseSelect()) {
for (int i = 0; i < update.getColumns().size(); i++) {
Expand Down
Loading

0 comments on commit 22c6eb0

Please sign in to comment.