Fix true update queries for blobs.

fork-5.53.8
Greyson Parrelli 2022-03-16 17:33:47 -04:00 zatwierdzone przez Cody Henthorne
rodzic 0ca438ed25
commit 0359f27cd9
2 zmienionych plików z 34 dodań i 2 usunięć

Wyświetl plik

@ -18,6 +18,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@ -122,8 +123,14 @@ public final class SqlUtil {
for (Map.Entry<String, Object> entry : valueSet) {
if (entry.getValue() != null) {
qualifier.append(entry.getKey()).append(" != ? OR ").append(entry.getKey()).append(" IS NULL");
fullArgs.add(String.valueOf(entry.getValue()));
if (entry.getValue() instanceof byte[]) {
byte[] data = (byte[]) entry.getValue();
qualifier.append("hex(").append(entry.getKey()).append(") != ? OR ").append(entry.getKey()).append(" IS NULL");
fullArgs.add(Hex.toStringCondensed(data).toUpperCase(Locale.US));
} else {
qualifier.append(entry.getKey()).append(" != ? OR ").append(entry.getKey()).append(" IS NULL");
fullArgs.add(String.valueOf(entry.getValue()));
}
} else {
qualifier.append(entry.getKey()).append(" NOT NULL");
}

Wyświetl plik

@ -9,6 +9,7 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.thoughtcrime.securesms.recipients.RecipientId;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -97,6 +98,22 @@ public final class SqlUtilTest {
assertArrayEquals(new String[] { "1", "2", "3" }, updateQuery.getWhereArgs());
}
@Test
public void buildTrueUpdateQuery_blobComplex() {
String selection = "_id = ?";
String[] args = new String[]{"1"};
ContentValues values = new ContentValues();
values.put("a", hexToBytes("FF"));
values.put("b", 2);
values.putNull("c");
SqlUtil.Query updateQuery = SqlUtil.buildTrueUpdateQuery(selection, args, values);
assertEquals("(_id = ?) AND (hex(a) != ? OR a IS NULL OR b != ? OR b IS NULL OR c NOT NULL)", updateQuery.getWhere());
assertArrayEquals(new String[] { "1", "FF", "2" }, updateQuery.getWhereArgs());
}
@Test
public void buildCollectionQuery_single() {
SqlUtil.Query updateQuery = SqlUtil.buildCollectionQuery("a", Arrays.asList(1));
@ -255,4 +272,12 @@ public final class SqlUtilTest {
assertEquals("INSERT INTO mytable (a, b) VALUES (?, ?)", output.get(1).getWhere());
assertArrayEquals(new String[] { "5", "6" }, output.get(1).getWhereArgs());
}
private static byte[] hexToBytes(String hex) {
try {
return Hex.fromStringCondensed(hex);
} catch (IOException e) {
throw new AssertionError(e);
}
}
}