relationPredicate function

MssqlCondition relationPredicate(
  1. List<String> foreignColumns,
  2. List<MssqlRelationKey> keys
)

Builds the predicate that fetches children for one batch of parent keys.

A single column becomes IN (…). A composite key cannot, so it becomes OR-ed equality groups — (A = @a0 AND B = @b0) OR … — which is the same query shape and the same number of round trips.

Implementation

MssqlCondition relationPredicate(
  List<String> foreignColumns,
  List<MssqlRelationKey> keys,
) {
  if (keys.isEmpty) {
    throw ArgumentError.value(
      keys,
      'keys',
      'Cannot build a predicate for none.',
    );
  }
  if (foreignColumns.length == 1) {
    return Col(
      foreignColumns.single,
    ).inList(keys.map((k) => k.values.single).toList());
  }
  return or(<MssqlCondition>[
    for (final key in keys)
      and(<MssqlCondition>[
        for (var i = 0; i < foreignColumns.length; i++)
          Col(foreignColumns[i]).eq(key.values[i]),
      ]),
  ]);
}