Skip to content

Commit 01296c3

Browse files
committed
fixes #826
1 parent 9bea1e3 commit 01296c3

File tree

6 files changed

+66
-21
lines changed

6 files changed

+66
-21
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Please provide feedback on:
2323
* 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)
2424

2525
## News
26+
* due to an API change the version will be 3.0
2627
* JSqlParser uses now Java 8 at the minimum
2728
* Released version **2.1** of JSqlParser
2829
* Released version **2.0** of JSqlParser
@@ -60,8 +61,9 @@ To help JSqlParser's development you are encouraged to provide
6061

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

63-
## Extensions in the latest SNAPSHOT version 2.2
64+
## Extensions in the latest SNAPSHOT version 3.0
6465

66+
* support for **update table1 inner join table2 ...** (API change)
6567
* support for **declare** statement
6668
* allow empty double quotes
6769
* allow **year**, **month** ... as column data type for **create table**

src/main/java/net/sf/jsqlparser/statement/update/Update.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626

2727
public class Update implements Statement {
2828

29-
private List<Table> tables;
29+
private Table table;
3030
private Expression where;
3131
private List<Column> columns;
3232
private List<Expression> expressions;
3333
private FromItem fromItem;
3434
private List<Join> joins;
35+
private List<Join> startJoins;
3536
private Select select;
3637
private boolean useColumnsBrackets = true;
3738
private boolean useSelect = false;
@@ -45,16 +46,16 @@ public void accept(StatementVisitor statementVisitor) {
4546
statementVisitor.visit(this);
4647
}
4748

48-
public List<Table> getTables() {
49-
return tables;
49+
public Table getTable() {
50+
return table;
5051
}
5152

5253
public Expression getWhere() {
5354
return where;
5455
}
5556

56-
public void setTables(List<Table> list) {
57-
tables = list;
57+
public void setTable(Table table) {
58+
this.table = table;
5859
}
5960

6061
public void setWhere(Expression expression) {
@@ -93,6 +94,14 @@ public void setJoins(List<Join> joins) {
9394
this.joins = joins;
9495
}
9596

97+
public List<Join> getStartJoins() {
98+
return startJoins;
99+
}
100+
101+
public void setStartJoins(List<Join> startJoins) {
102+
this.startJoins = startJoins;
103+
}
104+
96105
public Select getSelect() {
97106
return select;
98107
}
@@ -152,7 +161,17 @@ public void setReturningExpressionList(List<SelectExpressionItem> returningExpre
152161
@Override
153162
public String toString() {
154163
StringBuilder b = new StringBuilder("UPDATE ");
155-
b.append(PlainSelect.getStringList(getTables(), true, false)).append(" SET ");
164+
b.append(table);
165+
if (startJoins != null) {
166+
for (Join join : startJoins) {
167+
if (join.isSimple()) {
168+
b.append(", ").append(join);
169+
} else {
170+
b.append(" ").append(join);
171+
}
172+
}
173+
}
174+
b.append(" SET ");
156175

157176
if (!useSelect) {
158177
for (int i = 0; i < getColumns().size(); i++) {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,11 @@ public void visit(Delete delete) {
623623

624624
@Override
625625
public void visit(Update update) {
626-
for (Table table : update.getTables()) {
627-
visit(table);
626+
visit(update.getTable());
627+
if (update.getStartJoins() != null) {
628+
for (Join join : update.getStartJoins()) {
629+
join.getRightItem().accept(this);
630+
}
628631
}
629632
if (update.getExpressions() != null) {
630633
for (Expression expression : update.getExpressions()) {

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
1717
import net.sf.jsqlparser.schema.Column;
1818
import net.sf.jsqlparser.statement.select.Join;
19-
import net.sf.jsqlparser.statement.select.PlainSelect;
2019
import net.sf.jsqlparser.statement.update.Update;
2120
import net.sf.jsqlparser.statement.select.Select;
2221
import net.sf.jsqlparser.statement.select.SelectVisitor;
@@ -49,8 +48,17 @@ public void setBuffer(StringBuilder buffer) {
4948
}
5049

5150
public void deParse(Update update) {
52-
buffer.append("UPDATE ").append(PlainSelect.getStringList(update.getTables(), true, false)).
53-
append(" SET ");
51+
buffer.append("UPDATE ").append(update.getTable());
52+
if (update.getStartJoins() != null) {
53+
for (Join join : update.getStartJoins()) {
54+
if (join.isSimple()) {
55+
buffer.append(", ").append(join);
56+
} else {
57+
buffer.append(" ").append(join);
58+
}
59+
}
60+
}
61+
buffer.append(" SET ");
5462

5563
if (!update.isUseSelect()) {
5664
for (int i = 0; i < update.getColumns().size(); i++) {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ Update Update():
629629
{
630630
Update update = new Update();
631631
Table table = null;
632-
List<Table> tables = new ArrayList<Table>();
632+
List<Join> startJoins = null;
633633
Expression where = null;
634634
Column tableColumn = null;
635635
List<Expression> expList = new ArrayList<Expression>();
@@ -644,8 +644,9 @@ Update Update():
644644
List<SelectExpressionItem> returning = null;
645645
}
646646
{
647-
<K_UPDATE> table=TableWithAlias() { tables.add(table); }
648-
("," table=TableWithAlias() { tables.add(table); } )*
647+
<K_UPDATE> table=TableWithAlias()
648+
startJoins=JoinsList()
649+
649650
<K_SET>
650651
(
651652
LOOKAHEAD(3) tableColumn=Column() "=" value=SimpleExpression() { columns.add(tableColumn); expList.add(value); }
@@ -681,7 +682,8 @@ Update Update():
681682
{
682683
update.setColumns(columns);
683684
update.setExpressions(expList);
684-
update.setTables(tables);
685+
update.setTable(table);
686+
update.setStartJoins(startJoins);
685687
update.setFromItem(fromItem);
686688
update.setJoins(joins);
687689
update.setSelect(select);

src/test/java/net/sf/jsqlparser/statement/update/UpdateTest.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class UpdateTest {
3131
public void testUpdate() throws JSQLParserException {
3232
String statement = "UPDATE mytable set col1='as', col2=?, col3=565 Where o >= 3";
3333
Update update = (Update) parserManager.parse(new StringReader(statement));
34-
assertEquals("mytable", update.getTables().get(0).getName());
34+
assertEquals("mytable", update.getTable().toString());
3535
assertEquals(3, update.getColumns().size());
3636
assertEquals("col1", ((Column) update.getColumns().get(0)).getColumnName());
3737
assertEquals("col2", ((Column) update.getColumns().get(1)).getColumnName());
@@ -119,24 +119,35 @@ public void testUpdateDoesNotAllowLimitOffset() throws JSQLParserException {
119119
public void testUpdateWithFunctions() throws JSQLParserException {
120120
assertSqlCanBeParsedAndDeparsed("UPDATE tablename SET col = SUBSTRING(col2, 1, 2)");
121121
}
122-
122+
123123
@Test
124124
public void testUpdateIssue508LeftShift() throws JSQLParserException {
125125
assertSqlCanBeParsedAndDeparsed("UPDATE user SET num = 1 << 1 WHERE id = 1");
126126
}
127-
127+
128128
@Test
129129
public void testUpdateIssue338() throws JSQLParserException {
130130
assertSqlCanBeParsedAndDeparsed("UPDATE mytable SET status = (status & ~1)");
131131
}
132-
132+
133133
@Test
134134
public void testUpdateIssue338_1() throws JSQLParserException {
135135
assertSqlCanBeParsedAndDeparsed("UPDATE mytable SET status = (status & 1)");
136136
}
137-
137+
138138
@Test
139139
public void testUpdateIssue338_2() throws JSQLParserException {
140140
assertSqlCanBeParsedAndDeparsed("UPDATE mytable SET status = (status + 1)");
141141
}
142+
143+
@Test
144+
public void testUpdateIssue826() throws JSQLParserException {
145+
assertSqlCanBeParsedAndDeparsed("update message_topic inner join message_topic_config on\n"
146+
+ " message_topic.id=message_topic_config.topic_id \n"
147+
+ "set message_topic_config.enable_flag='N', \n"
148+
+ "message_topic_config.updated_by='test', \n"
149+
+ "message_topic_config.update_at='2019-07-16' \n"
150+
+ "where message_topic.name='test' \n"
151+
+ "AND message_topic_config.enable_flag='Y'", true);
152+
}
142153
}

0 commit comments

Comments
 (0)