Skip to content

Commit fc5e605

Browse files
committed
introduces Interval expression
1 parent f97c5a2 commit fc5e605

File tree

7 files changed

+137
-5
lines changed

7 files changed

+137
-5
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ Original project websites:
77
* http://jsqlparser.sourceforge.net
88
* http://sourceforge.net/projects/jsqlparser/
99

10+
## Extensions Version 0.8.4
11+
12+
* Added support for simple intervals
13+
14+
```sql
15+
select 5 - INTERVAL '45 MINUTE' from mytable
16+
```
17+
1018
## Extensions Version 0.8.3
1119

1220
* Added support for cross join

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,6 @@ public interface ExpressionVisitor {
134134
public void visit(AnalyticExpression aexpr);
135135

136136
public void visit(ExtractExpression eexpr);
137+
138+
public void visit(IntervalExpression iexpr);
137139
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
/*
23+
* Copyright (C) 2013 JSQLParser.
24+
*
25+
* This library is free software; you can redistribute it and/or
26+
* modify it under the terms of the GNU Lesser General Public
27+
* License as published by the Free Software Foundation; either
28+
* version 2.1 of the License, or (at your option) any later version.
29+
*
30+
* This library is distributed in the hope that it will be useful,
31+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
32+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33+
* Lesser General Public License for more details.
34+
*
35+
* You should have received a copy of the GNU Lesser General Public
36+
* License along with this library; if not, write to the Free Software
37+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
38+
* MA 02110-1301 USA
39+
*/
40+
package net.sf.jsqlparser.expression;
41+
42+
/**
43+
*
44+
* @author wumpz
45+
*/
46+
public class IntervalExpression implements Expression {
47+
private String parameter = null;
48+
49+
public String getParameter() {
50+
return parameter;
51+
}
52+
53+
public void setParameter(String parameter) {
54+
this.parameter = parameter;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return "INTERVAL " + parameter;
60+
}
61+
62+
@Override
63+
public void accept(ExpressionVisitor expressionVisitor) {
64+
expressionVisitor.visit(this);
65+
}
66+
}

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import net.sf.jsqlparser.expression.ExpressionVisitor;
3737
import net.sf.jsqlparser.expression.ExtractExpression;
3838
import net.sf.jsqlparser.expression.Function;
39+
import net.sf.jsqlparser.expression.IntervalExpression;
3940
import net.sf.jsqlparser.expression.InverseExpression;
4041
import net.sf.jsqlparser.expression.JdbcParameter;
4142
import net.sf.jsqlparser.expression.LongValue;
@@ -493,4 +494,8 @@ private void init() {
493494
otherItemNames = new ArrayList<String>();
494495
tables = new ArrayList<String>();
495496
}
497+
498+
@Override
499+
public void visit(IntervalExpression iexpr) {
500+
}
496501
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import net.sf.jsqlparser.expression.ExpressionVisitor;
3636
import net.sf.jsqlparser.expression.ExtractExpression;
3737
import net.sf.jsqlparser.expression.Function;
38+
import net.sf.jsqlparser.expression.IntervalExpression;
3839
import net.sf.jsqlparser.expression.InverseExpression;
3940
import net.sf.jsqlparser.expression.JdbcParameter;
4041
import net.sf.jsqlparser.expression.LongValue;
@@ -505,4 +506,9 @@ public void visit(MultiExpressionList multiExprList) {
505506
}
506507
}
507508
}
509+
510+
@Override
511+
public void visit(IntervalExpression iexpr) {
512+
buffer.append(iexpr.toString());
513+
}
508514
}

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import net.sf.jsqlparser.expression.DoubleValue;
6464
import net.sf.jsqlparser.expression.Expression;
6565
import net.sf.jsqlparser.expression.Function;
6666
import net.sf.jsqlparser.expression.InverseExpression;
67+
import net.sf.jsqlparser.expression.IntervalExpression;
6768
import net.sf.jsqlparser.expression.JdbcParameter;
6869
import net.sf.jsqlparser.expression.LongValue;
6970
import net.sf.jsqlparser.expression.DateValue;
@@ -239,6 +240,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
239240
| <K_EXTRACT:"EXTRACT">
240241
| <K_LATERAL:"LATERAL">
241242
| <K_MATERIALIZED:"MATERIALIZED">
243+
| <K_INTERVAL:"INTERVAL">
242244
}
243245

244246

@@ -1534,8 +1536,8 @@ Expression PrimaryExpression():
15341536
| "{t" token=<S_CHAR_LITERAL> "}" { retval = new TimeValue(token.image); }
15351537

15361538
| "{ts" token=<S_CHAR_LITERAL> "}" { retval = new TimestampValue(token.image); }
1537-
1538-
1539+
1540+
| retval = IntervalExpression()
15391541
)
15401542

15411543
[ "::" type=ColDataType() {
@@ -1554,6 +1556,17 @@ Expression PrimaryExpression():
15541556
}
15551557
}
15561558

1559+
IntervalExpression IntervalExpression() : {
1560+
IntervalExpression interval = new IntervalExpression();
1561+
Token token;
1562+
}
1563+
{
1564+
<K_INTERVAL> token=<S_CHAR_LITERAL> { interval.setParameter(token.image); }
1565+
{
1566+
return interval;
1567+
}
1568+
}
1569+
15571570
AnalyticExpression AnalyticExpression() :
15581571
{
15591572
AnalyticExpression retval = new AnalyticExpression();

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import net.sf.jsqlparser.expression.DoubleValue;
1010
import net.sf.jsqlparser.expression.Expression;
1111
import net.sf.jsqlparser.expression.Function;
12+
import net.sf.jsqlparser.expression.IntervalExpression;
1213
import net.sf.jsqlparser.expression.LongValue;
1314
import net.sf.jsqlparser.expression.StringValue;
1415
import net.sf.jsqlparser.expression.TimeValue;
1516
import net.sf.jsqlparser.expression.TimestampValue;
1617
import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication;
18+
import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction;
1719
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
1820
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
1921
import net.sf.jsqlparser.expression.operators.relational.InExpression;
@@ -30,9 +32,6 @@
3032
import net.sf.jsqlparser.statement.select.Select;
3133
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
3234
import net.sf.jsqlparser.statement.select.SetOperationList;
33-
import net.sf.jsqlparser.util.deparser.ExpressionDeParser;
34-
import net.sf.jsqlparser.util.deparser.SelectDeParser;
35-
import net.sf.jsqlparser.util.deparser.StatementDeParser;
3635
import org.apache.commons.io.IOUtils;
3736
import static net.sf.jsqlparser.test.TestUtils.*;
3837

@@ -952,4 +951,37 @@ public void testValues6BothVariants() throws JSQLParserException {
952951
String stmt = "SELECT I FROM (VALUES 1, 2, 3) AS MY_TEMP_TABLE(I) WHERE I IN (SELECT * FROM (VALUES 1, 2) AS TEST)";
953952
assertSqlCanBeParsedAndDeparsed(stmt);
954953
}
954+
955+
public void testInterval1() throws JSQLParserException {
956+
String stmt = "SELECT 5 + INTERVAL '3 days'";
957+
assertSqlCanBeParsedAndDeparsed(stmt);
958+
}
959+
960+
public void testInterval2() throws JSQLParserException {
961+
String stmt = "SELECT to_timestamp(to_char(now() - INTERVAL '45 MINUTE', 'YYYY-MM-DD-HH24:')) AS START_TIME FROM tab1";
962+
assertSqlCanBeParsedAndDeparsed(stmt);
963+
964+
Statement st = CCJSqlParserUtil.parse(stmt);
965+
Select select = (Select) st;
966+
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
967+
968+
assertEquals(1, plainSelect.getSelectItems().size());
969+
SelectExpressionItem item = (SelectExpressionItem) plainSelect.getSelectItems().get(0);
970+
Function function = (Function)item.getExpression();
971+
972+
assertEquals("to_timestamp", function.getName());
973+
974+
assertEquals(1, function.getParameters().getExpressions().size());
975+
976+
Function func2 = (Function) function.getParameters().getExpressions().get(0);
977+
978+
assertEquals("to_char", func2.getName());
979+
980+
assertEquals(2, func2.getParameters().getExpressions().size());
981+
Subtraction sub = (Subtraction) func2.getParameters().getExpressions().get(0);
982+
assertTrue(sub.getRightExpression() instanceof IntervalExpression);
983+
IntervalExpression iexpr = (IntervalExpression) sub.getRightExpression();
984+
985+
assertEquals("'45 MINUTE'", iexpr.getParameter());
986+
}
955987
}

0 commit comments

Comments
 (0)