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