Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
holden_csdn
tx-lcn
提交
625b7fb7
T
tx-lcn
项目概览
holden_csdn
/
tx-lcn
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
tx-lcn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
625b7fb7
编写于
9月 17, 2020
作者:
L
lorne
提交者:
GitHub
9月 17, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #545 from 1991wangliang/dev6.0
SQL分析优化
上级
58eecb90
61e731e4
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
138 addition
and
92 deletion
+138
-92
starter-txlcn-tc/src/main/resources/META-INF/spring.factories
...ter-txlcn-tc/src/main/resources/META-INF/spring.factories
+3
-5
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/JdbcSqlConfiguration.java
...com/codingapi/txlcn/tc/jdbc/sql/JdbcSqlConfiguration.java
+3
-2
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/MysqlSqlAnalyse.java
...java/com/codingapi/txlcn/tc/jdbc/sql/MysqlSqlAnalyse.java
+12
-11
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/analyse/AnalyseConfiguration.java
...ngapi/txlcn/tc/jdbc/sql/analyse/AnalyseConfiguration.java
+23
-0
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/analyse/MysqlSqlDetailAnalyse.java
...gapi/txlcn/tc/jdbc/sql/analyse/MysqlSqlDetailAnalyse.java
+1
-1
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/analyse/SqlDetailAnalyse.java
...codingapi/txlcn/tc/jdbc/sql/analyse/SqlDetailAnalyse.java
+3
-0
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/AnalyseStrategryFactory.java
...i/txlcn/tc/jdbc/sql/strategy/AnalyseStrategryFactory.java
+11
-12
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/MysqlAnalyseEnum.java
...odingapi/txlcn/tc/jdbc/sql/strategy/MysqlAnalyseEnum.java
+0
-18
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/SqlSqlAnalyseHandler.java
...gapi/txlcn/tc/jdbc/sql/strategy/SqlSqlAnalyseHandler.java
+7
-4
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/StrategyConfiguration.java
...api/txlcn/tc/jdbc/sql/strategy/StrategyConfiguration.java
+46
-0
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/mysql/MysqlDeleteAnalyseStrategy.java
...c/jdbc/sql/strategy/mysql/MysqlDeleteAnalyseStrategy.java
+7
-13
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/mysql/MysqlInsertAnalyseStrategy.java
...c/jdbc/sql/strategy/mysql/MysqlInsertAnalyseStrategy.java
+6
-6
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/mysql/MysqlUpdateAnalyseStrategy.java
...c/jdbc/sql/strategy/mysql/MysqlUpdateAnalyseStrategy.java
+6
-9
txlcn-tc/src/test/java/com/codingapi/txlcn/tc/jdbc/sql/MysqlSqlAnalyseTest.java
.../com/codingapi/txlcn/tc/jdbc/sql/MysqlSqlAnalyseTest.java
+10
-11
未找到文件。
starter-txlcn-tc/src/main/resources/META-INF/spring.factories
浏览文件 @
625b7fb7
...
...
@@ -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
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/JdbcSqlConfiguration.java
浏览文件 @
625b7fb7
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
);
}
}
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/MysqlSqlAnalyse.java
浏览文件 @
625b7fb7
...
...
@@ -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
sql
SqlAnalyseHandler
.
analyse
(
sql
,
connection
,
stmt
)
;
}
@Override
public
boolean
preAnalyse
(
String
sql
)
{
// SQL类型检查,只有对CUD(CURD)操作做处理
...
...
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/analyse/AnalyseConfiguration.java
0 → 100644
浏览文件 @
625b7fb7
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
();
}
}
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/analyse/MysqlAnalyse.java
→
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/analyse/Mysql
SqlDetail
Analyse.java
浏览文件 @
625b7fb7
...
...
@@ -23,7 +23,7 @@ import java.util.List;
import
java.util.Map
;
@Slf4j
public
class
Mysql
Analyse
implements
SqlDetailAnalyse
{
public
class
Mysql
SqlDetailAnalyse
implements
SqlDetailAnalyse
{
@Override
public
boolean
singleInsertAnalyse
(
String
sql
,
Connection
connection
,
Insert
statement
,
ItemsList
itemsList
,
String
pk
,
int
pkIndex
)
throws
SQLException
{
...
...
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/analyse/SqlDetailAnalyse.java
浏览文件 @
625b7fb7
...
...
@@ -13,6 +13,9 @@ import java.sql.SQLException;
import
java.util.List
;
import
java.util.Map
;
/**
* //todo 若作为各种数据库的适配的话应该还需要做其他的数据库的适配,这样的话还不能满足需求。
*/
public
interface
SqlDetailAnalyse
{
/**
...
...
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/AnalyseStrategryFactory.java
浏览文件 @
625b7fb7
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
;
}
}
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/MysqlAnalyseEnum.java
已删除
100644 → 0
浏览文件 @
58eecb90
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
(),;
}
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/SqlSqlAnalyseHandler.java
浏览文件 @
625b7fb7
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
;
}
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/StrategyConfiguration.java
0 → 100644
浏览文件 @
625b7fb7
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
);
}
}
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/
MysqlS
qlDeleteAnalyseStrategy.java
→
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/
mysql/Mys
qlDeleteAnalyseStrategy.java
浏览文件 @
625b7fb7
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
Mysql
Sql
DeleteAnalyseStrategy
(
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
;
}
}
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/MysqlInsertAnalyseStrategy.java
→
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/
mysql/
MysqlInsertAnalyseStrategy.java
浏览文件 @
625b7fb7
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
;
}
}
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/MysqlUpdateAnalyseStrategy.java
→
txlcn-tc/src/main/java/com/codingapi/txlcn/tc/jdbc/sql/strategy/
mysql/
MysqlUpdateAnalyseStrategy.java
浏览文件 @
625b7fb7
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
;
}
}
txlcn-tc/src/test/java/com/codingapi/txlcn/tc/jdbc/sql/MysqlSqlAnalyseTest.java
浏览文件 @
625b7fb7
...
...
@@ -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
Mysql
SqlDetail
Analyse
());
mysqlInsertAnalyseStrategy
.
analyse
(
sql
,
connection
,
stmt
);
}
else
if
(
stmt
instanceof
Delete
)
{
Mysql
SqlDeleteAnalyseStrategy
mysqlInsertAnalyseStrategy
=
new
MysqlSqlDeleteAnalyseStrategy
(
new
Mysq
lAnalyse
());
mysqlInsertAnalyseStrategy
.
mysqlAnalyseStrategy
(
sql
,
connection
,
stmt
);
Mysql
DeleteAnalyseStrategy
mysqlInsertAnalyseStrategy
=
new
MysqlDeleteAnalyseStrategy
(
new
MysqlSqlDetai
lAnalyse
());
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
Mysql
SqlDetail
Analyse
());
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录