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.util.SqlUtil; 020import com.mybatisflex.core.util.StringUtil; 021 022import java.util.List; 023 024/** 025 * 数据库 聚合函数,例如 count(id) ,max(account.age) 等等 026 */ 027public class FunctionQueryColumn extends QueryColumn { 028 029 protected String fnName; 030 protected QueryColumn column; 031 032 public FunctionQueryColumn(String fnName, String column) { 033 SqlUtil.keepColumnSafely(fnName); 034 SqlUtil.keepColumnSafely(column); 035 this.fnName = fnName; 036 this.column = new QueryColumn(column); 037 } 038 039 public FunctionQueryColumn(String fnName, QueryColumn column) { 040 SqlUtil.keepColumnSafely(fnName); 041 this.fnName = fnName; 042 this.column = column; 043 } 044 045 public String getFnName() { 046 return fnName; 047 } 048 049 public void setFnName(String fnName) { 050 this.fnName = fnName; 051 } 052 053 public QueryColumn getColumn() { 054 return column; 055 } 056 057 public void setColumn(QueryColumn column) { 058 this.column = column; 059 } 060 061 @Override 062 public String toSelectSql(List<QueryTable> queryTables, IDialect dialect) { 063 String sql = column.toSelectSql(queryTables, dialect); 064 return StringUtil.isBlank(sql) ? "" : fnName + "(" + sql + ")" + WrapperUtil.buildAsAlias(alias); 065 } 066 067 @Override 068 public QueryColumn as(String alias) { 069 SqlUtil.keepColumnSafely(alias); 070 this.alias = alias; 071 return this; 072 } 073 074 @Override 075 public String toString() { 076 return "FunctionQueryColumn{" + 077 "fnName='" + fnName + '\'' + 078 ", column=" + column + 079 '}'; 080 } 081}