001/*
002 *  Copyright (c) 2022-2024, 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.FlexConsts;
019import com.mybatisflex.core.dialect.IDialect;
020
021import java.util.List;
022
023/**
024 * CAST函数查询列
025 */
026public class CastQueryColumn extends QueryColumn implements HasParamsColumn {
027
028    private QueryColumn column;
029    private final String dataType;
030
031    public CastQueryColumn(QueryColumn column, String dataType) {
032        this.column = column;
033        this.dataType = dataType;
034    }
035
036    public CastQueryColumn(String column, String dataType) {
037        this.column = new QueryColumn(column);
038        this.dataType = dataType;
039    }
040
041    @Override
042    protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
043        return " CAST(" + column.toConditionSql(queryTables, dialect) + " AS " + dataType + ") ";
044    }
045
046    @Override
047    protected String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
048        return " CAST(" + column.toSelectSql(queryTables, dialect) + " AS " + dataType + ") " + WrapperUtil.buildColumnAlias(alias, dialect);
049    }
050
051    @Override
052    public CastQueryColumn clone() {
053        CastQueryColumn clone = (CastQueryColumn) super.clone();
054        clone.column = column.clone();
055        return clone;
056    }
057
058    @Override
059    public String toString() {
060        return "CastQueryColumn{" +
061            "column=" + column +
062            ", dataType='" + dataType + '\'' +
063            ", alias='" + alias + '\'' +
064            '}';
065    }
066
067    @Override
068    public Object[] getParamValues() {
069        if (column instanceof HasParamsColumn) {
070            return ((HasParamsColumn) column).getParamValues();
071        }
072        return FlexConsts.EMPTY_ARRAY;
073    }
074}