Skip to content

Commit 56af725

Browse files
committed
Add support for insert as row_alias
1 parent ededd86 commit 56af725

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

src/main/java/net/sf/jsqlparser/statement/insert/Insert.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package net.sf.jsqlparser.statement.insert;
1111

12+
import net.sf.jsqlparser.expression.Alias;
1213
import net.sf.jsqlparser.expression.OracleHint;
1314
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
1415
import net.sf.jsqlparser.schema.Column;
@@ -53,6 +54,7 @@ public class Insert implements Statement {
5354
private InsertConflictTarget conflictTarget;
5455
private InsertConflictAction conflictAction;
5556
private InsertDuplicateAction duplicateAction;
57+
private Alias rowAlias;
5658

5759
public List<UpdateSet> getDuplicateUpdateSets() {
5860
if (duplicateAction != null) {
@@ -340,6 +342,9 @@ public String toString() {
340342
if (setUpdateSets != null && !setUpdateSets.isEmpty()) {
341343
sql.append("SET ");
342344
sql = UpdateSet.appendUpdateSetsTo(sql, setUpdateSets);
345+
if (rowAlias != null) {
346+
sql.append(" ").append(rowAlias);
347+
}
343348
}
344349

345350
if (duplicateAction != null) {
@@ -411,4 +416,12 @@ public InsertDuplicateAction getDuplicateAction() {
411416
public void setDuplicateAction(InsertDuplicateAction duplicateAction) {
412417
this.duplicateAction = duplicateAction;
413418
}
419+
420+
public Alias getRowAlias() {
421+
return rowAlias;
422+
}
423+
424+
public void setRowAlias(Alias rowAlias) {
425+
this.rowAlias = rowAlias;
426+
}
414427
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ public void deParse(Insert insert) {
113113
if (insert.getSetUpdateSets() != null) {
114114
builder.append(" SET ");
115115
deparseUpdateSets(insert.getSetUpdateSets(), builder, expressionVisitor);
116+
if (insert.getRowAlias() != null) {
117+
builder.append(" ").append(insert.getRowAlias());
118+
}
116119
}
117120

118121
if (insert.getDuplicateAction() != null) {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2809,6 +2809,8 @@ Insert Insert():
28092809

28102810
String name = null;
28112811
boolean useAs = false;
2812+
boolean useSet = false;
2813+
Alias rowAlias = null;
28122814
OutputClause outputClause = null;
28132815

28142816
InsertConflictTarget conflictTarget = null;
@@ -2846,12 +2848,20 @@ Insert Insert():
28462848
<K_DEFAULT> <K_VALUES> { insert.setOnlyDefaultValues(true); }
28472849
|
28482850
(
2849-
<K_SET> updateSets = UpdateSets() { insert.withSetUpdateSets(updateSets); }
2851+
<K_SET> updateSets = UpdateSets() { insert.withSetUpdateSets(updateSets); useSet = true; }
28502852
)
28512853
|
28522854
select = Select()
28532855
)
28542856

2857+
[ LOOKAHEAD(2, { select instanceof Values || useSet }) rowAlias = Alias() {
2858+
if (select instanceof Values) {
2859+
select.setAlias(rowAlias);
2860+
} else {
2861+
insert.setRowAlias(rowAlias);
2862+
}
2863+
} ]
2864+
28552865
[ LOOKAHEAD(2) <K_ON> <K_DUPLICATE> <K_KEY> <K_UPDATE>
28562866
duplicateAction = InsertDuplicateAction() { insert.setDuplicateAction(duplicateAction); }
28572867
]

src/test/java/net/sf/jsqlparser/statement/insert/InsertTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,22 @@ public void testInsertValuesWithDuplicateEliminationInDeparsing() throws JSQLPar
395395
+ "ON DUPLICATE KEY UPDATE COUNTER = COUNTER + 1");
396396
}
397397

398+
@Test
399+
public void testInsertValuesAliasWithDuplicateEliminationIssue() throws JSQLParserException {
400+
assertSqlCanBeParsedAndDeparsed("INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4,5,6) AS new"
401+
+ " ON DUPLICATE KEY UPDATE c = new.a+new.b;");
402+
403+
assertSqlCanBeParsedAndDeparsed("INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4,5,6) AS new(m,n,p) "
404+
+ " ON DUPLICATE KEY UPDATE c = m+n;");
405+
}
406+
398407
@Test
399408
public void testInsertSetWithDuplicateEliminationInDeparsing() throws JSQLParserException {
400409
assertSqlCanBeParsedAndDeparsed("INSERT INTO mytable SET col1 = 122 "
401410
+ "ON DUPLICATE KEY UPDATE col2 = col2 + 1, col3 = 'saint'");
411+
412+
assertSqlCanBeParsedAndDeparsed("INSERT INTO t1 SET a=1,b=2,c=3 AS new"
413+
+ " ON DUPLICATE KEY UPDATE c = new.a+new.b;");
402414
}
403415

404416
@Test

0 commit comments

Comments
 (0)