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

com.kenshoo.jooq.DeleteQueryExtension Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.jooq;


import org.jooq.DSLContext;
import org.jooq.Query;
import org.jooq.Record;
import org.jooq.impl.TableImpl;

import java.util.List;

import static com.kenshoo.jooq.QueryExtension.JOIN_TEMP_TABLE_LIMIT;
import static org.jooq.lambda.Seq.seq;

public class DeleteQueryExtension implements AutoCloseable {

    private Query query;
    private TempTableCreator tempTableCreator;

    DeleteQueryExtension(final TableImpl table,
                         final List> fieldWithValues,
                         final DSLContext dslContext) {

        tempTableCreator = new TempTableCreator(dslContext, fieldWithValues);

        String query = "";

        if (shouldUseTempTable(fieldWithValues)) {
            query = "DELETE " + table.getName() +
                    " FROM " + table.getName() +
                    buildJoinToTempTableQuery(fieldWithValues);
        } else {
            query = "DELETE FROM " + table.getName() +
                    buildSimpleWhereInQuery(fieldWithValues);
        }

        this.query = dslContext.query(query);
    }

    private String buildJoinToTempTableQuery(final List> fieldWithValues) {
        return tempTableCreator.getJoinToTempTableClause(fieldWithValues);
    }

    private String buildSimpleWhereInQuery(final List> fieldWithValues) {
        FieldAndValues fieldAndValues = fieldWithValues.get(0);
        return " WHERE " + fieldAndValues.getField().getName() +
                " IN (" + seq(fieldAndValues.getValues()).toString(",") + ")";
    }

    private boolean shouldUseTempTable(List> fieldsWithValues) {
        return fieldsWithValues.size() > 1 || fieldsWithValues.get(0).getValues().size() > JOIN_TEMP_TABLE_LIMIT;
    }

    public Query getQuery() {
        return query;
    }

    @Override
    public void close() {
        tempTableCreator.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy