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);
}
}
}
}
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;
}
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));
}
}
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);
}
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;
}
Aggregations