Android Open Source - Android-Lib-Database Insert






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  w  ww .  j  a va 2 s  .  co m
import java.util.ArrayList;
import java.util.List;

/**
 * Provides methods for building SQLite <code>INSERT</code> queries.
 */
public class Insert extends QueryBuilder {
    private static final String INSERT    = "INSERT INTO %1$s (%2$s) VALUES (%3$s)"; //$NON-NLS-1$
    private static final String PARAMETER = "?";                                     //$NON-NLS-1$
    private static final String COMMA     = ", ";                                    //$NON-NLS-1$

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

    protected Insert() {
    }

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

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

        return this;
    }

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

        this.tableBuilder.append(table);

        return this;
    }

    /**
     * Sets the column with the given <code>value</code>.
     * @param column the column to set.
     * @param value the value to be set to the <code>column</code>.
     * @return a {@link Select} object for further query construction.
     * 
     */
    public Insert set(final String column, final Object value) {
        if (this.columnBuilder.length() > 0) {
            this.columnBuilder.append(Insert.COMMA);
            this.parameterBuilder.append(Insert.COMMA);
        }

        this.columnBuilder.append(column);
        this.parameterBuilder.append(Insert.PARAMETER);

        this.parameters.add(value);

        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(Insert.INSERT, this.tableBuilder.toString(), this.columnBuilder.toString(), this.parameterBuilder.toString()));

        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