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.CollectionUtil; 021import com.mybatisflex.core.util.SqlUtil; 022import com.mybatisflex.core.util.StringUtil; 023 024import java.util.Arrays; 025import java.util.List; 026 027/** 028 * 数据库 聚合函数,例如 CONVERT(NVARCHAR(30), GETDATE(), 126) 等等 029 */ 030public class StringFunctionQueryColumn extends QueryColumn { 031 032 protected String fnName; 033 protected List<String> params; 034 035 public StringFunctionQueryColumn(String fnName, String... params) { 036 SqlUtil.keepColumnSafely(fnName); 037 this.fnName = fnName; 038 this.params = Arrays.asList(params); 039 } 040 041 042 public String getFnName() { 043 return fnName; 044 } 045 046 public void setFnName(String fnName) { 047 this.fnName = fnName; 048 } 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 @Override 060 public String toSelectSql(List<QueryTable> queryTables, IDialect dialect) { 061 String sql = StringUtil.join(SqlConsts.DELIMITER, params); 062 if (StringUtil.isBlank(sql)) { 063 return SqlConsts.EMPTY; 064 } 065 if (StringUtil.isBlank(alias)) { 066 return fnName + WrapperUtil.withBracket(sql); 067 } 068 return fnName + WrapperUtil.withAlias(sql, alias, dialect); 069 } 070 071 @Override 072 String toConditionSql(List<QueryTable> queryTables, IDialect dialect) { 073 String sql = StringUtil.join(SqlConsts.DELIMITER, params); 074 if (StringUtil.isBlank(sql)) { 075 return SqlConsts.EMPTY; 076 } 077 return fnName + WrapperUtil.withBracket(sql); 078 } 079 080 081 @Override 082 public String toString() { 083 return "StringFunctionQueryColumn{" + 084 "fnName='" + fnName + '\'' + 085 ", params=" + params + 086 '}'; 087 } 088 089 @Override 090 public StringFunctionQueryColumn clone() { 091 StringFunctionQueryColumn clone = (StringFunctionQueryColumn) super.clone(); 092 // deep clone ... 093 clone.params = CollectionUtil.newArrayList(this.params); 094 return clone; 095 } 096 097}