extract static method

({String cleanedSql, List<String> paramNames}) extract(
  1. String sql
)

Extracts named parameters and returns SQL with positional placeholders.

paramNames is the ordered list of unique parameter names found.

Implementation

static ({String cleanedSql, List<String> paramNames}) extract(String sql) {
  final matches = _namedParamPattern.allMatches(sql);
  final seen = <String>{};
  final paramNames = <String>[];

  for (final match in matches) {
    final name = match.group(1)!;
    if (!seen.contains(name)) {
      seen.add(name);
      paramNames.add(name);
    }
  }

  final cleanedSql = sql.replaceAllMapped(_namedParamPattern, (_) => '?');

  return (cleanedSql: cleanedSql, paramNames: paramNames);
}