Skip to content

Commit

Permalink
fixes #278
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Jun 21, 2016
1 parent b1bea02 commit faf8c6a
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2016 JSQLParser
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
/*
* Copyright (C) 2016 JSQLParser.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package net.sf.jsqlparser.expression;

/**
*
* @author toben
*/
public class DateTimeLiteralExpression implements Expression {
private String value;
private DateTime type;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public DateTime getType() {
return type;
}

public void setType(DateTime type) {
this.type = type;
}

@Override
public void accept(ExpressionVisitor expressionVisitor) {
expressionVisitor.visit(this);
}

@Override
public String toString() {
return type.name() + " " + value;
}

public static enum DateTime {
DATE, TIME, TIMESTAMP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,6 @@ public interface ExpressionVisitor {

void visit(TimeKeyExpression timeKeyExpression);

void visit(DateTimeLiteralExpression literal);

}
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,9 @@ public void visit(TimeKeyExpression timeKeyExpression) {

}

@Override
public void visit(DateTimeLiteralExpression literal) {

}

}
5 changes: 5 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -658,4 +658,9 @@ public void visit(AlterView alterView) {
@Override
public void visit(TimeKeyExpression timeKeyExpression) {
}

@Override
public void visit(DateTimeLiteralExpression literal) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -581,4 +581,9 @@ public void visit(TimeKeyExpression timeKeyExpression) {
buffer.append(timeKeyExpression.toString());
}

@Override
public void visit(DateTimeLiteralExpression literal) {
buffer.append(literal.toString());
}

}
17 changes: 14 additions & 3 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_HIGH_PRIORITY : "HIGH_PRIORITY">
| <K_IGNORE : "IGNORE">
| <K_SEMI : "SEMI">
| <K_DATETIMELITERAL : ("DATE" | "TIME" | "TIMESTAMP") >
| <K_TIME_KEY_EXPR : ( "CURRENT_TIMESTAMP" | "CURRENT_TIME" | "CURRENT_DATE" ) ( "()" )?>
}

Expand Down Expand Up @@ -688,7 +689,7 @@ String RelObjectName() :
| tk=<K_COLUMN> | tk=<K_REPLACE> | tk=<K_TRUNCATE> | tk=<K_KEY> | tk=<K_ANY>
| tk=<K_OPEN> | tk=<K_OVER> | tk=<K_VALUES> | tk=<K_PERCENT> | tk=<K_PRIOR>
| tk=<K_SEPARATOR> | tk=<K_NO> | tk=<K_ACTION> | tk=<K_CASCADE> | tk=<K_END>
| tk=<K_TABLE>
| tk=<K_TABLE> | tk=<K_DATETIMELITERAL>
)

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

| LOOKAHEAD(2) retval=DateTimeLiteralExpression()

| LOOKAHEAD(["+" | "-"] Column()) [sign="+" | sign="-"] retval=Column()

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

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


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

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

DateTimeLiteralExpression DateTimeLiteralExpression() : {
DateTimeLiteralExpression expr = new DateTimeLiteralExpression();
Token t;
} {
t=<K_DATETIMELITERAL> { expr.setType(DateTimeLiteralExpression.DateTime.valueOf(t.image.toUpperCase())); }

t=<S_CHAR_LITERAL> { expr.setValue(t.image); return expr; }
}

JsonExpression JsonExpression() : {
JsonExpression result = new JsonExpression();
Column column;
Expand Down Expand Up @@ -2740,7 +2751,7 @@ ColDataType ColDataType():
}
{
( tk=<K_CHARACTER> [tk2=<K_VARYING>] { colDataType.setDataType(tk.image + (tk2!=null?" " + tk2.image:"")); }
| tk=<S_IDENTIFIER> { colDataType.setDataType(tk.image); } )
| ( tk=<S_IDENTIFIER> | tk=<K_DATETIMELITERAL> ) { colDataType.setDataType(tk.image); } )

[LOOKAHEAD(2) "(" ( (tk=<S_LONG> | tk=<S_CHAR_LITERAL> | tk=<S_IDENTIFIER>) { argumentsStringList.add(tk.image); } ["," {/*argumentsStringList.add(",");*/}] )* ")"]
[( "[" {tk=null;} [ tk=<S_LONG> ] { array.add(tk!=null?Integer.valueOf(tk.image):null); } "]" )+ { colDataType.setArrayData(array); } ]
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/net/sf/jsqlparser/test/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2199,4 +2199,8 @@ public void testTopExpressionIssue243_2() throws JSQLParserException {
public void testFunctionIssue284() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT NVL((SELECT 1 FROM DUAL), 1) AS A FROM TEST1");
}

public void testFunctionDateTimeValues() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT * FROM tab1 WHERE a > TIMESTAMP '2004-04-30 04:05:34.56'");
}
}

0 comments on commit faf8c6a

Please sign in to comment.