Skip to content

Commit faf8c6a

Browse files
committed
fixes #278
1 parent b1bea02 commit faf8c6a

File tree

7 files changed

+114
-3
lines changed

7 files changed

+114
-3
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2016 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) 2016 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 toben
45+
*/
46+
public class DateTimeLiteralExpression implements Expression {
47+
private String value;
48+
private DateTime type;
49+
50+
public String getValue() {
51+
return value;
52+
}
53+
54+
public void setValue(String value) {
55+
this.value = value;
56+
}
57+
58+
public DateTime getType() {
59+
return type;
60+
}
61+
62+
public void setType(DateTime type) {
63+
this.type = type;
64+
}
65+
66+
@Override
67+
public void accept(ExpressionVisitor expressionVisitor) {
68+
expressionVisitor.visit(this);
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return type.name() + " " + value;
74+
}
75+
76+
public static enum DateTime {
77+
DATE, TIME, TIMESTAMP;
78+
}
79+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,6 @@ public interface ExpressionVisitor {
167167

168168
void visit(TimeKeyExpression timeKeyExpression);
169169

170+
void visit(DateTimeLiteralExpression literal);
171+
170172
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,9 @@ public void visit(TimeKeyExpression timeKeyExpression) {
461461

462462
}
463463

464+
@Override
465+
public void visit(DateTimeLiteralExpression literal) {
466+
467+
}
468+
464469
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,4 +658,9 @@ public void visit(AlterView alterView) {
658658
@Override
659659
public void visit(TimeKeyExpression timeKeyExpression) {
660660
}
661+
662+
@Override
663+
public void visit(DateTimeLiteralExpression literal) {
664+
665+
}
661666
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,4 +581,9 @@ public void visit(TimeKeyExpression timeKeyExpression) {
581581
buffer.append(timeKeyExpression.toString());
582582
}
583583

584+
@Override
585+
public void visit(DateTimeLiteralExpression literal) {
586+
buffer.append(literal.toString());
587+
}
588+
584589
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
234234
| <K_HIGH_PRIORITY : "HIGH_PRIORITY">
235235
| <K_IGNORE : "IGNORE">
236236
| <K_SEMI : "SEMI">
237+
| <K_DATETIMELITERAL : ("DATE" | "TIME" | "TIMESTAMP") >
237238
| <K_TIME_KEY_EXPR : ( "CURRENT_TIMESTAMP" | "CURRENT_TIME" | "CURRENT_DATE" ) ( "()" )?>
238239
}
239240

@@ -688,7 +689,7 @@ String RelObjectName() :
688689
| tk=<K_COLUMN> | tk=<K_REPLACE> | tk=<K_TRUNCATE> | tk=<K_KEY> | tk=<K_ANY>
689690
| tk=<K_OPEN> | tk=<K_OVER> | tk=<K_VALUES> | tk=<K_PERCENT> | tk=<K_PRIOR>
690691
| tk=<K_SEPARATOR> | tk=<K_NO> | tk=<K_ACTION> | tk=<K_CASCADE> | tk=<K_END>
691-
| tk=<K_TABLE>
692+
| tk=<K_TABLE> | tk=<K_DATETIMELITERAL>
692693
)
693694

694695
{ return tk.image; }
@@ -2075,6 +2076,8 @@ Expression PrimaryExpression():
20752076
// support timestamp expressions
20762077
| token=<K_TIME_KEY_EXPR> { retval = new TimeKeyExpression(token.image); }
20772078

2079+
| LOOKAHEAD(2) retval=DateTimeLiteralExpression()
2080+
20782081
| LOOKAHEAD(["+" | "-"] Column()) [sign="+" | sign="-"] retval=Column()
20792082

20802083
| LOOKAHEAD(["+" | "-"] "(" BitwiseAndOr() ")") [sign="+" | sign="-"] "(" retval=BitwiseAndOr() ")" {retval = new Parenthesis(retval); }
@@ -2083,7 +2086,6 @@ Expression PrimaryExpression():
20832086

20842087
| token=<S_CHAR_LITERAL> { retval = new StringValue(token.image); }
20852088

2086-
20872089
| "{d" token=<S_CHAR_LITERAL> "}" { retval = new DateValue(token.image); }
20882090

20892091
| "{t" token=<S_CHAR_LITERAL> "}" { retval = new TimeValue(token.image); }
@@ -2145,6 +2147,15 @@ NumericBind NumericBind() : {
21452147
}
21462148
}
21472149

2150+
DateTimeLiteralExpression DateTimeLiteralExpression() : {
2151+
DateTimeLiteralExpression expr = new DateTimeLiteralExpression();
2152+
Token t;
2153+
} {
2154+
t=<K_DATETIMELITERAL> { expr.setType(DateTimeLiteralExpression.DateTime.valueOf(t.image.toUpperCase())); }
2155+
2156+
t=<S_CHAR_LITERAL> { expr.setValue(t.image); return expr; }
2157+
}
2158+
21482159
JsonExpression JsonExpression() : {
21492160
JsonExpression result = new JsonExpression();
21502161
Column column;
@@ -2740,7 +2751,7 @@ ColDataType ColDataType():
27402751
}
27412752
{
27422753
( tk=<K_CHARACTER> [tk2=<K_VARYING>] { colDataType.setDataType(tk.image + (tk2!=null?" " + tk2.image:"")); }
2743-
| tk=<S_IDENTIFIER> { colDataType.setDataType(tk.image); } )
2754+
| ( tk=<S_IDENTIFIER> | tk=<K_DATETIMELITERAL> ) { colDataType.setDataType(tk.image); } )
27442755

27452756
[LOOKAHEAD(2) "(" ( (tk=<S_LONG> | tk=<S_CHAR_LITERAL> | tk=<S_IDENTIFIER>) { argumentsStringList.add(tk.image); } ["," {/*argumentsStringList.add(",");*/}] )* ")"]
27462757
[( "[" {tk=null;} [ tk=<S_LONG> ] { array.add(tk!=null?Integer.valueOf(tk.image):null); } "]" )+ { colDataType.setArrayData(array); } ]

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,4 +2199,8 @@ public void testTopExpressionIssue243_2() throws JSQLParserException {
21992199
public void testFunctionIssue284() throws JSQLParserException {
22002200
assertSqlCanBeParsedAndDeparsed("SELECT NVL((SELECT 1 FROM DUAL), 1) AS A FROM TEST1");
22012201
}
2202+
2203+
public void testFunctionDateTimeValues() throws JSQLParserException {
2204+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM tab1 WHERE a > TIMESTAMP '2004-04-30 04:05:34.56'");
2205+
}
22022206
}

0 commit comments

Comments
 (0)