mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
8ce0ce7474
commit
af67ab7a89
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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 方言
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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());
|
||||
|
Loading…
x
Reference in New Issue
Block a user