visitMethodCall method
Implementation
@override
SqlFragment visitMethodCall(MethodCallExpr e) {
if (e.target is! MemberAccessExpr) {
throw StateError(
'SqlTranslator: method calls are only supported on columns '
'of the table alias in Fase 2.1. Got ${e.target.runtimeType}.',
);
}
final member = e.target as MemberAccessExpr;
if (member.target is! ParamExpr ||
(member.target as ParamExpr).name != tableAlias) {
throw StateError(
'SqlTranslator: method call on a non-alias column is not '
'supported in Fase 2.1.',
);
}
final col = member.name;
if (e.method == 'startsWith' && e.args.length == 1) {
return _startsWithFragment(col, e.args[0]);
}
if (e.method == 'endsWith' && e.args.length == 1) {
return _endsWithFragment(col, e.args[0]);
}
if (e.method == 'contains' && e.args.length == 1) {
// `<fn>(col, x) > 0` is case-sensitive, matching the
// in-memory `String.contains` semantics. (SQLite's `LIKE` is
// case-insensitive for ASCII by default, which would diverge
// from the d_rocket in-memory behavior.) The function name
// comes from the dialect: SQLite's `INSTR`, Postgres'
// `STRPOS` (or `POSITION`).
return _binaryOnStringArg(
col,
e.args[0],
dialect.stringContainsFunction(),
'>',
);
}
if (e.method == 'length' && e.args.isEmpty) {
return SqlFragment('LENGTH($col)');
}
if (e.method == 'toUpperCase' && e.args.isEmpty) {
return SqlFragment('UPPER($col)');
}
if (e.method == 'toLowerCase' && e.args.isEmpty) {
return SqlFragment('LOWER($col)');
}
if (e.method == 'trim' && e.args.isEmpty) {
return SqlFragment('TRIM($col)');
}
if (e.method == 'isEmpty' && e.args.isEmpty) {
return SqlFragment('($col = \'\')');
}
if (e.method == 'isNotEmpty' && e.args.isEmpty) {
return SqlFragment('($col <> \'\')');
}
throw StateError(
'SqlTranslator: method "${e.method}" is not supported for SQL. '
'Supported: startsWith, endsWith, contains, length, '
'toUpperCase, toLowerCase, trim, isEmpty, isNotEmpty.',
);
}