Search in sources :

Example 1 with ModelIndex

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());
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelIndex(com.amplifyframework.core.model.ModelIndex) Test(org.junit.Test)

Example 2 with ModelIndex

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));
}
Also used : ModelField(com.amplifyframework.core.model.ModelField) HashMap(java.util.HashMap) ModelIndex(com.amplifyframework.core.model.ModelIndex) PendingMutation(com.amplifyframework.datastore.syncengine.PendingMutation) Test(org.junit.Test)

Example 3 with ModelIndex

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);
}
Also used : ModelIndex(com.amplifyframework.core.model.ModelIndex) SQLiteTable(com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable) HashSet(java.util.HashSet) NonNull(androidx.annotation.NonNull)

Aggregations

ModelIndex (com.amplifyframework.core.model.ModelIndex)3 Test (org.junit.Test)2 NonNull (androidx.annotation.NonNull)1 ModelField (com.amplifyframework.core.model.ModelField)1 ModelSchema (com.amplifyframework.core.model.ModelSchema)1 SQLiteTable (com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable)1 PendingMutation (com.amplifyframework.datastore.syncengine.PendingMutation)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1