This commit is contained in:
Looly 2024-01-17 18:07:15 +08:00
parent 8ce0ce7474
commit af67ab7a89
9 changed files with 136 additions and 59 deletions

View File

@ -14,6 +14,7 @@ package org.dromara.hutool.db;
import org.dromara.hutool.core.func.SerFunction;
import org.dromara.hutool.db.dialect.Dialect;
import org.dromara.hutool.db.ds.DSWrapper;
import org.dromara.hutool.db.handler.*;
import org.dromara.hutool.db.sql.*;
import org.dromara.hutool.db.sql.Condition.LikeType;
@ -59,6 +60,9 @@ public abstract class AbstractDb<R extends AbstractDb<R>> extends DefaultConnect
*/
public AbstractDb(final DataSource ds, final Dialect dialect) {
super(ds);
if(ds instanceof DSWrapper){
this.caseInsensitive = ((DSWrapper) ds).getDbConfig().isCaseInsensitive();
}
this.runner = new DialectRunner(dialect);
}
// ------------------------------------------------------- Constructor end

View File

@ -12,8 +12,9 @@
package org.dromara.hutool.db.config;
import org.dromara.hutool.db.DbRuntimeException;
import org.dromara.hutool.db.driver.DriverUtil;
import org.dromara.hutool.db.sql.filter.SqlFilter;
import org.dromara.hutool.db.sql.filter.SqlFilterChain;
import java.util.Properties;
@ -29,6 +30,19 @@ import java.util.Properties;
*/
public class DbConfig {
// region ----- of
/**
* 创建DsConfig
*
* @param url jdbc url
* @param user 用户名
* @param pass 密码
* @return DsConfig
*/
public static DbConfig of(final String url, final String user, final String pass) {
return of().setUrl(url).setUser(user).setPass(pass).setDriver(DriverUtil.identifyDriver(url));
}
/**
* 创建DsConfig
*
@ -37,6 +51,7 @@ public class DbConfig {
public static DbConfig of() {
return new DbConfig();
}
// endregion
private String driver; //数据库驱动
private String url; //jdbc url
@ -58,42 +73,17 @@ public class DbConfig {
*/
private boolean returnGeneratedKey = true;
/**
* SQL过滤器用于在生成SQL前对SQL做操作如记录日志等
*/
private SqlFilterChain sqlFilters;
/**
* 构造
*/
public DbConfig() {
}
/**
* 构造
*
* @param url jdbc url
* @param user 用户名
* @param pass 密码
*/
public DbConfig(final String url, final String user, final String pass) {
init(url, user, pass);
}
/**
* 初始化
*
* @param url jdbc url
* @param user 用户名
* @param pass 密码
*/
public void init(final String url, final String user, final String pass) {
this.url = url;
this.user = user;
this.pass = pass;
this.driver = DriverUtil.identifyDriver(url);
try {
Class.forName(this.driver);
} catch (final ClassNotFoundException e) {
throw new DbRuntimeException(e, "Get jdbc driver from [{}] error!", url);
}
}
/**
* 获取JDBC驱动
*
@ -288,4 +278,18 @@ public class DbConfig {
returnGeneratedKey = isReturnGeneratedKey;
return this;
}
/**
* 增加SQL过滤器
*
* @param filter SQL过滤器
* @return this
*/
public DbConfig addSqlFilter(final SqlFilter filter){
if(null == this.sqlFilters){
this.sqlFilters = new SqlFilterChain();
}
this.sqlFilters.addChain(filter);
return this;
}
}

View File

@ -12,10 +12,8 @@
package org.dromara.hutool.db.dialect.impl;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.db.DbRuntimeException;
import org.dromara.hutool.db.Entity;
import org.dromara.hutool.db.Page;
@ -24,13 +22,12 @@ import org.dromara.hutool.db.dialect.Dialect;
import org.dromara.hutool.db.dialect.DialectName;
import org.dromara.hutool.db.sql.Condition;
import org.dromara.hutool.db.sql.Query;
import org.dromara.hutool.db.sql.SqlBuilder;
import org.dromara.hutool.db.sql.QuoteWrapper;
import org.dromara.hutool.db.sql.SqlBuilder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Set;
/**
* ANSI SQL 方言

View File

@ -23,7 +23,6 @@ import org.dromara.hutool.db.config.DbConfig;
import org.dromara.hutool.db.config.SettingConfigParser;
import org.dromara.hutool.log.LogUtil;
import javax.sql.DataSource;
import java.io.Closeable;
import java.util.Collection;
import java.util.Map;
@ -121,7 +120,7 @@ public class DSPool implements Closeable {
* @param group 分组{@code null}表示默认分组
* @return 数据源
*/
public DataSource getDataSource(String group) {
public DSWrapper getDataSource(String group) {
if (group == null) {
group = StrUtil.EMPTY;
}
@ -168,12 +167,8 @@ public class DSPool implements Closeable {
* @param group 分组{@code null}表示默认分组
* @return {@link DSWrapper} 数据源包装
*/
private DSWrapper createDSWrapper(String group) {
if (group == null) {
group = StrUtil.EMPTY;
}
final DbConfig dbConfig = this.configParser.parse(group);
return DSWrapper.wrap(factory.createDataSource(dbConfig), dbConfig.getDriver());
private DSWrapper createDSWrapper(final String group) {
final DbConfig dbConfig = this.configParser.parse(StrUtil.emptyIfNull(group));
return DSWrapper.wrap(factory.createDataSource(dbConfig), dbConfig);
}
}

View File

@ -63,7 +63,7 @@ public class DSUtil {
*
* @return 数据源
*/
public static DataSource getDS() {
public static DSWrapper getDS() {
return getDS(null);
}
@ -73,7 +73,7 @@ public class DSUtil {
* @param group 配置文件中对应的分组
* @return 数据源
*/
public static DataSource getDS(final String group) {
public static DSWrapper getDS(final String group) {
return DSPool.getInstance().getDataSource(group);
}

View File

@ -15,6 +15,7 @@ package org.dromara.hutool.db.ds;
import org.dromara.hutool.core.exception.CloneException;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.lang.wrapper.SimpleWrapper;
import org.dromara.hutool.db.config.DbConfig;
import javax.sql.DataSource;
import java.io.Closeable;
@ -36,28 +37,37 @@ import java.util.logging.Logger;
*/
public class DSWrapper extends SimpleWrapper<DataSource> implements DataSource, Closeable, Cloneable {
private final String driver;
private final DbConfig dbConfig;
/**
* 包装指定的DataSource
*
* @param ds 原始的DataSource
* @param driver 数据库驱动类名
* @param dbConfig 数据库驱动类名
* @return DataSourceWrapper
*/
public static DSWrapper wrap(final DataSource ds, final String driver) {
return new DSWrapper(ds, driver);
public static DSWrapper wrap(final DataSource ds, final DbConfig dbConfig) {
return new DSWrapper(ds, dbConfig);
}
/**
* 构造
*
* @param ds 原始的DataSource
* @param driver 数据库驱动类名
* @param ds 原始的DataSource
* @param dbConfig 数据库配置
*/
public DSWrapper(final DataSource ds, final String driver) {
public DSWrapper(final DataSource ds, final DbConfig dbConfig) {
super(ds);
this.driver = driver;
this.dbConfig = dbConfig;
}
/**
* 获取数据库配置
*
* @return 数据库配置
*/
public DbConfig getDbConfig(){
return this.dbConfig;
}
/**
@ -66,7 +76,7 @@ public class DSWrapper extends SimpleWrapper<DataSource> implements DataSource,
* @return 驱动名
*/
public String getDriver() {
return this.driver;
return this.dbConfig.getDriver();
}
@Override

View File

@ -13,6 +13,19 @@
/**
* Hutool-db是一个在JDBC基础上封装的数据库操作工具类通过包装使用ActiveRecord思想操作数据库<br>
* 在Hutool-db中使用Entity本质上是个Map代替Bean来使数据库操作更加灵活同时提供Bean和Entity的转换提供传统ORM的兼容支持
* <pre>{@code
* 数据库配置文件(db.setting)
* | <ConfigParser> 可选配置来源
* DbConfig
* | <DSFactory> 可选连接池
* DataSource
* |
* Connection
* | <DialectRunner> 可选数据库方言
* Db
* | <Entity>
* RsHandler
* }</pre>
*
* @author looly
*

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.db.sql.filter;
import org.dromara.hutool.core.lang.Chain;
import org.dromara.hutool.db.sql.BoundSql;
import org.jetbrains.annotations.NotNull;
import java.sql.Connection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* 多个{@link SqlFilter}
*
* @author Looly
*/
public class SqlFilterChain implements SqlFilter, Chain<SqlFilter, SqlFilterChain> {
private final List<SqlFilter> filters = new LinkedList<>();
@Override
public SqlFilterChain addChain(final SqlFilter element) {
filters.add(element);
return this;
}
@NotNull
@Override
public Iterator<SqlFilter> iterator() {
return filters.iterator();
}
@Override
public void filter(final Connection conn, final BoundSql boundSql, final boolean returnGeneratedKey) {
for (final SqlFilter filter : filters) {
filter.filter(conn, boundSql, returnGeneratedKey);
}
}
}

View File

@ -21,9 +21,12 @@ public class DataSourceWrapperTest {
@Test
public void cloneTest(){
final SimpleDataSource simpleDataSource = new SimpleDataSource(
new DbConfig("jdbc:sqlite:test.db", "", ""));
final DSWrapper wrapper = new DSWrapper(simpleDataSource, "test.driver");
final DbConfig dbConfig = DbConfig
.of("jdbc:sqlite:test.db", "", "")
.setDriver("test.driver");
final SimpleDataSource simpleDataSource = new SimpleDataSource(dbConfig);
final DSWrapper wrapper = new DSWrapper(simpleDataSource, dbConfig);
final DSWrapper clone = wrapper.clone();
Assertions.assertEquals("test.driver", clone.getDriver());