mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.ds.DSFactory;
|
||||
|
||||
@@ -18,14 +18,14 @@ import java.util.List;
|
||||
*
|
||||
*/
|
||||
public class DaoTemplate {
|
||||
|
||||
|
||||
/** 表名 */
|
||||
protected String tableName;
|
||||
/** 本表的主键字段,请在子类中覆盖或构造方法中指定,默认为id */
|
||||
protected String primaryKeyField = "id";
|
||||
/** SQL运行器 */
|
||||
protected Db db;
|
||||
|
||||
|
||||
//--------------------------------------------------------------- Constructor start
|
||||
/**
|
||||
* 构造,此构造需要自定义SqlRunner,主键默认为id
|
||||
@@ -34,7 +34,7 @@ public class DaoTemplate {
|
||||
public DaoTemplate(String tableName) {
|
||||
this(tableName, (String)null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构造,使用默认的池化连接池,读取默认配置文件的空分组,适用于只有一个数据库的情况
|
||||
* @param tableName 数据库表名
|
||||
@@ -43,11 +43,11 @@ public class DaoTemplate {
|
||||
public DaoTemplate(String tableName, String primaryKeyField) {
|
||||
this(tableName, primaryKeyField, DSFactory.get());
|
||||
}
|
||||
|
||||
|
||||
public DaoTemplate(String tableName, DataSource ds) {
|
||||
this(tableName, null, ds);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param tableName 表名
|
||||
@@ -57,7 +57,7 @@ public class DaoTemplate {
|
||||
public DaoTemplate(String tableName, String primaryKeyField, DataSource ds) {
|
||||
this(tableName, primaryKeyField, Db.use(ds));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param tableName 表名
|
||||
@@ -72,7 +72,7 @@ public class DaoTemplate {
|
||||
this.db = db;
|
||||
}
|
||||
//--------------------------------------------------------------- Constructor end
|
||||
|
||||
|
||||
//------------------------------------------------------------- Add start
|
||||
/**
|
||||
* 添加
|
||||
@@ -83,7 +83,7 @@ public class DaoTemplate {
|
||||
public int add(Entity entity) throws SQLException {
|
||||
return db.insert(fixEntity(entity));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加
|
||||
* @param entity 实体对象
|
||||
@@ -93,7 +93,7 @@ public class DaoTemplate {
|
||||
public List<Object> addForGeneratedKeys(Entity entity) throws SQLException {
|
||||
return db.insertForGeneratedKeys(fixEntity(entity));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加
|
||||
* @param entity 实体对象
|
||||
@@ -104,12 +104,12 @@ public class DaoTemplate {
|
||||
return db.insertForGeneratedKey(fixEntity(entity));
|
||||
}
|
||||
//------------------------------------------------------------- Add end
|
||||
|
||||
|
||||
//------------------------------------------------------------- Delete start
|
||||
/**
|
||||
* 删除
|
||||
* @param <T> 主键类型
|
||||
*
|
||||
*
|
||||
* @param pk 主键
|
||||
* @return 删除行数
|
||||
* @throws SQLException SQL执行异常
|
||||
@@ -120,10 +120,10 @@ public class DaoTemplate {
|
||||
}
|
||||
return this.del(Entity.create(tableName).set(primaryKeyField, pk));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
*
|
||||
* @param <T> 主键类型
|
||||
* @param field 字段名
|
||||
* @param value 字段值
|
||||
@@ -137,23 +137,23 @@ public class DaoTemplate {
|
||||
|
||||
return this.del(Entity.create(tableName).set(field, value));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
*
|
||||
* @param <T> 主键类型
|
||||
* @param where 删除条件,当条件为空时,返回0(防止误删全表)
|
||||
* @return 删除行数
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
public <T> int del(Entity where) throws SQLException {
|
||||
if (CollectionUtil.isEmpty(where)) {
|
||||
if (MapUtil.isEmpty(where)) {
|
||||
return 0;
|
||||
}
|
||||
return db.del(fixEntity(where));
|
||||
}
|
||||
//------------------------------------------------------------- Delete end
|
||||
|
||||
|
||||
//------------------------------------------------------------- Update start
|
||||
/**
|
||||
* 按照条件更新
|
||||
@@ -163,12 +163,12 @@ public class DaoTemplate {
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
public int update(Entity record, Entity where) throws SQLException{
|
||||
if (CollectionUtil.isEmpty(record)) {
|
||||
if (MapUtil.isEmpty(record)) {
|
||||
return 0;
|
||||
}
|
||||
return db.update(fixEntity(record), where);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新
|
||||
* @param entity 实体对象,必须包含主键
|
||||
@@ -176,7 +176,7 @@ public class DaoTemplate {
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
public int update(Entity entity) throws SQLException {
|
||||
if (CollectionUtil.isEmpty(entity)) {
|
||||
if (MapUtil.isEmpty(entity)) {
|
||||
return 0;
|
||||
}
|
||||
entity = fixEntity(entity);
|
||||
@@ -191,7 +191,7 @@ public class DaoTemplate {
|
||||
|
||||
return db.update(record, where);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 增加或者更新实体
|
||||
* @param entity 实体,当包含主键时更新,否则新增
|
||||
@@ -202,11 +202,11 @@ public class DaoTemplate {
|
||||
return null == entity.get(primaryKeyField) ? add(entity) : update(entity);
|
||||
}
|
||||
//------------------------------------------------------------- Update end
|
||||
|
||||
|
||||
//------------------------------------------------------------- Get start
|
||||
/**
|
||||
* 根据主键获取单个记录
|
||||
*
|
||||
*
|
||||
* @param <T> 主键类型
|
||||
* @param pk 主键值
|
||||
* @return 记录
|
||||
@@ -215,11 +215,11 @@ public class DaoTemplate {
|
||||
public <T> Entity get(T pk) throws SQLException {
|
||||
return this.get(primaryKeyField, pk);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据某个字段(最好是唯一字段)查询单个记录<br>
|
||||
* 当有多条返回时,只显示查询到的第一条
|
||||
*
|
||||
*
|
||||
* @param <T> 字段值类型
|
||||
* @param field 字段名
|
||||
* @param value 字段值
|
||||
@@ -229,10 +229,10 @@ public class DaoTemplate {
|
||||
public <T> Entity get(String field, T value) throws SQLException {
|
||||
return this.get(Entity.create(tableName).set(field, value));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据条件实体查询单个记录,当有多条返回时,只显示查询到的第一条
|
||||
*
|
||||
*
|
||||
* @param where 条件
|
||||
* @return 记录
|
||||
* @throws SQLException SQL执行异常
|
||||
@@ -241,11 +241,11 @@ public class DaoTemplate {
|
||||
return db.get(fixEntity(where));
|
||||
}
|
||||
//------------------------------------------------------------- Get end
|
||||
|
||||
|
||||
//------------------------------------------------------------- Find start
|
||||
/**
|
||||
* 根据某个字段值查询结果
|
||||
*
|
||||
*
|
||||
* @param <T> 字段值类型
|
||||
* @param field 字段名
|
||||
* @param value 字段值
|
||||
@@ -255,7 +255,7 @@ public class DaoTemplate {
|
||||
public <T> List<Entity> find(String field, T value) throws SQLException {
|
||||
return this.find(Entity.create(tableName).set(field, value));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询当前表的所有记录
|
||||
* @return 记录
|
||||
@@ -264,10 +264,10 @@ public class DaoTemplate {
|
||||
public List<Entity> findAll() throws SQLException {
|
||||
return this.find(Entity.create(tableName));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据某个字段值查询结果
|
||||
*
|
||||
*
|
||||
* @param where 查询条件
|
||||
* @return 记录
|
||||
* @throws SQLException SQL执行异常
|
||||
@@ -275,12 +275,12 @@ public class DaoTemplate {
|
||||
public List<Entity> find(Entity where) throws SQLException {
|
||||
return db.find(null, fixEntity(where));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据SQL语句查询结果<br>
|
||||
* SQL语句可以是非完整SQL语句,可以只提供查询的条件部分(例如WHERE部分)<br>
|
||||
* 此方法会自动补全SELECT * FROM [tableName] 部分,这样就无需关心表名,直接提供条件即可
|
||||
*
|
||||
*
|
||||
* @param sql SQL语句
|
||||
* @param params SQL占位符中对应的参数
|
||||
* @return 记录
|
||||
@@ -293,10 +293,10 @@ public class DaoTemplate {
|
||||
}
|
||||
return db.query(sql, params);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
*
|
||||
* @param where 条件
|
||||
* @param page 分页对象
|
||||
* @param selectFields 查询的字段列表
|
||||
@@ -306,10 +306,10 @@ public class DaoTemplate {
|
||||
public PageResult<Entity> page(Entity where, Page page, String... selectFields) throws SQLException{
|
||||
return db.page(Arrays.asList(selectFields), fixEntity(where), page);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
*
|
||||
* @param where 条件
|
||||
* @param page 分页对象
|
||||
* @return 分页结果集
|
||||
@@ -318,10 +318,10 @@ public class DaoTemplate {
|
||||
public PageResult<Entity> page(Entity where, Page page) throws SQLException{
|
||||
return db.page(fixEntity(where), page);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 满足条件的数据条目数量
|
||||
*
|
||||
*
|
||||
* @param where 条件
|
||||
* @return 数量
|
||||
* @throws SQLException SQL执行异常
|
||||
@@ -329,10 +329,10 @@ public class DaoTemplate {
|
||||
public long count(Entity where) throws SQLException{
|
||||
return db.count(fixEntity(where));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 指定条件的数据是否存在
|
||||
*
|
||||
*
|
||||
* @param where 条件
|
||||
* @return 是否存在
|
||||
* @throws SQLException SQL执行异常
|
||||
@@ -341,7 +341,7 @@ public class DaoTemplate {
|
||||
return this.count(where) > 0;
|
||||
}
|
||||
//------------------------------------------------------------- Find end
|
||||
|
||||
|
||||
/**
|
||||
* 修正Entity对象,避免null和填充表名
|
||||
* @param entity 实体类
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.dialect.Dialect;
|
||||
@@ -99,7 +99,7 @@ public class DialectRunner implements Serializable {
|
||||
*/
|
||||
public <T> T insert(Connection conn, Entity record, RsHandler<T> generatedKeysHandler) throws SQLException {
|
||||
checkConn(conn);
|
||||
if (CollUtil.isEmpty(record)) {
|
||||
if (MapUtil.isEmpty(record)) {
|
||||
throw new SQLException("Empty entity provided!");
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ public class DialectRunner implements Serializable {
|
||||
*/
|
||||
public int del(Connection conn, Entity where) throws SQLException {
|
||||
checkConn(conn);
|
||||
if (CollUtil.isEmpty(where)) {
|
||||
if (MapUtil.isEmpty(where)) {
|
||||
//不允许做全表删除
|
||||
throw new SQLException("Empty entity provided!");
|
||||
}
|
||||
@@ -153,10 +153,10 @@ public class DialectRunner implements Serializable {
|
||||
*/
|
||||
public int update(Connection conn, Entity record, Entity where) throws SQLException {
|
||||
checkConn(conn);
|
||||
if (CollUtil.isEmpty(record)) {
|
||||
if (MapUtil.isEmpty(record)) {
|
||||
throw new SQLException("Empty entity provided!");
|
||||
}
|
||||
if (CollUtil.isEmpty(where)) {
|
||||
if (MapUtil.isEmpty(where)) {
|
||||
//不允许做全表更新
|
||||
throw new SQLException("Empty where provided!");
|
||||
}
|
||||
|
@@ -1,130 +0,0 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import cn.hutool.db.dialect.Dialect;
|
||||
import cn.hutool.db.dialect.DialectFactory;
|
||||
import cn.hutool.db.ds.DSFactory;
|
||||
import cn.hutool.db.sql.Wrapper;
|
||||
|
||||
/**
|
||||
* SQL执行类<br>
|
||||
* 通过给定的数据源执行给定SQL或者给定数据源和方言,执行相应的CRUD操作<br>
|
||||
* SqlRunner中每一个方法都会打开和关闭一个链接<br>
|
||||
* 此类为线程安全的对象,可以单例使用
|
||||
*
|
||||
* @author Luxiaolei
|
||||
* @deprecated 请使用{@link Db}
|
||||
*/
|
||||
@Deprecated
|
||||
public class SqlRunner extends AbstractDb{
|
||||
private static final long serialVersionUID = 6626183393926198184L;
|
||||
|
||||
/**
|
||||
* 创建SqlRunner<br>
|
||||
* 使用默认数据源,自动探测数据库连接池
|
||||
* @return SqlRunner
|
||||
* @since 3.0.6
|
||||
*/
|
||||
public static SqlRunner create() {
|
||||
return create(DSFactory.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建SqlRunner<br>
|
||||
* 使用默认数据源,自动探测数据库连接池
|
||||
*
|
||||
* @param group 数据源分组
|
||||
* @return SqlRunner
|
||||
* @since 4.0.11
|
||||
*/
|
||||
public static SqlRunner create(String group) {
|
||||
return create(DSFactory.get(group));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建SqlRunner<br>
|
||||
* 会根据数据源连接的元信息识别目标数据库类型,进而使用合适的数据源
|
||||
* @param ds 数据源
|
||||
* @return SqlRunner
|
||||
*/
|
||||
public static SqlRunner create(DataSource ds) {
|
||||
return ds == null ? null : new SqlRunner(ds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建SqlRunner
|
||||
* @param ds 数据源
|
||||
* @param dialect 方言
|
||||
* @return SqlRunner
|
||||
*/
|
||||
public static SqlRunner create(DataSource ds, Dialect dialect) {
|
||||
return new SqlRunner(ds, dialect);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建SqlRunner
|
||||
* @param ds 数据源
|
||||
* @param driverClassName 数据库连接驱动类名
|
||||
* @return SqlRunner
|
||||
*/
|
||||
public static SqlRunner create(DataSource ds, String driverClassName) {
|
||||
return new SqlRunner(ds, DialectFactory.newDialect(driverClassName));
|
||||
}
|
||||
|
||||
//------------------------------------------------------- Constructor start
|
||||
/**
|
||||
* 构造,从DataSource中识别方言
|
||||
* @param ds 数据源
|
||||
*/
|
||||
public SqlRunner(DataSource ds) {
|
||||
this(ds, DialectFactory.getDialect(ds));
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param ds 数据源
|
||||
* @param driverClassName 数据库连接驱动类名,用于识别方言
|
||||
*/
|
||||
public SqlRunner(DataSource ds, String driverClassName) {
|
||||
this(ds, DialectFactory.newDialect(driverClassName));
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param ds 数据源
|
||||
* @param dialect 方言
|
||||
*/
|
||||
public SqlRunner(DataSource ds, Dialect dialect) {
|
||||
super(ds, dialect);
|
||||
}
|
||||
//------------------------------------------------------- Constructor end
|
||||
|
||||
//---------------------------------------------------------------------------- Getters and Setters start
|
||||
@Override
|
||||
public SqlRunner setWrapper(Character wrapperChar) {
|
||||
return (SqlRunner) super.setWrapper(wrapperChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlRunner setWrapper(Wrapper wrapper) {
|
||||
return (SqlRunner) super.setWrapper(wrapper);
|
||||
}
|
||||
//---------------------------------------------------------------------------- Getters and Setters end
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException{
|
||||
return ds.getConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeConnection(Connection conn) {
|
||||
DbUtil.close(conn);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------- Private method start
|
||||
//---------------------------------------------------------------------------- Private method end
|
||||
}
|
@@ -1,8 +1,8 @@
|
||||
package cn.hutool.db.ds;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.io.resource.NoResourceException;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.DbRuntimeException;
|
||||
import cn.hutool.db.DbUtil;
|
||||
@@ -18,13 +18,13 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
* 抽象数据源工厂<br>
|
||||
* 此工厂抽象类用于实现数据源的缓存,当用户多次调用{@link #getDataSource(String)} 时,工厂只需创建一次即可。<br>
|
||||
* 数据源是与配置文件中的分组相关的,每个分组的数据源相互独立,也就是每个分组的数据源是单例存在的。
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractDSFactory extends DSFactory {
|
||||
private static final long serialVersionUID = -6407302276272379881L;
|
||||
|
||||
|
||||
/** 数据库配置文件可选路径1 */
|
||||
private static final String DEFAULT_DB_SETTING_PATH = "config/db.setting";
|
||||
/** 数据库配置文件可选路径2 */
|
||||
@@ -37,7 +37,7 @@ public abstract class AbstractDSFactory extends DSFactory {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param dataSourceName 数据源名称
|
||||
* @param dataSourceClass 数据库连接池实现类,用于检测所提供的DataSource类是否存在,当传入的DataSource类不存在时抛出ClassNotFoundException<br>
|
||||
* 此参数的作用是在detectDSFactory方法自动检测所用连接池时,如果实现类不存在,调用此方法会自动抛出异常,从而切换到下一种连接池的检测。
|
||||
@@ -69,7 +69,7 @@ public abstract class AbstractDSFactory extends DSFactory {
|
||||
|
||||
/**
|
||||
* 获取配置,用于自定义添加配置项
|
||||
*
|
||||
*
|
||||
* @return Setting
|
||||
* @since 4.0.3
|
||||
*/
|
||||
@@ -97,7 +97,7 @@ public abstract class AbstractDSFactory extends DSFactory {
|
||||
|
||||
/**
|
||||
* 创建数据源
|
||||
*
|
||||
*
|
||||
* @param group 分组
|
||||
* @return {@link DataSourceWrapper} 数据源包装
|
||||
*/
|
||||
@@ -107,7 +107,7 @@ public abstract class AbstractDSFactory extends DSFactory {
|
||||
}
|
||||
|
||||
final Setting config = setting.getSetting(group);
|
||||
if (CollectionUtil.isEmpty(config)) {
|
||||
if (MapUtil.isEmpty(config)) {
|
||||
throw new DbRuntimeException("No config for group: [{}]", group);
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ public abstract class AbstractDSFactory extends DSFactory {
|
||||
|
||||
/**
|
||||
* 创建新的{@link DataSource}<br>
|
||||
*
|
||||
*
|
||||
* @param jdbcUrl JDBC连接字符串
|
||||
* @param driver 数据库驱动类名
|
||||
* @param user 用户名
|
||||
@@ -154,7 +154,7 @@ public abstract class AbstractDSFactory extends DSFactory {
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
if (CollectionUtil.isNotEmpty(dsMap)) {
|
||||
if (MapUtil.isNotEmpty(dsMap)) {
|
||||
Collection<DataSourceWrapper> values = dsMap.values();
|
||||
for (DataSourceWrapper ds : values) {
|
||||
ds.close();
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package cn.hutool.db.ds.pooled;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.DbRuntimeException;
|
||||
import cn.hutool.db.dialect.DriverUtil;
|
||||
@@ -9,7 +9,7 @@ import cn.hutool.setting.Setting;
|
||||
|
||||
/**
|
||||
* 数据库配置文件类,此类对应一个数据库配置文件
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
@@ -28,7 +28,7 @@ public class DbSetting {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param setting 数据库配置
|
||||
*/
|
||||
public DbSetting(Setting setting) {
|
||||
@@ -41,13 +41,13 @@ public class DbSetting {
|
||||
|
||||
/**
|
||||
* 获得数据库连接信息
|
||||
*
|
||||
*
|
||||
* @param group 分组
|
||||
* @return 分组
|
||||
*/
|
||||
public DbConfig getDbConfig(String group) {
|
||||
final Setting config = setting.getSetting(group);
|
||||
if (CollectionUtil.isEmpty(config)) {
|
||||
if (MapUtil.isEmpty(config)) {
|
||||
throw new DbRuntimeException("No Hutool pool config for group: [{}]", group);
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,5 @@
|
||||
package cn.hutool.db.ds.simple;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.DbRuntimeException;
|
||||
@@ -16,7 +15,7 @@ import java.util.Properties;
|
||||
|
||||
/***
|
||||
* 简易数据源,没有使用连接池,仅供测试或打开关闭连接非常少的场合使用!
|
||||
*
|
||||
*
|
||||
* @author loolly
|
||||
*
|
||||
*/
|
||||
@@ -37,7 +36,7 @@ public class SimpleDataSource extends AbstractDataSource {
|
||||
|
||||
/**
|
||||
* 获得一个数据源
|
||||
*
|
||||
*
|
||||
* @param group 数据源分组
|
||||
* @return {@link SimpleDataSource}
|
||||
*/
|
||||
@@ -47,7 +46,7 @@ public class SimpleDataSource extends AbstractDataSource {
|
||||
|
||||
/**
|
||||
* 获得一个数据源,无分组
|
||||
*
|
||||
*
|
||||
* @return {@link SimpleDataSource}
|
||||
*/
|
||||
synchronized public static SimpleDataSource getDataSource() {
|
||||
@@ -64,7 +63,7 @@ public class SimpleDataSource extends AbstractDataSource {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param group 数据库配置文件中的分组
|
||||
*/
|
||||
public SimpleDataSource(String group) {
|
||||
@@ -73,7 +72,7 @@ public class SimpleDataSource extends AbstractDataSource {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param setting 数据库配置
|
||||
* @param group 数据库配置文件中的分组
|
||||
*/
|
||||
@@ -82,7 +81,7 @@ public class SimpleDataSource extends AbstractDataSource {
|
||||
setting = new Setting(DEFAULT_DB_CONFIG_PATH);
|
||||
}
|
||||
final Setting config = setting.getSetting(group);
|
||||
if (CollectionUtil.isEmpty(config)) {
|
||||
if (MapUtil.isEmpty(config)) {
|
||||
throw new DbRuntimeException("No DataSource config for group: [{}]", group);
|
||||
}
|
||||
|
||||
@@ -99,7 +98,7 @@ public class SimpleDataSource extends AbstractDataSource {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param url jdbc url
|
||||
* @param user 用户名
|
||||
* @param pass 密码
|
||||
@@ -110,7 +109,7 @@ public class SimpleDataSource extends AbstractDataSource {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param url jdbc url
|
||||
* @param user 用户名
|
||||
* @param pass 密码
|
||||
@@ -124,7 +123,7 @@ public class SimpleDataSource extends AbstractDataSource {
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*
|
||||
*
|
||||
* @param url jdbc url
|
||||
* @param user 用户名
|
||||
* @param pass 密码
|
||||
@@ -135,7 +134,7 @@ public class SimpleDataSource extends AbstractDataSource {
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*
|
||||
*
|
||||
* @param url jdbc url
|
||||
* @param user 用户名
|
||||
* @param pass 密码
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package cn.hutool.db.nosql.mongo;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.RuntimeUtil;
|
||||
import cn.hutool.setting.Setting;
|
||||
@@ -15,10 +15,10 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
*
|
||||
*/
|
||||
public class MongoFactory {
|
||||
|
||||
|
||||
/** 各分组做组合key的时候分隔符 */
|
||||
private final static String GROUP_SEPRATER = ",";
|
||||
|
||||
|
||||
/** 数据源池 */
|
||||
private static final Map<String, MongoDS> DS_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
@@ -30,7 +30,7 @@ public class MongoFactory {
|
||||
// ------------------------------------------------------------------------ Get DS start
|
||||
/**
|
||||
* 获取MongoDB数据源<br>
|
||||
*
|
||||
*
|
||||
* @param host 主机
|
||||
* @param port 端口
|
||||
* @return MongoDB连接
|
||||
@@ -50,7 +50,7 @@ public class MongoFactory {
|
||||
/**
|
||||
* 获取MongoDB数据源<br>
|
||||
* 多个分组名对应的连接组成集群
|
||||
*
|
||||
*
|
||||
* @param groups 分组列表
|
||||
* @return MongoDB连接
|
||||
*/
|
||||
@@ -68,7 +68,7 @@ public class MongoFactory {
|
||||
|
||||
/**
|
||||
* 获取MongoDB数据源<br>
|
||||
*
|
||||
*
|
||||
* @param groups 分组列表
|
||||
* @return MongoDB连接
|
||||
*/
|
||||
@@ -78,7 +78,7 @@ public class MongoFactory {
|
||||
|
||||
/**
|
||||
* 获取MongoDB数据源<br>
|
||||
*
|
||||
*
|
||||
* @param setting 设定文件
|
||||
* @param groups 分组列表
|
||||
* @return MongoDB连接
|
||||
@@ -97,7 +97,7 @@ public class MongoFactory {
|
||||
|
||||
/**
|
||||
* 获取MongoDB数据源<br>
|
||||
*
|
||||
*
|
||||
* @param setting 配置文件
|
||||
* @param groups 分组列表
|
||||
* @return MongoDB连接
|
||||
@@ -106,12 +106,12 @@ public class MongoFactory {
|
||||
return getDS(setting, groups.toArray(new String[0]));
|
||||
}
|
||||
// ------------------------------------------------------------------------ Get DS ends
|
||||
|
||||
|
||||
/**
|
||||
* 关闭全部连接
|
||||
*/
|
||||
public static void closeAll() {
|
||||
if(CollectionUtil.isNotEmpty(DS_MAP)){
|
||||
if(MapUtil.isNotEmpty(DS_MAP)){
|
||||
for(MongoDS ds : DS_MAP.values()) {
|
||||
ds.close();
|
||||
}
|
||||
|
@@ -2,8 +2,8 @@ package cn.hutool.db.sql;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.lang.Editor;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.Entity;
|
||||
|
||||
@@ -18,15 +18,15 @@ import java.util.Map.Entry;
|
||||
*
|
||||
*/
|
||||
public class Wrapper {
|
||||
|
||||
|
||||
/** 前置包装符号 */
|
||||
private Character preWrapQuote;
|
||||
/** 后置包装符号 */
|
||||
private Character sufWrapQuote;
|
||||
|
||||
|
||||
public Wrapper() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param wrapQuote 单包装字符
|
||||
@@ -35,7 +35,7 @@ public class Wrapper {
|
||||
this.preWrapQuote = wrapQuote;
|
||||
this.sufWrapQuote = wrapQuote;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 包装符号
|
||||
* @param preWrapQuote 前置包装符号
|
||||
@@ -45,7 +45,7 @@ public class Wrapper {
|
||||
this.preWrapQuote = preWrapQuote;
|
||||
this.sufWrapQuote = sufWrapQuote;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------- Getters and Setters start
|
||||
/**
|
||||
* @return 前置包装符号
|
||||
@@ -60,7 +60,7 @@ public class Wrapper {
|
||||
public void setPreWrapQuote(Character preWrapQuote) {
|
||||
this.preWrapQuote = preWrapQuote;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return 后置包装符号
|
||||
*/
|
||||
@@ -75,7 +75,7 @@ public class Wrapper {
|
||||
this.sufWrapQuote = sufWrapQuote;
|
||||
}
|
||||
//--------------------------------------------------------------- Getters and Setters end
|
||||
|
||||
|
||||
/**
|
||||
* 包装字段名<br>
|
||||
* 有时字段与SQL的某些关键字冲突,导致SQL出错,因此需要将字段名用单引号或者反引号包装起来,避免冲突
|
||||
@@ -86,26 +86,26 @@ public class Wrapper {
|
||||
if(preWrapQuote == null || sufWrapQuote == null || StrUtil.isBlank(field)) {
|
||||
return field;
|
||||
}
|
||||
|
||||
|
||||
//如果已经包含包装的引号,返回原字符
|
||||
if(StrUtil.isSurround(field, preWrapQuote, sufWrapQuote)){
|
||||
return field;
|
||||
}
|
||||
|
||||
|
||||
//如果字段中包含通配符或者括号(字段通配符或者函数),不做包装
|
||||
if(StrUtil.containsAnyIgnoreCase(field, "*", "(", " ", " as ")) {
|
||||
return field;
|
||||
}
|
||||
|
||||
|
||||
//对于Oracle这类数据库,表名中包含用户名需要单独拆分包装
|
||||
if(field.contains(StrUtil.DOT)){
|
||||
final Collection<String> target = CollUtil.filter(StrUtil.split(field, StrUtil.C_DOT, 2), (Editor<String>) t -> StrUtil.format("{}{}{}", preWrapQuote, t, sufWrapQuote));
|
||||
final Collection<String> target = CollUtil.edit(StrUtil.split(field, CharUtil.DOT, 2), t -> StrUtil.format("{}{}{}", preWrapQuote, t, sufWrapQuote));
|
||||
return CollectionUtil.join(target, StrUtil.DOT);
|
||||
}
|
||||
|
||||
|
||||
return StrUtil.format("{}{}{}", preWrapQuote, field, sufWrapQuote);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 包装字段名<br>
|
||||
* 有时字段与SQL的某些关键字冲突,导致SQL出错,因此需要将字段名用单引号或者反引号包装起来,避免冲突
|
||||
@@ -116,15 +116,15 @@ public class Wrapper {
|
||||
if(ArrayUtil.isEmpty(fields)) {
|
||||
return fields;
|
||||
}
|
||||
|
||||
|
||||
String[] wrappedFields = new String[fields.length];
|
||||
for(int i = 0; i < fields.length; i++) {
|
||||
wrappedFields[i] = wrap(fields[i]);
|
||||
}
|
||||
|
||||
|
||||
return wrappedFields;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 包装字段名<br>
|
||||
* 有时字段与SQL的某些关键字冲突,导致SQL出错,因此需要将字段名用单引号或者反引号包装起来,避免冲突
|
||||
@@ -135,10 +135,10 @@ public class Wrapper {
|
||||
if(CollectionUtil.isEmpty(fields)) {
|
||||
return fields;
|
||||
}
|
||||
|
||||
|
||||
return Arrays.asList(wrap(fields.toArray(new String[0])));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 包装字段名<br>
|
||||
* 有时字段与SQL的某些关键字冲突,导致SQL出错,因此需要将字段名用单引号或者反引号包装起来,避免冲突
|
||||
@@ -149,20 +149,20 @@ public class Wrapper {
|
||||
if(null == entity) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
final Entity wrapedEntity = new Entity();
|
||||
|
||||
|
||||
//wrap table name
|
||||
wrapedEntity.setTableName(wrap(entity.getTableName()));
|
||||
|
||||
|
||||
//wrap fields
|
||||
for (Entry<String, Object> entry : entity.entrySet()) {
|
||||
wrapedEntity.set(wrap(entry.getKey()), entry.getValue());
|
||||
}
|
||||
|
||||
|
||||
return wrapedEntity;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 包装字段名<br>
|
||||
* 有时字段与SQL的某些关键字冲突,导致SQL出错,因此需要将字段名用单引号或者反引号包装起来,避免冲突
|
||||
@@ -179,7 +179,7 @@ public class Wrapper {
|
||||
clonedConditions[i] = clonedCondition;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return clonedConditions;
|
||||
}
|
||||
}
|
||||
|
@@ -4,19 +4,19 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SqlBuilderTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void queryNullTest() {
|
||||
SqlBuilder builder = SqlBuilder.create().select().from("user").where(new Condition("name", "= null"));
|
||||
Assert.assertEquals("SELECT * FROM user WHERE name IS NULL", builder.build());
|
||||
|
||||
|
||||
SqlBuilder builder2 = SqlBuilder.create().select().from("user").where(new Condition("name", "is null"));
|
||||
Assert.assertEquals("SELECT * FROM user WHERE name IS NULL", builder2.build());
|
||||
|
||||
SqlBuilder builder3 = SqlBuilder.create().select().from("user").where(LogicalOperator.AND, new Condition("name", "!= null"));
|
||||
|
||||
SqlBuilder builder3 = SqlBuilder.create().select().from("user").where(new Condition("name", "!= null"));
|
||||
Assert.assertEquals("SELECT * FROM user WHERE name IS NOT NULL", builder3.build());
|
||||
|
||||
SqlBuilder builder4 = SqlBuilder.create().select().from("user").where(LogicalOperator.AND, new Condition("name", "is not null"));
|
||||
|
||||
SqlBuilder builder4 = SqlBuilder.create().select().from("user").where(new Condition("name", "is not null"));
|
||||
Assert.assertEquals("SELECT * FROM user WHERE name IS NOT NULL", builder4.build());
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user