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.util.CollectionUtil;
019
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.List;
023
024public class WithBuilder {
025
026    private QueryWrapper queryWrapper;
027    private With with;
028
029    //withItem
030    private String name;
031    private List<String> params;
032
033    public WithBuilder() {
034    }
035
036    public WithBuilder(QueryWrapper queryWrapper, With with, String name) {
037        this.queryWrapper = queryWrapper;
038        this.with = with;
039        this.name = name;
040    }
041
042    public WithBuilder(QueryWrapper queryWrapper, With with, String name, List<String> params) {
043        this.queryWrapper = queryWrapper;
044        this.with = with;
045        this.name = name;
046        this.params = params;
047    }
048
049
050    public QueryWrapper asSelect(QueryWrapper newWrapper) {
051        WithItem withItem = new WithItem(name, params);
052        withItem.setWithDetail(new WithSelectDetail(newWrapper));
053        with.addWithItem(withItem);
054        return queryWrapper;
055    }
056
057
058    public QueryWrapper asValues(Object[] values, QueryWrapper newWrapper) {
059        WithItem withItem = new WithItem(name, params);
060        withItem.setWithDetail(new WithValuesDetail(Arrays.asList(values), newWrapper));
061        with.addWithItem(withItem);
062        return queryWrapper;
063    }
064
065
066    public QueryWrapper asValues(Collection values, QueryWrapper newWrapper) {
067        WithItem withItem = new WithItem(name, params);
068        withItem.setWithDetail(new WithValuesDetail(CollectionUtil.toList(values), newWrapper));
069        with.addWithItem(withItem);
070        return queryWrapper;
071    }
072
073    public QueryWrapper asRaw(String rawSQL, Object... params) {
074        WithItem withItem = new WithItem(name, this.params);
075        withItem.setWithDetail(new WithStringDetail(rawSQL, params));
076        with.addWithItem(withItem);
077        return queryWrapper;
078    }
079
080
081}