primaryKeyByUniqueColumns method Null safety

  1. @override
Future<int?> primaryKeyByUniqueColumns(
  1. Run instance,
  2. DatabaseExecutor executor
)
override

Find a record based on the existence of all contained fields annotated with @Sqlite(unique: true). The Brick-defined primary key of the table is not included in the query. Returns the Brick-defined primary key of the discovered record.

executor accepts a Database or Transaction.

Implementation

@override
Future<int?> primaryKeyByUniqueColumns(
    Run instance, DatabaseExecutor executor) async {
  final results = await executor.rawQuery('''
      SELECT * FROM `Run` WHERE id = ? LIMIT 1''', [instance.id]);

  // SQFlite returns [{}] when no results are found
  if (results.isEmpty || (results.length == 1 && results.first.isEmpty)) {
    return null;
  }

  return results.first['_brick_id'] as int;
}