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 not ( id > 100 and name like %%) 028 */ 029public class OperatorQueryCondition extends QueryCondition { 030 031 private final String operator; 032 private QueryCondition childCondition; 033 034 public OperatorQueryCondition(String operator, QueryCondition childCondition) { 035 this.operator = operator; 036 this.childCondition = childCondition; 037 } 038 039 @Override 040 public String toSql(List<QueryTable> queryTables, IDialect dialect) { 041 StringBuilder sql = new StringBuilder(); 042 043 //检测是否生效 044 if (checkEffective()) { 045 String childSql = childCondition.toSql(queryTables, dialect); 046 if (StringUtil.isNotBlank(childSql)) { 047 QueryCondition prevEffectiveCondition = getPrevEffectiveCondition(); 048 if (prevEffectiveCondition != null) { 049 sql.append(prevEffectiveCondition.connector); 050 } 051 sql.append(operator) 052 .append(SqlConsts.BRACKET_LEFT) 053 .append(childSql) 054 .append(SqlConsts.BRACKET_RIGHT); 055 } 056 } 057 058 if (this.next != null) { 059 return sql + next.toSql(queryTables, dialect); 060 } 061 062 return sql.toString(); 063 } 064 065 @Override 066 public Object getValue() { 067 return WrapperUtil.getValues(childCondition); 068 } 069 070 @Override 071 boolean containsTable(String... tables) { 072 if (childCondition != null && childCondition.containsTable(tables)) { 073 return true; 074 } 075 return nextContainsTable(tables); 076 } 077 078 @Override 079 public OperatorQueryCondition clone() { 080 OperatorQueryCondition clone = (OperatorQueryCondition) super.clone(); 081 // deep clone ... 082 clone.childCondition = ObjectUtil.clone(this.childCondition); 083 return clone; 084 } 085 086}