Skip to content

Commit

Permalink
fixes #826
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Aug 9, 2019
1 parent 9bea1e3 commit 01296c3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 21 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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
* 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 +61,9 @@ 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 **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 Down
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
7 changes: 5 additions & 2 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,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
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
10 changes: 6 additions & 4 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ Update Update():
{
Update update = new Update();
Table table = null;
List<Table> tables = new ArrayList<Table>();
List<Join> startJoins = null;
Expression where = null;
Column tableColumn = null;
List<Expression> expList = new ArrayList<Expression>();
Expand All @@ -644,8 +644,9 @@ Update Update():
List<SelectExpressionItem> returning = null;
}
{
<K_UPDATE> table=TableWithAlias() { tables.add(table); }
("," table=TableWithAlias() { tables.add(table); } )*
<K_UPDATE> table=TableWithAlias()
startJoins=JoinsList()

<K_SET>
(
LOOKAHEAD(3) tableColumn=Column() "=" value=SimpleExpression() { columns.add(tableColumn); expList.add(value); }
Expand Down Expand Up @@ -681,7 +682,8 @@ Update Update():
{
update.setColumns(columns);
update.setExpressions(expList);
update.setTables(tables);
update.setTable(table);
update.setStartJoins(startJoins);
update.setFromItem(fromItem);
update.setJoins(joins);
update.setSelect(select);
Expand Down
21 changes: 16 additions & 5 deletions src/test/java/net/sf/jsqlparser/statement/update/UpdateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class UpdateTest {
public void testUpdate() throws JSQLParserException {
String statement = "UPDATE mytable set col1='as', col2=?, col3=565 Where o >= 3";
Update update = (Update) parserManager.parse(new StringReader(statement));
assertEquals("mytable", update.getTables().get(0).getName());
assertEquals("mytable", update.getTable().toString());
assertEquals(3, update.getColumns().size());
assertEquals("col1", ((Column) update.getColumns().get(0)).getColumnName());
assertEquals("col2", ((Column) update.getColumns().get(1)).getColumnName());
Expand Down Expand Up @@ -119,24 +119,35 @@ public void testUpdateDoesNotAllowLimitOffset() throws JSQLParserException {
public void testUpdateWithFunctions() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("UPDATE tablename SET col = SUBSTRING(col2, 1, 2)");
}

@Test
public void testUpdateIssue508LeftShift() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("UPDATE user SET num = 1 << 1 WHERE id = 1");
}

@Test
public void testUpdateIssue338() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("UPDATE mytable SET status = (status & ~1)");
}

@Test
public void testUpdateIssue338_1() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("UPDATE mytable SET status = (status & 1)");
}

@Test
public void testUpdateIssue338_2() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("UPDATE mytable SET status = (status + 1)");
}

@Test
public void testUpdateIssue826() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("update message_topic inner join message_topic_config on\n"
+ " message_topic.id=message_topic_config.topic_id \n"
+ "set message_topic_config.enable_flag='N', \n"
+ "message_topic_config.updated_by='test', \n"
+ "message_topic_config.update_at='2019-07-16' \n"
+ "where message_topic.name='test' \n"
+ "AND message_topic_config.enable_flag='Y'", true);
}
}

0 comments on commit 01296c3

Please sign in to comment.