doUpdateSQL method

  1. @override
FutureOr doUpdateSQL(
  1. String entityName,
  2. String table,
  3. SQL sql,
  4. Object id,
  5. Transaction transaction,
  6. DBSQLiteConnectionWrapper connection, {
  7. bool allowAutoInsert = false,
})
override

Implementation

@override
FutureOr doUpdateSQL(
  String entityName,
  String table,
  SQL sql,
  Object id,
  Transaction transaction,
  DBSQLiteConnectionWrapper connection, {
  bool allowAutoInsert = false,
}) {
  if (sql.isDummy) return id;

  var results = _select(connection, sql);

  var affectedRows = connection.updatedRows;

  if (affectedRows == 0 && results.isEmpty) {
    var entry = sql.parametersByPlaceholder;
    if (!allowAutoInsert) {
      throw StateError(
        "Can't update not stored entity into table `$table`: $entry",
      );
    }

    var fields = sql.namedParameters!;

    return generateInsertSQL(
      transaction,
      entityName,
      table,
      fields,
    ).resolveMapped((insertSQL) {
      _log.info('Update not affecting any row! Auto inserting: $insertSQL');
      return doInsertSQL(
        entityName,
        table,
        insertSQL,
        transaction,
        connection,
      );
    });
  }

  return _resolveResultID(connection, results, sql, id);
}