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