Search in sources :

Example 1 with SQLDataTypeImpl

use of com.alibaba.druid.sql.ast.SQLDataTypeImpl in project druid by alibaba.

the class MySqlCreateTableParser method partitionClauseRest.

protected void partitionClauseRest(SQLPartitionBy clause) {
    if (identifierEquals("PARTITIONS")) {
        lexer.nextToken();
        SQLIntegerExpr countExpr = this.exprParser.integerExpr();
        clause.setPartitionsCount(countExpr);
    }
    if (lexer.token() == Token.PARTITION) {
        lexer.nextToken();
        if (identifierEquals("NUM")) {
            lexer.nextToken();
        }
        clause.setPartitionsCount(this.exprParser.expr());
        clause.putAttribute("ads.partition", Boolean.TRUE);
    }
    if (identifierEquals("SUBPARTITION")) {
        lexer.nextToken();
        accept(Token.BY);
        SQLSubPartitionBy subPartitionByClause = null;
        boolean linear = false;
        if (identifierEquals("LINEAR")) {
            lexer.nextToken();
            linear = true;
        }
        if (lexer.token() == Token.KEY) {
            MySqlSubPartitionByKey subPartitionKey = new MySqlSubPartitionByKey();
            lexer.nextToken();
            if (linear) {
                clause.setLinear(true);
            }
            accept(Token.LPAREN);
            for (; ; ) {
                subPartitionKey.addColumn(this.exprParser.name());
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                }
                break;
            }
            accept(Token.RPAREN);
            subPartitionByClause = subPartitionKey;
        } else if (identifierEquals("HASH")) {
            lexer.nextToken();
            SQLSubPartitionByHash subPartitionHash = new SQLSubPartitionByHash();
            if (linear) {
                clause.setLinear(true);
            }
            if (lexer.token() == Token.KEY) {
                lexer.nextToken();
                subPartitionHash.setKey(true);
            }
            accept(Token.LPAREN);
            subPartitionHash.setExpr(this.exprParser.expr());
            accept(Token.RPAREN);
            subPartitionByClause = subPartitionHash;
        } else if (identifierEquals("LIST")) {
            lexer.nextToken();
            MySqlSubPartitionByList subPartitionList = new MySqlSubPartitionByList();
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                SQLExpr expr = this.exprParser.expr();
                if (expr instanceof SQLIdentifierExpr && (identifierEquals("bigint") || identifierEquals("long"))) {
                    String dataType = lexer.stringVal();
                    lexer.nextToken();
                    SQLColumnDefinition column = this.exprParser.createColumnDefinition();
                    column.setName((SQLIdentifierExpr) expr);
                    column.setDataType(new SQLDataTypeImpl(dataType));
                    subPartitionList.addColumn(column);
                    subPartitionList.putAttribute("ads.subPartitionList", Boolean.TRUE);
                } else {
                    subPartitionList.setExpr(expr);
                }
                accept(Token.RPAREN);
            } else {
                acceptIdentifier("COLUMNS");
                accept(Token.LPAREN);
                for (; ; ) {
                    subPartitionList.addColumn(this.exprParser.parseColumn());
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    }
                    break;
                }
                accept(Token.RPAREN);
            }
            subPartitionByClause = subPartitionList;
        }
        if (identifierEquals("SUBPARTITION")) {
            lexer.nextToken();
            acceptIdentifier("OPTIONS");
            accept(Token.LPAREN);
            SQLAssignItem option = this.exprParser.parseAssignItem();
            accept(Token.RPAREN);
            option.setParent(subPartitionByClause);
            subPartitionByClause.getOptions().add(option);
        }
        if (identifierEquals("SUBPARTITIONS")) {
            lexer.nextToken();
            Number intValue = lexer.integerValue();
            SQLNumberExpr numExpr = new SQLNumberExpr(intValue);
            subPartitionByClause.setSubPartitionsCount(numExpr);
            lexer.nextToken();
        }
        if (subPartitionByClause != null) {
            subPartitionByClause.setLinear(linear);
            clause.setSubPartitionBy(subPartitionByClause);
        }
    }
}
Also used : MySqlSubPartitionByList(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByList) SQLSubPartitionByHash(com.alibaba.druid.sql.ast.SQLSubPartitionByHash) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLAssignItem(com.alibaba.druid.sql.ast.statement.SQLAssignItem) MySqlSubPartitionByKey(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByKey) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl) SQLSubPartitionBy(com.alibaba.druid.sql.ast.SQLSubPartitionBy)

Example 2 with SQLDataTypeImpl

use of com.alibaba.druid.sql.ast.SQLDataTypeImpl in project druid by alibaba.

the class MySqlStatementParser method parserParameters.

/**
     * parse create procedure parameters
     * 
     * @param parameters
     */
private void parserParameters(List<SQLParameter> parameters) {
    if (lexer.token() == Token.RPAREN) {
        return;
    }
    for (; ; ) {
        SQLParameter parameter = new SQLParameter();
        if (lexer.token() == Token.CURSOR) {
            lexer.nextToken();
            parameter.setName(this.exprParser.name());
            accept(Token.IS);
            SQLSelect select = this.createSQLSelectParser().select();
            SQLDataTypeImpl dataType = new SQLDataTypeImpl();
            dataType.setName("CURSOR");
            parameter.setDataType(dataType);
            parameter.setDefaultValue(new SQLQueryExpr(select));
        } else if (lexer.token() == Token.IN || lexer.token() == Token.OUT || lexer.token() == Token.INOUT) {
            if (lexer.token() == Token.IN) {
                parameter.setParamType(ParameterType.IN);
            } else if (lexer.token() == Token.OUT) {
                parameter.setParamType(ParameterType.OUT);
            } else if (lexer.token() == Token.INOUT) {
                parameter.setParamType(ParameterType.INOUT);
            }
            lexer.nextToken();
            parameter.setName(this.exprParser.name());
            parameter.setDataType(this.exprParser.parseDataType());
        } else {
            // default parameter type is in
            parameter.setParamType(ParameterType.DEFAULT);
            parameter.setName(this.exprParser.name());
            parameter.setDataType(this.exprParser.parseDataType());
            if (lexer.token() == Token.COLONEQ) {
                lexer.nextToken();
                parameter.setDefaultValue(this.exprParser.expr());
            }
        }
        parameters.add(parameter);
        if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI) {
            lexer.nextToken();
        }
        if (lexer.token() != Token.BEGIN && lexer.token() != Token.RPAREN) {
            continue;
        }
        break;
    }
}
Also used : SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLParameter(com.alibaba.druid.sql.ast.SQLParameter) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl)

Example 3 with SQLDataTypeImpl

use of com.alibaba.druid.sql.ast.SQLDataTypeImpl in project druid by alibaba.

the class OracleStatementParser method parserParameters.

private void parserParameters(List<SQLParameter> parameters) {
    for (; ; ) {
        SQLParameter parameter = new SQLParameter();
        if (lexer.token() == Token.CURSOR) {
            lexer.nextToken();
            parameter.setName(this.exprParser.name());
            accept(Token.IS);
            SQLSelect select = this.createSQLSelectParser().select();
            SQLDataTypeImpl dataType = new SQLDataTypeImpl();
            dataType.setName("CURSOR");
            parameter.setDataType(dataType);
            parameter.setDefaultValue(new SQLQueryExpr(select));
        } else {
            parameter.setName(this.exprParser.name());
            parameter.setDataType(this.exprParser.parseDataType());
            if (lexer.token() == Token.COLONEQ) {
                lexer.nextToken();
                parameter.setDefaultValue(this.exprParser.expr());
            }
        }
        parameters.add(parameter);
        if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI) {
            lexer.nextToken();
        }
        if (lexer.token() != Token.BEGIN && lexer.token() != Token.RPAREN) {
            continue;
        }
        break;
    }
}
Also used : SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLParameter(com.alibaba.druid.sql.ast.SQLParameter) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl)

Example 4 with SQLDataTypeImpl

use of com.alibaba.druid.sql.ast.SQLDataTypeImpl in project druid by alibaba.

the class OracleExprParser method parseDataType.

public SQLDataType parseDataType() {
    if (lexer.token() == Token.CONSTRAINT || lexer.token() == Token.COMMA) {
        return null;
    }
    if (lexer.token() == Token.DEFAULT || lexer.token() == Token.NOT || lexer.token() == Token.NULL) {
        return null;
    }
    if (lexer.token() == Token.INTERVAL) {
        lexer.nextToken();
        if (identifierEquals("YEAR")) {
            lexer.nextToken();
            OracleDataTypeIntervalYear interval = new OracleDataTypeIntervalYear();
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                interval.addArgument(this.expr());
                accept(Token.RPAREN);
            }
            accept(Token.TO);
            acceptIdentifier("MONTH");
            return interval;
        } else {
            acceptIdentifier("DAY");
            OracleDataTypeIntervalDay interval = new OracleDataTypeIntervalDay();
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                interval.addArgument(this.expr());
                accept(Token.RPAREN);
            }
            accept(Token.TO);
            acceptIdentifier("SECOND");
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                interval.getFractionalSeconds().add(this.expr());
                accept(Token.RPAREN);
            }
            return interval;
        }
    }
    String typeName;
    if (identifierEquals("LONG")) {
        lexer.nextToken();
        acceptIdentifier("RAW");
        typeName = "LONG RAW";
    } else {
        SQLName typeExpr = name();
        typeName = typeExpr.toString();
    }
    if ("TIMESTAMP".equalsIgnoreCase(typeName)) {
        OracleDataTypeTimestamp timestamp = new OracleDataTypeTimestamp();
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            timestamp.addArgument(this.expr());
            accept(Token.RPAREN);
        }
        if (lexer.token() == Token.WITH) {
            lexer.nextToken();
            if (identifierEquals("LOCAL")) {
                lexer.nextToken();
                timestamp.setWithLocalTimeZone(true);
            } else {
                timestamp.setWithTimeZone(true);
            }
            acceptIdentifier("TIME");
            acceptIdentifier("ZONE");
        }
        return timestamp;
    }
    if (isCharType(typeName)) {
        SQLCharacterDataType charType = new SQLCharacterDataType(typeName);
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            charType.addArgument(this.expr());
            if (identifierEquals("CHAR")) {
                lexer.nextToken();
                charType.setCharType(SQLCharacterDataType.CHAR_TYPE_CHAR);
            } else if (identifierEquals("BYTE")) {
                lexer.nextToken();
                charType.setCharType(SQLCharacterDataType.CHAR_TYPE_BYTE);
            }
            accept(Token.RPAREN);
        }
        return parseCharTypeRest(charType);
    }
    if (lexer.token() == Token.PERCENT) {
        lexer.nextToken();
        if (identifierEquals("TYPE")) {
            lexer.nextToken();
            typeName += "%TYPE";
        } else if (identifierEquals("ROWTYPE")) {
            lexer.nextToken();
            typeName += "%ROWTYPE";
        } else {
            throw new ParserException("syntax error : " + lexer.token() + " " + lexer.stringVal());
        }
    }
    SQLDataType dataType = new SQLDataTypeImpl(typeName);
    return parseDataTypeRest(dataType);
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) OracleDataTypeIntervalYear(com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalYear) OracleDataTypeTimestamp(com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeTimestamp) SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl) OracleDataTypeIntervalDay(com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalDay)

Example 5 with SQLDataTypeImpl

use of com.alibaba.druid.sql.ast.SQLDataTypeImpl in project druid by alibaba.

the class EqualTest_cast method test_exits.

public void test_exits() throws Exception {
    String sql = "cast(a as varchar(50))";
    String sql_c = "cast(b as varchar(50))";
    SQLCastExpr exprA, exprB, exprC;
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprA = (SQLCastExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprB = (SQLCastExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql_c);
        exprC = (SQLCastExpr) parser.expr();
    }
    Assert.assertEquals(exprA, exprB);
    Assert.assertNotEquals(exprA, exprC);
    Assert.assertTrue(exprA.equals(exprA));
    Assert.assertFalse(exprA.equals(new Object()));
    Assert.assertEquals(exprA.hashCode(), exprB.hashCode());
    Assert.assertEquals(new SQLCastExpr(), new SQLCastExpr());
    Assert.assertEquals(new SQLCastExpr().hashCode(), new SQLCastExpr().hashCode());
    Assert.assertEquals(new SQLDataTypeImpl(), new SQLDataTypeImpl());
    Assert.assertEquals(new SQLDataTypeImpl().hashCode(), new SQLDataTypeImpl().hashCode());
}
Also used : OracleExprParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser) SQLCastExpr(com.alibaba.druid.sql.ast.expr.SQLCastExpr) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl)

Aggregations

SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)5 SQLParameter (com.alibaba.druid.sql.ast.SQLParameter)2 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)2 SQLDataType (com.alibaba.druid.sql.ast.SQLDataType)1 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLSubPartitionBy (com.alibaba.druid.sql.ast.SQLSubPartitionBy)1 SQLSubPartitionByHash (com.alibaba.druid.sql.ast.SQLSubPartitionByHash)1 SQLCastExpr (com.alibaba.druid.sql.ast.expr.SQLCastExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)1 SQLAssignItem (com.alibaba.druid.sql.ast.statement.SQLAssignItem)1 SQLCharacterDataType (com.alibaba.druid.sql.ast.statement.SQLCharacterDataType)1 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)1 MySqlSubPartitionByKey (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByKey)1 MySqlSubPartitionByList (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSubPartitionByList)1 OracleDataTypeIntervalDay (com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalDay)1 OracleDataTypeIntervalYear (com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalYear)1 OracleDataTypeTimestamp (com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeTimestamp)1