All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dromara.hutool.db.sql.ConditionBuilder Maven / Gradle / Ivy

There is a newer version: 6.0.0.M3
Show newest version
/*
 * 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.lang.builder.Builder;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.text.CharUtil;
import org.dromara.hutool.core.text.StrUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 多条件构建封装
* 可以将多个条件构建为SQL语句的一部分,并将参数值转换为占位符,并提取对应位置的参数值。
* 例如:name = ? AND type IN (?, ?) AND other LIKE ? * * @author looly * @since 5.4.3 */ public class ConditionBuilder implements Builder { private static final long serialVersionUID = 1L; /** * 创建构建器 * * @param conditions 条件列表 * @return ConditionBuilder */ public static ConditionBuilder of(final Condition... conditions) { return new ConditionBuilder(conditions); } /** * 条件数组 */ private final Condition[] conditions; /** * 占位符对应的值列表 */ private List paramValues; /** * 构造 * * @param conditions 条件列表 */ public ConditionBuilder(final Condition... conditions) { this.conditions = conditions; } /** * 返回构建后的参数列表
* 此方法调用前必须调用{@link #build()} * * @return 参数列表 */ public List getParamValues() { return ListUtil.view(this.paramValues); } /** * 构建组合条件
* 例如:name = ? AND type IN (?, ?) AND other LIKE ? * * @return 构建后的SQL语句条件部分 */ @Override public String build() { if(null == this.paramValues){ this.paramValues = new ArrayList<>(); } else { this.paramValues.clear(); } return build(this.paramValues); } /** * 构建组合条件
* 例如:name = ? AND type IN (?, ?) AND other LIKE ? * * @param paramValues 用于写出参数的List,构建时会将参数写入此List * @return 构建后的SQL语句条件部分 */ public String build(final List paramValues) { if (ArrayUtil.isEmpty(conditions)) { return StrUtil.EMPTY; } final StringBuilder conditionStrBuilder = new StringBuilder(); boolean isFirst = true; for (final Condition condition : conditions) { // 添加逻辑运算符 if (isFirst) { isFirst = false; } else { // " AND " 或者 " OR " conditionStrBuilder.append(CharUtil.SPACE).append(condition.getLinkOperator()).append(CharUtil.SPACE); } // 构建条件部分:"name = ?"、"name IN (?,?,?)"、"name BETWEEN ?AND ?"、"name LIKE ?" conditionStrBuilder.append(condition.toString(paramValues)); } return conditionStrBuilder.toString(); } /** * 构建组合条件
* 例如:name = ? AND type IN (?, ?) AND other LIKE ? * * @return 构建后的SQL语句条件部分 */ @Override public String toString() { return build(); } }