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