001/* 002 * Copyright (c) 2022-2025, 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.hasText(childSql)) { 053 QueryCondition prevEffectiveCondition = getPrevEffectiveCondition(); 054 if (prevEffectiveCondition != null && this.connector != null) { 055 sql.append(this.connector); 056 } 057 sql.append(operator) 058 .append(SqlConsts.BRACKET_LEFT) 059 .append(childSql) 060 .append(SqlConsts.BRACKET_RIGHT); 061 } 062 } 063 064 if (this.next != null) { 065 return sql + next.toSql(queryTables, dialect); 066 } 067 068 return sql.toString(); 069 } 070 071 @Override 072 public Object getValue() { 073 return checkEffective() ? queryWrapper.getAllValueArray() : null; 074 } 075 076 @Override 077 boolean containsTable(String... tables) { 078 QueryCondition condition = queryWrapper.getWhereQueryCondition(); 079 return condition != null && condition.containsTable(tables); 080 } 081 082 @Override 083 public OperatorSelectCondition clone() { 084 OperatorSelectCondition clone = (OperatorSelectCondition) super.clone(); 085 // deep clone ... 086 clone.queryWrapper = ObjectUtil.clone(this.queryWrapper); 087 return clone; 088 } 089 090}