Skip to content

Commit a90c745

Browse files
committed
Merge pull request #193 from packageOk/master
support INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
2 parents ab1ad25 + f86fab7 commit a90c745

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public class Insert implements Statement {
4848
private boolean useDuplicate = false;
4949
private List<Column> duplicateUpdateColumns;
5050
private List<Expression> duplicateUpdateExpressionList;
51+
private String modifierPriority = null;
52+
private boolean modifierIgnore = false;
5153

5254
private boolean returningAllColumns = false;
5355

@@ -156,11 +158,34 @@ public void setDuplicateUpdateExpressionList(List<Expression> duplicateUpdateExp
156158
this.duplicateUpdateExpressionList = duplicateUpdateExpressionList;
157159
}
158160

161+
public String getModifierPriority() {
162+
return modifierPriority;
163+
}
164+
165+
public void setModifierPriority(String modifierPriority) {
166+
this.modifierPriority = modifierPriority;
167+
}
168+
169+
public boolean isModifierIgnore() {
170+
return modifierIgnore;
171+
}
172+
173+
public void setModifierIgnore(boolean modifierIgnore) {
174+
this.modifierIgnore = modifierIgnore;
175+
}
176+
159177
@Override
160178
public String toString() {
161179
StringBuilder sql = new StringBuilder();
162180

163-
sql.append("INSERT INTO ");
181+
sql.append("INSERT ");
182+
if(modifierPriority != null){
183+
sql.append(modifierPriority + " ");
184+
}
185+
if(modifierIgnore){
186+
sql.append("IGNORE ");
187+
}
188+
sql.append("INTO ");
164189
sql.append(table).append(" ");
165190
if (columns != null) {
166191
sql.append(PlainSelect.getStringList(columns, true, true)).append(" ");

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ public void setBuffer(StringBuilder buffer) {
7474
}
7575

7676
public void deParse(Insert insert) {
77-
buffer.append("INSERT INTO ");
77+
buffer.append("INSERT ");
78+
if(insert.getModifierPriority() != null){
79+
buffer.append(insert.getModifierPriority() + " ");
80+
}
81+
if(insert.isModifierIgnore()){
82+
buffer.append("IGNORE ");
83+
}
84+
buffer.append("INTO ");
85+
7886
buffer.append(insert.getTable().getFullyQualifiedName());
7987
if (insert.getColumns() != null) {
8088
buffer.append(" (");

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
228228
| <K_CASCADE: "CASCADE">
229229
| <K_RESTRICT: "RESTRICT">
230230
| <K_DUPLICATE: "DUPLICATE">
231+
| <K_LOW_PRIORITY : "LOW_PRIORITY">
232+
| <K_DELAYED : "DELAYED">
233+
| <K_HIGH_PRIORITY : "HIGH_PRIORITY">
234+
| <K_IGNORE : "IGNORE">
231235
}
232236

233237
TOKEN : /* Numeric Constants */
@@ -467,9 +471,18 @@ Insert Insert():
467471
boolean useDuplicate = false;
468472
List<Column> duplicateUpdateColumns = null;
469473
List<Expression> duplicateUpdateExpressionList = null;
474+
Token tk = null;
475+
String modifierPriority = null;
476+
boolean modifierIgnore = false;
470477
}
471478
{
472-
<K_INSERT> [<K_INTO>] table=Table()
479+
<K_INSERT>
480+
[(tk = <K_LOW_PRIORITY> | tk = <K_DELAYED> | tk = <K_HIGH_PRIORITY>)
481+
{if (tk!=null)
482+
modifierPriority = tk.image.toUpperCase();
483+
}]
484+
[<K_IGNORE>{ modifierIgnore = true; }]
485+
[<K_INTO>] table=Table()
473486

474487

475488
[LOOKAHEAD(2) "(" tableColumn=Column() { columns.add(tableColumn); } ("," tableColumn=Column() { columns.add(tableColumn); } )* ")" ]
@@ -530,6 +543,8 @@ Insert Insert():
530543
insert.setDuplicateUpdateColumns(duplicateUpdateColumns);
531544
insert.setDuplicateUpdateExpressionList(duplicateUpdateExpressionList);
532545
insert.setReturningExpressionList(returning);
546+
insert.setModifierPriority(modifierPriority);
547+
insert.setModifierIgnore(modifierIgnore);
533548
return insert;
534549
}
535550
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,15 @@ public void testHexValues3() throws JSQLParserException {
152152
public void testDuplicateKey() throws JSQLParserException {
153153
assertSqlCanBeParsedAndDeparsed("INSERT INTO Users0 (UserId, Key, Value) VALUES (51311, 'T_211', 18) ON DUPLICATE KEY UPDATE Value = 18");
154154
}
155+
156+
@Test
157+
public void testModifierIgnore() throws JSQLParserException {
158+
assertSqlCanBeParsedAndDeparsed("INSERT IGNORE INTO `AoQiSurvey_FlashVersion_Single` VALUES (302215163, 'WIN 16,0,0,235')");
159+
}
160+
161+
@Test
162+
public void testModifierPriority() throws JSQLParserException {
163+
assertSqlCanBeParsedAndDeparsed("INSERT DELAYED INTO kvPair (value, key) VALUES (?, ?)");
164+
assertSqlCanBeParsedAndDeparsed("INSERT LOW_PRIORITY INTO kvPair (value, key) VALUES (?, ?)");
165+
}
155166
}

0 commit comments

Comments
 (0)