use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat-Server by MyCATApache.
the class DefaultDruidParser method visitorParse.
/**
* 子类可覆盖(如果该方法解析得不到表名、字段等信息的,就覆盖该方法,覆盖成空方法,然后通过statementPparse去解析)
* 通过visitor解析:有些类型的Statement通过visitor解析得不到表名、
* @param stmt
*/
@Override
public void visitorParse(RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor) throws SQLNonTransientException {
stmt.accept(visitor);
ctx.setVisitor(visitor);
if (stmt instanceof SQLSelectStatement) {
SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
if (query instanceof MySqlSelectQueryBlock) {
if (((MySqlSelectQueryBlock) query).isForUpdate()) {
rrs.setSelectForUpdate(true);
}
}
}
List<List<Condition>> mergedConditionList = new ArrayList<List<Condition>>();
if (visitor.hasOrCondition()) {
//包含or语句
//TODO
//根据or拆分
mergedConditionList = visitor.splitConditions();
} else {
//不包含OR语句
mergedConditionList.add(visitor.getConditions());
}
if (visitor.getAliasMap() != null) {
for (Map.Entry<String, String> entry : visitor.getAliasMap().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key != null && key.indexOf("`") >= 0) {
key = key.replaceAll("`", "");
}
if (value != null && value.indexOf("`") >= 0) {
value = value.replaceAll("`", "");
}
//表名前面带database的,去掉
if (key != null) {
int pos = key.indexOf(".");
if (pos > 0) {
key = key.substring(pos + 1);
}
tableAliasMap.put(key.toUpperCase(), value);
}
// else {
// tableAliasMap.put(key, value);
// }
}
ctx.addTables(visitor.getTables());
visitor.getAliasMap().putAll(tableAliasMap);
ctx.setTableAliasMap(tableAliasMap);
}
ctx.setRouteCalculateUnits(this.buildRouteCalculateUnits(visitor, mergedConditionList));
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat-Server by MyCATApache.
the class HintSQLHandler method parseProcedure.
private Procedure parseProcedure(String sql, Map hintMap) {
boolean fields = hintMap.containsKey("list_fields");
boolean isResultList = hintMap != null && ("list".equals(hintMap.get("result_type")) || fields);
Procedure procedure = new Procedure();
procedure.setOriginSql(sql);
procedure.setResultList(isResultList);
List<String> sqls = Splitter.on(";").trimResults().splitToList(sql);
Set<String> outSet = new HashSet<>();
for (int i = sqls.size() - 1; i >= 0; i--) {
String s = sqls.get(i);
if (Strings.isNullOrEmpty(s)) {
continue;
}
SQLStatementParser parser = new MySqlStatementParser(s);
SQLStatement statement = parser.parseStatement();
if (statement instanceof SQLSelectStatement) {
MySqlSelectQueryBlock selectQuery = (MySqlSelectQueryBlock) ((SQLSelectStatement) statement).getSelect().getQuery();
if (selectQuery != null) {
List<SQLSelectItem> selectItems = selectQuery.getSelectList();
for (SQLSelectItem selectItem : selectItems) {
String select = selectItem.toString();
outSet.add(select);
procedure.getSelectColumns().add(select);
}
}
procedure.setSelectSql(s);
} else if (statement instanceof SQLCallStatement) {
SQLCallStatement sqlCallStatement = (SQLCallStatement) statement;
procedure.setName(sqlCallStatement.getProcedureName().getSimpleName());
List<SQLExpr> paramterList = sqlCallStatement.getParameters();
for (int i1 = 0; i1 < paramterList.size(); i1++) {
SQLExpr sqlExpr = paramterList.get(i1);
String pName = sqlExpr.toString();
String pType = outSet.contains(pName) ? ProcedureParameter.OUT : ProcedureParameter.IN;
ProcedureParameter parameter = new ProcedureParameter();
parameter.setIndex(i1 + 1);
parameter.setName(pName);
parameter.setParameterType(pType);
if (pName.startsWith("@")) {
procedure.getParamterMap().put(pName, parameter);
} else {
procedure.getParamterMap().put(String.valueOf(i1 + 1), parameter);
}
}
procedure.setCallSql(s);
} else if (statement instanceof SQLSetStatement) {
procedure.setSetSql(s);
SQLSetStatement setStatement = (SQLSetStatement) statement;
List<SQLAssignItem> sets = setStatement.getItems();
for (SQLAssignItem set : sets) {
String name = set.getTarget().toString();
SQLExpr value = set.getValue();
ProcedureParameter parameter = procedure.getParamterMap().get(name);
if (parameter != null) {
if (value instanceof SQLIntegerExpr) {
parameter.setValue(((SQLIntegerExpr) value).getNumber());
parameter.setJdbcType(Types.INTEGER);
} else if (value instanceof SQLNumberExpr) {
parameter.setValue(((SQLNumberExpr) value).getNumber());
parameter.setJdbcType(Types.NUMERIC);
} else if (value instanceof SQLTextLiteralExpr) {
parameter.setValue(((SQLTextLiteralExpr) value).getText());
parameter.setJdbcType(Types.VARCHAR);
} else if (value instanceof SQLValuableExpr) {
parameter.setValue(((SQLValuableExpr) value).getValue());
parameter.setJdbcType(Types.VARCHAR);
}
}
}
}
}
if (fields) {
String list_fields = (String) hintMap.get("list_fields");
List<String> listFields = Splitter.on(",").trimResults().splitToList(list_fields);
for (String field : listFields) {
if (!procedure.getParamterMap().containsKey(field)) {
ProcedureParameter parameter = new ProcedureParameter();
parameter.setParameterType(ProcedureParameter.OUT);
parameter.setName(field);
parameter.setJdbcType(-10);
parameter.setIndex(procedure.getParamterMap().size() + 1);
procedure.getParamterMap().put(field, parameter);
}
}
procedure.getListFields().addAll(listFields);
}
return procedure;
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat-Server by MyCATApache.
the class DruidSelectOracleParser method statementParse.
@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) {
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
//从mysql解析过来
if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
Limit limit = mysqlSelectQuery.getLimit();
if (limit == null) {
//使用oracle的解析,否则会有部分oracle语法识别错误
OracleStatementParser oracleParser = new OracleStatementParser(getCtx().getSql());
SQLSelectStatement oracleStmt = (SQLSelectStatement) oracleParser.parseStatement();
selectStmt = oracleStmt;
SQLSelectQuery oracleSqlSelectQuery = oracleStmt.getSelect().getQuery();
if (oracleSqlSelectQuery instanceof OracleSelectQueryBlock) {
parseNativePageSql(oracleStmt, rrs, (OracleSelectQueryBlock) oracleSqlSelectQuery, schema);
}
}
if (isNeedParseOrderAgg) {
parseOrderAggGroupMysql(schema, selectStmt, rrs, mysqlSelectQuery);
//更改canRunInReadDB属性
if ((mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) && rrs.isAutocommit() == false) {
rrs.setCanRunInReadDB(false);
}
}
}
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat-Server by MyCATApache.
the class DruidSelectParser method changeSql.
/**
* 改写sql:需要加limit的加上
*/
@Override
public void changeSql(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, LayerCachePool cachePool) throws SQLNonTransientException {
tryRoute(schema, rrs, cachePool);
rrs.copyLimitToNodes();
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
int limitStart = 0;
int limitSize = schema.getDefaultMaxLimit();
//clear group having
SQLSelectGroupByClause groupByClause = mysqlSelectQuery.getGroupBy();
// Modified by winbill, 20160614, do NOT include having clause when routing to multiple nodes
if (groupByClause != null && groupByClause.getHaving() != null && isRoutMultiNode(schema, rrs)) {
groupByClause.setHaving(null);
}
Map<String, Map<String, Set<ColumnRoutePair>>> allConditions = getAllConditions();
boolean isNeedAddLimit = isNeedAddLimit(schema, rrs, mysqlSelectQuery, allConditions);
if (isNeedAddLimit) {
Limit limit = new Limit();
limit.setRowCount(new SQLIntegerExpr(limitSize));
mysqlSelectQuery.setLimit(limit);
rrs.setLimitSize(limitSize);
String sql = getSql(rrs, stmt, isNeedAddLimit);
rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), sql, 0, limitSize, true);
}
Limit limit = mysqlSelectQuery.getLimit();
if (limit != null && !isNeedAddLimit) {
SQLIntegerExpr offset = (SQLIntegerExpr) limit.getOffset();
SQLIntegerExpr count = (SQLIntegerExpr) limit.getRowCount();
if (offset != null) {
limitStart = offset.getNumber().intValue();
rrs.setLimitStart(limitStart);
}
if (count != null) {
limitSize = count.getNumber().intValue();
rrs.setLimitSize(limitSize);
}
if (isNeedChangeLimit(rrs)) {
Limit changedLimit = new Limit();
changedLimit.setRowCount(new SQLIntegerExpr(limitStart + limitSize));
if (offset != null) {
if (limitStart < 0) {
String msg = "You have an error in your SQL syntax; check the manual that " + "corresponds to your MySQL server version for the right syntax to use near '" + limitStart + "'";
throw new SQLNonTransientException(ErrorCode.ER_PARSE_ERROR + " - " + msg);
} else {
changedLimit.setOffset(new SQLIntegerExpr(0));
}
}
mysqlSelectQuery.setLimit(changedLimit);
String sql = getSql(rrs, stmt, isNeedAddLimit);
rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), sql, 0, limitStart + limitSize, true);
//设置改写后的sql
ctx.setSql(sql);
} else {
rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), getCtx().getSql(), rrs.getLimitStart(), rrs.getLimitSize(), true);
// ctx.setSql(nativeSql);
}
}
if (rrs.isDistTable()) {
SQLTableSource from = mysqlSelectQuery.getFrom();
for (RouteResultsetNode node : rrs.getNodes()) {
SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
sqlIdentifierExpr.setParent(from);
sqlIdentifierExpr.setName(node.getSubTableName());
SQLExprTableSource from2 = new SQLExprTableSource(sqlIdentifierExpr);
mysqlSelectQuery.setFrom(from2);
node.setStatement(stmt.toString());
}
}
rrs.setCacheAble(isNeedCache(schema, rrs, mysqlSelectQuery, allConditions));
}
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat-Server by MyCATApache.
the class DruidSelectParser method statementParse.
@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) {
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
parseOrderAggGroupMysql(schema, stmt, rrs, mysqlSelectQuery);
//更改canRunInReadDB属性
if ((mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) && rrs.isAutocommit() == false) {
rrs.setCanRunInReadDB(false);
}
} else if (sqlSelectQuery instanceof MySqlUnionQuery) {
// MySqlUnionQuery unionQuery = (MySqlUnionQuery)sqlSelectQuery;
// MySqlSelectQueryBlock left = (MySqlSelectQueryBlock)unionQuery.getLeft();
// MySqlSelectQueryBlock right = (MySqlSelectQueryBlock)unionQuery.getLeft();
// System.out.println();
}
}
Aggregations