fix meta bug

This commit is contained in:
Looly 2019-11-01 10:23:25 +08:00
parent 29153dc0db
commit b737c97947
4 changed files with 88 additions and 61 deletions

View File

@ -6,8 +6,10 @@
## 5.0.5 ## 5.0.5
### 新特性 ### 新特性
* 【core】 增加MapUtil.removeAnyissue#612@Github
### Bug修复 ### Bug修复
* 【db】 修复MetaUtil.getTableMeta()方法未释放ResultSet的bugissue#I148GH@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -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的值并转换为字符串
* *

View File

@ -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 {
/** /**
@ -36,7 +34,7 @@ public class MetaUtil {
/** /**
* 获得所有表名 * 获得所有表名
* *
* @param ds 数据源 * @param ds 数据源
* @param types 表类型 * @param types 表类型
* @return 表名列表 * @return 表名列表
*/ */
@ -47,9 +45,9 @@ public class MetaUtil {
/** /**
* 获得所有表名 * 获得所有表名
* *
* @param ds 数据源 * @param ds 数据源
* @param schema 表数据库名对于Oracle为用户名 * @param schema 表数据库名对于Oracle为用户名
* @param types 表类型 * @param types 表类型
* @return 表名列表 * @return 表名列表
* @since 3.3.1 * @since 3.3.1
*/ */
@ -60,42 +58,41 @@ public class MetaUtil {
/** /**
* 获得所有表名 * 获得所有表名
* *
* @param ds 数据源 * @param ds 数据源
* @param schema 表数据库名对于Oracle为用户名 * @param schema 表数据库名对于Oracle为用户名
* @param tableName 表名 * @param tableName 表名
* @param types 表类型 * @param types 表类型
* @return 表名列表 * @return 表名列表
* @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();
// catalog和schema获取失败默认使用null代替 // catalog和schema获取失败默认使用null代替
String catalog = getCataLog(conn); String catalog = getCataLog(conn);
if(null == schema) { if (null == schema) {
schema = getSchema(conn); schema = getSchema(conn);
} }
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;
} while (rs.next()) {
String table; table = rs.getString("TABLE_NAME");
while (rs.next()) { if (StrUtil.isNotBlank(table)) {
table = rs.getString("TABLE_NAME"); tables.add(table);
if (StrUtil.isNotBlank(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;
} }
@ -124,15 +121,14 @@ public class MetaUtil {
/** /**
* 获得表的所有列名 * 获得表的所有列名
* *
* @param ds 数据源 * @param ds 数据源
* @param tableName 表名 * @param tableName 表名
* @return 列数组 * @return 列数组
* @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)) {
while (rs.next()) { if (null != rs) {
columnNames.add(rs.getString("COLUMN_NAME")); while (rs.next()) {
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);
} }
} }
@ -157,7 +156,7 @@ public class MetaUtil {
* 创建带有字段限制的Entity对象<br> * 创建带有字段限制的Entity对象<br>
* 此方法读取数据库中对应表的字段列表加入到Entity中当Entity被设置内容时会忽略对应表字段外的所有KEY * 此方法读取数据库中对应表的字段列表加入到Entity中当Entity被设置内容时会忽略对应表字段外的所有KEY
* *
* @param ds 数据源 * @param ds 数据源
* @param tableName 表名 * @param tableName 表名
* @return Entity对象 * @return Entity对象
*/ */
@ -169,7 +168,7 @@ public class MetaUtil {
/** /**
* 获得表的元信息 * 获得表的元信息
* *
* @param ds 数据源 * @param ds 数据源
* @param tableName 表名 * @param tableName 表名
* @return Table对象 * @return Table对象
*/ */
@ -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 (rs.next()) { if (null != rs) {
table.setComment(rs.getString("REMARKS")); if (rs.next()) {
table.setComment(rs.getString("REMARKS"));
}
}
} }
// 获得主键 // 获得主键
rs = metaData.getPrimaryKeys(catalog, schema, tableName); try (ResultSet rs = metaData.getPrimaryKeys(catalog, schema, tableName)) {
while (rs.next()) { if (null != rs) {
table.addPk(rs.getString("COLUMN_NAME")); while (rs.next()) {
table.addPk(rs.getString("COLUMN_NAME"));
}
}
} }
// 获得列 // 获得列
rs = metaData.getColumns(catalog, schema, tableName, null); try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
while (rs.next()) { if (null != rs) {
table.setColumn(Column.create(tableName, rs)); while (rs.next()) {
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;

View File

@ -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));
} }
/** /**