Skip to content

Commit 9ce74e2

Browse files
committed
fixes #915
1 parent 8abd6e7 commit 9ce74e2

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

src/main/java/net/sf/jsqlparser/expression/SQLServerHints.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
*/
1010
package net.sf.jsqlparser.expression;
1111

12+
import java.util.ArrayList;
13+
import java.util.List;
14+
1215
public class SQLServerHints {
1316

1417
private Boolean noLock;
18+
private String indexName;
1519

1620
public SQLServerHints() {
1721
}
@@ -29,10 +33,25 @@ public void setNoLock(Boolean noLock) {
2933
this.noLock = noLock;
3034
}
3135

36+
public String getIndexName() {
37+
return indexName;
38+
}
39+
40+
public void setIndexName(String indexName) {
41+
this.indexName = indexName;
42+
}
43+
3244
@Override
3345
public String toString() {
34-
return " WITH("
35-
+ (noLock != null ? "nolock" : "")
46+
List<String> hints = new ArrayList<>();
47+
if (indexName != null) {
48+
hints.add("INDEX (" + indexName + ")");
49+
}
50+
if (Boolean.TRUE.equals(noLock)) {
51+
hints.add("NOLOCK");
52+
}
53+
return " WITH ("
54+
+ String.join(", ", hints)
3655
+ ")";
3756
}
3857
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,13 +1502,21 @@ Alias Alias():
15021502
{ return alias; }
15031503
}
15041504

1505+
void SQLServerHint(SQLServerHints hints) : {
1506+
String str;
1507+
}
1508+
{
1509+
<K_INDEX> "(" str = RelObjectName() ")" { hints.setIndexName(str); }
1510+
|
1511+
<K_NOLOCK> { hints.withNoLock(); }
1512+
}
1513+
15051514
SQLServerHints SQLServerHints() : {
15061515
SQLServerHints hints = new SQLServerHints();
15071516
}
15081517
{
15091518
<K_WITH> "("
1510-
//at the moment only nolock
1511-
<K_NOLOCK> { hints.withNoLock(); }
1519+
SQLServerHint(hints) ("," SQLServerHint(hints) )*
15121520
")"
15131521
{ return hints; }
15141522
}

src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3215,7 +3215,17 @@ public void testMysqlMultipleIndexHints() throws JSQLParserException {
32153215

32163216
@Test
32173217
public void testSqlServerHints() throws JSQLParserException {
3218-
assertSqlCanBeParsedAndDeparsed("SELECT * FROM TB_Sys_Pedido WITH(nolock) WHERE ID_Pedido = :ID_Pedido");
3218+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM TB_Sys_Pedido WITH (NOLOCK) WHERE ID_Pedido = :ID_Pedido");
3219+
}
3220+
3221+
@Test
3222+
public void testSqlServerHintsWithIndexIssue915() throws JSQLParserException {
3223+
assertSqlCanBeParsedAndDeparsed("SELECT 1 FROM tableName1 WITH (INDEX (idx1), NOLOCK)");
3224+
}
3225+
3226+
@Test
3227+
public void testSqlServerHintsWithIndexIssue915_2() throws JSQLParserException {
3228+
assertSqlCanBeParsedAndDeparsed("SELECT 1 FROM tableName1 AS t1 WITH (INDEX (idx1)) JOIN tableName2 AS t2 WITH (INDEX (idx2)) ON t1.id = t2.id");
32193229
}
32203230

32213231
@Test

0 commit comments

Comments
 (0)