Search in sources :

Example 6 with SQLNumberExpr

use of com.alibaba.druid.sql.ast.expr.SQLNumberExpr in project druid by alibaba.

the class EqualTest_number method test_exits.

public void test_exits() throws Exception {
    String sql = "3.5";
    String sql_c = "3.51";
    SQLNumberExpr exprA, exprB, exprC;
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprA = (SQLNumberExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql);
        exprB = (SQLNumberExpr) parser.expr();
    }
    {
        OracleExprParser parser = new OracleExprParser(sql_c);
        exprC = (SQLNumberExpr) 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 SQLNumberExpr(), new SQLNumberExpr());
    Assert.assertEquals(new SQLNumberExpr().hashCode(), new SQLNumberExpr().hashCode());
}
Also used : OracleExprParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr)

Example 7 with SQLNumberExpr

use of com.alibaba.druid.sql.ast.expr.SQLNumberExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLInsertVisitor method supplyAutoIncrementColumn.

private void supplyAutoIncrementColumn(final Collection<String> autoIncrementColumns, final String tableName, final List<SQLExpr> columns, final List<SQLExpr> values) {
    boolean isPreparedStatement = !getParameters().isEmpty();
    GeneratedKeyContext generatedKeyContext = getParseContext().getParsedResult().getGeneratedKeyContext();
    if (isPreparedStatement) {
        generatedKeyContext.getColumns().addAll(autoIncrementColumns);
    }
    TableRule tableRule = getParseContext().getShardingRule().findTableRule(tableName);
    for (String each : autoIncrementColumns) {
        SQLExpr sqlExpr;
        Object id = tableRule.generateId(each);
        generatedKeyContext.putValue(each, id);
        if (isPreparedStatement) {
            sqlExpr = new SQLVariantRefExpr("?");
            getParameters().add(id);
            ((SQLVariantRefExpr) sqlExpr).setIndex(getParametersSize() - 1);
        } else {
            sqlExpr = (id instanceof Number) ? new SQLNumberExpr((Number) id) : new SQLCharExpr((String) id);
        }
        getParseContext().addCondition(each, tableName, BinaryOperator.EQUAL, sqlExpr, getDatabaseType(), getParameters());
        columns.add(new SQLIdentifierExpr(each));
        values.add(sqlExpr);
    }
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) GeneratedKeyContext(com.dangdang.ddframe.rdb.sharding.parser.result.GeneratedKeyContext) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 8 with SQLNumberExpr

use of com.alibaba.druid.sql.ast.expr.SQLNumberExpr in project Mycat-Server by MyCATApache.

the class HintSQLHandler method parseProcedure.

private Procedure parseProcedure(String sql, Map hintMap) {
    boolean fields = hintMap.containsKey("list_fields");
    boolean isResultList = hintMap != null && ("list".equals(hintMap.get("result_type")) || fields);
    Procedure procedure = new Procedure();
    procedure.setOriginSql(sql);
    procedure.setResultList(isResultList);
    List<String> sqls = Splitter.on(";").trimResults().splitToList(sql);
    Set<String> outSet = new HashSet<>();
    for (int i = sqls.size() - 1; i >= 0; i--) {
        String s = sqls.get(i);
        if (Strings.isNullOrEmpty(s)) {
            continue;
        }
        SQLStatementParser parser = new MySqlStatementParser(s);
        SQLStatement statement = parser.parseStatement();
        if (statement instanceof SQLSelectStatement) {
            MySqlSelectQueryBlock selectQuery = (MySqlSelectQueryBlock) ((SQLSelectStatement) statement).getSelect().getQuery();
            if (selectQuery != null) {
                List<SQLSelectItem> selectItems = selectQuery.getSelectList();
                for (SQLSelectItem selectItem : selectItems) {
                    String select = selectItem.toString();
                    outSet.add(select);
                    procedure.getSelectColumns().add(select);
                }
            }
            procedure.setSelectSql(s);
        } else if (statement instanceof SQLCallStatement) {
            SQLCallStatement sqlCallStatement = (SQLCallStatement) statement;
            procedure.setName(sqlCallStatement.getProcedureName().getSimpleName());
            List<SQLExpr> paramterList = sqlCallStatement.getParameters();
            for (int i1 = 0; i1 < paramterList.size(); i1++) {
                SQLExpr sqlExpr = paramterList.get(i1);
                String pName = sqlExpr.toString();
                String pType = outSet.contains(pName) ? ProcedureParameter.OUT : ProcedureParameter.IN;
                ProcedureParameter parameter = new ProcedureParameter();
                parameter.setIndex(i1 + 1);
                parameter.setName(pName);
                parameter.setParameterType(pType);
                if (pName.startsWith("@")) {
                    procedure.getParamterMap().put(pName, parameter);
                } else {
                    procedure.getParamterMap().put(String.valueOf(i1 + 1), parameter);
                }
            }
            procedure.setCallSql(s);
        } else if (statement instanceof SQLSetStatement) {
            procedure.setSetSql(s);
            SQLSetStatement setStatement = (SQLSetStatement) statement;
            List<SQLAssignItem> sets = setStatement.getItems();
            for (SQLAssignItem set : sets) {
                String name = set.getTarget().toString();
                SQLExpr value = set.getValue();
                ProcedureParameter parameter = procedure.getParamterMap().get(name);
                if (parameter != null) {
                    if (value instanceof SQLIntegerExpr) {
                        parameter.setValue(((SQLIntegerExpr) value).getNumber());
                        parameter.setJdbcType(Types.INTEGER);
                    } else if (value instanceof SQLNumberExpr) {
                        parameter.setValue(((SQLNumberExpr) value).getNumber());
                        parameter.setJdbcType(Types.NUMERIC);
                    } else if (value instanceof SQLTextLiteralExpr) {
                        parameter.setValue(((SQLTextLiteralExpr) value).getText());
                        parameter.setJdbcType(Types.VARCHAR);
                    } else if (value instanceof SQLValuableExpr) {
                        parameter.setValue(((SQLValuableExpr) value).getValue());
                        parameter.setJdbcType(Types.VARCHAR);
                    }
                }
            }
        }
    }
    if (fields) {
        String list_fields = (String) hintMap.get("list_fields");
        List<String> listFields = Splitter.on(",").trimResults().splitToList(list_fields);
        for (String field : listFields) {
            if (!procedure.getParamterMap().containsKey(field)) {
                ProcedureParameter parameter = new ProcedureParameter();
                parameter.setParameterType(ProcedureParameter.OUT);
                parameter.setName(field);
                parameter.setJdbcType(-10);
                parameter.setIndex(procedure.getParamterMap().size() + 1);
                procedure.getParamterMap().put(field, parameter);
            }
        }
        procedure.getListFields().addAll(listFields);
    }
    return procedure;
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLValuableExpr(com.alibaba.druid.sql.ast.expr.SQLValuableExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) List(java.util.List) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) HashSet(java.util.HashSet) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr)

Example 9 with SQLNumberExpr

use of com.alibaba.druid.sql.ast.expr.SQLNumberExpr in project druid by alibaba.

the class PagerUtils method limitOracle.

private static String limitOracle(SQLSelect select, String dbType, int offset, int count) {
    SQLSelectQuery query = select.getQuery();
    if (query instanceof SQLSelectQueryBlock) {
        OracleSelectQueryBlock queryBlock = (OracleSelectQueryBlock) query;
        if (queryBlock.getGroupBy() == null && select.getOrderBy() == null && offset <= 0) {
            SQLExpr condition = new //
            SQLBinaryOpExpr(//
            new SQLIdentifierExpr("ROWNUM"), //
            SQLBinaryOperator.LessThanOrEqual, //
            new SQLNumberExpr(count), JdbcConstants.ORACLE);
            if (queryBlock.getWhere() == null) {
                queryBlock.setWhere(condition);
            } else {
                queryBlock.setWhere(new //
                SQLBinaryOpExpr(//
                queryBlock.getWhere(), //
                SQLBinaryOperator.BooleanAnd, //
                condition, JdbcConstants.ORACLE));
            }
            return SQLUtils.toSQLString(select, dbType);
        }
    }
    OracleSelectQueryBlock countQueryBlock = new OracleSelectQueryBlock();
    countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
    countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLIdentifierExpr("ROWNUM"), "RN"));
    countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
    countQueryBlock.setWhere(new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.LessThanOrEqual, //
    new SQLNumberExpr(count + offset), JdbcConstants.ORACLE));
    if (offset <= 0) {
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    OracleSelectQueryBlock offsetQueryBlock = new OracleSelectQueryBlock();
    offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
    offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
    offsetQueryBlock.setWhere(new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("RN"), //
    SQLBinaryOperator.GreaterThan, //
    new SQLNumberExpr(offset), JdbcConstants.ORACLE));
    return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 10 with SQLNumberExpr

use of com.alibaba.druid.sql.ast.expr.SQLNumberExpr in project druid by alibaba.

the class PagerUtils method limitSQLServer.

private static String limitSQLServer(SQLSelect select, String dbType, int offset, int count) {
    SQLSelectQuery query = select.getQuery();
    SQLBinaryOpExpr gt = new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.GreaterThan, //
    new SQLNumberExpr(offset), JdbcConstants.SQL_SERVER);
    SQLBinaryOpExpr lteq = new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.LessThanOrEqual, //
    new SQLNumberExpr(count + offset), JdbcConstants.SQL_SERVER);
    SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq, JdbcConstants.SQL_SERVER);
    if (query instanceof SQLSelectQueryBlock) {
        SQLServerSelectQueryBlock queryBlock = (SQLServerSelectQueryBlock) query;
        if (offset <= 0) {
            queryBlock.setTop(new SQLServerTop(new SQLNumberExpr(count)));
            return SQLUtils.toSQLString(select, dbType);
        }
        SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
        SQLOrderBy orderBy = select.getOrderBy();
        aggregateExpr.setOver(new SQLOver(orderBy));
        select.setOrderBy(null);
        queryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
        SQLServerSelectQueryBlock countQueryBlock = new SQLServerSelectQueryBlock();
        countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
        countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
        countQueryBlock.setWhere(pageCondition);
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    SQLServerSelectQueryBlock countQueryBlock = new SQLServerSelectQueryBlock();
    countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
    countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
    if (offset <= 0) {
        countQueryBlock.setTop(new SQLServerTop(new SQLNumberExpr(count)));
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
    SQLOrderBy orderBy = select.getOrderBy();
    aggregateExpr.setOver(new SQLOver(orderBy));
    select.setOrderBy(null);
    countQueryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
    SQLServerSelectQueryBlock offsetQueryBlock = new SQLServerSelectQueryBlock();
    offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
    offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
    offsetQueryBlock.setWhere(pageCondition);
    return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLOver(com.alibaba.druid.sql.ast.SQLOver) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLServerTop(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop) SQLServerSelectQueryBlock(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Aggregations

SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)11 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)7 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)6 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)4 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)4 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)3 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)3 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)3 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)3 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)3 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)2 SQLOver (com.alibaba.druid.sql.ast.SQLOver)2 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)2 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)1 MockResultSet (com.alibaba.druid.mock.MockResultSet)1 MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)1