未验证 提交 625b7fb7 编写于 作者: L lorne 提交者: GitHub

Merge pull request #545 from 1991wangliang/dev6.0

SQL分析优化
......@@ -10,13 +10,11 @@ com.codingapi.txlcn.tc.jdbc.JdbcConfiguration,\
com.codingapi.txlcn.tc.jdbc.event.JdbcEventConfiguration,\
com.codingapi.txlcn.tc.jdbc.log.JdbcLogConfiguration,\
com.codingapi.txlcn.tc.jdbc.sql.JdbcSqlConfiguration,\
com.codingapi.txlcn.tc.jdbc.sql.analyse.AnalyseConfiguration,\
com.codingapi.txlcn.tc.jdbc.sql.strategy.StrategyConfiguration,\
com.codingapi.txlcn.tc.reporter.ReporterConfiguration,\
com.codingapi.txlcn.tc.resolver.ResolverConfiguration,\
com.codingapi.txlcn.tc.runner.TcRunnerConfiguration,\
com.codingapi.txlcn.tc.rpc.RpcTransactionConfiguration,\
com.codingapi.txlcn.tc.TCAutoConfiguration,\
com.codingapi.txlcn.tc.jdbc.sql.strategy.MysqlInsertAnalyseStrategy,\
com.codingapi.txlcn.tc.jdbc.sql.strategy.MysqlSqlDeleteAnalyseStrategy,\
com.codingapi.txlcn.tc.jdbc.sql.strategy.MysqlUpdateAnalyseStrategy,\
com.codingapi.txlcn.tc.jdbc.sql.strategy.AnalyseStrategryFactory,\
com.codingapi.txlcn.tc.jdbc.sql.analyse.MysqlAnalyse
\ No newline at end of file
package com.codingapi.txlcn.tc.jdbc.sql;
import com.codingapi.txlcn.tc.config.TxConfig;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.AnalyseStrategryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -21,7 +22,7 @@ public class JdbcSqlConfiguration {
}
@Bean
public SqlAnalyse mysqlSqlParser(){
return new MysqlSqlAnalyse();
public SqlAnalyse mysqlSqlParser(AnalyseStrategryFactory analyseStrategryFactory){
return new MysqlSqlAnalyse(analyseStrategryFactory);
}
}
......@@ -2,14 +2,11 @@ package com.codingapi.txlcn.tc.jdbc.sql;
import com.codingapi.txlcn.p6spy.common.StatementInformation;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.AnalyseStrategryFactory;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.MysqlAnalyseEnum;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.SqlSqlAnalyseHandler;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.update.Update;
import java.io.StringReader;
import java.sql.Connection;
......@@ -23,6 +20,12 @@ import java.sql.SQLException;
@Slf4j
public class MysqlSqlAnalyse implements SqlAnalyse {
private AnalyseStrategryFactory analyseStrategryFactory;
public MysqlSqlAnalyse(AnalyseStrategryFactory analyseStrategryFactory) {
this.analyseStrategryFactory = analyseStrategryFactory;
}
@Override
public String sqlType() {
return "mysql";
......@@ -37,15 +40,13 @@ public class MysqlSqlAnalyse implements SqlAnalyse {
// if else 实现并不是很优雅
CCJSqlParserManager parser = new CCJSqlParserManager();
Statement stmt = parser.parse(new StringReader(sql));
if (stmt instanceof Insert) {
return AnalyseStrategryFactory.getInvokeStrategy(MysqlAnalyseEnum.INSERT.name()).mysqlAnalyseStrategy(sql,connection,stmt);
} else if (stmt instanceof Update) {
return AnalyseStrategryFactory.getInvokeStrategy(MysqlAnalyseEnum.UPDATE.name()).mysqlAnalyseStrategy(sql,connection,stmt);
} else if (stmt instanceof Delete) {
return AnalyseStrategryFactory.getInvokeStrategy(MysqlAnalyseEnum.DELETE.name()).mysqlAnalyseStrategy(sql,connection,stmt);
SqlSqlAnalyseHandler sqlSqlAnalyseHandler = analyseStrategryFactory.getInvokeStrategy(sqlType(),stmt);
if(sqlSqlAnalyseHandler==null){
return sql;
}
return sql;
return sqlSqlAnalyseHandler.analyse(sql,connection,stmt);
}
@Override
public boolean preAnalyse(String sql) {
// SQL类型检查,只有对CUD(CURD)操作做处理
......
package com.codingapi.txlcn.tc.jdbc.sql.analyse;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
/**
* @author lorne
* @date 2020/9/17
* @description
*/
@Configurable
public class AnalyseConfiguration {
@Bean
@ConditionalOnMissingBean
public SqlDetailAnalyse mysqlSqlDetailAnalyse(){
return new MysqlSqlDetailAnalyse();
}
}
......@@ -23,7 +23,7 @@ import java.util.List;
import java.util.Map;
@Slf4j
public class MysqlAnalyse implements SqlDetailAnalyse{
public class MysqlSqlDetailAnalyse implements SqlDetailAnalyse{
@Override
public boolean singleInsertAnalyse(String sql, Connection connection, Insert statement, ItemsList itemsList, String pk, int pkIndex) throws SQLException {
......
......@@ -13,6 +13,9 @@ import java.sql.SQLException;
import java.util.List;
import java.util.Map;
/**
* //todo 若作为各种数据库的适配的话应该还需要做其他的数据库的适配,这样的话还不能满足需求。
*/
public interface SqlDetailAnalyse {
/**
......
package com.codingapi.txlcn.tc.jdbc.sql.strategy;
import com.google.common.collect.Maps;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import net.sf.jsqlparser.statement.Statement;
import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.List;
/**
* @author Gz.
......@@ -15,17 +12,19 @@ import java.util.Map;
public class AnalyseStrategryFactory {
private static Map<String, SqlSqlAnalyseHandler> strategyMap = Maps.newHashMap();
private List<SqlSqlAnalyseHandler> analyseHandlers;
public static SqlSqlAnalyseHandler getInvokeStrategy(String name) {
return strategyMap.get(name);
public AnalyseStrategryFactory(List<SqlSqlAnalyseHandler> analyseHandlers) {
this.analyseHandlers = analyseHandlers;
}
public static void register(String name, SqlSqlAnalyseHandler handler) {
if (StringUtils.isEmpty(name) || null == handler) {
return;
public SqlSqlAnalyseHandler getInvokeStrategy(String sqlType,Statement statement) {
for(SqlSqlAnalyseHandler handler : analyseHandlers){
if(handler.preAnalyse(sqlType,statement)){
return handler;
}
}
strategyMap.put(name, handler);
return null;
}
}
package com.codingapi.txlcn.tc.jdbc.sql.strategy;
import net.sf.jsqlparser.JSQLParserException;
import java.sql.Connection;
import java.sql.SQLException;
/**
* @author Gz.
* @description: 策略模式聚合类
* @date 2020-08-13 23:08:26
*/
public enum MysqlAnalyseEnum {
DELETE(),
INSERT(),
UPDATE(),;
}
package com.codingapi.txlcn.tc.jdbc.sql.strategy;
import com.codingapi.txlcn.p6spy.common.StatementInformation;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.statement.Statement;
import org.springframework.beans.factory.InitializingBean;
import java.sql.Connection;
import java.sql.SQLException;
......@@ -13,7 +11,12 @@ import java.sql.SQLException;
* @description: SQL分析则略接口
* @date 2020-08-13 23:08:26
*/
public interface SqlSqlAnalyseHandler extends InitializingBean {
public interface SqlSqlAnalyseHandler {
String analyse(String sql, Connection connection, Statement stmt) throws JSQLParserException, SQLException;
boolean preAnalyse(String sqlType,Statement stmt);
String mysqlAnalyseStrategy(String sql,Connection connection,Statement stmt) throws JSQLParserException, SQLException;
}
package com.codingapi.txlcn.tc.jdbc.sql.strategy;
import com.codingapi.txlcn.tc.jdbc.sql.analyse.SqlDetailAnalyse;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.mysql.MysqlDeleteAnalyseStrategy;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.mysql.MysqlInsertAnalyseStrategy;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.mysql.MysqlUpdateAnalyseStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import java.util.List;
/**
* @author lorne
* @date 2020/9/17
* @description
*/
@Configurable
public class StrategyConfiguration {
@Bean
public AnalyseStrategryFactory analyseStrategryFactory(@Autowired(required = false)List<SqlSqlAnalyseHandler> analyseHandlers){
return new AnalyseStrategryFactory(analyseHandlers);
}
@Bean
@ConditionalOnMissingBean
public SqlSqlAnalyseHandler mysqlInsertAnalyseStrategy(SqlDetailAnalyse sqlDetailAnalyse){
return new MysqlInsertAnalyseStrategy(sqlDetailAnalyse);
}
@Bean
@ConditionalOnMissingBean
public SqlSqlAnalyseHandler mysqlDeleteAnalyseStrategy(SqlDetailAnalyse sqlDetailAnalyse){
return new MysqlDeleteAnalyseStrategy(sqlDetailAnalyse);
}
@Bean
@ConditionalOnMissingBean
public SqlSqlAnalyseHandler mysqlUpdateAnalyseStrategy(SqlDetailAnalyse sqlDetailAnalyse){
return new MysqlUpdateAnalyseStrategy(sqlDetailAnalyse);
}
}
package com.codingapi.txlcn.tc.jdbc.sql.strategy;
package com.codingapi.txlcn.tc.jdbc.sql.strategy.mysql;
import com.codingapi.txlcn.tc.jdbc.database.DataBaseContext;
import com.codingapi.txlcn.tc.jdbc.database.SqlAnalyseHelper;
import com.codingapi.txlcn.tc.jdbc.database.SqlAnalyseInfo;
import com.codingapi.txlcn.tc.jdbc.database.TableList;
import com.codingapi.txlcn.tc.jdbc.sql.analyse.MysqlAnalyse;
import com.codingapi.txlcn.tc.jdbc.sql.analyse.SqlDetailAnalyse;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.SqlSqlAnalyseHandler;
import com.codingapi.txlcn.tc.utils.ListUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.delete.Delete;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.sql.Connection;
import java.sql.SQLException;
......@@ -29,17 +26,16 @@ import java.util.Map;
* @date 2020-08-13 23:08:26
*/
@Slf4j
@Component
public class MysqlSqlDeleteAnalyseStrategy implements SqlSqlAnalyseHandler {
public class MysqlDeleteAnalyseStrategy implements SqlSqlAnalyseHandler {
private SqlDetailAnalyse sqlDetailAnalyse;
public MysqlSqlDeleteAnalyseStrategy(SqlDetailAnalyse sqlDetailAnalyse){
public MysqlDeleteAnalyseStrategy(SqlDetailAnalyse sqlDetailAnalyse){
this.sqlDetailAnalyse = sqlDetailAnalyse;
}
@Override
public String mysqlAnalyseStrategy(String sql, Connection connection,Statement stmt) throws SQLException, JSQLParserException {
public String analyse(String sql, Connection connection, Statement stmt) throws SQLException, JSQLParserException {
String catalog = connection.getCatalog();
TableList tableList = DataBaseContext.getInstance().get(catalog);
Delete statement = (Delete) stmt;
......@@ -61,11 +57,9 @@ public class MysqlSqlDeleteAnalyseStrategy implements SqlSqlAnalyseHandler {
return sql;
}
@Override
public void afterPropertiesSet() {
AnalyseStrategryFactory.register(MysqlAnalyseEnum.DELETE.name(), this);
public boolean preAnalyse(String sqlType,Statement stmt) {
return "mysql".equals(sqlType)&&stmt instanceof Delete;
}
}
package com.codingapi.txlcn.tc.jdbc.sql.strategy;
package com.codingapi.txlcn.tc.jdbc.sql.strategy.mysql;
import com.codingapi.txlcn.tc.jdbc.database.DataBaseContext;
import com.codingapi.txlcn.tc.jdbc.database.SqlAnalyseHelper;
import com.codingapi.txlcn.tc.jdbc.database.TableInfo;
import com.codingapi.txlcn.tc.jdbc.database.TableList;
import com.codingapi.txlcn.tc.jdbc.sql.analyse.SqlDetailAnalyse;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.SqlSqlAnalyseHandler;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.operators.relational.ItemsList;
......@@ -23,10 +24,8 @@ import java.sql.SQLException;
* @date 2020-08-13 23:08:26
*/
@Slf4j
@Component
public class MysqlInsertAnalyseStrategy implements SqlSqlAnalyseHandler {
private SqlDetailAnalyse sqlDetailAnalyse;
public MysqlInsertAnalyseStrategy(SqlDetailAnalyse sqlDetailAnalyse){
......@@ -34,7 +33,7 @@ public class MysqlInsertAnalyseStrategy implements SqlSqlAnalyseHandler {
}
@Override
public String mysqlAnalyseStrategy(String sql, Connection connection,Statement stmt) throws SQLException, JSQLParserException {
public String analyse(String sql, Connection connection, Statement stmt) throws SQLException, JSQLParserException {
boolean defaultAutoCommit = connection.getAutoCommit();
connection.setAutoCommit(false);
String catalog = connection.getCatalog();
......@@ -71,7 +70,8 @@ public class MysqlInsertAnalyseStrategy implements SqlSqlAnalyseHandler {
}
@Override
public void afterPropertiesSet() {
AnalyseStrategryFactory.register(MysqlAnalyseEnum.INSERT.name(), this);
public boolean preAnalyse(String sqlType,Statement stmt) {
return "mysql".equals(sqlType)&&stmt instanceof Insert;
}
}
package com.codingapi.txlcn.tc.jdbc.sql.strategy;
package com.codingapi.txlcn.tc.jdbc.sql.strategy.mysql;
import com.codingapi.txlcn.tc.jdbc.database.DataBaseContext;
import com.codingapi.txlcn.tc.jdbc.database.SqlAnalyseHelper;
import com.codingapi.txlcn.tc.jdbc.database.SqlAnalyseInfo;
import com.codingapi.txlcn.tc.jdbc.database.TableList;
import com.codingapi.txlcn.tc.jdbc.sql.analyse.MysqlAnalyse;
import com.codingapi.txlcn.tc.jdbc.sql.analyse.SqlDetailAnalyse;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.SqlSqlAnalyseHandler;
import com.codingapi.txlcn.tc.utils.ListUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
......@@ -14,8 +14,6 @@ import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.update.Update;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.sql.Connection;
import java.sql.SQLException;
......@@ -29,7 +27,6 @@ import java.util.Map;
* @date 2020-08-13 23:08:26
*/
@Slf4j
@Component
public class MysqlUpdateAnalyseStrategy implements SqlSqlAnalyseHandler {
private SqlDetailAnalyse sqlDetailAnalyse;
......@@ -39,7 +36,7 @@ public class MysqlUpdateAnalyseStrategy implements SqlSqlAnalyseHandler {
}
@Override
public String mysqlAnalyseStrategy(String sql, Connection connection , Statement stmt) throws SQLException, JSQLParserException {
public String analyse(String sql, Connection connection , Statement stmt) throws SQLException, JSQLParserException {
String catalog = connection.getCatalog();
TableList tableList = DataBaseContext.getInstance().get(catalog);
Update statement = (Update) stmt;
......@@ -62,9 +59,9 @@ public class MysqlUpdateAnalyseStrategy implements SqlSqlAnalyseHandler {
return sql;
}
@Override
public void afterPropertiesSet() {
AnalyseStrategryFactory.register(MysqlAnalyseEnum.UPDATE.name(), this);
public boolean preAnalyse(String sqlType,Statement stmt) {
return "mysql".equals(sqlType)&&stmt instanceof Update;
}
}
......@@ -5,9 +5,10 @@ import com.codingapi.txlcn.tc.jdbc.database.DataBaseContext;
import com.codingapi.txlcn.tc.jdbc.database.JdbcAnalyseUtils;
import com.codingapi.txlcn.tc.jdbc.database.TableInfo;
import com.codingapi.txlcn.tc.jdbc.database.TableList;
import com.codingapi.txlcn.tc.jdbc.sql.analyse.MysqlAnalyse;
import com.codingapi.txlcn.tc.jdbc.sql.analyse.SqlDetailAnalyse;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.*;
import com.codingapi.txlcn.tc.jdbc.sql.analyse.MysqlSqlDetailAnalyse;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.mysql.MysqlInsertAnalyseStrategy;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.mysql.MysqlDeleteAnalyseStrategy;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.mysql.MysqlUpdateAnalyseStrategy;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
......@@ -18,8 +19,6 @@ import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.update.Update;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.aspectj.lang.annotation.Before;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
......@@ -78,11 +77,11 @@ public class MysqlSqlAnalyseTest {
CCJSqlParserManager parser = new CCJSqlParserManager();
Statement stmt = parser.parse(new StringReader(sql));
if (stmt instanceof Update) {
MysqlUpdateAnalyseStrategy mysqlInsertAnalyseStrategy = new MysqlUpdateAnalyseStrategy(new MysqlAnalyse());
mysqlInsertAnalyseStrategy.mysqlAnalyseStrategy(sql, connection, stmt);
MysqlUpdateAnalyseStrategy mysqlInsertAnalyseStrategy = new MysqlUpdateAnalyseStrategy(new MysqlSqlDetailAnalyse());
mysqlInsertAnalyseStrategy.analyse(sql, connection, stmt);
} else if (stmt instanceof Delete) {
MysqlSqlDeleteAnalyseStrategy mysqlInsertAnalyseStrategy = new MysqlSqlDeleteAnalyseStrategy(new MysqlAnalyse());
mysqlInsertAnalyseStrategy.mysqlAnalyseStrategy(sql, connection, stmt);
MysqlDeleteAnalyseStrategy mysqlInsertAnalyseStrategy = new MysqlDeleteAnalyseStrategy(new MysqlSqlDetailAnalyse());
mysqlInsertAnalyseStrategy.analyse(sql, connection, stmt);
}
}
......@@ -97,8 +96,8 @@ public class MysqlSqlAnalyseTest {
CCJSqlParserManager parser = new CCJSqlParserManager();
Statement stmt = parser.parse(new StringReader(sql));
if (stmt instanceof Insert) {
MysqlInsertAnalyseStrategy mysqlInsertAnalyseStrategy = new MysqlInsertAnalyseStrategy(new MysqlAnalyse());
String s = mysqlInsertAnalyseStrategy.mysqlAnalyseStrategy(sql, connection, stmt);
MysqlInsertAnalyseStrategy mysqlInsertAnalyseStrategy = new MysqlInsertAnalyseStrategy(new MysqlSqlDetailAnalyse());
String s = mysqlInsertAnalyseStrategy.analyse(sql, connection, stmt);
System.out.println(s);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册