【fix sonar】DbReadTableUtil文件
This commit is contained in:
+165
-168
@@ -1,33 +1,23 @@
|
|||||||
package com.jero.codegenerate.database;
|
package com.jero.codegenerate.database;
|
||||||
|
|
||||||
import java.io.PrintStream;
|
import com.jero.codegenerate.database.util.CodeStringUtils;
|
||||||
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.DbConvertDef;
|
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 lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang.ArrayUtils;
|
import org.apache.commons.lang.ArrayUtils;
|
||||||
import org.apache.commons.lang.StringUtils;
|
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 org.springframework.stereotype.Component;
|
||||||
|
import java.sql.*;
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class DbReadTableUtil {
|
public class DbReadTableUtil {
|
||||||
|
|
||||||
private static Connection connection;
|
|
||||||
private static Statement statement;
|
|
||||||
|
|
||||||
private DbReadTableUtil() {
|
private DbReadTableUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +47,8 @@ public class DbReadTableUtil {
|
|||||||
public static List<String> listTableName() throws SQLException {
|
public static List<String> listTableName() throws SQLException {
|
||||||
String sqlStr = null;
|
String sqlStr = null;
|
||||||
ArrayList<String> list = new ArrayList<>(0);
|
ArrayList<String> list = new ArrayList<>(0);
|
||||||
|
Connection connection = null;
|
||||||
|
Statement statement = null;
|
||||||
try {
|
try {
|
||||||
Class.forName(CodeConfigProperties.driverName);
|
Class.forName(CodeConfigProperties.driverName);
|
||||||
connection = DriverManager.getConnection(CodeConfigProperties.databaseUrl, CodeConfigProperties.username, CodeConfigProperties.password);
|
connection = DriverManager.getConnection(CodeConfigProperties.databaseUrl, CodeConfigProperties.username, CodeConfigProperties.password);
|
||||||
@@ -95,12 +86,13 @@ public class DbReadTableUtil {
|
|||||||
try {
|
try {
|
||||||
if (statement != null) {
|
if (statement != null) {
|
||||||
statement.close();
|
statement.close();
|
||||||
statement = null;
|
|
||||||
}
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
connection.close();
|
connection.close();
|
||||||
connection = null;
|
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -116,10 +108,11 @@ public class DbReadTableUtil {
|
|||||||
* @param tableName
|
* @param tableName
|
||||||
* @return java.util.List<com.jero.codegenerate.generate.pojo.ColumnVo>
|
* @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;
|
String sqlStr = null;
|
||||||
ArrayList<ColumnVo> list = new ArrayList<>();
|
ArrayList<ColumnVo> list = new ArrayList<>();
|
||||||
|
Connection connection = null;
|
||||||
|
Statement statement = null;
|
||||||
int row;
|
int row;
|
||||||
try {
|
try {
|
||||||
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
|
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
|
||||||
@@ -128,25 +121,7 @@ public class DbReadTableUtil {
|
|||||||
// 表格所属的库
|
// 表格所属的库
|
||||||
String catalog = connection.getCatalog();
|
String catalog = connection.getCatalog();
|
||||||
log.info(CONNECT_DATABASE_NAME + catalog);
|
log.info(CONNECT_DATABASE_NAME + catalog);
|
||||||
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.MYSQL)) {
|
sqlStr = getSqlStr(tableName, sqlStr, catalog);
|
||||||
// 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));
|
|
||||||
}
|
|
||||||
|
|
||||||
log.debug("--------------sql-------------" + sqlStr);
|
log.debug("--------------sql-------------" + sqlStr);
|
||||||
ResultSet resultSet = statement.executeQuery(sqlStr);
|
ResultSet resultSet = statement.executeQuery(sqlStr);
|
||||||
@@ -159,11 +134,7 @@ public class DbReadTableUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ColumnVo columnVo = new ColumnVo();
|
ColumnVo columnVo = new ColumnVo();
|
||||||
if (com.jero.codegenerate.properties.CodeConfigProperties.dbFiledConvert) {
|
setColumn(resultSet, columnVo);
|
||||||
columnVo.setFieldName(convertFieldNameToCamelCase(resultSet.getString(1).toLowerCase()));
|
|
||||||
} else {
|
|
||||||
columnVo.setFieldName(resultSet.getString(1).toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
columnVo.setFieldDbName(resultSet.getString(1).toUpperCase());
|
columnVo.setFieldDbName(resultSet.getString(1).toUpperCase());
|
||||||
columnVo.setFieldType(convertFieldNameToCamelCase(resultSet.getString(2).toLowerCase()));
|
columnVo.setFieldType(convertFieldNameToCamelCase(resultSet.getString(2).toLowerCase()));
|
||||||
@@ -186,26 +157,11 @@ public class DbReadTableUtil {
|
|||||||
|
|
||||||
while(resultSet.previous()) {
|
while(resultSet.previous()) {
|
||||||
ColumnVo columnVo1 = new ColumnVo();
|
ColumnVo columnVo1 = new ColumnVo();
|
||||||
if (com.jero.codegenerate.properties.CodeConfigProperties.dbFiledConvert) {
|
setColumn(resultSet, columnVo1);
|
||||||
columnVo1.setFieldName(convertFieldNameToCamelCase(resultSet.getString(1).toLowerCase()));
|
|
||||||
} else {
|
|
||||||
columnVo1.setFieldName(resultSet.getString(1).toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
columnVo1.setFieldDbName(resultSet.getString(1).toUpperCase());
|
columnVo1.setFieldDbName(resultSet.getString(1).toUpperCase());
|
||||||
log.debug(COLUMN_GET_FIELD_NAME + columnVo1.getFieldName());
|
log.debug(COLUMN_GET_FIELD_NAME + columnVo1.getFieldName());
|
||||||
if (!com.jero.codegenerate.properties.CodeConfigProperties.dbTableId.equals(columnVo1.getFieldName()) && !CodeStringUtils.isPageFilterFieldsContainDbName(columnVo1.getFieldDbName().toLowerCase(), pageFilterFieldsStrings)) {
|
getList(list, resultSet, pageFilterFieldsStrings, columnVo1);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.debug("读取表成功");
|
log.debug("读取表成功");
|
||||||
@@ -215,17 +171,17 @@ public class DbReadTableUtil {
|
|||||||
try {
|
try {
|
||||||
if (statement != null) {
|
if (statement != null) {
|
||||||
statement.close();
|
statement.close();
|
||||||
statement = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (connection != null) {
|
|
||||||
connection.close();
|
|
||||||
connection = null;
|
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
if (connection != null) {
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<ColumnVo> result = new ArrayList<>();
|
ArrayList<ColumnVo> result = new ArrayList<>();
|
||||||
@@ -237,6 +193,22 @@ public class DbReadTableUtil {
|
|||||||
|
|
||||||
return result;
|
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
|
* @date 2021/4/2 10:02
|
||||||
@@ -247,7 +219,8 @@ public class DbReadTableUtil {
|
|||||||
ResultSet resultSet;
|
ResultSet resultSet;
|
||||||
String sqlStr = null;
|
String sqlStr = null;
|
||||||
ArrayList<ColumnVo> list = new ArrayList<>();
|
ArrayList<ColumnVo> list = new ArrayList<>();
|
||||||
|
Connection connection = null;
|
||||||
|
Statement statement = null;
|
||||||
int row;
|
int row;
|
||||||
try {
|
try {
|
||||||
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
|
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
|
||||||
@@ -256,25 +229,7 @@ public class DbReadTableUtil {
|
|||||||
// 表格所属的库
|
// 表格所属的库
|
||||||
String catalog = connection.getCatalog();
|
String catalog = connection.getCatalog();
|
||||||
log.info(CONNECT_DATABASE_NAME + catalog);
|
log.info(CONNECT_DATABASE_NAME + catalog);
|
||||||
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.MYSQL)) {
|
sqlStr = getSqlStr(tableName, sqlStr, catalog);
|
||||||
// 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));
|
|
||||||
}
|
|
||||||
|
|
||||||
resultSet = statement.executeQuery(sqlStr);
|
resultSet = statement.executeQuery(sqlStr);
|
||||||
// 游标指向结果集末尾
|
// 游标指向结果集末尾
|
||||||
@@ -286,11 +241,7 @@ public class DbReadTableUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ColumnVo columnVo = new ColumnVo();
|
ColumnVo columnVo = new ColumnVo();
|
||||||
if (com.jero.codegenerate.properties.CodeConfigProperties.dbFiledConvert) {
|
setColumn(resultSet, columnVo);
|
||||||
columnVo.setFieldName(convertFieldNameToCamelCase(resultSet.getString(1).toLowerCase()));
|
|
||||||
} else {
|
|
||||||
columnVo.setFieldName(resultSet.getString(1).toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
columnVo.setFieldDbName(resultSet.getString(1).toUpperCase());
|
columnVo.setFieldDbName(resultSet.getString(1).toUpperCase());
|
||||||
columnVo.setPrecision(TableConvert.isFieldValueBlank(resultSet.getString(4)));
|
columnVo.setPrecision(TableConvert.isFieldValueBlank(resultSet.getString(4)));
|
||||||
@@ -311,11 +262,7 @@ public class DbReadTableUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ColumnVo columnVo1 = new ColumnVo();
|
ColumnVo columnVo1 = new ColumnVo();
|
||||||
if (com.jero.codegenerate.properties.CodeConfigProperties.dbFiledConvert) {
|
setColumn(resultSet, columnVo1);
|
||||||
columnVo1.setFieldName(convertFieldNameToCamelCase(resultSet.getString(1).toLowerCase()));
|
|
||||||
} else {
|
|
||||||
columnVo1.setFieldName(resultSet.getString(1).toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
columnVo1.setFieldDbName(resultSet.getString(1).toUpperCase());
|
columnVo1.setFieldDbName(resultSet.getString(1).toUpperCase());
|
||||||
columnVo1.setPrecision(TableConvert.isFieldValueBlank(resultSet.getString(4)));
|
columnVo1.setPrecision(TableConvert.isFieldValueBlank(resultSet.getString(4)));
|
||||||
@@ -334,17 +281,17 @@ public class DbReadTableUtil {
|
|||||||
try {
|
try {
|
||||||
if (statement != null) {
|
if (statement != null) {
|
||||||
statement.close();
|
statement.close();
|
||||||
statement = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (connection != null) {
|
|
||||||
connection.close();
|
|
||||||
connection = null;
|
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
if (connection != null) {
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<ColumnVo> result = new ArrayList<>();
|
ArrayList<ColumnVo> result = new ArrayList<>();
|
||||||
@@ -356,6 +303,38 @@ public class DbReadTableUtil {
|
|||||||
|
|
||||||
return result;
|
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
|
* @date 2021/4/2 9:18
|
||||||
@@ -364,7 +343,8 @@ public class DbReadTableUtil {
|
|||||||
*/
|
*/
|
||||||
public static boolean c(String tableName) {
|
public static boolean c(String tableName) {
|
||||||
String sqlStr = null;
|
String sqlStr = null;
|
||||||
|
Connection connection = null;
|
||||||
|
Statement statement = null;
|
||||||
try {
|
try {
|
||||||
log.debug("数据库驱动: " + com.jero.codegenerate.properties.CodeConfigProperties.driverName);
|
log.debug("数据库驱动: " + com.jero.codegenerate.properties.CodeConfigProperties.driverName);
|
||||||
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
|
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
|
||||||
@@ -399,6 +379,21 @@ public class DbReadTableUtil {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return false;
|
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 (!"datetime".equals(fieldType) && !fieldType.contains("time")) {
|
||||||
if ("date".equals(fieldType)) {
|
setColumnVo(columnVo, fieldType, scale);
|
||||||
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");
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
columnVo.setClassType("easyui-datetimebox");
|
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
|
* @date 2021/4/6 13:43
|
||||||
@@ -502,13 +502,7 @@ public class DbReadTableUtil {
|
|||||||
} else if (value.contains("double")) {
|
} else if (value.contains("double")) {
|
||||||
value = "java.lang.Double";
|
value = "java.lang.Double";
|
||||||
} else if (value.contains("number")) {
|
} else if (value.contains("number")) {
|
||||||
if (StringUtils.isNotBlank(scale) && Integer.parseInt(scale) > 0) {
|
value = getString(precision, scale, bigDecimal);
|
||||||
value = bigDecimal;
|
|
||||||
} else if (StringUtils.isNotBlank(precision) && Integer.parseInt(precision) > 10) {
|
|
||||||
value = "java.lang.Long";
|
|
||||||
} else {
|
|
||||||
value = "java.lang.Integer";
|
|
||||||
}
|
|
||||||
} else if (value.contains("decimal")) {
|
} else if (value.contains("decimal")) {
|
||||||
value = bigDecimal;
|
value = bigDecimal;
|
||||||
} else if (value.contains("date")) {
|
} else if (value.contains("date")) {
|
||||||
@@ -528,48 +522,51 @@ public class DbReadTableUtil {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
//获取当前使用数据库名
|
private static String getString(String precision, String scale, String bigDecimal) {
|
||||||
public String database() throws Exception {
|
String value;
|
||||||
Class.forName(com.jero.codegenerate.properties.CodeConfigProperties.driverName);
|
if (StringUtils.isNotBlank(scale) && Integer.parseInt(scale) > 0) {
|
||||||
connection = DriverManager.getConnection(com.jero.codegenerate.properties.CodeConfigProperties.databaseUrl, com.jero.codegenerate.properties.CodeConfigProperties.username, com.jero.codegenerate.properties.CodeConfigProperties.password);
|
value = bigDecimal;
|
||||||
statement = connection.createStatement(1005, 1007);
|
} else if (StringUtils.isNotBlank(precision) && Integer.parseInt(precision) > 10) {
|
||||||
String sqlStr = "select database()";
|
value = "java.lang.Long";
|
||||||
ResultSet resultSet = statement.executeQuery(sqlStr);
|
} else {
|
||||||
while(resultSet.next()) {
|
value = "java.lang.Integer";
|
||||||
String name = resultSet.getString(1);
|
|
||||||
this.release(connection,statement);
|
|
||||||
return name;
|
|
||||||
}
|
}
|
||||||
this.release(connection,statement);
|
return value;
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//通过表名获取表备注
|
//通过表名获取表备注
|
||||||
public String remarks(String a) throws Exception {
|
public String remarks(String a) throws SQLException, ClassNotFoundException {
|
||||||
String database = this.database();
|
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);
|
String sqlStr = "SELECT table_comment FROM information_schema.TABLES WHERE table_name = " + "'"+a+"'";
|
||||||
connection = DriverManager.getConnection(com.jero.codegenerate.properties.CodeConfigProperties.databaseUrl, com.jero.codegenerate.properties.CodeConfigProperties.username, com.jero.codegenerate.properties.CodeConfigProperties.password);
|
ResultSet resultSet = statement.executeQuery(sqlStr);
|
||||||
statement = connection.createStatement(1005, 1007);
|
while(resultSet.next()) {
|
||||||
|
String name = resultSet.getString(1);
|
||||||
String sqlStr = "SELECT table_comment FROM information_schema.TABLES WHERE table_schema = " + "'"+database+"'" + " AND table_name = " + "'"+a+"'";
|
return name;
|
||||||
ResultSet resultSet = statement.executeQuery(sqlStr);
|
}
|
||||||
while(resultSet.next()) {
|
}catch (ClassNotFoundException | SQLException e){
|
||||||
String name = resultSet.getString(1);
|
throw e;
|
||||||
this.release(connection,statement);
|
}finally {
|
||||||
return name;
|
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 "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
//释放连接资源
|
|
||||||
public void release(Connection connection, Statement statement) throws Exception {
|
|
||||||
if (statement != null) {
|
|
||||||
statement.close();
|
|
||||||
}
|
|
||||||
if (connection != null) {
|
|
||||||
connection.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user