Skip to content

Commit f387bc9

Browse files
authored
fix(core): query field now stores model name (#1133)
1 parent 5c193b4 commit f387bc9

File tree

8 files changed

+130
-46
lines changed

8 files changed

+130
-46
lines changed

aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelTree.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ private void recurseTree(
110110
Collection<String> parentIds
111111
) {
112112
SQLiteTable parentTable = SQLiteTable.fromSchema(modelSchema);
113+
final String parentTableName = parentTable.getName();
114+
final String parentPrimaryKeyName = parentTable.getPrimaryKey().getName();
113115
for (ModelAssociation association : modelSchema.getAssociations().values()) {
114116
switch (association.getName()) {
115117
case "HasOne":
@@ -118,7 +120,7 @@ private void recurseTree(
118120
ModelSchema childSchema = registry.getModelSchemaForModelClass(childModel);
119121
SQLiteTable childTable = SQLiteTable.fromSchema(childSchema);
120122
String childPrimaryKey = childTable.getPrimaryKey().getAliasedName();
121-
QueryField queryField = QueryField.field(parentTable.getPrimaryKeyColumnName());
123+
QueryField queryField = QueryField.field(parentTableName, parentPrimaryKeyName);
122124

123125
// Chain predicates with OR operator.
124126
QueryPredicate predicate = QueryPredicates.none();

aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteStorageAdapter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,8 @@ private <T extends Model> void writeData(
729729
final ModelSchema modelSchema =
730730
modelSchemaRegistry.getModelSchemaForModelClass(modelName);
731731
final SQLiteTable sqliteTable = SQLiteTable.fromSchema(modelSchema);
732-
final String primaryKeyName = sqliteTable.getPrimaryKeyColumnName();
733-
final QueryPredicate matchId = QueryField.field(primaryKeyName).eq(item.getId());
732+
final String primaryKeyName = sqliteTable.getPrimaryKey().getName();
733+
final QueryPredicate matchId = QueryField.field(modelName, primaryKeyName).eq(item.getId());
734734

735735
// Generate SQL command for given action
736736
final SqlCommand sqlCommand;
@@ -836,9 +836,9 @@ private boolean modelExists(Model model, QueryPredicate predicate) throws DataSt
836836
final ModelSchema schema = modelSchemaRegistry.getModelSchemaForModelClass(modelName);
837837
final SQLiteTable table = SQLiteTable.fromSchema(schema);
838838
final String tableName = table.getName();
839-
final String primaryKeyName = table.getPrimaryKeyColumnName();
839+
final String primaryKeyName = table.getPrimaryKey().getName();
840840

841-
final QueryPredicate matchId = QueryField.field(primaryKeyName).eq(model.getId());
841+
final QueryPredicate matchId = QueryField.field(tableName, primaryKeyName).eq(model.getId());
842842
final QueryPredicate condition = matchId.and(predicate);
843843
try (Cursor cursor = getQueryAllCursor(tableName, Where.matches(condition))) {
844844
return cursor.moveToFirst();
@@ -855,8 +855,8 @@ private Model query(Model model) {
855855
final String modelName = getModelName(model);
856856
final ModelSchema schema = modelSchemaRegistry.getModelSchemaForModelClass(modelName);
857857
final SQLiteTable table = SQLiteTable.fromSchema(schema);
858-
final String primaryKeyName = table.getPrimaryKeyColumnName();
859-
final QueryPredicate matchId = QueryField.field(primaryKeyName).eq(model.getId());
858+
final String primaryKeyName = table.getPrimaryKey().getName();
859+
final QueryPredicate matchId = QueryField.field(modelName, primaryKeyName).eq(model.getId());
860860

861861
Iterator<? extends Model> result = Single.<Iterator<? extends Model>>create(emitter -> {
862862
if (model instanceof SerializedModel) {

aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/adapter/SQLPredicate.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.amplifyframework.datastore.storage.sqlite.TypeConverter;
3737
import com.amplifyframework.util.GsonFactory;
3838
import com.amplifyframework.util.Immutable;
39+
import com.amplifyframework.util.Wrap;
3940

4041
import java.util.Iterator;
4142
import java.util.LinkedList;
@@ -114,7 +115,7 @@ private StringBuilder parsePredicate(QueryPredicate queryPredicate) throws DataS
114115
return new StringBuilder("1 = 0");
115116
}
116117
if (queryPredicate instanceof QueryPredicateOperation) {
117-
QueryPredicateOperation<?> qpo = (QueryPredicateOperation) queryPredicate;
118+
QueryPredicateOperation<?> qpo = (QueryPredicateOperation<?>) queryPredicate;
118119
return parsePredicateOperation(qpo);
119120
}
120121
if (queryPredicate instanceof QueryPredicateGroup) {
@@ -132,14 +133,16 @@ private StringBuilder parsePredicate(QueryPredicate queryPredicate) throws DataS
132133
// Utility method to recursively parse a given predicate operation.
133134
private StringBuilder parsePredicateOperation(QueryPredicateOperation<?> operation) throws DataStoreException {
134135
final StringBuilder builder = new StringBuilder();
135-
final String field = operation.field();
136+
final String model = Wrap.inBackticks(operation.modelName());
137+
final String field = Wrap.inBackticks(operation.field());
138+
final String column = model == null ? operation.field() : model + "." + field;
136139
final QueryOperator<?> op = operation.operator();
137140
switch (op.type()) {
138141
case BETWEEN:
139142
BetweenQueryOperator<?> betweenOp = (BetweenQueryOperator<?>) op;
140143
addBinding(betweenOp.start());
141144
addBinding(betweenOp.end());
142-
return builder.append(field)
145+
return builder.append(column)
143146
.append(SqlKeyword.DELIMITER)
144147
.append(SqlKeyword.BETWEEN)
145148
.append(SqlKeyword.DELIMITER)
@@ -152,7 +155,7 @@ private StringBuilder parsePredicateOperation(QueryPredicateOperation<?> operati
152155
ContainsQueryOperator containsOp = (ContainsQueryOperator) op;
153156
addBinding(containsOp.value());
154157
return builder.append("instr(")
155-
.append(field)
158+
.append(column)
156159
.append(",")
157160
.append("?")
158161
.append(")")
@@ -163,7 +166,7 @@ private StringBuilder parsePredicateOperation(QueryPredicateOperation<?> operati
163166
case BEGINS_WITH:
164167
BeginsWithQueryOperator beginsWithOp = (BeginsWithQueryOperator) op;
165168
addBinding(beginsWithOp.value() + "%");
166-
return builder.append(field)
169+
return builder.append(column)
167170
.append(SqlKeyword.DELIMITER)
168171
.append(SqlKeyword.LIKE)
169172
.append(SqlKeyword.DELIMITER)
@@ -175,7 +178,7 @@ private StringBuilder parsePredicateOperation(QueryPredicateOperation<?> operati
175178
case LESS_OR_EQUAL:
176179
case GREATER_OR_EQUAL:
177180
addBinding(getOperatorValue(op));
178-
return builder.append(field)
181+
return builder.append(column)
179182
.append(SqlKeyword.DELIMITER)
180183
.append(SqlKeyword.fromQueryOperator(op.type()))
181184
.append(SqlKeyword.DELIMITER)
@@ -227,13 +230,13 @@ private Object getOperatorValue(QueryOperator<?> qOp) throws DataStoreException
227230
case EQUAL:
228231
return ((EqualQueryOperator) qOp).value();
229232
case LESS_OR_EQUAL:
230-
return ((LessOrEqualQueryOperator) qOp).value();
233+
return ((LessOrEqualQueryOperator<?>) qOp).value();
231234
case LESS_THAN:
232-
return ((LessThanQueryOperator) qOp).value();
235+
return ((LessThanQueryOperator<?>) qOp).value();
233236
case GREATER_OR_EQUAL:
234-
return ((GreaterOrEqualQueryOperator) qOp).value();
237+
return ((GreaterOrEqualQueryOperator<?>) qOp).value();
235238
case GREATER_THAN:
236-
return ((GreaterThanQueryOperator) qOp).value();
239+
return ((GreaterThanQueryOperator<?>) qOp).value();
237240
default:
238241
throw new DataStoreException(
239242
"Tried to parse an unsupported QueryOperator type",

aws-datastore/src/main/java/com/amplifyframework/datastore/syncengine/VersionRepository.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
import com.amplifyframework.core.model.Model;
2121
import com.amplifyframework.core.model.query.Where;
22-
import com.amplifyframework.core.model.query.predicate.QueryField;
23-
import com.amplifyframework.core.model.query.predicate.QueryPredicate;
2422
import com.amplifyframework.datastore.DataStoreException;
2523
import com.amplifyframework.datastore.appsync.ModelMetadata;
2624
import com.amplifyframework.datastore.storage.LocalStorageAdapter;
@@ -55,10 +53,9 @@ final class VersionRepository {
5553
* @return Current version known locally
5654
*/
5755
<T extends Model> Single<Integer> findModelVersion(T model) {
58-
// The ModelMetadata for the model uses the same ID as an identifier.
59-
final QueryPredicate hasMatchingId = QueryField.field("id").eq(model.getId());
6056
return Single.create(emitter -> {
61-
localStorageAdapter.query(ModelMetadata.class, Where.matches(hasMatchingId), iterableResults -> {
57+
// The ModelMetadata for the model uses the same ID as an identifier.
58+
localStorageAdapter.query(ModelMetadata.class, Where.id(model.getId()), iterableResults -> {
6259
try {
6360
emitter.onSuccess(extractVersion(model, iterableResults));
6461
} catch (DataStoreException badVersionFailure) {

core/src/main/java/com/amplifyframework/core/model/query/QuerySortBy.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
package com.amplifyframework.core.model.query;
1717

1818
import androidx.annotation.NonNull;
19+
import androidx.annotation.Nullable;
1920
import androidx.core.util.ObjectsCompat;
2021

2122
import com.amplifyframework.core.Consumer;
2223
import com.amplifyframework.core.model.query.predicate.QueryField;
2324
import com.amplifyframework.datastore.DataStoreCategoryBehavior;
25+
import com.amplifyframework.util.Wrap;
2426

2527
import java.util.Objects;
2628

@@ -32,6 +34,7 @@
3234
* {@link QueryField#descending()} helper methods (e.g. Todo.DESCRIPTION.ascending() or Todo.DESCRIPTION.descending())
3335
*/
3436
public final class QuerySortBy {
37+
private final String modelName;
3538
private final String field;
3639
private final QuerySortOrder sortOrder;
3740

@@ -42,14 +45,36 @@ public final class QuerySortBy {
4245
* @param sortOrder order to sort by, either ASCENDING or DESCENDING.
4346
*/
4447
public QuerySortBy(@NonNull String field, @NonNull QuerySortOrder sortOrder) {
48+
this(null, field, sortOrder);
49+
}
50+
51+
/**
52+
* Constructor for {@code QuerySortBy}.
53+
*
54+
* @param modelName name of the model being sorted.
55+
* @param field name of field to sort by.
56+
* @param sortOrder order to sort by, either ASCENDING or DESCENDING.
57+
*/
58+
public QuerySortBy(@Nullable String modelName, @NonNull String field, @NonNull QuerySortOrder sortOrder) {
59+
this.modelName = modelName;
4560
this.field = Objects.requireNonNull(field);
4661
this.sortOrder = Objects.requireNonNull(sortOrder);
4762
}
4863

64+
/**
65+
* Returns the model being sorted.
66+
* @return the model being sorted.
67+
*/
68+
@Nullable
69+
public String getModelName() {
70+
return modelName;
71+
}
72+
4973
/**
5074
* Returns the field to sort by.
5175
* @return the field to sort by.
5276
*/
77+
@NonNull
5378
public String getField() {
5479
return field;
5580
}
@@ -58,6 +83,7 @@ public String getField() {
5883
* Returns the order to sort by, either ASCENDING or DESCENDING.
5984
* @return the order to sort by, either ASCENDING or DESCENDING.
6085
*/
86+
@NonNull
6187
public QuerySortOrder getSortOrder() {
6288
return sortOrder;
6389
}
@@ -73,19 +99,21 @@ public boolean equals(Object object) {
7399
}
74100

75101
QuerySortBy that = (QuerySortBy) object;
76-
return ObjectsCompat.equals(field, that.field) &&
102+
return ObjectsCompat.equals(modelName, that.modelName) &&
103+
ObjectsCompat.equals(field, that.field) &&
77104
ObjectsCompat.equals(sortOrder, that.sortOrder);
78105
}
79106

80107
@Override
81108
public int hashCode() {
82-
return ObjectsCompat.hash(field, sortOrder);
109+
return ObjectsCompat.hash(modelName, field, sortOrder);
83110
}
84111

85112
@Override
86113
public String toString() {
87114
return "QuerySortBy{" +
88-
"field='" + field + '\'' +
115+
"model=" + (modelName == null ? null : Wrap.inSingleQuotes(modelName)) +
116+
", field=" + Wrap.inSingleQuotes(field) +
89117
", sortOrder=" + sortOrder +
90118
'}';
91119
}

core/src/main/java/com/amplifyframework/core/model/query/Where.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import androidx.annotation.NonNull;
1919

20+
import com.amplifyframework.core.model.PrimaryKey;
2021
import com.amplifyframework.core.model.query.predicate.QueryField;
2122
import com.amplifyframework.core.model.query.predicate.QueryPredicate;
2223

@@ -56,7 +57,9 @@ public static QueryOptions matches(@NonNull final QueryPredicate queryPredicate)
5657
* @return options with proper predicate and pagination to match a model by its id.
5758
*/
5859
public static QueryOptions id(@NonNull final String modelId) {
59-
return matches(QueryField.field("id").eq(Objects.requireNonNull(modelId))).paginated(Page.firstResult());
60+
final QueryField idField = QueryField.field(PrimaryKey.fieldName());
61+
return matches(idField.eq(Objects.requireNonNull(modelId)))
62+
.paginated(Page.firstResult());
6063
}
6164

6265
/**

0 commit comments

Comments
 (0)