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; /* www . ja v a 2s.c o 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>UPDATE</code> queries. */ public class Update extends QueryBuilder { private static final String UPDATE = "UPDATE %1$s SET %2$s"; //$NON-NLS-1$ private static final String SET = "%1$s = %2$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 PARAMETER = "?"; //$NON-NLS-1$ private static final String COMMA = ", "; //$NON-NLS-1$ private final StringBuilder tableBuilder = new StringBuilder(); private final StringBuilder setBuilder = new StringBuilder(); private final List<Object> parameters = new ArrayList<Object>(); private String whereClause; protected Update() { } /** * Specifies the table that the query updates to. * @param table the name of table that the query updates into. * @return a {@link Select} object for further query construction. */ public Update table(final Class<?> table) { if (this.tableBuilder.length() > 0) { this.tableBuilder.append(Update.COMMA); } this.tableBuilder.append(QueryBuilder.getTableName(table)); return this; } /** * Specifies the table that the query updates to. * @param table the name of table that the query updates into. * @return a {@link Select} object for further query construction. */ public Update table(final String table) { if (this.tableBuilder.length() > 0) { this.tableBuilder.append(Update.COMMA); } this.tableBuilder.append(table); return this; } /** * Specifies the table that the query updates to. * @param table the name of table that the query updates into. * @param alias an alias for the table specified. * @return a {@link Select} object for further query construction. */ public Update table(final Class<?> table, final String alias) { if (this.tableBuilder.length() > 0) { this.tableBuilder.append(Update.COMMA); } this.tableBuilder.append(String.format(Update.TABLE_ALIAS, QueryBuilder.getTableName(table), alias)); return this; } /** * Specifies the table that the query updates to. * @param table the name of table that the query updates into. * @param alias an alias for the table specified. * @return a {@link Select} object for further query construction. */ public Update table(final String table, final String alias) { if (this.tableBuilder.length() > 0) { this.tableBuilder.append(Update.COMMA); } this.tableBuilder.append(String.format(Update.TABLE_ALIAS, table, alias)); return this; } /** * Sets the column with the given <code>value</code>. * @param column the column to update. * @param value the value to be updated to the given <code>column</code>. * @return a {@link Select} object for further query construction. */ public Update set(final String column, final Object value) { return this.set(column, value, false); } public Update set(final String column, final Object value, final boolean isIdentifier) { if (this.setBuilder.length() > 0) { this.setBuilder.append(Update.COMMA); } if (isIdentifier) { this.setBuilder.append(String.format(Update.SET, column, value.toString())); } else { this.setBuilder.append(String.format(Update.SET, column, Update.PARAMETER)); this.parameters.add(value); } return this; } /** * Specifies filter conditions that determine the rows to update. * @param predicate the conditions on which to rows to update. * @return a {@link Select} object for further query construction. */ public Update where(final Predicate predicate) { this.whereClause = String.format(Update.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(Update.UPDATE, this.tableBuilder.toString(), this.setBuilder.toString())); if (!TextUtils.isEmpty(this.whereClause)) { builder.append(this.whereClause); } return new Query(builder.toString(), this.parameters); } }