Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 55777e52 authored by Jeff Sharkey's avatar Jeff Sharkey Committed by Jeff Sharkey
Browse files

Add support for appending standalone phrases.

When users are building queries, they often need to append several
standalone SQL clauses, and it's tedious to track their first clause
so they can manually append " AND " to each subsequent clause.

So add new appendWherePhrase() API which appends a standalone phrase
which is AND'ed together with any existing WHERE query.

Also fix bug in update() which would turn null values into the
string literal "null" instead of passing them through as SQL NULL.

Test: atest cts/tests/tests/database/src/android/database/sqlite/cts/SQLiteQueryBuilderTest.java
Bug: 111085900
Merged-In: Ia280dd864895654239503e080eaef925f5620d37
Change-Id: Ia280dd864895654239503e080eaef925f5620d37
parent 0ae655fd
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -12671,6 +12671,8 @@ package android.database.sqlite {
    method public void appendWhere(java.lang.CharSequence, java.lang.String...);
    method public void appendWhereEscapeString(java.lang.String);
    method public void appendWhereEscapeString(java.lang.String, java.lang.String...);
    method public void appendWhereExpression(java.lang.CharSequence);
    method public void appendWhereExpression(java.lang.CharSequence, java.lang.String...);
    method public java.lang.String buildQuery(java.lang.String[], java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String);
    method public deprecated java.lang.String buildQuery(java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String, java.lang.String);
    method public static java.lang.String buildQueryString(boolean, java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String);
+41 −1
Original line number Diff line number Diff line
@@ -156,6 +156,45 @@ public class SQLiteQueryBuilder {
        mWhereArgs = ArrayUtils.concat(String.class, mWhereArgs, inWhereArgs);
    }

    /**
     * Append a standalone expression to the {@code WHERE} clause of this query.
     * <p>
     * This method differs from {@link #appendWhere(CharSequence)} in that it
     * automatically appends {@code AND} to any existing {@code WHERE} clause
     * already under construction before appending the given standalone
     * expression.
     *
     * @param inWhere the standalone expression to append to the {@code WHERE}
     *            clause. It will be wrapped in parentheses when it's appended.
     */
    public void appendWhereExpression(@NonNull CharSequence inWhere) {
        appendWhereExpression(inWhere, EmptyArray.STRING);
    }

    /**
     * Append a standalone expression to the {@code WHERE} clause of this query.
     * <p>
     * This method differs from {@link #appendWhere(CharSequence)} in that it
     * automatically appends {@code AND} to any existing {@code WHERE} clause
     * already under construction before appending the given standalone
     * expression.
     *
     * @param inWhere the standalone expression to append to the {@code WHERE}
     *            clause. It will be wrapped in parentheses when it's appended.
     * @param inWhereArgs list of arguments to be bound to any '?' occurrences
     *            in the standalone expression.
     */
    public void appendWhereExpression(@NonNull CharSequence inWhere, String... inWhereArgs) {
        if (mWhereClause == null) {
            mWhereClause = new StringBuilder(inWhere.length() + 16);
        }
        if (mWhereClause.length() > 0) {
            mWhereClause.append(" AND ");
        }
        mWhereClause.append('(').append(inWhere).append(')');
        mWhereArgs = ArrayUtils.concat(String.class, mWhereArgs, inWhereArgs);
    }

    /**
     * Append a chunk to the {@code WHERE} clause of the query. All chunks
     * appended are surrounded by parenthesis and {@code AND}ed with the
@@ -615,7 +654,8 @@ public class SQLiteQueryBuilder {
        final ArrayMap<String, Object> rawValues = values.getValues();
        final String[] updateArgs = new String[rawValues.size()];
        for (int i = 0; i < updateArgs.length; i++) {
            updateArgs[i] = String.valueOf(rawValues.valueAt(i));
            final Object arg = rawValues.valueAt(i);
            updateArgs[i] = (arg != null) ? arg.toString() : null;
        }

        final String sql = buildUpdate(values, selection);