This commit is contained in:
Looly
2023-03-27 02:02:10 +08:00
parent 16f7549c7d
commit 4fca8310e7
96 changed files with 307 additions and 315 deletions

View File

@@ -1,8 +1,7 @@
package cn.hutool.db;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.IoUtil;
import cn.hutool.db.sql.SqlLog;
import cn.hutool.db.ds.DSKeys;
import cn.hutool.log.Log;
import cn.hutool.log.level.Level;
import cn.hutool.setting.Setting;
@@ -15,38 +14,6 @@ import cn.hutool.setting.Setting;
public final class DbUtil {
private final static Log log = Log.get();
/**
* 连续关闭一系列的SQL相关对象<br>
* 这些对象必须按照顺序关闭,否则会出错。
*
* @param objsToClose 需要关闭的对象
*/
public static void close(final Object... objsToClose) {
for (final Object obj : objsToClose) {
if (null != obj) {
if (obj instanceof AutoCloseable) {
IoUtil.close((AutoCloseable) obj);
} else {
log.warn("Object {} not a ResultSet or Statement or PreparedStatement or Connection!", obj.getClass().getName());
}
}
}
}
/**
* 移除配置文件中的Show SQL相关配置项<br>
* 此方法用于移除用户配置在分组下的配置项目
*
* @param setting 配置项
* @since 5.7.2
*/
public static void removeShowSqlParams(final Setting setting) {
setting.remove(SqlLog.KEY_SHOW_SQL);
setting.remove(SqlLog.KEY_FORMAT_SQL);
setting.remove(SqlLog.KEY_SHOW_PARAMS);
setting.remove(SqlLog.KEY_SQL_LEVEL);
}
/**
* 从配置文件中读取SQL打印选项读取后会去除相应属性
*
@@ -55,10 +22,10 @@ public final class DbUtil {
*/
public static void setShowSqlGlobal(final Setting setting) {
// 初始化SQL显示
final boolean isShowSql = Convert.toBoolean(setting.remove(SqlLog.KEY_SHOW_SQL), false);
final boolean isFormatSql = Convert.toBoolean(setting.remove(SqlLog.KEY_FORMAT_SQL), false);
final boolean isShowParams = Convert.toBoolean(setting.remove(SqlLog.KEY_SHOW_PARAMS), false);
String sqlLevelStr = setting.remove(SqlLog.KEY_SQL_LEVEL);
final boolean isShowSql = Convert.toBoolean(setting.remove(DSKeys.KEY_SHOW_SQL), false);
final boolean isFormatSql = Convert.toBoolean(setting.remove(DSKeys.KEY_FORMAT_SQL), false);
final boolean isShowParams = Convert.toBoolean(setting.remove(DSKeys.KEY_SHOW_PARAMS), false);
String sqlLevelStr = setting.remove(DSKeys.KEY_SQL_LEVEL);
if (null != sqlLevelStr) {
sqlLevelStr = sqlLevelStr.toUpperCase();
}

View File

@@ -1,5 +1,6 @@
package cn.hutool.db;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.StrUtil;
@@ -85,7 +86,7 @@ public class DialectRunner implements Serializable {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
DbUtil.close(ps);
IoUtil.closeQuietly(ps);
}
}
@@ -114,7 +115,7 @@ public class DialectRunner implements Serializable {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
DbUtil.close(ps);
IoUtil.closeQuietly(ps);
}
} else {
return insertOrUpdate(conn, record, keys);
@@ -168,7 +169,7 @@ public class DialectRunner implements Serializable {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
DbUtil.close(ps);
IoUtil.closeQuietly(ps);
}
}
@@ -195,7 +196,7 @@ public class DialectRunner implements Serializable {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
DbUtil.close(ps);
IoUtil.closeQuietly(ps);
}
}
@@ -234,7 +235,7 @@ public class DialectRunner implements Serializable {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
DbUtil.close(ps);
IoUtil.closeQuietly(ps);
}
}

View File

@@ -1,5 +1,7 @@
package cn.hutool.db;
import cn.hutool.core.io.IoUtil;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@@ -15,6 +17,9 @@ import java.util.Map;
*
*/
public enum ThreadLocalConnection {
/**
* 单例
*/
INSTANCE;
private final ThreadLocal<GroupedConnection> threadLocal = new ThreadLocal<>();
@@ -97,7 +102,7 @@ public enum ThreadLocalConnection {
// ignore
}
connMap.remove(ds);
DbUtil.close(conn);
IoUtil.closeQuietly(conn);
}
return this;
}

View File

@@ -1,16 +1,15 @@
package cn.hutool.db.dialect;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.text.StrUtil;
import cn.hutool.db.DbRuntimeException;
import cn.hutool.db.ds.DSWrapper;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import javax.sql.DataSource;
import cn.hutool.core.text.StrUtil;
import cn.hutool.db.DbRuntimeException;
import cn.hutool.db.DbUtil;
import cn.hutool.db.ds.DSWrapper;
/**
* 驱动相关工具类,包括自动获取驱动类名
*
@@ -55,7 +54,7 @@ public class DriverUtil {
}
driver = identifyDriver(conn);
} finally {
DbUtil.close(conn);
IoUtil.closeQuietly(conn);
}
return driver;

View File

@@ -182,7 +182,7 @@ public abstract class AbstractDSFactory implements DSFactory {
// 移除用户可能误加入的show sql配置项
// issue#I3VW0R@Gitee
DbUtil.removeShowSqlParams(config);
removeShowSqlParams(config);
// 自动识别Driver
String driver = config.getAndRemove(DSKeys.KEY_ALIAS_DRIVER);
@@ -194,4 +194,18 @@ public abstract class AbstractDSFactory implements DSFactory {
return DSWrapper.wrap(createDataSource(url, driver, user, pass, config), driver);
}
/**
* 移除配置文件中的Show SQL相关配置项<br>
* 此方法用于移除用户配置在分组下的配置项目
*
* @param setting 配置项
* @since 5.7.2
*/
private static void removeShowSqlParams(final Setting setting) {
setting.remove(DSKeys.KEY_SHOW_SQL);
setting.remove(DSKeys.KEY_FORMAT_SQL);
setting.remove(DSKeys.KEY_SHOW_PARAMS);
setting.remove(DSKeys.KEY_SQL_LEVEL);
}
}

View File

@@ -3,20 +3,47 @@ package cn.hutool.db.ds;
/**
* 数据源配置的字段名
*
* @since 6.0.0
* @author Looly
* @since 6.0.0
*/
public interface DSKeys {
/** 某些数据库需要的特殊配置项需要的配置项 */
/**
* 配置文件中配置属性名是否显示SQL
*/
String KEY_SHOW_SQL = "showSql";
/**
* 配置文件中配置属性名是否格式化SQL
*/
String KEY_FORMAT_SQL = "formatSql";
/**
* 配置文件中配置属性名:是否显示参数
*/
String KEY_SHOW_PARAMS = "showParams";
/**
* 配置文件中配置属性名:显示的日志级别
*/
String KEY_SQL_LEVEL = "sqlLevel";
/**
* 某些数据库需要的特殊配置项需要的配置项
*/
String[] KEY_CONN_PROPS = {"remarks", "useInformationSchema"};
/** 别名字段名URL */
String[] KEY_ALIAS_URL = { "url", "jdbcUrl" };
/** 别名字段名:驱动名 */
String[] KEY_ALIAS_DRIVER = { "driver", "driverClassName" };
/** 别名字段名:用户名 */
String[] KEY_ALIAS_USER = { "user", "username" };
/** 别名字段名:密码 */
String[] KEY_ALIAS_PASSWORD = { "pass", "password" };
/**
* 别名字段名URL
*/
String[] KEY_ALIAS_URL = {"url", "jdbcUrl"};
/**
* 别名字段名:驱动名
*/
String[] KEY_ALIAS_DRIVER = {"driver", "driverClassName"};
/**
* 别名字段名:用户名
*/
String[] KEY_ALIAS_USER = {"user", "username"};
/**
* 别名字段名:密码
*/
String[] KEY_ALIAS_PASSWORD = {"pass", "password"};
}

View File

@@ -116,7 +116,7 @@ public class DSWrapper implements Wrapper<DataSource>, DataSource, Closeable, Cl
@Override
public void close() {
if (this.ds instanceof AutoCloseable) {
IoUtil.close((AutoCloseable) this.ds);
IoUtil.closeQuietly((AutoCloseable) this.ds);
}
}

View File

@@ -23,7 +23,7 @@ public class GlobalDSFactory {
// JVM关闭时关闭所有连接池
RuntimeUtil.addShutdownHook(()->{
if (null != factory) {
IoUtil.close(factory);
IoUtil.closeQuietly(factory);
StaticLog.debug("DataSource: [{}] closed.", factory.getDataSourceName());
factory = null;
}
@@ -68,7 +68,7 @@ public class GlobalDSFactory {
return factory;// 数据源工厂不变时返回原数据源工厂
}
// 自定义数据源工厂前关闭之前的数据源
IoUtil.close(factory);
IoUtil.closeQuietly(factory);
}
StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.getDataSourceName());

View File

@@ -1,7 +1,7 @@
package cn.hutool.db.ds.pooled;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.db.DbUtil;
import cn.hutool.setting.dialect.Props;
import java.sql.Connection;
@@ -11,10 +11,10 @@ import java.util.Properties;
/**
* 池化
* @author Looly
*
* @author Looly
*/
public class PooledConnection extends ConnectionWraper{
public class PooledConnection extends ConnectionWraper {
private final PooledDataSource ds;
private boolean isClosed;
@@ -41,13 +41,19 @@ public class PooledConnection extends ConnectionWraper{
// 其它参数
final Properties connProps = config.getConnProps();
if(MapUtil.isNotEmpty(connProps)){
if (MapUtil.isNotEmpty(connProps)) {
info.putAll(connProps);
}
this.raw = DriverManager.getConnection(config.getUrl(), info);
}
/**
* 构造
*
* @param ds {@link PooledDataSource}
* @param conn {@link Connection}
*/
public PooledConnection(final PooledDataSource ds, final Connection conn) {
this.ds = ds;
this.raw = conn;
@@ -74,6 +80,7 @@ public class PooledConnection extends ConnectionWraper{
/**
* 打开连接
*
* @return this
*/
protected PooledConnection open() {
@@ -83,10 +90,11 @@ public class PooledConnection extends ConnectionWraper{
/**
* 释放连接
*
* @return this
*/
protected PooledConnection release() {
DbUtil.close(this.raw);
IoUtil.closeQuietly(this.raw);
return this;
}
}

View File

@@ -155,7 +155,7 @@ public class PooledDataSource extends AbstractDataSource {
@Override
protected void finalize() {
IoUtil.close(this);
IoUtil.closeQuietly(this);
}
/**

View File

@@ -2,17 +2,13 @@ package cn.hutool.db.meta;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.text.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.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@@ -102,7 +98,7 @@ public class MetaUtil {
} catch (final Exception e) {
throw new DbRuntimeException("Get tables error!", e);
} finally {
DbUtil.close(conn);
IoUtil.closeQuietly(conn);
}
return tables;
}
@@ -158,7 +154,7 @@ public class MetaUtil {
} catch (final Exception e) {
throw new DbRuntimeException("Get columns error!", e);
} finally {
DbUtil.close(conn);
IoUtil.closeQuietly(conn);
}
}
@@ -277,7 +273,7 @@ public class MetaUtil {
} catch (final SQLException e) {
throw new DbRuntimeException("Get columns error!", e);
} finally {
DbUtil.close(conn);
IoUtil.closeQuietly(conn);
}
return table;

View File

@@ -1,18 +1,13 @@
package cn.hutool.db.sql;
import cn.hutool.core.collection.iter.ArrayIter;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.func.SerFunction;
import cn.hutool.db.DbRuntimeException;
import cn.hutool.db.DbUtil;
import cn.hutool.db.StatementUtil;
import cn.hutool.db.handler.RsHandler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import java.util.Map;
/**
@@ -59,7 +54,7 @@ public class SqlExecutor {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
DbUtil.close(ps);
IoUtil.closeQuietly(ps);
}
}
@@ -81,7 +76,7 @@ public class SqlExecutor {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
DbUtil.close(call);
IoUtil.closeQuietly(call);
}
}
@@ -150,8 +145,8 @@ public class SqlExecutor {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
DbUtil.close(ps);
DbUtil.close(rs);
IoUtil.closeQuietly(ps);
IoUtil.closeQuietly(rs);
}
}
@@ -174,7 +169,7 @@ public class SqlExecutor {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
DbUtil.close(ps);
IoUtil.closeQuietly(ps);
}
}
@@ -215,7 +210,7 @@ public class SqlExecutor {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
DbUtil.close(statement);
IoUtil.closeQuietly(statement);
}
}
@@ -273,7 +268,7 @@ public class SqlExecutor {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
DbUtil.close(ps);
IoUtil.closeQuietly(ps);
}
}
@@ -295,7 +290,7 @@ public class SqlExecutor {
ps = statementFunc.apply(conn);
return executeQuery(ps, rsh);
} finally {
DbUtil.close(ps);
IoUtil.closeQuietly(ps);
}
}
@@ -375,7 +370,7 @@ public class SqlExecutor {
try {
return query(ps, rsh, params);
} finally {
DbUtil.close(ps);
IoUtil.closeQuietly(ps);
}
}
@@ -398,7 +393,7 @@ public class SqlExecutor {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
DbUtil.close(rs);
IoUtil.closeQuietly(rs);
}
}
// -------------------------------------------------------------------------------------------------------------------------------- Private method end

View File

@@ -11,43 +11,37 @@ import cn.hutool.log.level.Level;
* @since 4.1.0
*/
public enum SqlLog {
/**
* 单例
*/
INSTANCE;
/**
* 配置文件中配置属性名是否显示SQL
*/
public static final String KEY_SHOW_SQL = "showSql";
/**
* 配置文件中配置属性名是否格式化SQL
*/
public static final String KEY_FORMAT_SQL = "formatSql";
/**
* 配置文件中配置属性名:是否显示参数
*/
public static final String KEY_SHOW_PARAMS = "showParams";
/**
* 配置文件中配置属性名:显示的日志级别
*/
public static final String KEY_SQL_LEVEL = "sqlLevel";
private final static Log log = LogFactory.get();
/** 是否debugSQL */
/**
* 是否debugSQL
*/
private boolean showSql;
/** 是否格式化SQL */
/**
* 是否格式化SQL
*/
private boolean formatSql;
/** 是否显示参数 */
/**
* 是否显示参数
*/
private boolean showParams;
/** 默认日志级别 */
/**
* 默认日志级别
*/
private Level level = Level.DEBUG;
/**
* 设置全局配置是否通过debug日志显示SQL
*
* @param isShowSql 是否显示SQL
* @param isFormatSql 是否格式化显示的SQL
* @param isShowSql 是否显示SQL
* @param isFormatSql 是否格式化显示的SQL
* @param isShowParams 是否打印参数
* @param level 日志级别
* @param level 日志级别
*/
public void init(final boolean isShowSql, final boolean isFormatSql, final boolean isShowParams, final Level level) {
this.showSql = isShowSql;
@@ -81,7 +75,7 @@ public enum SqlLog {
/**
* 打印SQL日志
*
* @param sql SQL语句
* @param sql SQL语句
* @param paramValues 参数可为null
*/
public void log(final String sql, final Object paramValues) {

View File

@@ -161,7 +161,7 @@ public class SqlUtil {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
IoUtil.close(reader);
IoUtil.closeQuietly(reader);
}
}
@@ -181,7 +181,7 @@ public class SqlUtil {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
IoUtil.close(in);
IoUtil.closeQuietly(in);
}
}
@@ -204,9 +204,9 @@ public class SqlUtil {
} catch (final SQLException e) {
throw new DbRuntimeException(e);
} finally {
IoUtil.close(out);
IoUtil.closeQuietly(out);
if (closeAfterUse) {
IoUtil.close(dataStream);
IoUtil.closeQuietly(dataStream);
}
}
return blob;