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.exception.FlexExceptions; 020import com.mybatisflex.core.util.CollectionUtil; 021import com.mybatisflex.core.util.ObjectUtil; 022import com.mybatisflex.core.util.StringUtil; 023 024import java.util.List; 025 026import static com.mybatisflex.core.constant.SqlConsts.*; 027 028public class WithItem implements CloneSupport<WithItem> { 029 030 private String name; 031 private List<String> params; 032 033 private WithDetail withDetail; 034 035 public WithItem() { 036 } 037 038 public WithItem(String name, List<String> params) { 039 this.name = name; 040 this.params = params; 041 } 042 043 public String getName() { 044 return name; 045 } 046 047 public void setName(String name) { 048 this.name = name; 049 } 050 051 public List<String> getParams() { 052 return params; 053 } 054 055 public void setParams(List<String> params) { 056 this.params = params; 057 } 058 059 public WithDetail getWithDetail() { 060 return withDetail; 061 } 062 063 public void setWithDetail(WithDetail withDetail) { 064 this.withDetail = withDetail; 065 } 066 067 public String toSql(IDialect dialect) { 068 StringBuilder sql = new StringBuilder(name); 069 if (CollectionUtil.isNotEmpty(params)) { 070 sql.append(BRACKET_LEFT).append(StringUtil.join(DELIMITER, params)).append(BRACKET_RIGHT); 071 } 072 sql.append(AS).append(BRACKET_LEFT); 073 sql.append(withDetail.toSql(dialect)); 074 return sql.append(BRACKET_RIGHT).toString(); 075 } 076 077 public Object[] getParamValues() { 078 return withDetail.getParamValues(); 079 } 080 081 @Override 082 public WithItem clone() { 083 try { 084 WithItem clone = (WithItem) super.clone(); 085 // deep clone ... 086 clone.withDetail = ObjectUtil.clone(this.withDetail); 087 clone.params = CollectionUtil.newArrayList(this.params); 088 return clone; 089 } catch (CloneNotSupportedException e) { 090 throw FlexExceptions.wrap(e); 091 } 092 } 093 094}