package io.mycat.route.util;
import java.sql.SQLNonTransientException;
import java.sql.SQLSyntaxErrorException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLCharacterDataType;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.wall.spi.WallVisitorUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import io.mycat.MycatServer;
import io.mycat.backend.datasource.PhysicalDBNode;
import io.mycat.backend.datasource.PhysicalDBPool;
import io.mycat.backend.datasource.PhysicalDatasource;
import io.mycat.backend.mysql.nio.handler.FetchStoreNodeOfChildTableHandler;
import io.mycat.backend.mysql.nio.handler.JDBCFetchStoreNodeOfChildTableHandler;
import io.mycat.cache.LayerCachePool;
import io.mycat.config.ErrorCode;
import io.mycat.config.MycatConfig;
import io.mycat.config.model.SchemaConfig;
import io.mycat.config.model.TableConfig;
import io.mycat.config.model.rule.RuleConfig;
import io.mycat.route.RouteResultset;
import io.mycat.route.RouteResultsetNode;
import io.mycat.route.SessionSQLPair;
import io.mycat.route.function.AbstractPartitionAlgorithm;
import io.mycat.route.function.SlotFunction;
import io.mycat.route.parser.druid.DruidShardingParseInfo;
import io.mycat.route.parser.druid.RouteCalculateUnit;
import io.mycat.server.ServerConnection;
import io.mycat.server.parser.ServerParse;
import io.mycat.sqlengine.mpp.ColumnRoutePair;
import io.mycat.sqlengine.mpp.LoadData;
import io.mycat.util.StringUtil;
/**
* 从ServerRouterUtil中抽取的一些公用方法,路由解析工具类
* @author wang.dw
*
*/
public class RouterUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(RouterUtil.class);
/**
* 移除执行语句中的数据库名
*
* @param stmt 执行语句
* @param schema 数据库名
* @return 执行语句
* @author mycat
*
* @modification 修正移除schema的方法
* @date 2016/12/29
* @modifiedBy Hash Zhang
*
*/
public static String removeSchema(String stmt, String schema) {
final String upStmt = stmt.toUpperCase();
final String upSchema = schema.toUpperCase() + ".";
final String upSchema2 = new StringBuilder("`").append(schema.toUpperCase()).append("`.").toString();
int strtPos = 0;
int indx = 0;
int indx1 = upStmt.indexOf(upSchema, strtPos);
int indx2 = upStmt.indexOf(upSchema2, strtPos);
boolean flag = indx1 < indx2 ? indx1 == -1 : indx2 != -1;
indx = !flag ? indx1 > 0 ? indx1 : indx2 : indx2 > 0 ? indx2 : indx1;
if (indx < 0) {
return stmt;
}
int firstE = upStmt.indexOf("'");
int endE = upStmt.lastIndexOf("'");
StringBuilder sb = new StringBuilder();
while (indx > 0) {
sb.append(stmt.substring(strtPos, indx));
if (flag) {
strtPos = indx + upSchema2.length();
} else {
strtPos = indx + upSchema.length();
}
if (indx > firstE && indx < endE && countChar(stmt, indx) % 2 == 1) {
sb.append(stmt.substring(indx, indx + schema.length() + 1));
}
indx1 = upStmt.indexOf(upSchema, strtPos);
indx2 = upStmt.indexOf(upSchema2, strtPos);
flag = indx1 < indx2 ? indx1 == -1 : indx2 != -1;
indx = !flag ? indx1 > 0 ? indx1 : indx2 : indx2 > 0 ? indx2 : indx1;
}
sb.append(stmt.substring(strtPos));
return sb.toString();
}
private static int countChar(String sql,int end)
{
int count=0;
boolean skipChar = false;
for (int i = 0; i < end; i++) {
if(sql.charAt(i)=='\'' && !skipChar) {
count++;
skipChar = false;
}else if( sql.charAt(i)=='\\'){
skipChar = true;
}else{
skipChar = false;
}
}
return count;
}
/**
* 获取第一个节点作为路由
*
* @param rrs 数据路由集合
* @param dataNode 数据库所在节点
* @param stmt 执行语句
* @return 数据路由集合
*
* @author mycat
*/
public static RouteResultset routeToSingleNode(RouteResultset rrs,
String dataNode, String stmt) {
if (dataNode == null) {
return rrs;
}
RouteResultsetNode[] nodes = new RouteResultsetNode[1];
nodes[0] = new RouteResultsetNode(dataNode, rrs.getSqlType(), stmt);//rrs.getStatement()
nodes[0].setSource(rrs);
rrs.setNodes(nodes);
rrs.setFinishedRoute(true);
if(rrs.getDataNodeSlotMap().containsKey(dataNode)){
nodes[0].setSlot(rrs.getDataNodeSlotMap().get(dataNode));
}
if (rrs.getCanRunInReadDB() != null) {
nodes[0].setCanRunInReadDB(rrs.getCanRunInReadDB());
}
if(rrs.getRunOnSlave() != null){
nodes[0].setRunOnSlave(rrs.getRunOnSlave());
}
return rrs;
}
/**
* 修复DDL路由
*
* @return RouteResultset
* @author aStoneGod
*/
public static RouteResultset routeToDDLNode(RouteResultset rrs, int sqlType, String stmt,SchemaConfig schema) throws SQLSyntaxErrorException {
stmt = getFixedSql(stmt);
String tablename = "";
//去除sql前面的注册,如/* ApplicationName=DBeaver 6.0.1 - Main */,这个注册会导致create table出错
stmt = stmt.replaceFirst("\\/\\*.*\\*\\/\\s*", "");
final String upStmt = stmt.toUpperCase();
String rrsStmt = new String(upStmt);
if(upStmt.startsWith("CREATE")){
if (upStmt.contains("CREATE INDEX ") || upStmt.contains("CREATE UNIQUE INDEX ")){
tablename = RouterUtil.getTableName(stmt, RouterUtil.getCreateIndexPos(upStmt, 0));
/**
* Date:2017年11月2日
* @author SvenAugustus
修复oracle 语法不支持 drop index i_t_f on t_test,只有drop index i_t_f;
*/
if(!"oracle".equalsIgnoreCase(schema.getDefaultDataNodeDbType())){
int onInd = upStmt.indexOf("ON", 0);
rrsStmt= upStmt.substring(0, onInd);
}
}else {
tablename = RouterUtil.getTableName(stmt, RouterUtil.getCreateTablePos(upStmt, 0));
}
}else if(upStmt.startsWith("DROP")){
if (upStmt.contains("DROP INDEX ")){
tablename = RouterUtil.getTableName(stmt, RouterUtil.getDropIndexPos(upStmt, 0));
}else {
tablename = RouterUtil.getTableName(stmt, RouterUtil.getDropTablePos(upStmt, 0));
}
}else if(upStmt.startsWith("ALTER")){
tablename = RouterUtil.getTableName(stmt, RouterUtil.getAlterTablePos(upStmt, 0));
}else if (upStmt.startsWith("TRUNCATE")){
tablename = RouterUtil.getTableName(stmt, RouterUtil.getTruncateTablePos(upStmt, 0));
}
tablename = tablename.toUpperCase();
if (schema.getTables().containsKey(tablename)){
if(ServerParse.DDL==sqlType){
List<String> dataNodes = new ArrayList<>();
Map<String, TableConfig> tables = schema.getTables();
TableConfig tc=tables.get(tablename);
if (tables != null && (tc != null)) {
dataNodes = tc.getDataNodes();
}
boolean isSlotFunction= tc.getRule() != null && t
没有合适的资源?快使用搜索试试~ 我知道了~
Mycat-Server-java入门
共1179个文件
java:1039个
xml:31个
txt:23个
需积分: 1 0 下载量 111 浏览量
2024-09-05
10:25:52
上传
评论
收藏 5.43MB ZIP 举报
温馨提示
MyCATACIDMySQL, ,MySQL java入门 java入门 java入门 java入门 java入门
资源推荐
资源详情
资源评论
收起资源包目录
Mycat-Server-java入门 (1179个子文件)
dataMigrate.bat 753B
test_stand_select_perf.bat 607B
test_stand_update_perf.bat 606B
test_stand_merge_sel_perf.bat 605B
test_globaltable_insert_perf.bat 605B
startup_nowrap.bat 597B
test_globalseq_insert_perf.bat 596B
test_stand_insert_perf.bat 594B
init_zk_data.bat 576B
testng-reports.css 5KB
testng.css 303B
Dockerfile 367B
schema.dtd 3KB
server.dtd 2KB
rule.dtd 1KB
collapseall.gif 157B
.gitignore 2KB
.gitignore 40B
index.html 9KB
Default test.html 2KB
emailable-report.html 1KB
toc.html 1KB
index.html 731B
testng.xml.html 716B
methods-alphabetical.html 217B
methods.html 217B
index.html 186B
main.html 119B
classes.html 95B
methods-not-run.html 50B
reporter-output.html 39B
groups.html 38B
RouterUtil.java 75KB
ManagerParseShow.java 61KB
DruidMysqlRouteStrategyTest.java 56KB
MycatSchemaStatVisitor.java 50KB
MycatServer.java 45KB
XMLSchemaLoader.java 39KB
MultiNodeQueryHandler.java 36KB
PureJavaCrc32.java 35KB
TimSort.java 34KB
DruidSelectParser.java 34KB
DruidMycatRouteStrategy.java 32KB
SystemConfig.java 32KB
UnsafeRowGrouper.java 31KB
BytesToBytesMap.java 30KB
ServerLoadDataInfileHandler.java 30KB
UTF8String.java 29KB
BytesTools.java 29KB
ErrorCode.java 29KB
ServerParserTest.java 29KB
UTF8StringSuite.java 28KB
JDBCConnection.java 28KB
GlobalTableUtil.java 25KB
MongoResultSet.java 25KB
SequoiaResultSet.java 25KB
MySQLConnection.java 24KB
PhysicalDBPool.java 24KB
FrontendConnection.java 23KB
UnsafeExternalSorter.java 23KB
RingBuffer.java 23KB
MysqlDefs.java 23KB
NonBlockingSession.java 23KB
ServerParseSet.java 23KB
BinlogStream.java 23KB
AbstractBytesToBytesMapSuite.java 22KB
SwitchCommitListener.java 22KB
ServerParse.java 21KB
MigrateHandler.java 21KB
ManagerParserTest.java 21KB
PhysicalDatasource.java 20KB
ShareJoin.java 19KB
DataNodeMergeManager.java 19KB
SingleNodeHandler.java 18KB
DecryptUtil.java 18KB
UnsafeRow.java 17KB
FastByteOperations.java 17KB
DataNodeMemoryManager.java 16KB
AbstractConnection.java 16KB
StringUtil.java 16KB
ServerConnection.java 16KB
ParseUtil.java 16KB
MongoSQLParser.java 15KB
PostgreSQLBackendConnection.java 15KB
MultiNodeCoordinator.java 15KB
Platform.java 14KB
DDLRouteTest.java 14KB
ByteBufferUtil.java 14KB
DruidInsertParser.java 14KB
UnsafeExternalSorterSuite.java 14KB
ByteUtil.java 14KB
MycatSchemaStatVisitorTest.java 14KB
SequoiaSQLParser.java 14KB
XMLServerLoader.java 14KB
RouteResultset.java 13KB
ServerParseSelect.java 13KB
PostgreSQLBackendConnectionHandler.java 13KB
RandomDataValueUtil.java 13KB
DistributedSequenceHandler.java 13KB
ShowDirectMemory.java 12KB
共 1179 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12
资源评论
csbysj2020
- 粉丝: 2431
- 资源: 5449
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 多表查询文件students.txt
- yolo算法-垃圾检测数据集-5659张图像带标签-金属-硬纸板-玻璃-塑料.zip
- yolo算法-安全帽-反光衣智慧工地数据集-7538张图像带标签-靴子-头盔-背心.zip
- yolo算法-垃圾桶数据集-6238张图像带标签-金属-纸张-硬纸板-塑料-玻璃.zip
- yolo算法-垃圾桶数据集-6392张图像带标签-纸板-铝-纸张-硬纸板-塑料-玻璃.zip
- yolo算法-卡车工作数据集-1062张图像带标签-底盘工作-堆垛机-人-底盘已卸载.zip
- 提供了丰富多样的 Spring、SpringMVC、MyBatis 和 Spring Boot 案例
- 各省环境规制强度数据(Excel/Shp格式)
- 和橘子学AI绘图【Midjourney】课程
- 聚焦于 Java 安全和框架漏洞复现、代码审计等内容的项目,同时还涉及 Java 基础语法以及日常知识点的分享
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功