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 */
016
017package com.mybatisflex.core.query;
018
019import com.mybatisflex.core.BaseMapper;
020import com.mybatisflex.core.mybatis.Mappers;
021import com.mybatisflex.core.table.TableInfo;
022import com.mybatisflex.core.table.TableInfoFactory;
023
024/**
025 * {@link QueryWrapper} 链式调用。
026 *
027 * @author 王帅
028 * @since 2023-07-22
029 */
030public class QueryChain<T> extends QueryWrapperAdapter<QueryChain<T>> implements MapperQueryChain<T> {
031
032    private final BaseMapper<T> baseMapper;
033
034    public QueryChain(BaseMapper<T> baseMapper) {
035        this.baseMapper = baseMapper;
036    }
037
038    public static <T> QueryChain<T> of(Class<T> entityClass) {
039        BaseMapper<T> baseMapper = Mappers.ofEntityClass(entityClass);
040        return new QueryChain<>(baseMapper);
041    }
042
043    public static <E> QueryChain<E> of(BaseMapper<E> baseMapper) {
044        return new QueryChain<>(baseMapper);
045    }
046
047    @Override
048    public BaseMapper<T> baseMapper() {
049        return baseMapper;
050    }
051
052    @Override
053    public QueryWrapper toQueryWrapper() {
054        return this;
055    }
056
057    @Override
058    public String toSQL() {
059        TableInfo tableInfo = TableInfoFactory.ofMapperClass(baseMapper.getClass());
060        CPI.setFromIfNecessary(this, tableInfo.getSchema(), tableInfo.getTableName());
061        return super.toSQL();
062    }
063
064}