Skip to content

Commit 6a414aa

Browse files
committed
introduces sql server hints
1 parent f12bb31 commit 6a414aa

File tree

4 files changed

+81
-9
lines changed

4 files changed

+81
-9
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.expression;
11+
12+
public class SQLServerHints {
13+
14+
private Boolean noLock;
15+
16+
public SQLServerHints() {
17+
}
18+
19+
public SQLServerHints withNoLock() {
20+
this.noLock = true;
21+
return this;
22+
}
23+
24+
public Boolean getNoLock() {
25+
return noLock;
26+
}
27+
28+
public void setNoLock(Boolean noLock) {
29+
this.noLock = noLock;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return " WITH("
35+
+ (noLock != null ? "nolock" : "")
36+
+ ")";
37+
}
38+
}

src/main/java/net/sf/jsqlparser/schema/Table.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public class Table extends ASTNodeAccessImpl implements FromItem, MultiPartName
3434
private Alias alias;
3535
private Pivot pivot;
3636
private UnPivot unpivot;
37-
private MySQLIndexHint hint;
37+
private MySQLIndexHint mysqlHints;
38+
private SQLServerHints sqlServerHints;
3839

3940
public Table() {
4041
}
@@ -160,11 +161,19 @@ public void setUnPivot(UnPivot unpivot) {
160161
}
161162

162163
public MySQLIndexHint getIndexHint() {
163-
return hint;
164+
return mysqlHints;
164165
}
165166

166167
public void setHint(MySQLIndexHint hint) {
167-
this.hint = hint;
168+
this.mysqlHints = hint;
169+
}
170+
171+
public SQLServerHints getSqlServerHints() {
172+
return sqlServerHints;
173+
}
174+
175+
public void setSqlServerHints(SQLServerHints sqlServerHints) {
176+
this.sqlServerHints = sqlServerHints;
168177
}
169178

170179
@Override
@@ -173,6 +182,7 @@ public String toString() {
173182
+ ((alias != null) ? alias.toString() : "")
174183
+ ((pivot != null) ? " " + pivot : "")
175184
+ ((unpivot != null) ? " " + unpivot : "")
176-
+ ((hint != null) ? hint.toString() : "");
185+
+ ((mysqlHints != null) ? mysqlHints.toString() : "")
186+
+ ((sqlServerHints != null) ? sqlServerHints.toString() : "");
177187
}
178188
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ public void visit(Table tableName) {
238238
if (indexHint != null) {
239239
buffer.append(indexHint);
240240
}
241+
SQLServerHints sqlServerHints = tableName.getSqlServerHints();
242+
if (sqlServerHints != null) {
243+
buffer.append(sqlServerHints);
244+
}
241245
}
242246

243247
@Override

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
229229
| <K_NEXTVAL:"NEXTVAL">
230230
| <K_NO:"NO">
231231
| <K_NOCYCLE:"NOCYCLE">
232+
| <K_NOLOCK:"NOLOCK">
232233
| <K_NOT:"NOT">
233234
| <K_NOVALIDATE : "NOVALIDATE">
234235
| <K_NULL:"NULL">
@@ -1136,7 +1137,7 @@ String RelObjectNameWithoutValue() :
11361137
| tk=<K_ZONE> | tk=<K_COLUMNS> | tk=<K_DESCRIBE> | tk=<K_FN> | tk=<K_PATH>
11371138
| tk=<K_DATE_LITERAL> | tk=<K_NEXTVAL> | tk=<K_TRUE> | tk=<K_FALSE> | tk=<K_DUPLICATE>
11381139
| tk=<K_READ> | tk=<K_SIZE> | tk=<K_SESSION>
1139-
| tk=<K_VIEW>
1140+
| tk=<K_VIEW> | <K_NOLOCK>
11401141
/* | tk=<K_PLACING> | tk=<K_BOTH> | tk=<K_LEADING> | tk=<K_TRAILING> */
11411142
)
11421143

@@ -1501,6 +1502,17 @@ Alias Alias():
15011502
{ return alias; }
15021503
}
15031504

1505+
SQLServerHints SQLServerHints() : {
1506+
SQLServerHints hints = new SQLServerHints();
1507+
}
1508+
{
1509+
<K_WITH> "("
1510+
//at the moment only nolock
1511+
<K_NOLOCK> { hints.withNoLock(); }
1512+
")"
1513+
{ return hints; }
1514+
}
1515+
15041516
MySQLIndexHint MySQLIndexHint():
15051517
{
15061518
Token actionToken = null;
@@ -1711,6 +1723,7 @@ FromItem FromItem():
17111723
UnPivot unpivot = null;
17121724
Alias alias = null;
17131725
MySQLIndexHint indexHint = null;
1726+
SQLServerHints sqlServerHints = null;
17141727
}
17151728
{
17161729
(
@@ -1746,10 +1759,17 @@ FromItem FromItem():
17461759
[(LOOKAHEAD(2) pivot=PivotXml()|pivot=Pivot()) { fromItem.setPivot(pivot); } ]
17471760
[
17481761
LOOKAHEAD(2)
1749-
indexHint=MySQLIndexHint() {
1750-
if (fromItem instanceof Table)
1751-
((Table) fromItem).setHint(indexHint);
1752-
}
1762+
(
1763+
indexHint = MySQLIndexHint() {
1764+
if (fromItem instanceof Table)
1765+
((Table) fromItem).setHint(indexHint);
1766+
}
1767+
|
1768+
sqlServerHints = SQLServerHints() {
1769+
if (fromItem instanceof Table)
1770+
((Table) fromItem).setSqlServerHints(sqlServerHints);
1771+
}
1772+
)
17531773
]
17541774
)
17551775
)

0 commit comments

Comments
 (0)