All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mybatis.dynamic.sql.delete.DeleteDSL Maven / Gradle / Ivy

/*
 *    Copyright ${license.git.copyrightYears} the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.dynamic.sql.delete;

import java.util.Objects;
import java.util.function.Function;

import org.jetbrains.annotations.NotNull;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
import org.mybatis.dynamic.sql.where.AbstractWhereSupport;
import org.mybatis.dynamic.sql.where.WhereModel;

public class DeleteDSL extends AbstractWhereSupport.DeleteWhereBuilder> implements Buildable {

    private final Function adapterFunction;
    private final SqlTable table;
    private final DeleteWhereBuilder whereBuilder = new DeleteWhereBuilder();

    private DeleteDSL(SqlTable table, Function adapterFunction) {
        this.table = Objects.requireNonNull(table);
        this.adapterFunction = Objects.requireNonNull(adapterFunction);
    }

    @Override
    public DeleteWhereBuilder where() {
        return whereBuilder;
    }

    /**
     * WARNING! Calling this method could result in an delete statement that deletes
     * all rows in a table.
     *
     * @return the model class
     */
    @NotNull
    @Override
    public R build() {
        DeleteModel deleteModel = DeleteModel.withTable(table)
                .withWhereModel(whereBuilder.buildWhereModel())
                .build();
        return adapterFunction.apply(deleteModel);
    }

    public static  DeleteDSL deleteFrom(Function adapterFunction, SqlTable table) {
        return new DeleteDSL<>(table, adapterFunction);
    }

    public static DeleteDSL deleteFrom(SqlTable table) {
        return deleteFrom(Function.identity(), table);
    }

    public class DeleteWhereBuilder extends AbstractWhereDSL implements Buildable {

        private DeleteWhereBuilder() {}

        @NotNull
        @Override
        public R build() {
            return DeleteDSL.this.build();
        }

        @Override
        protected DeleteWhereBuilder getThis() {
            return this;
        }

        protected WhereModel buildWhereModel() {
            return internalBuild();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy