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.constant.SqlConsts;
019import com.mybatisflex.core.dialect.IDialect;
020import com.mybatisflex.core.util.ObjectUtil;
021import com.mybatisflex.core.util.StringUtil;
022
023import java.util.List;
024
025/**
026 * 操作类型的操作
027 * 示例1:and EXISTS (select 1 from ... where ....)
028 * 示例2:and not EXISTS (select ... from ... where ....)
029 */
030public class OperatorSelectCondition extends QueryCondition {
031
032    //操作符,例如 exist, not exist
033    private final String operator;
034    private QueryWrapper queryWrapper;
035
036    public OperatorSelectCondition(String operator, QueryWrapper queryWrapper) {
037        this.operator = operator;
038        this.queryWrapper = queryWrapper;
039    }
040
041    @Override
042    public String toSql(List<QueryTable> queryTables, IDialect dialect) {
043        StringBuilder sql = new StringBuilder();
044
045        //检测是否生效
046        if (checkEffective()) {
047            String childSql = dialect.buildSelectSql(queryWrapper);
048            if (StringUtil.isNotBlank(childSql)) {
049
050                QueryCondition prevEffectiveCondition = getPrevEffectiveCondition();
051                if (prevEffectiveCondition != null) {
052                    sql.append(prevEffectiveCondition.connector);
053                }
054                sql.append(operator)
055                    .append(SqlConsts.BRACKET_LEFT)
056                    .append(childSql)
057                    .append(SqlConsts.BRACKET_RIGHT);
058            }
059        }
060
061        if (this.next != null) {
062            return sql + next.toSql(queryTables, dialect);
063        }
064
065        return sql.toString();
066    }
067
068    @Override
069    public Object getValue() {
070        return queryWrapper.getValueArray();
071    }
072
073    @Override
074    boolean containsTable(String... tables) {
075        QueryCondition condition = queryWrapper.getWhereQueryCondition();
076        return condition != null && condition.containsTable(tables);
077    }
078
079    @Override
080    public OperatorSelectCondition clone() {
081        OperatorSelectCondition clone = (OperatorSelectCondition) super.clone();
082        // deep clone ...
083        clone.queryWrapper = ObjectUtil.clone(this.queryWrapper);
084        return clone;
085    }
086
087}