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;
019
020public class UnionWrapper {
021
022    private String key;
023    private QueryWrapper queryWrapper;
024
025    public static UnionWrapper union(QueryWrapper queryWrapper) {
026        UnionWrapper unionWrapper = new UnionWrapper();
027        unionWrapper.key = " UNION ";
028        unionWrapper.queryWrapper = queryWrapper;
029        return unionWrapper;
030    }
031
032    public static UnionWrapper unionAll(QueryWrapper queryWrapper) {
033        UnionWrapper unionWrapper = new UnionWrapper();
034        unionWrapper.key = " UNION ALL ";
035        unionWrapper.queryWrapper = queryWrapper;
036        return unionWrapper;
037    }
038
039
040    private UnionWrapper() {
041    }
042
043    public String getKey() {
044        return key;
045    }
046
047    public void setKey(String key) {
048        this.key = key;
049    }
050
051    public QueryWrapper getQueryWrapper() {
052        return queryWrapper;
053    }
054
055    public void setQueryWrapper(QueryWrapper queryWrapper) {
056        this.queryWrapper = queryWrapper;
057    }
058
059    public void buildSql(StringBuilder sqlBuilder, IDialect dialect) {
060        sqlBuilder.append(key).append("(").append(dialect.buildSelectSql(queryWrapper)).append(")");
061    }
062}