@Test public void buildTrueUpdateQuery_complexSelection() { String selection = "_id = ? AND (foo = ? OR bar != ?)"; String[] args = new String[]{"1", "2", "3"}; ContentValues values = new ContentValues(); values.put("a", 4); SqlUtil.Query updateQuery = SqlUtil.buildTrueUpdateQuery(selection, args, values); assertEquals("(_id = ? AND (foo = ? OR bar != ?)) AND (a != ? OR a IS NULL)", updateQuery.getWhere()); assertArrayEquals(new String[] { "1", "2", "3", "4" }, updateQuery.getWhereArgs()); }
public static @NonNull Query buildTrueUpdateQuery(@NonNull String selection, @NonNull String[] args, @NonNull ContentValues contentValues) { StringBuilder qualifier = new StringBuilder(); Set<Map.Entry<String, Object>> valueSet = contentValues.valueSet(); List<String> fullArgs = new ArrayList<>(args.length + valueSet.size()); fullArgs.addAll(Arrays.asList(args)); int i = 0; 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())); } else { qualifier.append(entry.getKey()).append(" NOT NULL"); } if (i != valueSet.size() - 1) { qualifier.append(" OR "); } i++; } return new Query("(" + selection + ") AND (" + qualifier + ")", fullArgs.toArray(new String[0])); }
SqlUtil { public static @NonNull Query buildTrueUpdateQuery(@NonNull String selection, @NonNull String[] args, @NonNull ContentValues contentValues) { StringBuilder qualifier = new StringBuilder(); Set<Map.Entry<String, Object>> valueSet = contentValues.valueSet(); List<String> fullArgs = new ArrayList<>(args.length + valueSet.size()); fullArgs.addAll(Arrays.asList(args)); int i = 0; 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())); } else { qualifier.append(entry.getKey()).append(" NOT NULL"); } if (i != valueSet.size() - 1) { qualifier.append(" OR "); } i++; } return new Query("(" + selection + ") AND (" + qualifier + ")", fullArgs.toArray(new String[0])); } }
SqlUtil { public static @NonNull Query buildTrueUpdateQuery(@NonNull String selection, @NonNull String[] args, @NonNull ContentValues contentValues) { StringBuilder qualifier = new StringBuilder(); Set<Map.Entry<String, Object>> valueSet = contentValues.valueSet(); List<String> fullArgs = new ArrayList<>(args.length + valueSet.size()); fullArgs.addAll(Arrays.asList(args)); int i = 0; 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())); } else { qualifier.append(entry.getKey()).append(" NOT NULL"); } if (i != valueSet.size() - 1) { qualifier.append(" OR "); } i++; } return new Query("(" + selection + ") AND (" + qualifier + ")", fullArgs.toArray(new String[0])); } private  SqlUtil(); }
SqlUtil { public static @NonNull Query buildTrueUpdateQuery(@NonNull String selection, @NonNull String[] args, @NonNull ContentValues contentValues) { StringBuilder qualifier = new StringBuilder(); Set<Map.Entry<String, Object>> valueSet = contentValues.valueSet(); List<String> fullArgs = new ArrayList<>(args.length + valueSet.size()); fullArgs.addAll(Arrays.asList(args)); int i = 0; 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())); } else { qualifier.append(entry.getKey()).append(" NOT NULL"); } if (i != valueSet.size() - 1) { qualifier.append(" OR "); } i++; } return new Query("(" + selection + ") AND (" + qualifier + ")", fullArgs.toArray(new String[0])); } private  SqlUtil(); static boolean tableExists(@NonNull SQLiteDatabase db, @NonNull String table); static boolean columnExists(@NonNull SQLiteDatabase db, @NonNull String table, @NonNull String column); static String[] buildArgs(Object... objects); static @NonNull Query buildTrueUpdateQuery(@NonNull String selection,
                                                    @NonNull String[] args,
                                                    @NonNull ContentValues contentValues); static @NonNull Query buildCollectionQuery(@NonNull String column, @NonNull Collection<? extends Object> values); static String[] appendArg(@NonNull String[] args, String addition); }
SqlUtil { public static @NonNull Query buildTrueUpdateQuery(@NonNull String selection, @NonNull String[] args, @NonNull ContentValues contentValues) { StringBuilder qualifier = new StringBuilder(); Set<Map.Entry<String, Object>> valueSet = contentValues.valueSet(); List<String> fullArgs = new ArrayList<>(args.length + valueSet.size()); fullArgs.addAll(Arrays.asList(args)); int i = 0; 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())); } else { qualifier.append(entry.getKey()).append(" NOT NULL"); } if (i != valueSet.size() - 1) { qualifier.append(" OR "); } i++; } return new Query("(" + selection + ") AND (" + qualifier + ")", fullArgs.toArray(new String[0])); } private  SqlUtil(); static boolean tableExists(@NonNull SQLiteDatabase db, @NonNull String table); static boolean columnExists(@NonNull SQLiteDatabase db, @NonNull String table, @NonNull String column); static String[] buildArgs(Object... objects); static @NonNull Query buildTrueUpdateQuery(@NonNull String selection,
                                                    @NonNull String[] args,
                                                    @NonNull ContentValues contentValues); static @NonNull Query buildCollectionQuery(@NonNull String column, @NonNull Collection<? extends Object> values); static String[] appendArg(@NonNull String[] args, String addition);  }