extract static method
Extracts named parameters and returns SQL with positional placeholders.
paramNames preserves placeholder occurrence order, including repeats.
Implementation
static ({String cleanedSql, List<String> paramNames}) extract(String sql) {
final paramNames = <String>[];
final cleanedSql = StringBuffer();
var index = 0;
while (index < sql.length) {
final char = sql[index];
if (char == "'" || char == '"' || char == '[' || char == '`') {
final end = _consumeQuotedSegment(sql, start: index, delimiter: char);
cleanedSql.write(sql.substring(index, end));
index = end;
continue;
}
final dollarQuotedEnd = _consumeDollarQuotedSegment(sql, start: index);
if (dollarQuotedEnd != null) {
cleanedSql.write(sql.substring(index, dollarQuotedEnd));
index = dollarQuotedEnd;
continue;
}
if (char == '-' && _peek(sql, index + 1) == '-') {
final end = _consumeLineComment(sql, start: index);
cleanedSql.write(sql.substring(index, end));
index = end;
continue;
}
if (char == '/' && _peek(sql, index + 1) == '*') {
final end = _consumeBlockComment(sql, start: index);
cleanedSql.write(sql.substring(index, end));
index = end;
continue;
}
final placeholderName = _tryReadPlaceholder(sql, index);
if (placeholderName != null) {
paramNames.add(placeholderName.name);
cleanedSql.write('?');
index = placeholderName.end;
continue;
}
cleanedSql.write(char);
index++;
}
return (cleanedSql: cleanedSql.toString(), paramNames: paramNames);
}