Search in sources :

Example 6 with SQLMethodInvokeExpr

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

the class OracleSchemaStatVisitor method visit.

public boolean visit(OracleSelectTableReference x) {
    SQLExpr expr = x.getExpr();
    if (expr instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr methodInvoke = (SQLMethodInvokeExpr) expr;
        if ("TABLE".equalsIgnoreCase(methodInvoke.getMethodName()) && methodInvoke.getParameters().size() == 1) {
            expr = methodInvoke.getParameters().get(0);
        }
    }
    Map<String, String> aliasMap = getAliasMap();
    if (expr instanceof SQLName) {
        String ident;
        if (expr instanceof SQLPropertyExpr) {
            String owner = ((SQLPropertyExpr) expr).getOwner().toString();
            String name = ((SQLPropertyExpr) expr).getName();
            if (aliasMap.containsKey(owner)) {
                owner = aliasMap.get(owner);
            }
            ident = owner + "." + name;
        } else {
            ident = expr.toString();
        }
        if (containsSubQuery(ident)) {
            return false;
        }
        if ("DUAL".equalsIgnoreCase(ident)) {
            return false;
        }
        x.putAttribute(ATTR_TABLE, ident);
        TableStat stat = getTableStat(ident);
        Mode mode = getMode();
        switch(mode) {
            case Delete:
                stat.incrementDeleteCount();
                break;
            case Insert:
                stat.incrementInsertCount();
                break;
            case Update:
                stat.incrementUpdateCount();
                break;
            case Select:
                stat.incrementSelectCount();
                break;
            case Merge:
                stat.incrementMergeCount();
                break;
            default:
                break;
        }
        putAliasMap(aliasMap, x.getAlias(), ident);
        putAliasMap(aliasMap, ident, ident);
        return false;
    }
    accept(x.getExpr());
    return false;
}
Also used : SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) Mode(com.alibaba.druid.stat.TableStat.Mode) SQLName(com.alibaba.druid.sql.ast.SQLName) TableStat(com.alibaba.druid.stat.TableStat) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 7 with SQLMethodInvokeExpr

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

the class SchemaStatVisitor method getColumn.

protected Column getColumn(SQLExpr expr) {
    Map<String, String> aliasMap = getAliasMap();
    if (aliasMap == null) {
        return null;
    }
    if (expr instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr methodInvokeExp = (SQLMethodInvokeExpr) expr;
        if (methodInvokeExp.getParameters().size() == 1) {
            SQLExpr firstExpr = methodInvokeExp.getParameters().get(0);
            return getColumn(firstExpr);
        }
    }
    if (expr instanceof SQLCastExpr) {
        expr = ((SQLCastExpr) expr).getExpr();
    }
    if (expr instanceof SQLPropertyExpr) {
        SQLExpr owner = ((SQLPropertyExpr) expr).getOwner();
        String column = ((SQLPropertyExpr) expr).getName();
        if (owner instanceof SQLIdentifierExpr) {
            String tableName = ((SQLIdentifierExpr) owner).getName();
            String table = tableName;
            String tableNameLower = tableName.toLowerCase();
            if (aliasMap.containsKey(tableNameLower)) {
                table = aliasMap.get(tableNameLower);
            }
            if (containsSubQuery(tableNameLower)) {
                table = null;
            }
            if (variants.containsKey(table)) {
                return null;
            }
            if (table != null) {
                return new Column(table, column);
            }
            return handleSubQueryColumn(tableName, column);
        }
        return null;
    }
    if (expr instanceof SQLIdentifierExpr) {
        Column attrColumn = (Column) expr.getAttribute(ATTR_COLUMN);
        if (attrColumn != null) {
            return attrColumn;
        }
        String column = ((SQLIdentifierExpr) expr).getName();
        String table = getCurrentTable();
        if (table != null && aliasMap.containsKey(table)) {
            table = aliasMap.get(table);
            if (table == null) {
                return null;
            }
        }
        if (table != null) {
            return new Column(table, column);
        }
        if (variants.containsKey(column)) {
            return null;
        }
        return new Column("UNKNOWN", column);
    }
    return null;
}
Also used : SQLCastExpr(com.alibaba.druid.sql.ast.expr.SQLCastExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) Column(com.alibaba.druid.stat.TableStat.Column) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 8 with SQLMethodInvokeExpr

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

the class WallVisitorUtils method getValue.

public static Object getValue(WallVisitor visitor, SQLExpr x) {
    if (x != null && x.getAttributes().containsKey(EVAL_VALUE)) {
        return getValueFromAttributes(visitor, x);
    }
    if (x instanceof SQLBinaryOpExpr) {
        return getValue(visitor, (SQLBinaryOpExpr) x);
    }
    if (x instanceof SQLBooleanExpr) {
        return ((SQLBooleanExpr) x).getValue();
    }
    if (x instanceof SQLNumericLiteralExpr) {
        return ((SQLNumericLiteralExpr) x).getNumber();
    }
    if (x instanceof SQLCharExpr) {
        return ((SQLCharExpr) x).getText();
    }
    if (x instanceof SQLNCharExpr) {
        return ((SQLNCharExpr) x).getText();
    }
    if (x instanceof SQLNotExpr) {
        Object result = getValue(visitor, ((SQLNotExpr) x).getExpr());
        if (result instanceof Boolean) {
            return !((Boolean) result).booleanValue();
        }
    }
    if (x instanceof SQLQueryExpr) {
        if (isSimpleCountTableSource(visitor, ((SQLQueryExpr) x).getSubQuery())) {
            return Integer.valueOf(1);
        }
        if (isSimpleCaseTableSource(visitor, ((SQLQueryExpr) x).getSubQuery())) {
            SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) ((SQLQueryExpr) x).getSubQuery().getQuery();
            SQLCaseExpr caseExpr = (SQLCaseExpr) queryBlock.getSelectList().get(0).getExpr();
            Object result = getValue(caseExpr);
            if (visitor != null && !visitor.getConfig().isCaseConditionConstAllow()) {
                boolean leftIsName = false;
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    SQLExpr left = ((SQLBinaryOpExpr) x.getParent()).getLeft();
                    if (left instanceof SQLName) {
                        leftIsName = true;
                    }
                }
                if (!leftIsName && result != null) {
                    addViolation(visitor, ErrorCode.CONST_CASE_CONDITION, "const case condition", caseExpr);
                }
            }
            return result;
        }
    }
    String dbType = null;
    if (visitor != null) {
        dbType = visitor.getDbType();
    }
    if (//
    x instanceof SQLMethodInvokeExpr || //
    x instanceof SQLBetweenExpr || //
    x instanceof SQLInListExpr || //
    x instanceof SQLUnaryExpr) {
        return eval(visitor, dbType, x, Collections.emptyList());
    }
    if (x instanceof SQLCaseExpr) {
        if (visitor != null && !visitor.getConfig().isCaseConditionConstAllow()) {
            SQLCaseExpr caseExpr = (SQLCaseExpr) x;
            boolean leftIsName = false;
            if (caseExpr.getParent() instanceof SQLBinaryOpExpr) {
                SQLExpr left = ((SQLBinaryOpExpr) caseExpr.getParent()).getLeft();
                if (left instanceof SQLName) {
                    leftIsName = true;
                }
            }
            if (!leftIsName && caseExpr.getValueExpr() == null && caseExpr.getItems().size() > 0) {
                SQLCaseExpr.Item item = caseExpr.getItems().get(0);
                Object conditionVal = getValue(visitor, item.getConditionExpr());
                Object itemVal = getValue(visitor, item.getValueExpr());
                if (conditionVal instanceof Boolean && itemVal != null) {
                    addViolation(visitor, ErrorCode.CONST_CASE_CONDITION, "const case condition", caseExpr);
                }
            }
        }
        return eval(visitor, dbType, x, Collections.emptyList());
    }
    return null;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) Item(com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLNotExpr(com.alibaba.druid.sql.ast.expr.SQLNotExpr) SQLBetweenExpr(com.alibaba.druid.sql.ast.expr.SQLBetweenExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr) SQLCaseExpr(com.alibaba.druid.sql.ast.expr.SQLCaseExpr)

Example 9 with SQLMethodInvokeExpr

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

the class EqualTest_extract_oracle method test_exits.

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

Example 10 with SQLMethodInvokeExpr

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

the class ParseContext method evalExpression.

private ValuePair evalExpression(final DatabaseType databaseType, final SQLObject sqlObject, final List<Object> parameters) {
    if (sqlObject instanceof SQLMethodInvokeExpr) {
        // TODO 解析函数中的sharingValue不支持
        return null;
    }
    SQLEvalVisitor visitor;
    switch(databaseType.name().toLowerCase()) {
        case JdbcUtils.MYSQL:
        case JdbcUtils.H2:
            visitor = new MySQLEvalVisitor();
            break;
        default:
            visitor = SQLEvalVisitorUtils.createEvalVisitor(databaseType.name());
    }
    visitor.setParameters(parameters);
    sqlObject.accept(visitor);
    Object value = SQLEvalVisitorUtils.getValue(sqlObject);
    if (null == value) {
        // TODO 对于NULL目前解析为空字符串,此处待考虑解决方法
        return null;
    }
    Comparable<?> finalValue;
    if (value instanceof Comparable<?>) {
        finalValue = (Comparable<?>) value;
    } else {
        finalValue = "";
    }
    Integer index = (Integer) sqlObject.getAttribute(MySQLEvalVisitor.EVAL_VAR_INDEX);
    if (null == index) {
        index = -1;
    }
    return new ValuePair(finalValue, index);
}
Also used : SQLEvalVisitor(com.alibaba.druid.sql.visitor.SQLEvalVisitor) MySQLEvalVisitor(com.dangdang.ddframe.rdb.sharding.parser.visitor.basic.mysql.MySQLEvalVisitor) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) MySQLEvalVisitor(com.dangdang.ddframe.rdb.sharding.parser.visitor.basic.mysql.MySQLEvalVisitor) SQLObject(com.alibaba.druid.sql.ast.SQLObject)

Aggregations

SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)14 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)10 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 ParserException (com.alibaba.druid.sql.parser.ParserException)3 SQLName (com.alibaba.druid.sql.ast.SQLName)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)2 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)2 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)2 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)2 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)1 MockResultSet (com.alibaba.druid.mock.MockResultSet)1 MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)1 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)1 SQLOver (com.alibaba.druid.sql.ast.SQLOver)1