Merge "Make TranslatingCursor deal with dupe column names"

This commit is contained in:
Anton Hansson
2019-03-28 10:24:59 +00:00
committed by Android (Google) Code Review
2 changed files with 56 additions and 4 deletions

View File

@@ -22,6 +22,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.CancellationSignal;
import android.util.ArraySet;
import com.android.internal.util.ArrayUtils;
@@ -59,7 +60,7 @@ public class TranslatingCursor extends CrossProcessCursorWrapper {
private final boolean mDropLast;
private final int mAuxiliaryColumnIndex;
private final int[] mTranslateColumnIndices;
private final ArraySet<Integer> mTranslateColumnIndices;
public TranslatingCursor(@NonNull Cursor cursor, @NonNull Config config,
@NonNull Translator translator, boolean dropLast) {
@@ -70,9 +71,12 @@ public class TranslatingCursor extends CrossProcessCursorWrapper {
mDropLast = dropLast;
mAuxiliaryColumnIndex = cursor.getColumnIndexOrThrow(config.auxiliaryColumn);
mTranslateColumnIndices = new int[config.translateColumns.length];
for (int i = 0; i < mTranslateColumnIndices.length; ++i) {
mTranslateColumnIndices[i] = cursor.getColumnIndex(config.translateColumns[i]);
mTranslateColumnIndices = new ArraySet<>();
for (int i = 0; i < cursor.getColumnCount(); ++i) {
String columnName = cursor.getColumnName(i);
if (ArrayUtils.contains(config.translateColumns, columnName)) {
mTranslateColumnIndices.add(i);
}
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.database;
import static com.google.common.truth.Truth.assertThat;
import android.database.TranslatingCursor.Translator;
import android.net.Uri;
import junit.framework.TestCase;
public class TranslatingCursorTest extends TestCase {
public void testDuplicateColumnName() {
MatrixCursor base = new MatrixCursor(new String[] {"_id", "colA", "colB", "colA"});
base.addRow(new Object[] { 0, "r1_a", "r1_b", "r1_a"});
base.addRow(new Object[] { 1, "r2_a", "r2_b", "r2_a"});
Translator translator = (data, idIndex, matchingColumn, cursor) -> data.toUpperCase();
TranslatingCursor.Config config = new TranslatingCursor.Config(Uri.EMPTY, "_id", "colA");
TranslatingCursor translating = new TranslatingCursor(base, config, translator, false);
translating.moveToNext();
String[] expected = new String[] { "ignored", "R1_A", "r1_b", "R1_A" };
for (int i = 1; i < translating.getColumnCount(); i++) {
assertThat(translating.getString(i)).isEqualTo(expected[i]);
}
translating.moveToNext();
expected = new String[] { "ignored", "R2_A", "r2_b", "R2_A" };
for (int i = 1; i < translating.getColumnCount(); i++) {
assertThat(translating.getString(i)).isEqualTo(expected[i]);
}
}
}