Skip to content

Commit 817b0cd

Browse files
chore: simplify sqlcommandfactory (#947)
The SQLCommandFactory had required model instances to form SQL commands. However, these conditions may be specified by passed conditions, instead, and the model instances aren't necessary. Applies additional null-safety guaranteed in the SQLiteCommandFactory implementation.
1 parent c9ca0c9 commit 817b0cd

File tree

3 files changed

+26
-35
lines changed

3 files changed

+26
-35
lines changed

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

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

1818
import androidx.annotation.NonNull;
1919

20-
import com.amplifyframework.core.model.Model;
2120
import com.amplifyframework.core.model.ModelSchema;
2221
import com.amplifyframework.core.model.query.QueryOptions;
2322
import com.amplifyframework.core.model.query.predicate.QueryPredicate;
@@ -30,7 +29,6 @@
3029
* {@link ModelSchema} and the {@link com.amplifyframework.core.model.Model}.
3130
*/
3231
interface SQLCommandFactory {
33-
3432
/**
3533
* Generates the CREATE TABLE SQL command from the {@link ModelSchema}.
3634
* @param modelSchema the schema of a {@link com.amplifyframework.core.model.Model}
@@ -75,25 +73,19 @@ SqlCommand queryFor(@NonNull ModelSchema modelSchema,
7573
* prepared statement that can be bound later with inputs.
7674
*
7775
* @param modelSchema schema of the model
78-
* @param item the model instance
79-
* @param <T> type of the model
8076
* @return the SQL command that encapsulates the UPDATE command
8177
*/
8278
@NonNull
83-
<T extends Model> SqlCommand updateFor(@NonNull ModelSchema modelSchema,
84-
@NonNull T item,
85-
@NonNull QueryPredicate predicate) throws DataStoreException;
79+
SqlCommand updateFor(@NonNull ModelSchema modelSchema,
80+
@NonNull QueryPredicate predicate) throws DataStoreException;
8681

8782
/**
8883
* Generates the DELETE command in a raw string representation.
8984
*
9085
* @param modelSchema schema of the model
91-
* @param item the model instance
92-
* @param <T> type of the model
9386
* @return the SQL command that encapsulates the DELETE command
9487
*/
9588
@NonNull
96-
<T extends Model> SqlCommand deleteFor(@NonNull ModelSchema modelSchema,
97-
@NonNull T item,
98-
@NonNull QueryPredicate predicate) throws DataStoreException;
89+
SqlCommand deleteFor(@NonNull ModelSchema modelSchema,
90+
@NonNull QueryPredicate predicate) throws DataStoreException;
9991
}

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ public SqlCommand insertFor(@NonNull ModelSchema modelSchema) {
320320
stringBuilder.append(")");
321321
final String preparedInsertStatement = stringBuilder.toString();
322322
final SQLiteStatement compiledInsertStatement =
323-
databaseConnectionHandle.compileStatement(preparedInsertStatement);
323+
databaseConnectionHandle == null ?
324+
null : databaseConnectionHandle.compileStatement(preparedInsertStatement);
324325
return new SqlCommand(table.getName(), preparedInsertStatement, columns,
325326
Collections.emptyList(), compiledInsertStatement);
326327
}
@@ -334,9 +335,8 @@ public SqlCommand insertFor(@NonNull ModelSchema modelSchema) {
334335
@NonNull
335336
@WorkerThread
336337
@Override
337-
public <T extends Model> SqlCommand updateFor(@NonNull ModelSchema modelSchema,
338-
@NonNull T item,
339-
@NonNull QueryPredicate predicate) throws DataStoreException {
338+
public SqlCommand updateFor(@NonNull ModelSchema modelSchema,
339+
@NonNull QueryPredicate predicate) throws DataStoreException {
340340
final SQLiteTable table = SQLiteTable.fromSchema(modelSchema);
341341
final StringBuilder stringBuilder = new StringBuilder();
342342
stringBuilder.append("UPDATE")
@@ -373,7 +373,8 @@ public <T extends Model> SqlCommand updateFor(@NonNull ModelSchema modelSchema,
373373

374374
final String preparedUpdateStatement = stringBuilder.toString();
375375
final SQLiteStatement compiledUpdateStatement =
376-
databaseConnectionHandle.compileStatement(preparedUpdateStatement);
376+
databaseConnectionHandle == null ?
377+
null : databaseConnectionHandle.compileStatement(preparedUpdateStatement);
377378
return new SqlCommand(table.getName(),
378379
preparedUpdateStatement,
379380
columns,
@@ -387,24 +388,23 @@ public <T extends Model> SqlCommand updateFor(@NonNull ModelSchema modelSchema,
387388
*/
388389
@NonNull
389390
@Override
390-
public <T extends Model> SqlCommand deleteFor(@NonNull ModelSchema modelSchema,
391-
@NonNull T item,
392-
@NonNull QueryPredicate predicate) throws DataStoreException {
391+
public SqlCommand deleteFor(@NonNull ModelSchema modelSchema,
392+
@NonNull QueryPredicate predicate) throws DataStoreException {
393393
final SQLiteTable table = SQLiteTable.fromSchema(modelSchema);
394-
final StringBuilder stringBuilder = new StringBuilder();
395394
final SQLPredicate sqlPredicate = new SQLPredicate(predicate);
396-
stringBuilder.append("DELETE FROM")
397-
.append(SqlKeyword.DELIMITER)
398-
.append(Wrap.inBackticks(table.getName()))
399-
.append(SqlKeyword.DELIMITER)
400-
.append(SqlKeyword.WHERE)
401-
.append(SqlKeyword.DELIMITER)
402-
.append(sqlPredicate)
403-
.append(";");
404395

405-
final String preparedDeleteStatement = stringBuilder.toString();
396+
final String preparedDeleteStatement =
397+
"DELETE FROM" +
398+
SqlKeyword.DELIMITER +
399+
Wrap.inBackticks(table.getName()) +
400+
SqlKeyword.DELIMITER +
401+
SqlKeyword.WHERE +
402+
SqlKeyword.DELIMITER +
403+
sqlPredicate +
404+
";";
406405
final SQLiteStatement compiledDeleteStatement =
407-
databaseConnectionHandle.compileStatement(preparedDeleteStatement);
406+
databaseConnectionHandle == null ?
407+
null : databaseConnectionHandle.compileStatement(preparedDeleteStatement);
408408
return new SqlCommand(table.getName(),
409409
preparedDeleteStatement,
410410
Collections.emptyList(),

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public <T extends Model> void save(
288288
final QueryPredicate condition = !QueryPredicates.all().equals(predicate)
289289
? idCheck.and(predicate)
290290
: idCheck;
291-
sqlCommand = sqlCommandFactory.updateFor(modelSchema, item, condition);
291+
sqlCommand = sqlCommandFactory.updateFor(modelSchema, condition);
292292
if (!sqlCommand.hasCompiledSqlStatement()) {
293293
onError.accept(new DataStoreException(
294294
"Error in saving the model. No update statement " +
@@ -448,9 +448,8 @@ public <T extends Model> void delete(
448448
final QueryPredicate condition = !QueryPredicates.all().equals(predicate)
449449
? idCheck.and(predicate)
450450
: idCheck;
451-
final SqlCommand sqlCommand = sqlCommandFactory.deleteFor(modelSchema, item, condition);
452-
if (sqlCommand == null || sqlCommand.sqlStatement() == null
453-
|| !sqlCommand.hasCompiledSqlStatement()) {
451+
final SqlCommand sqlCommand = sqlCommandFactory.deleteFor(modelSchema, condition);
452+
if (sqlCommand.sqlStatement() == null || !sqlCommand.hasCompiledSqlStatement()) {
454453
onError.accept(new DataStoreException(
455454
"No delete statement found for the Model: " + modelSchema.getName(),
456455
AmplifyException.TODO_RECOVERY_SUGGESTION

0 commit comments

Comments
 (0)