use of com.amplifyframework.core.model.ModelIndex in project amplify-android by aws-amplify.
the class SqlCommandTest method modelWithIndexReturnsExpectedCreateIndexCommand.
/**
* Test if {@link ModelSchema} with index returns an expected
* CREATE INDEX SQL command.
*/
@Test
public void modelWithIndexReturnsExpectedCreateIndexCommand() {
final ModelIndex index = ModelIndex.builder().indexName("idBasedIndex").indexFieldNames(Collections.singletonList("id")).build();
final ModelSchema modelSchema = ModelSchema.builder().name("Person").indexes(Collections.singletonMap("idBasedIndex", index)).build();
final Iterator<SqlCommand> sqlCommandIterator = sqlCommandFactory.createIndexesFor(modelSchema).iterator();
assertTrue(sqlCommandIterator.hasNext());
final SqlCommand createIndexSqlCommand = sqlCommandIterator.next();
assertEquals("Person", createIndexSqlCommand.tableName());
assertEquals("CREATE INDEX IF NOT EXISTS `idBasedIndex` ON `Person` (`id`);", createIndexSqlCommand.sqlStatement());
}
use of com.amplifyframework.core.model.ModelIndex in project amplify-android by aws-amplify.
the class PendingMutationPersistentRecordTest method modelSchemaGenerationSucceeds.
/**
* Generation of a {@link ModelSchema} for the {@link PendingMutation.PersistentRecord}
* succeeds.
* @throws AmplifyException from Amplify configuration
*/
@Test
public void modelSchemaGenerationSucceeds() throws AmplifyException {
List<ModelField> fields = Arrays.asList(ModelField.builder().name("id").targetType("ID").isRequired(true).javaClassForValue(String.class).build(), ModelField.builder().name("containedModelId").targetType("String").isRequired(true).javaClassForValue(String.class).build(), ModelField.builder().name("serializedMutationData").targetType("String").isRequired(true).javaClassForValue(String.class).build(), ModelField.builder().name("containedModelClassName").targetType("String").isRequired(true).javaClassForValue(String.class).build());
Map<String, ModelField> expectedFieldsMap = new HashMap<>();
for (ModelField field : fields) {
expectedFieldsMap.put(field.getName(), field);
}
final ModelIndex index = ModelIndex.builder().indexFieldNames(Collections.singletonList("containedModelClassName")).indexName("containedModelClassNameBasedIndex").build();
assertEquals(// Expected
ModelSchema.builder().name("PersistentRecord").pluralName("PersistentRecords").fields(expectedFieldsMap).indexes(Collections.singletonMap("containedModelClassNameBasedIndex", index)).modelClass(PendingMutation.PersistentRecord.class).build(), // Actual
ModelSchema.fromModelClass(PendingMutation.PersistentRecord.class));
}
use of com.amplifyframework.core.model.ModelIndex in project amplify-android by aws-amplify.
the class SQLiteCommandFactory method createIndexesFor.
@NonNull
@Override
public Set<SqlCommand> createIndexesFor(@NonNull ModelSchema modelSchema) {
final SQLiteTable table = SQLiteTable.fromSchema(modelSchema);
Set<SqlCommand> indexCommands = new HashSet<>();
for (ModelIndex modelIndex : modelSchema.getIndexes().values()) {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("CREATE INDEX IF NOT EXISTS").append(SqlKeyword.DELIMITER).append(Wrap.inBackticks(modelIndex.getIndexName())).append(SqlKeyword.DELIMITER).append(SqlKeyword.ON).append(SqlKeyword.DELIMITER).append(Wrap.inBackticks(table.getName())).append(SqlKeyword.DELIMITER);
stringBuilder.append("(");
Iterator<String> iterator = modelIndex.getIndexFieldNames().iterator();
while (iterator.hasNext()) {
final String indexColumnName = iterator.next();
stringBuilder.append(Wrap.inBackticks(indexColumnName));
if (iterator.hasNext()) {
stringBuilder.append(",").append(SqlKeyword.DELIMITER);
}
}
stringBuilder.append(");");
indexCommands.add(new SqlCommand(table.getName(), stringBuilder.toString()));
}
return Immutable.of(indexCommands);
}
Aggregations