Carry over bugfixes from MediaProvider.

A few months ago MediaProvider forked SQLiteQueryBuilder and we
fixed bugs in how the new insert(), update(), and delete() methods
were executed, but those fixes never made their way back upstream.

This CL is a clean cherry-pick of existing logic.

Bug: 155149941
Test: atest android.database.sqlite.cts.SQLiteQueryBuilderTest
Change-Id: I00d2ee88539b12d27a2ed99fff0546d1e3543b0e
This commit is contained in:
Jeff Sharkey
2020-05-19 09:37:11 -06:00
parent f1e9d52fd8
commit 0fd3e39760
2 changed files with 55 additions and 3 deletions

View File

@@ -191,6 +191,58 @@ public class DatabaseUtils {
}
}
/** {@hide} */
public static long executeInsert(@NonNull SQLiteDatabase db, @NonNull String sql,
@Nullable Object[] bindArgs) throws SQLException {
try (SQLiteStatement st = db.compileStatement(sql)) {
bindArgs(st, bindArgs);
return st.executeInsert();
}
}
/** {@hide} */
public static int executeUpdateDelete(@NonNull SQLiteDatabase db, @NonNull String sql,
@Nullable Object[] bindArgs) throws SQLException {
try (SQLiteStatement st = db.compileStatement(sql)) {
bindArgs(st, bindArgs);
return st.executeUpdateDelete();
}
}
/** {@hide} */
private static void bindArgs(@NonNull SQLiteStatement st, @Nullable Object[] bindArgs) {
if (bindArgs == null) return;
for (int i = 0; i < bindArgs.length; i++) {
final Object bindArg = bindArgs[i];
switch (getTypeOfObject(bindArg)) {
case Cursor.FIELD_TYPE_NULL:
st.bindNull(i + 1);
break;
case Cursor.FIELD_TYPE_INTEGER:
st.bindLong(i + 1, ((Number) bindArg).longValue());
break;
case Cursor.FIELD_TYPE_FLOAT:
st.bindDouble(i + 1, ((Number) bindArg).doubleValue());
break;
case Cursor.FIELD_TYPE_BLOB:
st.bindBlob(i + 1, (byte[]) bindArg);
break;
case Cursor.FIELD_TYPE_STRING:
default:
if (bindArg instanceof Boolean) {
// Provide compatibility with legacy
// applications which may pass Boolean values in
// bind args.
st.bindLong(i + 1, ((Boolean) bindArg).booleanValue() ? 1 : 0);
} else {
st.bindString(i + 1, bindArg.toString());
}
break;
}
}
}
/**
* Binds the given Object to the given SQLiteProgram using the proper
* typing. For example, bind numbers as longs/doubles, and everything else

View File

@@ -626,7 +626,7 @@ public class SQLiteQueryBuilder {
Log.d(TAG, sql);
}
}
return db.executeSql(sql, sqlArgs);
return DatabaseUtils.executeInsert(db, sql, sqlArgs);
}
/**
@@ -702,7 +702,7 @@ public class SQLiteQueryBuilder {
Log.d(TAG, sql);
}
}
return db.executeSql(sql, sqlArgs);
return DatabaseUtils.executeUpdateDelete(db, sql, sqlArgs);
}
/**
@@ -762,7 +762,7 @@ public class SQLiteQueryBuilder {
Log.d(TAG, sql);
}
}
return db.executeSql(sql, sqlArgs);
return DatabaseUtils.executeUpdateDelete(db, sql, sqlArgs);
}
private void enforceStrictColumns(@Nullable String[] projection) {