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