Skip to content

Commit

Permalink
insertManyReturning in transaction and fix didGenerate for Insert
Browse files Browse the repository at this point in the history
  • Loading branch information
juancastillo0 committed Dec 8, 2023
1 parent 854e18e commit 4144ec7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,20 @@ class SqlTypedController<T extends SqlReturnModel,
Future<SqlExecution> insertMany(List<SqlInsertModel<T>> models) =>
SqlExec.insertMany(type, models).run(executor.executor);

Future<List<T>> insertManyReturning(List<SqlInsertModel<T>> models) =>
SqlExec.insertMany(type, models)
Future<List<T>> insertManyReturning(List<SqlInsertModel<T>> models) async {
if (models.isEmpty) {
return [];
} else if (models.length == 1) {
return SqlExec.insertMany(type, models, useDefault: false)
.addReturning(type)
.run(executor.executor);
} else {
final result = await executor.executor.transaction(() {
return Future.wait(models.map(insertReturning));
});
return result!;
}
}

Future<T?> updateReturning(SqlUniqueKeyModel<T, U> key, U model) =>
SqlExec.update(key, model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class SqliteExecutor extends SqlExecutor {
Future<T?> transaction<T>(Future<T> Function() transact) async {
bool started = false;
try {
db.execute('START TRANSACTION');
db.execute('BEGIN TRANSACTION');
started = true;
final result = await transact();
db.execute('COMMIT');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class GenModel {
tableName: tableName ?? this.tableName,
);
}

@override
String toString() {
return 'GenModel(useOptional: $useOptional, interfaces: $interfaces, allNullable: $allNullable, tableName: $tableName)';
}
}

void addModelClass(
Expand Down Expand Up @@ -225,8 +230,7 @@ String? addModelClassInsert(
GenModel model = const GenModel(),
}) {
final name = '${className}Insert';
final didGenerate =
t.fields.any((e) => e.defaultValue != null && !e.nullable);
final didGenerate = t.fields.any((e) => e.defaultValue != null || e.optional);
if (didGenerate) {
addModelClass(
buf,
Expand Down

0 comments on commit 4144ec7

Please sign in to comment.