Search in sources :

Example 1 with PGInetExpr

use of com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGInetExpr 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);
}
Also used : SQLBinaryExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryExpr) SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) PGPointExpr(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGPointExpr) PGLineSegmentsExpr(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGLineSegmentsExpr) PGMacAddrExpr(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGMacAddrExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) PGInetExpr(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGInetExpr) SQLTimestampExpr(com.alibaba.druid.sql.ast.expr.SQLTimestampExpr) PGTypeCastExpr(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGTypeCastExpr) PGPolygonExpr(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGPolygonExpr) PGCidrExpr(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGCidrExpr) PGDateField(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGDateField) PGCircleExpr(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGCircleExpr) PGBoxExpr(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGBoxExpr) SQLArrayExpr(com.alibaba.druid.sql.ast.expr.SQLArrayExpr) PGExtractExpr(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGExtractExpr)

Example 2 with PGInetExpr

use of com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGInetExpr in project druid by alibaba.

the class InetTest method test_timestamp.

public void test_timestamp() throws Exception {
    String sql = "inet '0.0.0.255'";
    PGExprParser parser = new PGExprParser(sql);
    PGInetExpr expr = (PGInetExpr) parser.expr();
    Assert.assertEquals("inet '0.0.0.255'", expr.toString());
}
Also used : PGExprParser(com.alibaba.druid.sql.dialect.postgresql.parser.PGExprParser) PGInetExpr(com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGInetExpr)

Aggregations

PGInetExpr (com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGInetExpr)2 SQLDataType (com.alibaba.druid.sql.ast.SQLDataType)1 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLArrayExpr (com.alibaba.druid.sql.ast.expr.SQLArrayExpr)1 SQLBinaryExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLTimestampExpr (com.alibaba.druid.sql.ast.expr.SQLTimestampExpr)1 PGBoxExpr (com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGBoxExpr)1 PGCidrExpr (com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGCidrExpr)1 PGCircleExpr (com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGCircleExpr)1 PGDateField (com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGDateField)1 PGExtractExpr (com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGExtractExpr)1 PGLineSegmentsExpr (com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGLineSegmentsExpr)1 PGMacAddrExpr (com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGMacAddrExpr)1 PGPointExpr (com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGPointExpr)1 PGPolygonExpr (com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGPolygonExpr)1 PGTypeCastExpr (com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGTypeCastExpr)1 PGExprParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGExprParser)1