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    public QueryWrapper getQueryWrapper() {
042        return queryWrapper;
043    }
044
045    @Override
046    public String toSql(List<QueryTable> queryTables, IDialect dialect) {
047        StringBuilder sql = new StringBuilder();
048
049        //检测是否生效
050        if (checkEffective()) {
051            String childSql = dialect.buildSelectSql(queryWrapper);
052            if (StringUtil.isNotBlank(childSql)) {
053
054                QueryCondition prevEffectiveCondition = getPrevEffectiveCondition();
055                if (prevEffectiveCondition != null) {
056                    sql.append(prevEffectiveCondition.connector);
057                }
058                sql.append(operator)
059                    .append(SqlConsts.BRACKET_LEFT)
060                    .append(childSql)
061                    .append(SqlConsts.BRACKET_RIGHT);
062            }
063        }
064
065        if (this.next != null) {
066            return sql + next.toSql(queryTables, dialect);
067        }
068
069        return sql.toString();
070    }
071
072    @Override
073    public Object getValue() {
074        return queryWrapper.getAllValueArray();
075    }
076
077    @Override
078    boolean containsTable(String... tables) {
079        QueryCondition condition = queryWrapper.getWhereQueryCondition();
080        return condition != null && condition.containsTable(tables);
081    }
082
083    @Override
084    public OperatorSelectCondition clone() {
085        OperatorSelectCondition clone = (OperatorSelectCondition) super.clone();
086        // deep clone ...
087        clone.queryWrapper = ObjectUtil.clone(this.queryWrapper);
088        return clone;
089    }
090
091}