001/**
002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package com.mybatisflex.core.query;
017
018import com.mybatisflex.core.dialect.IDialect;
019import com.mybatisflex.core.util.StringUtil;
020
021import java.util.List;
022
023/**
024 * 操作类型的操作
025 * 示例1:and EXISTS (select 1 from ... where ....)
026 * 示例2:and not EXISTS (select ... from ... where ....)
027 */
028public class OperatorSelectCondition extends QueryCondition {
029    //操作符,例如 exist, not exist
030    private String operator;
031    private QueryWrapper queryWrapper;
032
033    public OperatorSelectCondition(String operator, QueryWrapper queryWrapper) {
034        this.operator = operator;
035        this.queryWrapper = queryWrapper;
036    }
037
038    @Override
039    public String toSql(List<QueryTable> queryTables, IDialect dialect) {
040        StringBuilder sql = new StringBuilder();
041
042        //检测是否生效
043        if (checkEffective()) {
044            String childSql = dialect.buildSelectSql(queryWrapper);
045            if (StringUtil.isNotBlank(childSql)) {
046
047                QueryCondition effectiveBefore = getEffectiveBefore();
048                if (effectiveBefore != null) {
049                    sql.append(effectiveBefore.connector);
050                }
051                sql.append(operator).append("(").append(childSql).append(")");
052            }
053        }
054
055        if (this.next != null) {
056            return sql + next.toSql(queryTables, dialect);
057        }
058
059        return sql.toString();
060    }
061
062    @Override
063    public Object getValue() {
064        return queryWrapper.getValueArray();
065    }
066}