【update】常量重命名与使用常量替换魔法值

This commit is contained in:
mzc5649
2021-04-02 09:12:26 +08:00
parent 0bd6543409
commit 150def1b83
2 changed files with 45 additions and 34 deletions
@@ -12,6 +12,7 @@ import java.util.Iterator;
import java.util.List;
import com.jero.codegenerate.properties.CodeConfigProperties;
import com.jero.codegenerate.database.util.DbConvertDef;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
@@ -45,7 +46,12 @@ public class DbReadTableUtil {
new DbReadTableUtil();
printStream.println(ArrayUtils.toString(listTableName()));
}
/**
* 获取所有表名的列表
* @date 2021/4/2 4:29
* @param
* @return java.util.List<java.lang.String>
*/
public static List<String> listTableName() throws SQLException {
String sqlStr = null;
ArrayList list = new ArrayList(0);
@@ -56,20 +62,21 @@ public class DbReadTableUtil {
statement = connection.createStatement(1005, 1007);
String catalog = connection.getCatalog();
log.info(" connect databaseName : " + catalog);
if (CodeConfigProperties.databaseType.equals("mysql")) {
sqlStr = MessageFormat.format("select distinct table_name from information_schema.columns where table_schema = {0}", TableConvert.c(catalog));
if (CodeConfigProperties.databaseType.equals(DbConvertDef.MYSQL)) {
// mysql查询所有表的sql
sqlStr = MessageFormat.format(DbConvertDef.MYSQL_ALLTABLES_SQL, TableConvert.c(catalog));
}
if (CodeConfigProperties.databaseType.equals("oracle")) {
sqlStr = " select distinct colstable.table_name as table_name from user_tab_cols colstable order by colstable.table_name";
if (CodeConfigProperties.databaseType.equals(DbConvertDef.ORACLE)) {
// oracle查询所有表的sql
sqlStr = DbConvertDef.ORACLE_ALLTABLES_SQL;
}
if (CodeConfigProperties.databaseType.equals("postgresql")) {
sqlStr = "select tablename from pg_tables where schemaname='public'";
if (CodeConfigProperties.databaseType.equals(DbConvertDef.POSTGRESQL)) {
// postgresql查询所有表的sql
sqlStr = DbConvertDef.POSTGRESQL_ALLTABLES_SQL;
}
if (CodeConfigProperties.databaseType.equals("sqlserver")) {
sqlStr = "select distinct c.name as table_name from sys.objects c where c.type = 'U' ";
if (CodeConfigProperties.databaseType.equals(DbConvertDef.SQLSERVER)) {
// sqlserver查询所有表的sql
sqlStr = DbConvertDef.SQLSERVER_ALLTABLES_SQL;
}
log.debug("--------------sql-------------" + sqlStr);
@@ -114,20 +121,24 @@ public class DbReadTableUtil {
statement = connection.createStatement(1005, 1007);
String catalog = connection.getCatalog();
log.info(" connect databaseName : " + catalog);
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals("mysql")) {
sqlStr = MessageFormat.format("select column_name,data_type,column_comment,numeric_precision,numeric_scale,character_maximum_length,is_nullable nullable from information_schema.columns where table_name = {0} and table_schema = {1} order by ORDINAL_POSITION", TableConvert.c(tableName), TableConvert.c(catalog));
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.MYSQL)) {
// mysql查询所有列sql
sqlStr = MessageFormat.format(DbConvertDef.MYSQL_ALLCOLUMNS_SQL, TableConvert.c(tableName), TableConvert.c(catalog));
}
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals("oracle")) {
sqlStr = MessageFormat.format(" select colstable.column_name column_name, colstable.data_type data_type, commentstable.comments column_comment, colstable.Data_Precision column_precision, colstable.Data_Scale column_scale,colstable.Char_Length,colstable.nullable from user_tab_cols colstable inner join user_col_comments commentstable on colstable.column_name = commentstable.column_name where colstable.table_name = commentstable.table_name and colstable.table_name = {0}", TableConvert.c(tableName.toUpperCase()));
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.ORACLE)) {
// oracle查询所有列sql
sqlStr = MessageFormat.format(DbConvertDef.ORACLE_ALLCOLUMNS_SQL, TableConvert.c(tableName.toUpperCase()));
}
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals("postgresql")) {
sqlStr = MessageFormat.format("select icm.column_name as field,icm.udt_name as type,fieldtxt.descript as comment, icm.numeric_precision_radix as column_precision ,icm.numeric_scale as column_scale ,icm.character_maximum_length as Char_Length,icm.is_nullable as attnotnull from information_schema.columns icm, (SELECT A.attnum,( SELECT description FROM pg_catalog.pg_description WHERE objoid = A.attrelid AND objsubid = A.attnum ) AS descript,A.attname FROM\tpg_catalog.pg_attribute A WHERE A.attrelid = ( SELECT oid FROM pg_class WHERE relname = {0} ) AND A.attnum > 0 AND NOT A.attisdropped ORDER BY\tA.attnum ) fieldtxt where icm.table_name={1} and fieldtxt.attname = icm.column_name", TableConvert.c(tableName), TableConvert.c(tableName));
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.POSTGRESQL)) {
// postgresql查询所有列sql
sqlStr = MessageFormat.format(DbConvertDef.POSTGRESQL_ALLCOLUMNS_SQL, TableConvert.c(tableName), TableConvert.c(tableName));
}
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals("sqlserver")) {
sqlStr = MessageFormat.format("select distinct cast(a.name as varchar(50)) column_name, cast(b.name as varchar(50)) data_type, cast(e.value as NVARCHAR(200)) comment, cast(ColumnProperty(a.object_id,a.Name,'''Precision''') as int) num_precision, cast(ColumnProperty(a.object_id,a.Name,'''Scale''') as int) num_scale, a.max_length, (case when a.is_nullable=1 then '''y''' else '''n''' end) nullable,column_id from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id left join (select top 1 * from sys.objects where type = '''U''' and name ={0} order by name) c on a.object_id=c.object_id left join sys.extended_properties e on e.major_id=c.object_id and e.minor_id=a.column_id and e.class=1 where c.name={0} order by a.column_id", TableConvert.c(tableName));
if (com.jero.codegenerate.properties.CodeConfigProperties.databaseType.equals(DbConvertDef.SQLSERVER)) {
// sqlserver查询所有列sql
sqlStr = MessageFormat.format(DbConvertDef.SQLSERVER_ALLCOLUMNS_SQL, TableConvert.c(tableName));
}
log.debug("--------------sql-------------" + sqlStr);
@@ -1,18 +1,18 @@
package com.jero.codegenerate.database.util;
public interface DbConvertDef {
String a = "Y";
String b = "N";
String c = "mysql";
String d = "oracle";
String e = "sqlserver";
String f = "postgresql";
String g = "select column_name,data_type,column_comment,numeric_precision,numeric_scale,character_maximum_length,is_nullable nullable from information_schema.columns where table_name = {0} and table_schema = {1} order by ORDINAL_POSITION";
String h = " select colstable.column_name column_name, colstable.data_type data_type, commentstable.comments column_comment, colstable.Data_Precision column_precision, colstable.Data_Scale column_scale,colstable.Char_Length,colstable.nullable from user_tab_cols colstable inner join user_col_comments commentstable on colstable.column_name = commentstable.column_name where colstable.table_name = commentstable.table_name and colstable.table_name = {0}";
String i = "select distinct cast(a.name as varchar(50)) column_name, cast(b.name as varchar(50)) data_type, cast(e.value as NVARCHAR(200)) comment, cast(ColumnProperty(a.object_id,a.Name,'''Precision''') as int) num_precision, cast(ColumnProperty(a.object_id,a.Name,'''Scale''') as int) num_scale, a.max_length, (case when a.is_nullable=1 then '''y''' else '''n''' end) nullable,column_id from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id left join (select top 1 * from sys.objects where type = '''U''' and name ={0} order by name) c on a.object_id=c.object_id left join sys.extended_properties e on e.major_id=c.object_id and e.minor_id=a.column_id and e.class=1 where c.name={0} order by a.column_id";
String j = "select icm.column_name as field,icm.udt_name as type,fieldtxt.descript as comment, icm.numeric_precision_radix as column_precision ,icm.numeric_scale as column_scale ,icm.character_maximum_length as Char_Length,icm.is_nullable as attnotnull from information_schema.columns icm, (SELECT A.attnum,( SELECT description FROM pg_catalog.pg_description WHERE objoid = A.attrelid AND objsubid = A.attnum ) AS descript,A.attname FROM\tpg_catalog.pg_attribute A WHERE A.attrelid = ( SELECT oid FROM pg_class WHERE relname = {0} ) AND A.attnum > 0 AND NOT A.attisdropped ORDER BY\tA.attnum ) fieldtxt where icm.table_name={1} and fieldtxt.attname = icm.column_name";
String k = "select distinct table_name from information_schema.columns where table_schema = {0}";
String l = " select distinct colstable.table_name as table_name from user_tab_cols colstable order by colstable.table_name";
String m = "select distinct c.name as table_name from sys.objects c where c.type = 'U' ";
String n = "select tablename from pg_tables where schemaname='public'";
String Y = "Y";
String N = "N";
String MYSQL = "mysql";
String ORACLE = "oracle";
String SQLSERVER = "sqlserver";
String POSTGRESQL = "postgresql";
String MYSQL_ALLCOLUMNS_SQL = "select column_name,data_type,column_comment,numeric_precision,numeric_scale,character_maximum_length,is_nullable nullable from information_schema.columns where table_name = {0} and table_schema = {1} order by ORDINAL_POSITION";
String ORACLE_ALLCOLUMNS_SQL = " select colstable.column_name column_name, colstable.data_type data_type, commentstable.comments column_comment, colstable.Data_Precision column_precision, colstable.Data_Scale column_scale,colstable.Char_Length,colstable.nullable from user_tab_cols colstable inner join user_col_comments commentstable on colstable.column_name = commentstable.column_name where colstable.table_name = commentstable.table_name and colstable.table_name = {0}";
String SQLSERVER_ALLCOLUMNS_SQL = "select distinct cast(a.name as varchar(50)) column_name, cast(b.name as varchar(50)) data_type, cast(e.value as NVARCHAR(200)) comment, cast(ColumnProperty(a.object_id,a.Name,'''Precision''') as int) num_precision, cast(ColumnProperty(a.object_id,a.Name,'''Scale''') as int) num_scale, a.max_length, (case when a.is_nullable=1 then '''y''' else '''n''' end) nullable,column_id from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id left join (select top 1 * from sys.objects where type = '''U''' and name ={0} order by name) c on a.object_id=c.object_id left join sys.extended_properties e on e.major_id=c.object_id and e.minor_id=a.column_id and e.class=1 where c.name={0} order by a.column_id";
String POSTGRESQL_ALLCOLUMNS_SQL = "select icm.column_name as field,icm.udt_name as type,fieldtxt.descript as comment, icm.numeric_precision_radix as column_precision ,icm.numeric_scale as column_scale ,icm.character_maximum_length as Char_Length,icm.is_nullable as attnotnull from information_schema.columns icm, (SELECT A.attnum,( SELECT description FROM pg_catalog.pg_description WHERE objoid = A.attrelid AND objsubid = A.attnum ) AS descript,A.attname FROM\tpg_catalog.pg_attribute A WHERE A.attrelid = ( SELECT oid FROM pg_class WHERE relname = {0} ) AND A.attnum > 0 AND NOT A.attisdropped ORDER BY\tA.attnum ) fieldtxt where icm.table_name={1} and fieldtxt.attname = icm.column_name";
String MYSQL_ALLTABLES_SQL = "select distinct table_name from information_schema.columns where table_schema = {0}";
String ORACLE_ALLTABLES_SQL = "select distinct colstable.table_name as table_name from user_tab_cols colstable order by colstable.table_name";
String SQLSERVER_ALLTABLES_SQL = "select distinct c.name as table_name from sys.objects c where c.type = 'U' ";
String POSTGRESQL_ALLTABLES_SQL = "select tablename from pg_tables where schemaname='public'";
}