mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix meta bug
This commit is contained in:
parent
29153dc0db
commit
b737c97947
@ -6,8 +6,10 @@
|
|||||||
## 5.0.5
|
## 5.0.5
|
||||||
|
|
||||||
### 新特性
|
### 新特性
|
||||||
|
* 【core】 增加MapUtil.removeAny(issue#612@Github)
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
|
* 【db】 修复MetaUtil.getTableMeta()方法未释放ResultSet的bug(issue#I148GH@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -818,6 +818,24 @@ public class MapUtil {
|
|||||||
return filter(map, (Filter<Entry<K, V>>) entry -> ArrayUtil.contains(keys, entry.getKey()));
|
return filter(map, (Filter<Entry<K, V>>) entry -> ArrayUtil.contains(keys, entry.getKey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 去掉Map中指定key的键值对,修改原Map
|
||||||
|
*
|
||||||
|
* @param <K> Key类型
|
||||||
|
* @param <V> Value类型
|
||||||
|
* @param map Map
|
||||||
|
* @param keys 键列表
|
||||||
|
* @return 修改后的key
|
||||||
|
* @since 5.0.5
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <K, V> Map<K, V> removeAny(Map<K, V> map, final K... keys) {
|
||||||
|
for (K key : keys) {
|
||||||
|
map.remove(key);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取Map指定key的值,并转换为字符串
|
* 获取Map指定key的值,并转换为字符串
|
||||||
*
|
*
|
||||||
|
@ -1,5 +1,12 @@
|
|||||||
package cn.hutool.db.meta;
|
package cn.hutool.db.meta;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.db.DbRuntimeException;
|
||||||
|
import cn.hutool.db.DbUtil;
|
||||||
|
import cn.hutool.db.Entity;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DatabaseMetaData;
|
import java.sql.DatabaseMetaData;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@ -8,19 +15,10 @@ import java.sql.SQLException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import cn.hutool.db.DbRuntimeException;
|
|
||||||
import cn.hutool.db.DbUtil;
|
|
||||||
import cn.hutool.db.Entity;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据库元数据信息工具类
|
* 数据库元数据信息工具类
|
||||||
*
|
*
|
||||||
* @author looly
|
* @author looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class MetaUtil {
|
public class MetaUtil {
|
||||||
/**
|
/**
|
||||||
@ -68,9 +66,8 @@ public class MetaUtil {
|
|||||||
* @since 3.3.1
|
* @since 3.3.1
|
||||||
*/
|
*/
|
||||||
public static List<String> getTables(DataSource ds, String schema, String tableName, TableType... types) {
|
public static List<String> getTables(DataSource ds, String schema, String tableName, TableType... types) {
|
||||||
final List<String> tables = new ArrayList<String>();
|
final List<String> tables = new ArrayList<>();
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
ResultSet rs = null;
|
|
||||||
try {
|
try {
|
||||||
conn = ds.getConnection();
|
conn = ds.getConnection();
|
||||||
|
|
||||||
@ -81,10 +78,8 @@ public class MetaUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final DatabaseMetaData metaData = conn.getMetaData();
|
final DatabaseMetaData metaData = conn.getMetaData();
|
||||||
rs = metaData.getTables(catalog, schema, tableName, Convert.toStrArray(types));
|
try (ResultSet rs = metaData.getTables(catalog, schema, tableName, Convert.toStrArray(types))) {
|
||||||
if (rs == null) {
|
if (null != rs) {
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String table;
|
String table;
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
table = rs.getString("TABLE_NAME");
|
table = rs.getString("TABLE_NAME");
|
||||||
@ -92,10 +87,12 @@ public class MetaUtil {
|
|||||||
tables.add(table);
|
tables.add(table);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new DbRuntimeException("Get tables error!", e);
|
throw new DbRuntimeException("Get tables error!", e);
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.close(rs, conn);
|
DbUtil.close(conn);
|
||||||
}
|
}
|
||||||
return tables;
|
return tables;
|
||||||
}
|
}
|
||||||
@ -130,9 +127,8 @@ public class MetaUtil {
|
|||||||
* @throws DbRuntimeException SQL执行异常
|
* @throws DbRuntimeException SQL执行异常
|
||||||
*/
|
*/
|
||||||
public static String[] getColumnNames(DataSource ds, String tableName) {
|
public static String[] getColumnNames(DataSource ds, String tableName) {
|
||||||
List<String> columnNames = new ArrayList<String>();
|
List<String> columnNames = new ArrayList<>();
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
ResultSet rs = null;
|
|
||||||
try {
|
try {
|
||||||
conn = ds.getConnection();
|
conn = ds.getConnection();
|
||||||
|
|
||||||
@ -141,15 +137,18 @@ public class MetaUtil {
|
|||||||
String schema = getSchema(conn);
|
String schema = getSchema(conn);
|
||||||
|
|
||||||
final DatabaseMetaData metaData = conn.getMetaData();
|
final DatabaseMetaData metaData = conn.getMetaData();
|
||||||
rs = metaData.getColumns(catalog, schema, tableName, null);
|
try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
|
||||||
|
if (null != rs) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
columnNames.add(rs.getString("COLUMN_NAME"));
|
columnNames.add(rs.getString("COLUMN_NAME"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return columnNames.toArray(new String[columnNames.size()]);
|
return columnNames.toArray(new String[columnNames.size()]);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new DbRuntimeException("Get columns error!", e);
|
throw new DbRuntimeException("Get columns error!", e);
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.close(rs, conn);
|
DbUtil.close(conn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,7 +176,6 @@ public class MetaUtil {
|
|||||||
public static Table getTableMeta(DataSource ds, String tableName) {
|
public static Table getTableMeta(DataSource ds, String tableName) {
|
||||||
final Table table = Table.create(tableName);
|
final Table table = Table.create(tableName);
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
ResultSet rs = null;
|
|
||||||
try {
|
try {
|
||||||
conn = ds.getConnection();
|
conn = ds.getConnection();
|
||||||
|
|
||||||
@ -188,26 +186,35 @@ public class MetaUtil {
|
|||||||
final DatabaseMetaData metaData = conn.getMetaData();
|
final DatabaseMetaData metaData = conn.getMetaData();
|
||||||
|
|
||||||
// 获得表元数据(表注释)
|
// 获得表元数据(表注释)
|
||||||
rs = metaData.getTables(catalog, schema, tableName, new String[] { TableType.TABLE.value() });
|
try (ResultSet rs = metaData.getTables(catalog, schema, tableName, new String[]{TableType.TABLE.value()})) {
|
||||||
|
if (null != rs) {
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
table.setComment(rs.getString("REMARKS"));
|
table.setComment(rs.getString("REMARKS"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 获得主键
|
// 获得主键
|
||||||
rs = metaData.getPrimaryKeys(catalog, schema, tableName);
|
try (ResultSet rs = metaData.getPrimaryKeys(catalog, schema, tableName)) {
|
||||||
|
if (null != rs) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
table.addPk(rs.getString("COLUMN_NAME"));
|
table.addPk(rs.getString("COLUMN_NAME"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 获得列
|
// 获得列
|
||||||
rs = metaData.getColumns(catalog, schema, tableName, null);
|
try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
|
||||||
|
if (null != rs) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
table.setColumn(Column.create(tableName, rs));
|
table.setColumn(Column.create(tableName, rs));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new DbRuntimeException("Get columns error!", e);
|
throw new DbRuntimeException("Get columns error!", e);
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.close(rs, conn);
|
DbUtil.close(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
return table;
|
return table;
|
||||||
|
@ -162,7 +162,7 @@ public class SqlExecutor {
|
|||||||
* @throws SQLException SQL执行异常
|
* @throws SQLException SQL执行异常
|
||||||
*/
|
*/
|
||||||
public static int[] executeBatch(Connection conn, String sql, Object[]... paramsBatch) throws SQLException {
|
public static int[] executeBatch(Connection conn, String sql, Object[]... paramsBatch) throws SQLException {
|
||||||
return executeBatch(conn, sql, new ArrayIter<Object[]>(paramsBatch));
|
return executeBatch(conn, sql, new ArrayIter<>(paramsBatch));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -198,7 +198,7 @@ public class SqlExecutor {
|
|||||||
* @since 4.5.6
|
* @since 4.5.6
|
||||||
*/
|
*/
|
||||||
public static int[] executeBatch(Connection conn, String... sqls) throws SQLException {
|
public static int[] executeBatch(Connection conn, String... sqls) throws SQLException {
|
||||||
return executeBatch(conn, new ArrayIter<String>(sqls));
|
return executeBatch(conn, new ArrayIter<>(sqls));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user