uniqueKeyFor method

MssqlUniqueKeySchema? uniqueKeyFor(
  1. List<String> columns, {
  2. bool requireUnconditional = true,
})

The unique key covering exactly columns, or null.

With requireUnconditional false, a filtered or disabled index is returned too, so a caller can explain why it was not usable.

Implementation

MssqlUniqueKeySchema? uniqueKeyFor(
  List<String> columns, {
  bool requireUnconditional = true,
}) {
  final wanted = columns.map((c) => c.toLowerCase()).toList()..sort();
  for (final key in uniqueKeys) {
    if (requireUnconditional && !key.guaranteesUniqueness) continue;
    final have = key.columns.map((c) => c.toLowerCase()).toList()..sort();
    if (have.length != wanted.length) continue;
    var same = true;
    for (var i = 0; i < have.length; i++) {
      if (have[i] != wanted[i]) same = false;
    }
    if (same) return key;
  }
  return null;
}