This commit is contained in:
Looly 2022-05-05 11:32:54 +08:00
parent e05a3b19e0
commit 34704bd326
3 changed files with 32 additions and 22 deletions

View File

@ -82,13 +82,13 @@ public class MetaUtil {
conn = ds.getConnection();
// catalog和schema获取失败默认使用null代替
String catalog = getCatalog(conn);
final String catalog = getCatalog(conn);
if (null == schema) {
schema = getSchema(conn);
}
final DatabaseMetaData metaData = conn.getMetaData();
try (ResultSet rs = metaData.getTables(catalog, schema, tableName, Convert.toStrArray(types))) {
try (final ResultSet rs = metaData.getTables(catalog, schema, tableName, Convert.toStrArray(types))) {
if (null != rs) {
String table;
while (rs.next()) {
@ -116,9 +116,9 @@ public class MetaUtil {
*/
public static String[] getColumnNames(ResultSet rs) throws DbRuntimeException {
try {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String[] labelNames = new String[columnCount];
final ResultSetMetaData rsmd = rs.getMetaData();
final int columnCount = rsmd.getColumnCount();
final String[] labelNames = new String[columnCount];
for (int i = 0; i < labelNames.length; i++) {
labelNames[i] = rsmd.getColumnLabel(i + 1);
}
@ -137,17 +137,17 @@ public class MetaUtil {
* @throws DbRuntimeException SQL执行异常
*/
public static String[] getColumnNames(DataSource ds, String tableName) {
List<String> columnNames = new ArrayList<>();
final List<String> columnNames = new ArrayList<>();
Connection conn = null;
try {
conn = ds.getConnection();
// catalog和schema获取失败默认使用null代替
String catalog = getCatalog(conn);
String schema = getSchema(conn);
final String catalog = getCatalog(conn);
final String schema = getSchema(conn);
final DatabaseMetaData metaData = conn.getMetaData();
try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
try (final ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
if (null != rs) {
while (rs.next()) {
columnNames.add(rs.getString("COLUMN_NAME"));
@ -225,7 +225,7 @@ public class MetaUtil {
final DatabaseMetaData metaData = conn.getMetaData();
// 获得表元数据表注释
try (ResultSet rs = metaData.getTables(catalog, schema, tableName, new String[]{TableType.TABLE.value()})) {
try (final ResultSet rs = metaData.getTables(catalog, schema, tableName, new String[]{TableType.TABLE.value()})) {
if (null != rs) {
if (rs.next()) {
table.setComment(rs.getString("REMARKS"));
@ -234,7 +234,7 @@ public class MetaUtil {
}
// 获得主键
try (ResultSet rs = metaData.getPrimaryKeys(catalog, schema, tableName)) {
try (final ResultSet rs = metaData.getPrimaryKeys(catalog, schema, tableName)) {
if (null != rs) {
while (rs.next()) {
table.addPk(rs.getString("COLUMN_NAME"));
@ -243,7 +243,7 @@ public class MetaUtil {
}
// 获得列
try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
try (final ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
if (null != rs) {
while (rs.next()) {
table.setColumn(Column.create(table, rs));
@ -252,7 +252,7 @@ public class MetaUtil {
}
// 获得索引信息(since 5.7.23)
try (ResultSet rs = metaData.getIndexInfo(catalog, schema, tableName, false, false)) {
try (final ResultSet rs = metaData.getIndexInfo(catalog, schema, tableName, false, false)) {
final Map<String, IndexInfo> indexInfoMap = new LinkedHashMap<>();
if (null != rs) {
while (rs.next()) {

View File

@ -2,6 +2,7 @@ package cn.hutool.db.meta;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.DbRuntimeException;
import cn.hutool.db.ds.DSFactory;
import org.junit.Assert;
import org.junit.Test;
@ -20,25 +21,34 @@ public class MetaUtilTest {
@Test
public void getTablesTest() {
List<String> tables = MetaUtil.getTables(ds);
final List<String> tables = MetaUtil.getTables(ds);
Assert.assertEquals("user", tables.get(0));
}
@Test
public void getTableMetaTest() {
Table table = MetaUtil.getTableMeta(ds, "user");
final Table table = MetaUtil.getTableMeta(ds, "user");
Assert.assertEquals(CollectionUtil.newHashSet("id"), table.getPkNames());
}
@Test
public void getColumnNamesTest() {
String[] names = MetaUtil.getColumnNames(ds, "user");
final String[] names = MetaUtil.getColumnNames(ds, "user");
Assert.assertArrayEquals(StrUtil.splitToArray("id,name,age,birthday,gender", ','), names);
}
@Test
public void getTableIndexInfoTest() {
Table table = MetaUtil.getTableMeta(ds, "user_1");
final Table table = MetaUtil.getTableMeta(ds, "user_1");
Assert.assertEquals(table.getIndexInfoList().size(), 2);
}
/**
* 表不存在抛出异常
*/
@Test(expected = DbRuntimeException.class)
public void getTableNotExistTest() {
final Table table = MetaUtil.getTableMeta(ds, "user_not_exist");
Assert.assertEquals(table.getIndexInfoList().size(), 2);
}
}

View File

@ -194,7 +194,7 @@ public final class SensitiveUtil {
* @return 敏感词过滤处理后的bean对象
*/
public static <T> T sensitiveFilter(T bean, boolean isGreedMatch, SensitiveProcessor sensitiveProcessor) {
String jsonText = JSONUtil.toJsonStr(bean);
final String jsonText = JSONUtil.toJsonStr(bean);
@SuppressWarnings("unchecked") final Class<T> c = (Class<T>) bean.getClass();
return JSONUtil.toBean(sensitiveFilter(jsonText, isGreedMatch, sensitiveProcessor), c);
}
@ -224,7 +224,7 @@ public final class SensitiveUtil {
}
//敏感词过滤场景下不需要密集匹配
List<FoundWord> foundWordList = getFoundAllSensitive(text, true, isGreedMatch);
final List<FoundWord> foundWordList = getFoundAllSensitive(text, true, isGreedMatch);
if (CollUtil.isEmpty(foundWordList)) {
return text;
}
@ -233,10 +233,10 @@ public final class SensitiveUtil {
final Map<Integer, FoundWord> foundWordMap = new HashMap<>(foundWordList.size(), 1);
foundWordList.forEach(foundWord -> foundWordMap.put(foundWord.getStartIndex(), foundWord));
int length = text.length();
StringBuilder textStringBuilder = new StringBuilder();
final int length = text.length();
final StringBuilder textStringBuilder = new StringBuilder();
for (int i = 0; i < length; i++) {
FoundWord fw = foundWordMap.get(i);
final FoundWord fw = foundWordMap.get(i);
if (fw != null) {
textStringBuilder.append(sensitiveProcessor.process(fw));
i = fw.getEndIndex();