org.dromara.hutool.db.sql.StatementBuilder Maven / Gradle / Ivy
/*
* Copyright (c) 2013-2024 Hutool Team and hutool.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.hutool.db.sql;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.collection.iter.ArrayIter;
import org.dromara.hutool.core.convert.ConvertUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.builder.Builder;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.db.DbException;
import org.dromara.hutool.db.Entity;
import org.dromara.hutool.db.sql.filter.SqlFilter;
import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* {@link PreparedStatement}构建器,构建结果为{@link StatementWrapper}
*
* @author looly
* @since 6.0.0
*/
public class StatementBuilder implements Builder {
private static final long serialVersionUID = 1L;
/**
* 创建构建器
*
* @return StatementBuilder
*/
public static StatementBuilder of() {
return new StatementBuilder();
}
private final BoundSql boundSql = new BoundSql();
private Connection connection;
private boolean returnGeneratedKey = true;
private SqlFilter sqlFilter;
/**
* 设置SQL日志
*
* @param sqlFilter {@link SqlFilter}
* @return this
*/
public StatementBuilder setSqlFilter(final SqlFilter sqlFilter) {
this.sqlFilter = sqlFilter;
return this;
}
/**
* 设置连接
*
* @param connection {@link Connection}
* @return this
*/
public StatementBuilder setConnection(final Connection connection) {
this.connection = connection;
return this;
}
/**
* 设置执行的SQL语句
*
* @param sql SQL语句
* @return this
*/
public StatementBuilder setSql(final String sql) {
this.boundSql.setSql(sql);
return this;
}
/**
* 设置SQL的"?"对应的参数
*
* @param params 参数数组
* @return this
*/
public StatementBuilder setParams(final Object... params) {
this.boundSql.setParams(ListUtil.of(params));
return this;
}
/**
* 设置SQL的"?"对应的参数
*
* @param params 参数列表
* @return this
*/
public StatementBuilder setParamList(final List