applyPagination function
Helper to apply sorting and pagination to a SQL query at runtime.
Implementation
String applyPagination(
String sql, {
List<Sort>? sort,
int? limit,
int? offset,
Map<String, String>? fieldToColumn,
}) {
var result = sql;
if (sort != null && sort.isNotEmpty) {
final orderClauses = sort.map((s) {
final column = fieldToColumn?[s.field] ?? s.field;
return '$column ${s.direction == Direction.asc ? 'ASC' : 'DESC'}';
}).join(', ');
result += ' ORDER BY $orderClauses';
}
if (limit != null) {
result += ' LIMIT $limit';
}
if (offset != null) {
result += ' OFFSET $offset';
}
return result;
}