exists method
Future<bool>
exists(
- MssqlSession session, {
- MssqlDialect dialect = MssqlDialect.sql2012,
- Duration? timeout,
Whether the query matches anything.
SELECT TOP (1) 1 rather than a count: the server can stop at the first
match instead of counting every one of them. The 1 is bound as a
parameter rather than written into the SQL so that the statement stays
one the builder wrote from end to end, and therefore one whose lost
connection the driver may retry.
An unpaged query loses its ordering here: sorting rows that are not read
costs the server work and changes no answer. A paged one keeps it, since
"does a hundred-and-first row exist" is a question about the ordering.
Grouping is left alone either way: one row out of GROUP BY still means
the query matched something.
Implementation
Future<bool> exists(
MssqlSession session, {
MssqlDialect dialect = MssqlDialect.sql2012,
Duration? timeout,
}) async {
final probe = select(<MssqlExpression>[const MssqlProbeLiteral()]);
final start = offset;
final limited = start == null
? probe.orderBy(const <MssqlOrder>[]).top(1)
: probe.paged(offset: start, rows: 1);
final rows = await limited.get(session, dialect: dialect, timeout: timeout);
return rows.isNotEmpty;
}