use of com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGExtractExpr in project druid by alibaba.
the class ExtractTest_Year method test_timestamp.
public void test_timestamp() throws Exception {
String sql = "EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40')";
PGExprParser parser = new PGExprParser(sql);
PGExtractExpr expr = (PGExtractExpr) parser.expr();
System.out.println(expr.toString());
}
use of com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGExtractExpr in project druid by alibaba.
the class PGExprParser method primaryRest.
public SQLExpr primaryRest(SQLExpr expr) {
if (lexer.token() == Token.COLONCOLON) {
lexer.nextToken();
SQLDataType dataType = this.parseDataType();
PGTypeCastExpr castExpr = new PGTypeCastExpr();
castExpr.setExpr(expr);
castExpr.setDataType(dataType);
return primaryRest(castExpr);
}
if (lexer.token() == Token.LBRACKET) {
SQLArrayExpr array = new SQLArrayExpr();
array.setExpr(expr);
lexer.nextToken();
this.exprList(array.getValues(), array);
accept(Token.RBRACKET);
return primaryRest(array);
}
if (expr.getClass() == SQLIdentifierExpr.class) {
String ident = ((SQLIdentifierExpr) expr).getName();
if ("TIMESTAMP".equalsIgnoreCase(ident)) {
if (//
lexer.token() != Token.LITERAL_ALIAS && //
lexer.token() != Token.LITERAL_CHARS && lexer.token() != Token.WITH) {
return new SQLIdentifierExpr("TIMESTAMP");
}
SQLTimestampExpr timestamp = new SQLTimestampExpr();
if (lexer.token() == Token.WITH) {
lexer.nextToken();
acceptIdentifier("TIME");
acceptIdentifier("ZONE");
timestamp.setWithTimeZone(true);
}
String literal = lexer.stringVal();
timestamp.setLiteral(literal);
accept(Token.LITERAL_CHARS);
if (identifierEquals("AT")) {
lexer.nextToken();
acceptIdentifier("TIME");
acceptIdentifier("ZONE");
String timezone = lexer.stringVal();
timestamp.setTimeZone(timezone);
accept(Token.LITERAL_CHARS);
}
return primaryRest(timestamp);
} else if ("EXTRACT".equalsIgnoreCase(ident)) {
accept(Token.LPAREN);
PGExtractExpr extract = new PGExtractExpr();
String fieldName = lexer.stringVal();
PGDateField field = PGDateField.valueOf(fieldName.toUpperCase());
lexer.nextToken();
extract.setField(field);
accept(Token.FROM);
SQLExpr source = this.expr();
extract.setSource(source);
accept(Token.RPAREN);
return primaryRest(extract);
} else if ("POINT".equalsIgnoreCase(ident)) {
SQLExpr value = this.primary();
PGPointExpr point = new PGPointExpr();
point.setValue(value);
return primaryRest(point);
} else if ("BOX".equalsIgnoreCase(ident)) {
SQLExpr value = this.primary();
PGBoxExpr box = new PGBoxExpr();
box.setValue(value);
return primaryRest(box);
} else if ("macaddr".equalsIgnoreCase(ident)) {
SQLExpr value = this.primary();
PGMacAddrExpr macaddr = new PGMacAddrExpr();
macaddr.setValue(value);
return primaryRest(macaddr);
} else if ("inet".equalsIgnoreCase(ident)) {
SQLExpr value = this.primary();
PGInetExpr inet = new PGInetExpr();
inet.setValue(value);
return primaryRest(inet);
} else if ("cidr".equalsIgnoreCase(ident)) {
SQLExpr value = this.primary();
PGCidrExpr cidr = new PGCidrExpr();
cidr.setValue(value);
return primaryRest(cidr);
} else if ("polygon".equalsIgnoreCase(ident)) {
SQLExpr value = this.primary();
PGPolygonExpr polygon = new PGPolygonExpr();
polygon.setValue(value);
return primaryRest(polygon);
} else if ("circle".equalsIgnoreCase(ident)) {
SQLExpr value = this.primary();
PGCircleExpr circle = new PGCircleExpr();
circle.setValue(value);
return primaryRest(circle);
} else if ("lseg".equalsIgnoreCase(ident)) {
SQLExpr value = this.primary();
PGLineSegmentsExpr lseg = new PGLineSegmentsExpr();
lseg.setValue(value);
return primaryRest(lseg);
} else if (ident.equalsIgnoreCase("b") && lexer.token() == Token.LITERAL_CHARS) {
String charValue = lexer.stringVal();
lexer.nextToken();
expr = new SQLBinaryExpr(charValue);
return primaryRest(expr);
}
}
return super.primaryRest(expr);
}
Aggregations