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.dialect.IDialect;
019import com.mybatisflex.core.dialect.OperateType;
020import com.mybatisflex.core.util.StringUtil;
021
022/**
023 * 查询的 table,
024 * 实例1:用于构建 select * from (select ...) 中的第二个 select
025 * 实例2:用于构建 left join (select ...) 中的 select
026 */
027public class SelectQueryTable extends QueryTable {
028
029    private QueryWrapper queryWrapper;
030
031    public SelectQueryTable(QueryWrapper queryWrapper) {
032        super();
033        this.queryWrapper = queryWrapper;
034    }
035
036    public QueryWrapper getQueryWrapper() {
037        return queryWrapper;
038    }
039
040    public void setQueryWrapper(QueryWrapper queryWrapper) {
041        this.queryWrapper = queryWrapper;
042    }
043
044    @Override
045    Object[] getValueArray() {
046        return queryWrapper.getAllValueArray();
047    }
048
049    @Override
050    public String toSql(IDialect dialect, OperateType operateType) {
051        String sql = dialect.buildSelectSql(queryWrapper);
052        if (StringUtil.hasText(alias)) {
053            return WrapperUtil.withAlias(sql, alias, dialect);
054        } else {
055            return WrapperUtil.withBracket(sql);
056        }
057    }
058
059    @Override
060    public SelectQueryTable clone() {
061        SelectQueryTable clone = (SelectQueryTable) super.clone();
062        // deep clone ...
063        clone.queryWrapper = this.queryWrapper.clone();
064        return clone;
065    }
066
067    @Override
068    public String toString() {
069        return queryWrapper.toSQL();
070    }
071}