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.dialect.IDialect;
019import com.mybatisflex.core.util.ObjectUtil;
020import com.mybatisflex.core.util.StringUtil;
021
022import java.util.List;
023
024/**
025 * 子查询列。
026 *
027 * @author michael
028 * @author 王帅
029 */
030public class SelectQueryColumn extends QueryColumn implements HasParamsColumn {
031
032    private QueryWrapper queryWrapper;
033
034    public SelectQueryColumn(QueryWrapper queryWrapper) {
035        this.queryWrapper = queryWrapper;
036    }
037
038    public QueryWrapper getQueryWrapper() {
039        return queryWrapper;
040    }
041
042    @Override
043    protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
044        return WrapperUtil.withBracket(dialect.forSelectByQuery(queryWrapper));
045    }
046
047    @Override
048    protected String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
049        String selectSql = dialect.forSelectByQuery(queryWrapper);
050        if (StringUtil.hasText(selectSql) && StringUtil.hasText(alias)) {
051            selectSql = WrapperUtil.withAlias(selectSql, alias, dialect);
052        }
053        return selectSql;
054    }
055
056    @Override
057    public SelectQueryColumn clone() {
058        SelectQueryColumn clone = (SelectQueryColumn) super.clone();
059        // deep clone ...
060        clone.queryWrapper = ObjectUtil.clone(this.queryWrapper);
061        return clone;
062    }
063
064    @Override
065    public Object[] getParamValues() {
066        return queryWrapper.getAllValueArray();
067    }
068
069}