Browse Source

fix meta bug

Looly 6 years ago
parent
commit
b737c97947

+ 2 - 0
CHANGELOG.md

@@ -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)
 
 
 -------------------------------------------------------------------------------------------------------------
 -------------------------------------------------------------------------------------------------------------
 
 

+ 18 - 0
hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java

@@ -819,6 +819,24 @@ public class MapUtil {
 	}
 	}
 
 
 	/**
 	/**
+	 * 去掉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的值,并转换为字符串
 	 *
 	 *
 	 * @param map Map
 	 * @param map Map

+ 66 - 59
hutool-db/src/main/java/cn/hutool/db/meta/MetaUtil.java

@@ -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,24 +15,15 @@ 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 {
 	/**
 	/**
 	 * 获得所有表名
 	 * 获得所有表名
-	 * 
+	 *
 	 * @param ds 数据源
 	 * @param ds 数据源
 	 * @return 表名列表
 	 * @return 表名列表
 	 */
 	 */
@@ -35,8 +33,8 @@ public class MetaUtil {
 
 
 	/**
 	/**
 	 * 获得所有表名
 	 * 获得所有表名
-	 * 
-	 * @param ds 数据源
+	 *
+	 * @param ds    数据源
 	 * @param types 表类型
 	 * @param types 表类型
 	 * @return 表名列表
 	 * @return 表名列表
 	 */
 	 */
@@ -46,10 +44,10 @@ 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
 	 */
 	 */
@@ -59,50 +57,49 @@ public class MetaUtil {
 
 
 	/**
 	/**
 	 * 获得所有表名
 	 * 获得所有表名
-	 * 
-	 * @param ds 数据源
-	 * @param schema 表数据库名,对于Oracle为用户名
+	 *
+	 * @param ds        数据源
+	 * @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));
-			if (rs == null) {
-				return null;
-			}
-			String table;
-			while (rs.next()) {
-				table = rs.getString("TABLE_NAME");
-				if (StrUtil.isNotBlank(table)) {
-					tables.add(table);
+			try (ResultSet rs = metaData.getTables(catalog, schema, tableName, Convert.toStrArray(types))) {
+				if (null != rs) {
+					String table;
+					while (rs.next()) {
+						table = rs.getString("TABLE_NAME");
+						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;
 	}
 	}
 
 
 	/**
 	/**
 	 * 获得结果集的所有列名
 	 * 获得结果集的所有列名
-	 * 
+	 *
 	 * @param rs 结果集
 	 * @param rs 结果集
 	 * @return 列名数组
 	 * @return 列名数组
 	 * @throws DbRuntimeException SQL执行异常
 	 * @throws DbRuntimeException SQL执行异常
@@ -123,16 +120,15 @@ 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,23 +137,26 @@ 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);
-			while (rs.next()) {
-				columnNames.add(rs.getString("COLUMN_NAME"));
+			try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
+				if (null != rs) {
+					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);
 		}
 		}
 	}
 	}
 
 
 	/**
 	/**
 	 * 创建带有字段限制的Entity对象<br>
 	 * 创建带有字段限制的Entity对象<br>
 	 * 此方法读取数据库中对应表的字段列表,加入到Entity中,当Entity被设置内容时,会忽略对应表字段外的所有KEY
 	 * 此方法读取数据库中对应表的字段列表,加入到Entity中,当Entity被设置内容时,会忽略对应表字段外的所有KEY
-	 * 
-	 * @param ds 数据源
+	 *
+	 * @param ds        数据源
 	 * @param tableName 表名
 	 * @param tableName 表名
 	 * @return Entity对象
 	 * @return Entity对象
 	 */
 	 */
@@ -168,8 +167,8 @@ 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() });
-			if (rs.next()) {
-				table.setComment(rs.getString("REMARKS"));
+			try (ResultSet rs = metaData.getTables(catalog, schema, tableName, new String[]{TableType.TABLE.value()})) {
+				if (null != rs) {
+					if (rs.next()) {
+						table.setComment(rs.getString("REMARKS"));
+					}
+				}
 			}
 			}
 
 
 			// 获得主键
 			// 获得主键
-			rs = metaData.getPrimaryKeys(catalog, schema, tableName);
-			while (rs.next()) {
-				table.addPk(rs.getString("COLUMN_NAME"));
+			try (ResultSet rs = metaData.getPrimaryKeys(catalog, schema, tableName)) {
+				if (null != rs) {
+					while (rs.next()) {
+						table.addPk(rs.getString("COLUMN_NAME"));
+					}
+				}
 			}
 			}
 
 
 			// 获得列
 			// 获得列
-			rs = metaData.getColumns(catalog, schema, tableName, null);
-			while (rs.next()) {
-				table.setColumn(Column.create(tableName, rs));
+			try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
+				if (null != 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;
@@ -215,7 +222,7 @@ public class MetaUtil {
 
 
 	/**
 	/**
 	 * 获取catalog,获取失败返回{@code null}
 	 * 获取catalog,获取失败返回{@code null}
-	 * 
+	 *
 	 * @param conn {@link Connection} 数据库连接,{@code null}时返回null
 	 * @param conn {@link Connection} 数据库连接,{@code null}时返回null
 	 * @return catalog,获取失败返回{@code null}
 	 * @return catalog,获取失败返回{@code null}
 	 * @since 4.6.0
 	 * @since 4.6.0
@@ -235,7 +242,7 @@ public class MetaUtil {
 
 
 	/**
 	/**
 	 * 获取schema,获取失败返回{@code null}
 	 * 获取schema,获取失败返回{@code null}
-	 * 
+	 *
 	 * @param conn {@link Connection} 数据库连接,{@code null}时返回null
 	 * @param conn {@link Connection} 数据库连接,{@code null}时返回null
 	 * @return schema,获取失败返回{@code null}
 	 * @return schema,获取失败返回{@code null}
 	 * @since 4.6.0
 	 * @since 4.6.0

+ 2 - 2
hutool-db/src/main/java/cn/hutool/db/sql/SqlExecutor.java

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