提交 32efaf88 编写于 作者: G Gz

代码冲突

上级 d87e980b
package com.codingapi.txlcn.tc.jdbc.sql.strategy;
import com.codingapi.txlcn.tc.jdbc.database.SqlAnalyseInfo;
import com.codingapi.txlcn.tc.jdbc.database.TableInfo;
import com.codingapi.txlcn.tc.jdbc.database.TableList;
import com.codingapi.txlcn.tc.utils.ListUtil;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.Join;
import java.sql.JDBCType;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
/**
* @author Gz.
* @description: sql分析助手类
* @date 2020-08-17 23:26:01
* todo 指责有点乱,SCP原则,若定位是SqlAnalyseHelper应该划分在com.codingapi.txlcn.tc.jdbc.database包下。
*/
@Slf4j
public class SqlAnalyseHelper {
/**
* 检查是否包含主键
* @param table
* @param tableList
* @return
*/
public static boolean checkTableContainsPk(Table table, TableList tableList ) {
TableInfo tableInfo = tableList.getTable(table.getName());
if(tableInfo==null){
return false;
}
if(ListUtil.isNotEmpty(tableInfo.getPrimaryKeys())){
return true;
}
return false;
}
public static boolean checkWhereContainsPk(Table table, TableList tableList, String whereSql){
TableInfo tableInfo = tableList.getTable(table.getName());
return whereSql.toUpperCase().contains(tableInfo.getPrimaryKeys().get(0).toUpperCase());
}
/**
* sql分析
* @param tableList
* @param tables
* @param where
* @return
*/
// public static SqlAnalyseInfo sqlAnalyse(TableList tableList, Table tables, Expression where){
// SqlAnalyseInfo sqlAnalyseInfo ;
// if(tables.size() == 1){
// sqlAnalyseInfo = SqlAnalyseHelper.sqlAnalyseSingleTable(tableList, tables,where);
// }else {
// sqlAnalyseInfo = SqlAnalyseHelper.sqlAnalyseMultiTable(tableList, tables,where);
// }
// return SqlAnalyseHelper.sqlAnalyseSingleTable(tableList, tables,where);
// }
public static SqlAnalyseInfo sqlAnalyseSingleTable(TableList tableList, Table table, Expression where, List<Join> joinList) {
SqlAnalyseInfo sqlAnalyseInfo = new SqlAnalyseInfo();
//单表操作
StringBuilder querySql = new StringBuilder("select ");
TableInfo tableInfo = tableList.getTable(table.getName());
String primaryKey = tableInfo.getPrimaryKeys().get(0);
String select = "";
StringBuilder tableName = new StringBuilder();
if(null != table.getAlias()){
select = table.getAlias().getName().concat(".").concat(primaryKey);
tableName.append(table.getName()).append(" ").append(table.getAlias());
}else {
select = primaryKey;
tableName.append(table.getName());
}
for (Join join : joinList) {
Table table1 = (Table)join.getRightItem();
if(table1.getAlias()!=null){
tableName.append(",").append(table1.getName()).append(" ").append(table1.getAlias());
}else {
tableName.append(",").append(table1.getName());
}
}
querySql = querySql.append(select).append(" from ").append(tableName).append(" where ").append(where.toString());
log.info("update Sql Analyse =[{}]",querySql);
JDBCType type = tableInfo.getColumnInfos().stream().filter(m -> m.getName().equals(primaryKey)).findFirst().get().getType();
sqlAnalyseInfo.setSelect(select);
sqlAnalyseInfo.setQuerySql(querySql.toString());
sqlAnalyseInfo.setPrimaryKey(primaryKey);
sqlAnalyseInfo.setPrimaryKeyType(type);
return sqlAnalyseInfo;
}
public static SqlAnalyseInfo sqlAnalyseMultiTable(TableList tableList, Table table, Expression where) {
SqlAnalyseInfo sqlAnalyseInfo = new SqlAnalyseInfo();
StringBuilder querySql = new StringBuilder("select ");
String select = null;
Set<String> s2 = new TreeSet<>();
TableInfo tableInfo = tableList.getTable(table.getName());
if(ListUtil.isNotEmpty(tableInfo.getPrimaryKeys())){
//TODO sql中没有别名的情况
if(StringUtils.isBlank(select)){
select = table.getAlias().getName().concat(".").concat(tableInfo.getPrimaryKeys().get(0));
String pk = tableInfo.getPrimaryKeys().get(0);
JDBCType type = tableInfo.getColumnInfos().stream().filter(m -> m.getName().equals(pk)).findFirst().get().getType();
sqlAnalyseInfo.setPrimaryKey(pk);
sqlAnalyseInfo.setPrimaryKeyType(type);
}
String tableName = table.getName().concat(" ").concat(table.getAlias().getName());
s2.add(tableName);
}
String from = s2.stream().collect(Collectors.joining(","));
querySql.append(select).append(" from ").append(from).append(" where ").append(where.toString());
log.info("update Sql Analyse =[{}]",querySql.toString());
sqlAnalyseInfo.setSelect(select);
sqlAnalyseInfo.setQuerySql(querySql.toString());
return sqlAnalyseInfo;
}
public static String getNewSql(String sql, SqlAnalyseInfo sqlAnalyseInfo, List<Map<String, Object>> query) {
String select = sqlAnalyseInfo.getSelect();
String primaryKey = sqlAnalyseInfo.getPrimaryKey();
JDBCType primaryKeyType = sqlAnalyseInfo.getPrimaryKeyType();
sql = sql.concat(" and ").concat(select).concat(" in ( ");
int size = query.size();
for (int i = 0; i < query.size(); i++) {
if(JDBCType.VARCHAR.getName().equals(primaryKeyType.getName())
||JDBCType.NVARCHAR.getName().equals(primaryKeyType.getName())
||JDBCType.CHAR.getName().equals(primaryKeyType.getName())
){
sql = sql.concat("'").concat((String) query.get(i).get(primaryKey)).concat("'");
}
else if(JDBCType.INTEGER.getName().equals(primaryKeyType.getName())
||JDBCType.SMALLINT.getName().equals(primaryKeyType.getName())
||JDBCType.TINYINT.getName().equals(primaryKeyType.getName())
){
sql = sql.concat(Integer.toString((Integer) query.get(i).get(primaryKey)));
}
else if(JDBCType.BIGINT.getName().equals(primaryKeyType.getName())){
sql = sql.concat(Long.toString((Long) query.get(i).get(primaryKey)));
}else{
sql = sql.concat("'").concat((String) query.get(i).get(primaryKey)).concat("'");
}
if(i + 1 < size){
sql = sql.concat(" , ");
}
}
sql = sql.concat(")");
return sql;
}
}
......@@ -5,18 +5,28 @@ 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.strategy.MysqlAnalyseContextEnum;
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.MysqlInsertAnalyseStrategy;
import com.codingapi.txlcn.tc.jdbc.sql.strategy.SqlSqlAnalyseHandler;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.*;
import net.sf.jsqlparser.statement.delete.Delete;
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;
import javax.sql.DataSource;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.SQLException;
......@@ -34,6 +44,10 @@ public class MysqlSqlAnalyseTest {
@Autowired
private DataSource dataSource;
@Test
public void analyse() throws SQLException, JSQLParserException {
String sql = "insert into lcn_demo(name,module) values('123','tc-c')";
......@@ -60,7 +74,16 @@ public class MysqlSqlAnalyseTest {
String sql = "DELETE t2,t3 FROM lcn_sql_parse_test2 t2 ,lcn_sql_parse_test3 t3 where t3.job = t2.dept_name AND t2.dept_name = 'test' and t3.name = 'a' ";
sql = "update lcn_sql_parse_test3 t3 ,lcn_sql_parse_test2 t2 set t3.age = 56 ,t2.dept_name = 'dev' where t3.job = t2.dept_name and t2.dept_name = 'test'";
Connection connection = dataSource.getConnection();
MysqlAnalyseContextEnum.valueOf(sql.toUpperCase().substring(0,6)).executeStrategry(sql,connection);
CCJSqlParserManager parser = new CCJSqlParserManager();
Statement stmt = parser.parse(new StringReader(sql));
if (stmt instanceof Insert) {
System.out.println(AnalyseStrategryFactory.getInvokeStrategy(MysqlAnalyseEnum.INSERT.name()).mysqlAnalyseStrategy(sql, connection, stmt));
} else if (stmt instanceof Update) {
System.out.println(AnalyseStrategryFactory.getInvokeStrategy(MysqlAnalyseEnum.UPDATE.name()).mysqlAnalyseStrategy(sql, connection, stmt));
} else if (stmt instanceof Delete) {
System.out.println(AnalyseStrategryFactory.getInvokeStrategy(MysqlAnalyseEnum.DELETE.name()).mysqlAnalyseStrategy(sql, connection, stmt));
}
}
@Test
......@@ -69,6 +92,26 @@ public class MysqlSqlAnalyseTest {
sql = "insert into lcn_sql_parse_test1 (id, name, sex, job, home_address, age, dept_id) values (null,'gz','1','test','bjc',12,1)";
sql = "INSERT INTO lcn_sql_parse_test1 (name, sex, job, home_address, age, dept_id) VALUES ('gz', '1', 'test', 'bjc', 12, 4),('gz', '1', 'test', 'bjc', 12, 5),('gz', '1', 'test', 'bjc', 12, 6)";
Connection connection = dataSource.getConnection();
MysqlAnalyseContextEnum.valueOf(sql.toUpperCase().substring(0,6)).executeStrategry(sql,connection);
CCJSqlParserManager parser = new CCJSqlParserManager();
Statement stmt = parser.parse(new StringReader(sql));
if (stmt instanceof Insert) {
System.out.println(AnalyseStrategryFactory.getInvokeStrategy(MysqlAnalyseEnum.INSERT.name()).mysqlAnalyseStrategy(sql, connection, stmt));
} else if (stmt instanceof Update) {
System.out.println(AnalyseStrategryFactory.getInvokeStrategy(MysqlAnalyseEnum.UPDATE.name()).mysqlAnalyseStrategy(sql, connection, stmt));
} else if (stmt instanceof Delete) {
System.out.println(AnalyseStrategryFactory.getInvokeStrategy(MysqlAnalyseEnum.DELETE.name()).mysqlAnalyseStrategy(sql, connection, stmt));
}
}
@Test
public void xx() throws JSQLParserException {
String sql = "insert into lcn_sql_parse_test2 (dept_name) values ('a')";
CCJSqlParserManager parser = new CCJSqlParserManager();
Statement stmt = parser.parse(new StringReader(sql));
if(stmt instanceof Insert){
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册