use of com.alibaba.druid.sql.ast.SQLObject 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);
}
use of com.alibaba.druid.sql.ast.SQLObject in project Mycat-Server by MyCATApache.
the class MycatSchemaStatVisitor method getColumn.
@Override
protected Column getColumn(SQLExpr expr) {
Map<String, String> aliasMap = getAliasMap();
if (aliasMap == null) {
return null;
}
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;
if (aliasMap.containsKey(table)) {
table = aliasMap.get(table);
}
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);
}
if (expr instanceof SQLBetweenExpr) {
SQLBetweenExpr betweenExpr = (SQLBetweenExpr) expr;
if (betweenExpr.getTestExpr() != null) {
String tableName = null;
String column = null;
if (betweenExpr.getTestExpr() instanceof SQLPropertyExpr) {
//字段带别名的
tableName = ((SQLIdentifierExpr) ((SQLPropertyExpr) betweenExpr.getTestExpr()).getOwner()).getName();
column = ((SQLPropertyExpr) betweenExpr.getTestExpr()).getName();
SQLObject query = this.subQueryMap.get(tableName);
if (query == null) {
if (aliasMap.containsKey(tableName)) {
tableName = aliasMap.get(tableName);
}
return new Column(tableName, column);
}
return handleSubQueryColumn(tableName, column);
} else if (betweenExpr.getTestExpr() instanceof SQLIdentifierExpr) {
column = ((SQLIdentifierExpr) betweenExpr.getTestExpr()).getName();
//字段不带别名的,此处如果是多表,容易出现ambiguous,
//不知道这个字段是属于哪个表的,fdbparser用了defaultTable,即join语句的leftTable
tableName = getOwnerTableName(betweenExpr, column);
}
String table = tableName;
if (aliasMap.containsKey(table)) {
table = aliasMap.get(table);
}
if (variants.containsKey(table)) {
return null;
}
if (table != null && !"".equals(table)) {
return new Column(table, column);
}
}
}
return null;
}
use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class MySqlOutputVisitor method visit.
@Override
public boolean visit(SQLAlterTableStatement x) {
if (x.isIgnore()) {
print0(ucase ? "ALTER IGNORE TABLE " : "alter ignore table ");
} else {
print0(ucase ? "ALTER TABLE " : "alter table ");
}
printTableSourceExpr(x.getName());
incrementIndent();
for (int i = 0; i < x.getItems().size(); ++i) {
SQLAlterTableItem item = x.getItems().get(i);
if (i != 0) {
print(',');
}
println();
item.accept(this);
}
if (x.isRemovePatiting()) {
println();
print0(ucase ? "REMOVE PARTITIONING" : "remove partitioning");
}
if (x.isUpgradePatiting()) {
println();
print0(ucase ? "UPGRADE PARTITIONING" : "upgrade partitioning");
}
if (x.getTableOptions().size() > 0) {
println();
}
decrementIndent();
int i = 0;
for (Map.Entry<String, SQLObject> option : x.getTableOptions().entrySet()) {
String key = option.getKey();
if (i != 0) {
print(' ');
}
print0(ucase ? key : key.toLowerCase());
if ("TABLESPACE".equals(key)) {
print(' ');
option.getValue().accept(this);
continue;
} else if ("UNION".equals(key)) {
print0(" = (");
option.getValue().accept(this);
print(')');
continue;
}
print0(" = ");
option.getValue().accept(this);
i++;
}
return false;
}
use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class SchemaStatVisitor method orderByAddColumn.
private void orderByAddColumn(String table, String columnName, SQLObject expr) {
Column column = new Column(table, columnName);
SQLObject parent = expr.getParent();
if (parent instanceof SQLSelectOrderByItem) {
SQLOrderingSpecification type = ((SQLSelectOrderByItem) parent).getType();
column.getAttributes().put("orderBy.type", type);
}
orderByColumns.add(column);
}
use of com.alibaba.druid.sql.ast.SQLObject in project druid by alibaba.
the class MySqlWallVisitor method visit.
public boolean visit(SQLPropertyExpr x) {
if (x.getOwner() instanceof SQLVariantRefExpr) {
SQLVariantRefExpr varExpr = (SQLVariantRefExpr) x.getOwner();
SQLObject parent = x.getParent();
String varName = varExpr.getName();
if (varName.equalsIgnoreCase("@@session") || varName.equalsIgnoreCase("@@global")) {
if (!(parent instanceof SQLSelectItem) && !(parent instanceof SQLAssignItem)) {
violations.add(new IllegalSQLObjectViolation(ErrorCode.VARIANT_DENY, "variable in condition not allow", toSQL(x)));
return false;
}
if (!checkVar(x.getParent(), x.getName())) {
boolean isTop = WallVisitorUtils.isTopNoneFromSelect(this, x);
if (!isTop) {
boolean allow = true;
if (isDeny(varName) && (WallVisitorUtils.isWhereOrHaving(x) || WallVisitorUtils.checkSqlExpr(varExpr))) {
allow = false;
}
if (!allow) {
violations.add(new IllegalSQLObjectViolation(ErrorCode.VARIANT_DENY, "variable not allow : " + x.getName(), toSQL(x)));
}
}
}
return false;
}
}
WallVisitorUtils.check(this, x);
return true;
}
Aggregations