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