Back to project page Android-Lib-Database.
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.
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); } }