use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project druid by alibaba.
the class WallVisitorUtils method isSimpleConstExpr.
private static boolean isSimpleConstExpr(SQLExpr sqlExpr) {
List<SQLExpr> parts = getParts(sqlExpr);
if (parts.isEmpty()) {
return false;
}
for (SQLExpr part : parts) {
if (isFirst(part)) {
Object evalValue = part.getAttribute(EVAL_VALUE);
if (evalValue == null) {
if (part instanceof SQLBooleanExpr) {
evalValue = ((SQLBooleanExpr) part).getValue();
} else if (part instanceof SQLNumericLiteralExpr) {
evalValue = ((SQLNumericLiteralExpr) part).getNumber();
} else if (part instanceof SQLCharExpr) {
evalValue = ((SQLCharExpr) part).getText();
} else if (part instanceof SQLNCharExpr) {
evalValue = ((SQLNCharExpr) part).getText();
}
}
Boolean result = SQLEvalVisitorUtils.castToBoolean(evalValue);
if (result != null && result) {
return true;
}
}
boolean isSimpleConstExpr = false;
if (part == sqlExpr || part instanceof SQLLiteralExpr) {
isSimpleConstExpr = true;
} else if (part instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) part;
if (binaryOpExpr.getOperator() == SQLBinaryOperator.Equality || binaryOpExpr.getOperator() == SQLBinaryOperator.NotEqual || binaryOpExpr.getOperator() == SQLBinaryOperator.GreaterThan) {
if (binaryOpExpr.getLeft() instanceof SQLIntegerExpr && binaryOpExpr.getRight() instanceof SQLIntegerExpr) {
isSimpleConstExpr = true;
}
}
}
if (!isSimpleConstExpr) {
return false;
}
}
return true;
}
use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project Mycat_plus by coderczp.
the class DruidCreateTableParser method statementParse.
@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
MySqlCreateTableStatement createStmt = (MySqlCreateTableStatement) stmt;
if (createStmt.getQuery() != null) {
String msg = "create table from other table not supported :" + stmt;
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
String tableName = StringUtil.removeBackquote(createStmt.getTableSource().toString().toUpperCase());
if (schema.getTables().containsKey(tableName)) {
TableConfig tableConfig = schema.getTables().get(tableName);
AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
if (algorithm instanceof SlotFunction) {
SQLColumnDefinition column = new SQLColumnDefinition();
column.setDataType(new SQLCharacterDataType("int"));
column.setName(new SQLIdentifierExpr("_slot"));
column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改"));
((SQLCreateTableStatement) stmt).getTableElementList().add(column);
String sql = createStmt.toString();
rrs.setStatement(sql);
ctx.setSql(sql);
}
}
ctx.addTable(tableName);
}
use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project Mycat_plus by coderczp.
the class MycatSchemaStatVisitor method visit.
@Override
public boolean visit(SQLBetweenExpr x) {
String begin = null;
if (x.beginExpr instanceof SQLCharExpr) {
begin = (String) ((SQLCharExpr) x.beginExpr).getValue();
} else {
begin = x.beginExpr.toString();
}
String end = null;
if (x.endExpr instanceof SQLCharExpr) {
end = (String) ((SQLCharExpr) x.endExpr).getValue();
} else {
end = x.endExpr.toString();
}
Column column = getColumn(x);
if (column == null) {
return true;
}
Condition condition = null;
for (Condition item : this.getConditions()) {
if (item.getColumn().equals(column) && item.getOperator().equals("between")) {
condition = item;
break;
}
}
if (condition == null) {
condition = new Condition();
condition.setColumn(column);
condition.setOperator("between");
this.conditions.add(condition);
}
condition.getValues().add(begin);
condition.getValues().add(end);
return true;
}
use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project Mycat_plus by coderczp.
the class RouterUtil method changeCreateTable.
private static String changeCreateTable(SchemaConfig schema, String tableName, String sql) {
if (schema.getTables().containsKey(tableName)) {
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement insertStatement = parser.parseStatement();
if (insertStatement instanceof MySqlCreateTableStatement) {
TableConfig tableConfig = schema.getTables().get(tableName);
AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
if (algorithm instanceof SlotFunction) {
SQLColumnDefinition column = new SQLColumnDefinition();
column.setDataType(new SQLCharacterDataType("int"));
column.setName(new SQLIdentifierExpr("_slot"));
column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改"));
((SQLCreateTableStatement) insertStatement).getTableElementList().add(column);
return insertStatement.toString();
}
}
}
return sql;
}
use of com.alibaba.druid.sql.ast.expr.SQLCharExpr in project Mycat_plus by coderczp.
the class ServerLoadDataInfileHandler method buildResultSet.
private RouteResultset buildResultSet(Map<String, LoadData> routeMap) {
// 强制local
statement.setLocal(true);
// 默认druid会过滤掉路径的分隔符,所以这里重新设置下
SQLLiteralExpr fn = new SQLCharExpr(fileName);
statement.setFileName(fn);
String srcStatement = statement.toString();
RouteResultset rrs = new RouteResultset(srcStatement, ServerParse.LOAD_DATA_INFILE_SQL);
rrs.setLoadData(true);
rrs.setStatement(srcStatement);
rrs.setAutocommit(serverConnection.isAutocommit());
rrs.setFinishedRoute(true);
int size = routeMap.size();
RouteResultsetNode[] routeResultsetNodes = new RouteResultsetNode[size];
int index = 0;
for (String dn : routeMap.keySet()) {
RouteResultsetNode rrNode = new RouteResultsetNode(dn, ServerParse.LOAD_DATA_INFILE_SQL, srcStatement);
rrNode.setSource(rrs);
rrNode.setTotalNodeSize(size);
rrNode.setStatement(srcStatement);
LoadData newLoadData = new LoadData();
ObjectUtil.copyProperties(loadData, newLoadData);
newLoadData.setLocal(true);
LoadData loadData1 = routeMap.get(dn);
// if (isHasStoreToFile)
if (// 此处判断是否有保存分库load的临时文件dn1.txt/dn2.txt,不是判断是否有clientTemp.txt
loadData1.getFileName() != null) {
newLoadData.setFileName(loadData1.getFileName());
} else {
newLoadData.setData(loadData1.getData());
}
rrNode.setLoadData(newLoadData);
routeResultsetNodes[index] = rrNode;
index++;
}
rrs.setNodes(routeResultsetNodes);
return rrs;
}
Aggregations