Search in sources :

Example 1 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class LogFilter method statement_executeErrorAfter.

@Override
protected void statement_executeErrorAfter(StatementProxy statement, String sql, Throwable error) {
    if (this.isStatementLogErrorEnabled()) {
        if (!isStatementExecutableSqlLogEnable()) {
            statementLogError("{conn-" + statement.getConnectionProxy().getId() + ", " + stmtId(statement) + "} execute error. " + sql, error);
        } else {
            int parametersSize = statement.getParametersSize();
            if (parametersSize > 0) {
                List<Object> parameters = new ArrayList<Object>(parametersSize);
                for (int i = 0; i < parametersSize; ++i) {
                    JdbcParameter jdbcParam = statement.getParameter(i);
                    parameters.add(jdbcParam != null ? jdbcParam.getValue() : null);
                }
                DbType dbType = DbType.of(statement.getConnectionProxy().getDirectDataSource().getDbType());
                String formattedSql = SQLUtils.format(sql, dbType, parameters, this.statementSqlFormatOption);
                statementLogError("{conn-" + statement.getConnectionProxy().getId() + ", " + stmtId(statement) + "} execute error. " + formattedSql, error);
            } else {
                statementLogError("{conn-" + statement.getConnectionProxy().getId() + ", " + stmtId(statement) + "} execute error. " + sql, error);
            }
        }
    }
}
Also used : JdbcParameter(com.alibaba.druid.proxy.jdbc.JdbcParameter) ArrayList(java.util.ArrayList) DbType(com.alibaba.druid.DbType)

Example 2 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class SQLSelect method getDbType.

public DbType getDbType() {
    DbType dbType = null;
    SQLObject parent = this.getParent();
    if (parent instanceof SQLStatement) {
        dbType = ((SQLStatement) parent).getDbType();
    }
    if (dbType == null && parent instanceof OracleSQLObject) {
        dbType = DbType.oracle;
    }
    if (dbType == null && query instanceof SQLSelectQueryBlock) {
        dbType = ((SQLSelectQueryBlock) query).dbType;
    }
    return dbType;
}
Also used : OracleSQLObject(com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject) OracleSQLObject(com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject) DbType(com.alibaba.druid.DbType)

Example 3 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class TabledDataPrinter method _printSqlData.

public static void _printSqlData(List<Map<String, Object>> content, Option opt) {
    PrintStream out = opt.getPrintStream();
    if (opt.getId() != -1) {
        List<Map<String, Object>> matchedContent = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> sqlStat : content) {
            Integer idStr = (Integer) sqlStat.get("ID");
            if (idStr == opt.getId()) {
                matchedContent.add(sqlStat);
                if (opt.isDetailPrint()) {
                    DbType dbType = DbType.of((String) sqlStat.get("DbType"));
                    String sql = (String) sqlStat.get("SQL");
                    out.println("Formatted SQL:");
                    out.println(SQLUtils.format(sql, dbType));
                    out.println();
                }
                break;
            }
        }
        content = matchedContent;
    }
    if (opt.isDetailPrint()) {
        out.println(getVerticalFormattedOutput(content, sqlColField));
    } else {
        out.println(getFormattedOutput(content, sqlRowTitle, sqlRowField));
    }
}
Also used : PrintStream(java.io.PrintStream) ArrayList(java.util.ArrayList) Map(java.util.Map) DbType(com.alibaba.druid.DbType)

Example 4 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class DruidStatService method getSqlStat.

private String getSqlStat(Integer id) {
    Map<String, Object> map = statManagerFacade.getSqlStatData(id);
    if (map == null) {
        return returnJSONResult(RESULT_CODE_ERROR, null);
    }
    DbType dbType = DbType.of((String) map.get("DbType"));
    String sql = (String) map.get("SQL");
    map.put("formattedSql", SQLUtils.format(sql, dbType));
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
    if (!statementList.isEmpty()) {
        SQLStatement sqlStmt = statementList.get(0);
        SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
        sqlStmt.accept(visitor);
        map.put("parsedTable", visitor.getTables().toString());
        map.put("parsedFields", visitor.getColumns().toString());
        map.put("parsedConditions", visitor.getConditions().toString());
        map.put("parsedRelationships", visitor.getRelationships().toString());
        map.put("parsedOrderbycolumns", visitor.getOrderByColumns().toString());
    }
    DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
    Date maxTimespanOccurTime = (Date) map.get("MaxTimespanOccurTime");
    if (maxTimespanOccurTime != null) {
        map.put("MaxTimespanOccurTime", format.format(maxTimespanOccurTime));
    }
    return returnJSONResult(map == null ? RESULT_CODE_ERROR : RESULT_CODE_SUCCESS, map);
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SimpleDateFormat(java.text.SimpleDateFormat) DbType(com.alibaba.druid.DbType) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) Date(java.util.Date)

Example 5 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class WallVisitorUtils method getValue.

public static Object getValue(WallVisitor visitor, SQLExpr x) {
    if (x != null && x.containsAttribute(EVAL_VALUE)) {
        return getValueFromAttributes(visitor, x);
    }
    if (x instanceof SQLBinaryOpExpr) {
        return getValue(visitor, (SQLBinaryOpExpr) x);
    }
    if (x instanceof SQLBinaryOpExprGroup) {
        return getValue(visitor, (SQLBinaryOpExprGroup) x);
    }
    if (x instanceof SQLBooleanExpr) {
        return ((SQLBooleanExpr) x).getBooleanValue();
    }
    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;
        }
    }
    DbType 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 : Item(com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item) DbType(com.alibaba.druid.DbType)

Aggregations

DbType (com.alibaba.druid.DbType)114 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)76 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)70 ArrayList (java.util.ArrayList)50 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)45 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)21 TableStat (com.alibaba.druid.stat.TableStat)10 JSONArray (com.alibaba.fastjson.JSONArray)10 MySqlExportParameterVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor)5 ExportParameterVisitor (com.alibaba.druid.sql.visitor.ExportParameterVisitor)5 ParserException (com.alibaba.druid.sql.parser.ParserException)4 Map (java.util.Map)4 JdbcParameter (com.alibaba.druid.proxy.jdbc.JdbcParameter)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)2 OracleSQLObject (com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject)2