Android Open Source - Android-Lib-Database Delete






From Project

Back to project page Android-Lib-Database.

License

The source code is released under:

Apache License

If you think the Android project Android-Lib-Database listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package android.lib.database.query;
//from   ww  w .j  av a  2  s . co  m
import java.util.ArrayList;
import java.util.List;

import android.lib.database.predicate.Predicate;
import android.text.TextUtils;

/**
 * Provides methods for building SQLite <code>DELETE</code> queries.
 */
public class Delete extends QueryBuilder {
    private static final String DELETE      = "DELETE FROM %s"; //$NON-NLS-1$
    private static final String WHERE       = " WHERE %s"; //$NON-NLS-1$
    private static final String TABLE_ALIAS = "%1$s AS %2$s"; //$NON-NLS-1$
    private static final String COMMA       = ", "; //$NON-NLS-1$

    private final StringBuilder tableBuilder = new StringBuilder();
    private final List<Object>  parameters   = new ArrayList<Object>();

    private String whereClause;

    protected Delete() {
    }

    /**
     * Specifies the table containing the data that the query deletes from.
     * @param table the name of table containing the data that the query deletes from.
     * @return a {@link Select} object for further query construction.
     */
    public Delete from(final Class<?> table) {
        if (this.tableBuilder.length() > 0) {
            this.tableBuilder.append(Delete.COMMA);
        }

        this.tableBuilder.append(QueryBuilder.getTableName(table));

        return this;
    }

    /**
     * Specifies the table containing the data that the query deletes from.
     * @param table the name of table containing the data that the query deletes from.
     * @return a {@link Select} object for further query construction.
     */
    public Delete from(final String table) {
        if (this.tableBuilder.length() > 0) {
            this.tableBuilder.append(Delete.COMMA);
        }

        this.tableBuilder.append(table);

        return this;
    }

    /**
     * Specifies the table containing the data that the query deletes from.
     * @param table the name of table containing the data that the query deletes from.
     * @param alias an alias for the table specified.
     * @return a {@link Select} object for further query construction.
     */
    public Delete from(final Class<?> table, final String alias) {
        if (this.tableBuilder.length() > 0) {
            this.tableBuilder.append(Delete.COMMA);
        }

        this.tableBuilder.append(String.format(Delete.TABLE_ALIAS, QueryBuilder.getTableName(table), alias));

        return this;
    }

    /**
     * Specifies the table containing the data that the query deletes from.
     * @param table the name of table containing the data that the query deletes from.
     * @param alias an alias for the table specified.
     * @return a {@link Select} object for further query construction.
     */
    public Delete from(final String table, final String alias) {
        if (this.tableBuilder.length() > 0) {
            this.tableBuilder.append(Delete.COMMA);
        }

        this.tableBuilder.append(String.format(Delete.TABLE_ALIAS, table, alias));

        return this;
    }

    /**
     * Specifies filter conditions that determine the rows to delete.
     * @param predicate the conditions on which the rows to delete.
     * @return a {@link Select} object for further query construction.
     */
    public Delete where(final Predicate predicate) {
        this.whereClause = String.format(Delete.WHERE, predicate.toString());

        this.parameters.addAll(predicate.getParameters());

        return this;
    }

    /**
     * Builds a SQL statement and abstracts it in a {@link Query} object ready for execution.
     * @return a {@link Query} object ready for execution.
     */
    @Override
    public Query build() {
        final StringBuilder builder = new StringBuilder(String.format(Delete.DELETE, this.tableBuilder.toString()));

        if (!TextUtils.isEmpty(this.whereClause)) {
            builder.append(this.whereClause);
        }

        return new Query(builder.toString(), this.parameters);
    }
}




Java Source Code List

android.lib.database.Column.java
android.lib.database.CompositeIndex.java
android.lib.database.DatabaseOpenHelper.java
android.lib.database.Database.java
android.lib.database.DateConverter.java
android.lib.database.Index.java
android.lib.database.JSONRowMapper.java
android.lib.database.RowMapper.java
android.lib.database.Table.java
android.lib.database.TypeConverter.java
android.lib.database.UniqueCompositeIndex.java
android.lib.database.UnsupportedTypeException.java
android.lib.database.UseConverter.java
android.lib.database.predicate.ManySidedPredicate.java
android.lib.database.predicate.Predicate.java
android.lib.database.predicate.ThreeSidedPredicate.java
android.lib.database.predicate.TwoSidedPredicate.java
android.lib.database.query.Delete.java
android.lib.database.query.Insert.java
android.lib.database.query.QueryBuilder.java
android.lib.database.query.Query.java
android.lib.database.query.Select.java
android.lib.database.query.Update.java