【fix sonar】DbReadTableUtil文件

This commit is contained in:
梁琦涛
2023-03-31 17:37:51 +08:00
parent 5c989c102d
commit 7f0c8f1c55
@@ -1,33 +1,23 @@
package com.jero.codegenerate.database;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.jero.codegenerate.properties.CodeConfigProperties;
import com.jero.codegenerate.database.util.CodeStringUtils;
import com.jero.codegenerate.database.util.DbConvertDef;
import com.jero.codegenerate.generate.pojo.ColumnVo;
import com.jero.codegenerate.generate.util.TableConvert;
import com.jero.codegenerate.properties.CodeConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import com.jero.codegenerate.database.util.CodeStringUtils;
import com.jero.codegenerate.generate.pojo.ColumnVo;
import com.jero.codegenerate.generate.util.TableConvert;
import org.springframework.stereotype.Component;
import java.sql.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Component
public class DbReadTableUtil {
private static Connection connection;
private static Statement statement;
private DbReadTableUtil() {
}
@@ -57,7 +47,8 @@ public class DbReadTableUtil {
public static List<String> listTableName() throws SQLException {
String sqlStr = null;
ArrayList<String> list = new ArrayList<>(0);
Connection connection = null;
Statement statement = null;
try {
Class.forName(CodeConfigProperties.driverName);
connection = DriverManager.getConnection(CodeConfigProperties.databaseUrl, CodeConfigProperties.username, CodeConfigProperties.password);
@@ -95,12 +86,13 @@ public class DbReadTableUtil {
try {
if (statement != null) {
statement.close();
statement = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
connection = null;
}
} catch (SQLException e) {
e.printStackTrace();
@@ -116,10 +108,11 @@ public class DbReadTableUtil {
* @param tableName
* @return java.util.List<com.jero.codegenerate.generate.pojo.ColumnVo>
*/
public static List<ColumnVo> listColumns(String tableName) throws Exception {
public static List<ColumnVo> listColumns(String tableName) throws Exception{
String sqlStr = null;
ArrayList<ColumnVo> list = new ArrayList<>();
Connection connection = null;
Statement statement = null;
int row;
try {
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
@@ -128,25 +121,7 @@ public class DbReadTableUtil {
// 表格所属的库
String catalog = connection.getCatalog();
log.info(CONNECT_DATABASE_NAME + catalog);
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.MYSQL)) {
// mysql查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.MYSQL_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName), TableConvert.formatStr(catalog));
}
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.ORACLE)) {
// oracle查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.ORACLE_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName.toUpperCase()));
}
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.POSTGRESQL)) {
// postgresql查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.POSTGRESQL_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName), TableConvert.formatStr(tableName));
}
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.SQLSERVER)) {
// sqlserver查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.SQLSERVER_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName));
}
sqlStr = getSqlStr(tableName, sqlStr, catalog);
log.debug("--------------sql-------------" + sqlStr);
ResultSet resultSet = statement.executeQuery(sqlStr);
@@ -159,11 +134,7 @@ public class DbReadTableUtil {
}
ColumnVo columnVo = new ColumnVo();
if (com.jero.codegenerate.properties.CodeConfigProperties.dbFiledConvert) {
columnVo.setFieldName(convertFieldNameToCamelCase(resultSet.getString(1).toLowerCase()));
} else {
columnVo.setFieldName(resultSet.getString(1).toLowerCase());
}
setColumn(resultSet, columnVo);
columnVo.setFieldDbName(resultSet.getString(1).toUpperCase());
columnVo.setFieldType(convertFieldNameToCamelCase(resultSet.getString(2).toLowerCase()));
@@ -186,26 +157,11 @@ public class DbReadTableUtil {
while(resultSet.previous()) {
ColumnVo columnVo1 = new ColumnVo();
if (com.jero.codegenerate.properties.CodeConfigProperties.dbFiledConvert) {
columnVo1.setFieldName(convertFieldNameToCamelCase(resultSet.getString(1).toLowerCase()));
} else {
columnVo1.setFieldName(resultSet.getString(1).toLowerCase());
}
setColumn(resultSet, columnVo1);
columnVo1.setFieldDbName(resultSet.getString(1).toUpperCase());
log.debug(COLUMN_GET_FIELD_NAME + columnVo1.getFieldName());
if (!com.jero.codegenerate.properties.CodeConfigProperties.dbTableId.equals(columnVo1.getFieldName()) && !CodeStringUtils.isPageFilterFieldsContainDbName(columnVo1.getFieldDbName().toLowerCase(), pageFilterFieldsStrings)) {
columnVo1.setFieldType(convertFieldNameToCamelCase(resultSet.getString(2).toLowerCase()));
columnVo1.setFieldDbType(convertFieldNameToCamelCase(resultSet.getString(2).toLowerCase()));
log.debug("-----po.setFieldType------------" + columnVo1.getFieldType());
columnVo1.setPrecision(resultSet.getString(4));
columnVo1.setScale(resultSet.getString(5));
columnVo1.setCharmaxLength(resultSet.getString(6));
columnVo1.setNullable(TableConvert.getNullable(resultSet.getString(7)));
setupColumnVo(columnVo1);
columnVo1.setFiledComment(StringUtils.isBlank(resultSet.getString(3)) ? columnVo1.getFieldName() : resultSet.getString(3));
list.add(columnVo1);
}
getList(list, resultSet, pageFilterFieldsStrings, columnVo1);
}
log.debug("读取表成功");
@@ -215,17 +171,17 @@ public class DbReadTableUtil {
try {
if (statement != null) {
statement.close();
statement = null;
}
if (connection != null) {
connection.close();
connection = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
ArrayList<ColumnVo> result = new ArrayList<>();
@@ -237,6 +193,22 @@ public class DbReadTableUtil {
return result;
}
private static void getList(ArrayList<ColumnVo> list, ResultSet resultSet, String[] pageFilterFieldsStrings, ColumnVo columnVo1) throws SQLException {
if (!CodeConfigProperties.dbTableId.equals(columnVo1.getFieldName()) && !CodeStringUtils.isPageFilterFieldsContainDbName(columnVo1.getFieldDbName().toLowerCase(), pageFilterFieldsStrings)) {
columnVo1.setFieldType(convertFieldNameToCamelCase(resultSet.getString(2).toLowerCase()));
columnVo1.setFieldDbType(convertFieldNameToCamelCase(resultSet.getString(2).toLowerCase()));
log.debug("-----po.setFieldType------------" + columnVo1.getFieldType());
columnVo1.setPrecision(resultSet.getString(4));
columnVo1.setScale(resultSet.getString(5));
columnVo1.setCharmaxLength(resultSet.getString(6));
columnVo1.setNullable(TableConvert.getNullable(resultSet.getString(7)));
setupColumnVo(columnVo1);
columnVo1.setFiledComment(StringUtils.isBlank(resultSet.getString(3)) ? columnVo1.getFieldName() : resultSet.getString(3));
list.add(columnVo1);
}
}
/**
* 获取该表下原本所有列(字段)信息的集合
* @date 2021/4/2 10:02
@@ -247,7 +219,8 @@ public class DbReadTableUtil {
ResultSet resultSet;
String sqlStr = null;
ArrayList<ColumnVo> list = new ArrayList<>();
Connection connection = null;
Statement statement = null;
int row;
try {
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
@@ -256,25 +229,7 @@ public class DbReadTableUtil {
// 表格所属的库
String catalog = connection.getCatalog();
log.info(CONNECT_DATABASE_NAME + catalog);
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.MYSQL)) {
// mysql查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.MYSQL_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName), TableConvert.formatStr(catalog));
}
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.ORACLE)) {
// oracle查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.ORACLE_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName.toUpperCase()));
}
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.POSTGRESQL)) {
// postgresql查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.POSTGRESQL_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName), TableConvert.formatStr(tableName));
}
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.SQLSERVER)) {
// sqlserver查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.SQLSERVER_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName));
}
sqlStr = getSqlStr(tableName, sqlStr, catalog);
resultSet = statement.executeQuery(sqlStr);
// 游标指向结果集末尾
@@ -286,11 +241,7 @@ public class DbReadTableUtil {
}
ColumnVo columnVo = new ColumnVo();
if (com.jero.codegenerate.properties.CodeConfigProperties.dbFiledConvert) {
columnVo.setFieldName(convertFieldNameToCamelCase(resultSet.getString(1).toLowerCase()));
} else {
columnVo.setFieldName(resultSet.getString(1).toLowerCase());
}
setColumn(resultSet, columnVo);
columnVo.setFieldDbName(resultSet.getString(1).toUpperCase());
columnVo.setPrecision(TableConvert.isFieldValueBlank(resultSet.getString(4)));
@@ -311,11 +262,7 @@ public class DbReadTableUtil {
}
ColumnVo columnVo1 = new ColumnVo();
if (com.jero.codegenerate.properties.CodeConfigProperties.dbFiledConvert) {
columnVo1.setFieldName(convertFieldNameToCamelCase(resultSet.getString(1).toLowerCase()));
} else {
columnVo1.setFieldName(resultSet.getString(1).toLowerCase());
}
setColumn(resultSet, columnVo1);
columnVo1.setFieldDbName(resultSet.getString(1).toUpperCase());
columnVo1.setPrecision(TableConvert.isFieldValueBlank(resultSet.getString(4)));
@@ -334,17 +281,17 @@ public class DbReadTableUtil {
try {
if (statement != null) {
statement.close();
statement = null;
}
if (connection != null) {
connection.close();
connection = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
ArrayList<ColumnVo> result = new ArrayList<>();
@@ -356,6 +303,38 @@ public class DbReadTableUtil {
return result;
}
private static void setColumn(ResultSet resultSet, ColumnVo columnVo1) throws SQLException {
if (CodeConfigProperties.dbFiledConvert) {
columnVo1.setFieldName(convertFieldNameToCamelCase(resultSet.getString(1).toLowerCase()));
} else {
columnVo1.setFieldName(resultSet.getString(1).toLowerCase());
}
}
private static String getSqlStr(String tableName, String sqlStr, String catalog) {
if (CodeConfigProperties.databaseType.equals(DbConvertDef.MYSQL)) {
// mysql查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.MYSQL_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName), TableConvert.formatStr(catalog));
}
if (CodeConfigProperties.databaseType.equals(DbConvertDef.ORACLE)) {
// oracle查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.ORACLE_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName.toUpperCase()));
}
if (CodeConfigProperties.databaseType.equals(DbConvertDef.POSTGRESQL)) {
// postgresql查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.POSTGRESQL_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName), TableConvert.formatStr(tableName));
}
if (CodeConfigProperties.databaseType.equals(DbConvertDef.SQLSERVER)) {
// sqlserver查询表的所有列信息sql
sqlStr = MessageFormat.format(DbConvertDef.SQLSERVER_ALLCOLUMNS_SQL, TableConvert.formatStr(tableName));
}
return sqlStr;
}
/**
* 查询是否该表不存在或没有字段
* @date 2021/4/2 9:18
@@ -364,7 +343,8 @@ public class DbReadTableUtil {
*/
public static boolean c(String tableName) {
String sqlStr = null;
Connection connection = null;
Statement statement = null;
try {
log.debug("数据库驱动: " + com.jero.codegenerate.properties.CodeConfigProperties.driverName);
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
@@ -399,6 +379,21 @@ public class DbReadTableUtil {
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
@@ -463,26 +458,31 @@ public class DbReadTableUtil {
}
if (!"datetime".equals(fieldType) && !fieldType.contains("time")) {
if ("date".equals(fieldType)) {
columnVo.setClassType("easyui-datebox");
} else if (fieldType.contains("int")) {
columnVo.setOptionType("n");
} else if ("number".equals(fieldType)) {
if (StringUtils.isNotBlank(scale) && Integer.parseInt(scale) > 0) {
columnVo.setOptionType("d");
}
} else if (!"float".equals(fieldType) && !"double".equals(fieldType) && !"decimal".equals(fieldType)) {
if ("numeric".equals(fieldType)) {
columnVo.setOptionType("d");
}
} else {
columnVo.setOptionType("d");
}
setColumnVo(columnVo, fieldType, scale);
} else {
columnVo.setClassType("easyui-datetimebox");
}
}
private static void setColumnVo(ColumnVo columnVo, String fieldType, String scale) {
if ("date".equals(fieldType)) {
columnVo.setClassType("easyui-datebox");
} else if (fieldType.contains("int")) {
columnVo.setOptionType("n");
} else if ("number".equals(fieldType)) {
if (StringUtils.isNotBlank(scale) && Integer.parseInt(scale) > 0) {
columnVo.setOptionType("d");
}
} else if (!"float".equals(fieldType) && !"double".equals(fieldType) && !"decimal".equals(fieldType)) {
if ("numeric".equals(fieldType)) {
columnVo.setOptionType("d");
}
} else {
columnVo.setOptionType("d");
}
}
/**
* 返回字段类型
* @date 2021/4/6 13:43
@@ -502,13 +502,7 @@ public class DbReadTableUtil {
} else if (value.contains("double")) {
value = "java.lang.Double";
} else if (value.contains("number")) {
if (StringUtils.isNotBlank(scale) && Integer.parseInt(scale) > 0) {
value = bigDecimal;
} else if (StringUtils.isNotBlank(precision) && Integer.parseInt(precision) > 10) {
value = "java.lang.Long";
} else {
value = "java.lang.Integer";
}
value = getString(precision, scale, bigDecimal);
} else if (value.contains("decimal")) {
value = bigDecimal;
} else if (value.contains("date")) {
@@ -528,48 +522,51 @@ public class DbReadTableUtil {
return value;
}
//获取当前使用数据库名
public String database() throws Exception {
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
connection = DriverManager.getConnection(com.jero.codegenerate.properties.CodeConfigProperties.databaseUrl, com.jero.codegenerate.properties.CodeConfigProperties.username, com.jero.codegenerate.properties.CodeConfigProperties.password);
statement = connection.createStatement(1005, 1007);
String sqlStr = "select database()";
ResultSet resultSet = statement.executeQuery(sqlStr);
while(resultSet.next()) {
String name = resultSet.getString(1);
this.release(connection,statement);
return name;
private static String getString(String precision, String scale, String bigDecimal) {
String value;
if (StringUtils.isNotBlank(scale) && Integer.parseInt(scale) > 0) {
value = bigDecimal;
} else if (StringUtils.isNotBlank(precision) && Integer.parseInt(precision) > 10) {
value = "java.lang.Long";
} else {
value = "java.lang.Integer";
}
this.release(connection,statement);
return "";
return value;
}
//通过表名获取表备注
public String remarks(String a) throws Exception {
String database = this.database();
public String remarks(String a) throws SQLException, ClassNotFoundException {
Connection connection = null;
Statement statement = null;
try {
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
connection = DriverManager.getConnection(com.jero.codegenerate.properties.CodeConfigProperties.databaseUrl, com.jero.codegenerate.properties.CodeConfigProperties.username, com.jero.codegenerate.properties.CodeConfigProperties.password);
statement = connection.createStatement(1005, 1007);
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
connection = DriverManager.getConnection(com.jero.codegenerate.properties.CodeConfigProperties.databaseUrl, com.jero.codegenerate.properties.CodeConfigProperties.username, com.jero.codegenerate.properties.CodeConfigProperties.password);
statement = connection.createStatement(1005, 1007);
String sqlStr = "SELECT table_comment FROM information_schema.TABLES WHERE table_schema = " + "'"+database+"'" + " AND table_name = " + "'"+a+"'";
ResultSet resultSet = statement.executeQuery(sqlStr);
while(resultSet.next()) {
String name = resultSet.getString(1);
this.release(connection,statement);
return name;
String sqlStr = "SELECT table_comment FROM information_schema.TABLES WHERE table_name = " + "'"+a+"'";
ResultSet resultSet = statement.executeQuery(sqlStr);
while(resultSet.next()) {
String name = resultSet.getString(1);
return name;
}
}catch (ClassNotFoundException | SQLException e){
throw e;
}finally {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
this.release(connection,statement);
return "";
}
//释放连接资源
public void release(Connection connection, Statement statement) throws Exception {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
}