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 not ( id > 100 and name like %%)
026 */
027public class OperatorQueryCondition extends QueryCondition {
028    
029    private String operator;
030    private QueryCondition child;
031
032    public OperatorQueryCondition(String operator, QueryCondition child) {
033        this.operator = operator;
034        this.child = child;
035    }
036
037    @Override
038    public String toSql(List<QueryTable> queryTables, IDialect dialect) {
039        StringBuilder sql = new StringBuilder();
040
041        //检测是否生效
042        if (checkEffective()) {
043            String childSql = child.toSql(queryTables, dialect);
044            if (StringUtil.isNotBlank(childSql)) {
045                QueryCondition effectiveBefore = getEffectiveBefore();
046                if (effectiveBefore != null) {
047                    sql.append(effectiveBefore.connector);
048                }
049                sql.append(operator).append("(").append(childSql).append(")");
050            }
051        }
052
053        if (this.next != null) {
054            return sql + next.toSql(queryTables, dialect);
055        }
056
057        return sql.toString();
058    }
059
060    @Override
061    public Object getValue() {
062        return WrapperUtil.getValues(child);
063    }
064}