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.constant.SqlConsts;
019import com.mybatisflex.core.dialect.IDialect;
020
021import java.util.List;
022
023/**
024 * SQL 原生片段
025 */
026public class RawFragment extends QueryCondition {
027
028
029    protected String content;
030
031
032    public RawFragment(String content) {
033        this.content = content;
034    }
035
036    public RawFragment(String content, Object... paras) {
037        this.content = content;
038        this.setValue(paras);
039    }
040
041
042    @Override
043    public String toSql(List<QueryTable> queryTables, IDialect dialect) {
044        StringBuilder sql = new StringBuilder();
045
046        //检测是否生效
047        if (checkEffective()) {
048            QueryCondition prevEffectiveCondition = getPrevEffectiveCondition();
049            if (prevEffectiveCondition != null) {
050                sql.append(prevEffectiveCondition.connector);
051            }
052            sql.append(SqlConsts.BLANK).append(content).append(SqlConsts.BLANK);
053        }
054
055        if (this.next != null) {
056            return sql + next.toSql(queryTables, dialect);
057        }
058
059        return sql.toString();
060    }
061
062    @Override
063    public RawFragment clone() {
064        return (RawFragment) super.clone();
065    }
066
067
068    public String getContent() {
069        return content;
070    }
071
072}