search method

S search(
  1. String? term,
  2. List<MssqlTextExpression> columns(
    1. TFields fields
    ), {
  3. MssqlLikePosition position = MssqlLikePosition.anywhere,
})

Adds a text search over columns when term has a non-whitespace character.

Null, empty, and whitespace-only terms add no filter and do not call columns. A term that has content is used as typed — it is not trimmed — so a leading space the user entered is a leading space in SQL. %, _ and [ in the term are escaped by MssqlLike; they never become wildcards.

columns must return MssqlTextExpressions. A numeric column is not cast to text here: that would hide total.contains the typed columns exist to forbid. Related to-one columns belong on TFields once relation joins are generated; this method does not invent aliases.

Implementation

S search(
  String? term,
  List<MssqlTextExpression> Function(TFields fields) columns, {
  MssqlLikePosition position = MssqlLikePosition.anywhere,
}) {
  if (term == null || term.trim().isEmpty) return _self;
  final exprs = columns(fields);
  if (exprs.isEmpty) {
    throw ArgumentError.value(
      exprs,
      'columns',
      'search needs at least one text column.',
    );
  }
  MssqlCondition match(MssqlTextExpression expression) => switch (position) {
    MssqlLikePosition.anywhere => expression.contains(term),
    MssqlLikePosition.starting => expression.startsWith(term),
    MssqlLikePosition.ending => expression.endsWith(term),
    MssqlLikePosition.exact => expression.like(term),
  };
  if (exprs.length == 1) return where((_) => match(exprs.first));
  return where((_) => or(exprs.map(match)));
}