Skip to content

Commit

Permalink
TSQL Compliant NEXT VALUE FOR sequence_id (but keeping the spurious N…
Browse files Browse the repository at this point in the history
…EXTVAL FOR expression)
  • Loading branch information
manticore-projects committed May 28, 2021
1 parent 9184cda commit c8d0ebb
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
9 changes: 9 additions & 0 deletions nb-configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,14 @@
<com-junichi11-netbeans-changelf.use-global>false</com-junichi11-netbeans-changelf.use-global>
<org-netbeans-modules-javascript2-requirejs.enabled>true</org-netbeans-modules-javascript2-requirejs.enabled>
<netbeans.hint.jdkPlatform>JDK_1.8</netbeans.hint.jdkPlatform>
<org-netbeans-modules-editor-indent.text.xml.CodeStyle.project.expand-tabs>false</org-netbeans-modules-editor-indent.text.xml.CodeStyle.project.expand-tabs>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.enable-indent>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.enable-indent>
<org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>none</org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>4</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>4</org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>
<org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>4</org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>
<org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>120</org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
<org-netbeans-modules-editor-indent.CodeStyle.usedProfile>project</org-netbeans-modules-editor-indent.CodeStyle.usedProfile>
</properties>
</project-shared-configuration>
30 changes: 26 additions & 4 deletions src/main/java/net/sf/jsqlparser/expression/NextValExpression.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*-
/* -
* #%L
* JSQLParser library
* %%
Expand All @@ -10,14 +10,34 @@
package net.sf.jsqlparser.expression;

import java.util.List;
import java.util.regex.Pattern;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;

public class NextValExpression extends ASTNodeAccessImpl implements Expression {

private List<String> nameList;
public static final Pattern NEXT_VALUE_PATTERN = Pattern.compile("NEXT\\s+VALUE\\s+FOR", Pattern.CASE_INSENSITIVE);
private final List<String> nameList;
private boolean usingNextValueFor = false;

public NextValExpression(List<String> nameList) {
public NextValExpression(List<String> nameList, String image) {
this.nameList = nameList;
// Test if we shall use NEXT VALUE FOR instead of NEXTVAL FOR
if (NEXT_VALUE_PATTERN.matcher(image).matches()) {
usingNextValueFor = true;
}
}

public boolean isUsingNextValueFor() {
return usingNextValueFor;
}

public void setUsingNextValueFor(boolean usingNextValueFor) {
this.usingNextValueFor = usingNextValueFor;
}

public NextValExpression withNextValueFor(boolean usingNextValueFor) {
setUsingNextValueFor(usingNextValueFor);
return this;
}

public List<String> getNameList() {
Expand All @@ -37,7 +57,9 @@ public String getName() {

@Override
public String toString() {
return "NEXTVAL FOR " + getName();
return (usingNextValueFor
? "NEXT VALUE FOR "
: "NEXTVAL FOR ") + getName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ public void visit(DateTimeLiteralExpression literal) {

@Override
public void visit(NextValExpression nextVal) {
buffer.append("NEXTVAL FOR ").append(nextVal.getName());
buffer.append(nextVal.isUsingNextValueFor() ? "NEXT VALUE FOR " : "NEXTVAL FOR ").append(nextVal.getName());
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_MOVEMENT: "MOVEMENT">
| <K_NATURAL:"NATURAL">
| <K_NEXT:"NEXT">
| <K_NEXTVAL:"NEXTVAL">
| <K_NEXTVAL: ( (("NEXTVAL")(" ")+("FOR")) | ( ("NEXT")(" ")+("VALUE") (" ")+("FOR") ) )>
| <K_NO:"NO">
| <K_NOCACHE:"NOCACHE">
| <K_NOCYCLE:"NOCYCLE">
Expand Down Expand Up @@ -3436,11 +3436,12 @@ Expression PrimaryExpression() #PrimaryExpression:

NextValExpression NextValExpression() : {
List<String> data = new ArrayList<String>();
Token token;
}
{
<K_NEXTVAL> <K_FOR> data = RelObjectNameList()
token=<K_NEXTVAL> data = RelObjectNameList()
{
return new NextValExpression(data);
return new NextValExpression(data, token.image);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ReflectionModelTest {
new net.sf.jsqlparser.expression.MySQLGroupConcat(),
new net.sf.jsqlparser.expression.MySQLIndexHint("action", "indexQualifier",
asList("idx_name", "idx_name_col")),
new net.sf.jsqlparser.expression.NextValExpression(asList("sequence")),
new net.sf.jsqlparser.expression.NextValExpression(asList("sequence" ), "NEXT VALUE"),
new net.sf.jsqlparser.expression.NotExpression(),
new net.sf.jsqlparser.expression.NullValue(), new net.sf.jsqlparser.expression.NumericBind(),
new net.sf.jsqlparser.expression.OracleHierarchicalExpression(),
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/insert/InsertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ public void testInsertMultiRowValueDifferent() throws JSQLParserException {
fail("should not work");
}

@Test
@Ignore
public void testOracleInsertMultiRowValue() throws JSQLParserException {
String sqlStr
= "INSERT ALL\n"
+ " INTO suppliers (supplier_id, supplier_name) VALUES (1000, 'IBM')\n"
+ " INTO suppliers (supplier_id, supplier_name) VALUES (2000, 'Microsoft')\n"
+ " INTO suppliers (supplier_id, supplier_name) VALUES (3000, 'Google')\n"
+ "SELECT * FROM dual;";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

@Test
public void testSimpleInsert() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("INSERT INTO example (num, name, address, tel) VALUES (1, 'name', 'test ', '1234-1234')");
Expand Down Expand Up @@ -332,6 +344,11 @@ public void testNextVal() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("INSERT INTO tracker (monitor_id, user_id, module_name, item_id, item_summary, team_id, date_modified, action, visible, id) VALUES (?, ?, ?, ?, ?, ?, to_date(?, 'YYYY-MM-DD HH24:MI:SS'), ?, ?, NEXTVAL FOR TRACKER_ID_SEQ)");
}

@Test
public void testNextValueFor() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("INSERT INTO tracker (monitor_id, user_id, module_name, item_id, item_summary, team_id, date_modified, action, visible, id) VALUES (?, ?, ?, ?, ?, ?, to_date(?, 'YYYY-MM-DD HH24:MI:SS'), ?, ?, NEXT VALUE FOR TRACKER_ID_SEQ)");
}

@Test
public void testNextValIssue773() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("INSERT INTO tableA (ID, c1, c2) SELECT hibernate_sequence.nextval, c1, c2 FROM tableB");
Expand All @@ -358,4 +375,14 @@ public void testOracleHint() throws JSQLParserException {

//@todo: add a testcase supposed to not finding a misplaced hint
}

@Test
public void testInsertTableArrays4() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"INSERT INTO sal_emp\n"
+ " VALUES ('Carol',\n"
+ " ARRAY[20000, 25000, 25000, 25000],\n"
+ " ARRAY[['breakfast', 'consulting'], ['meeting', 'lunch']])",
true);
}
}

0 comments on commit c8d0ebb

Please sign in to comment.