Skip to content

Commit b4022de

Browse files
committed
Improve string.Trim/TrimStart/TrimEnd support
1 parent 1a99c0f commit b4022de

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

Orm/Xtensive.Orm/Orm/Providers/Expressions/MemberCompilers/StringCompilers.cs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,38 @@ public static SqlExpression StringTrim(SqlExpression _this)
139139
return SqlDml.Trim(_this);
140140
}
141141

142+
[Compiler(typeof(string), nameof(string.TrimStart))]
143+
public static SqlExpression StringTrimStart(SqlExpression _this)
144+
{
145+
return SqlDml.Trim(_this, SqlTrimType.Leading);
146+
}
147+
148+
[Compiler(typeof(string), nameof(string.TrimEnd))]
149+
public static SqlExpression StringTrimEnd(SqlExpression _this)
150+
{
151+
return SqlDml.Trim(_this, SqlTrimType.Trailing);
152+
}
153+
142154
private static SqlExpression GenericTrim(SqlExpression _this, SqlExpression trimChars, SqlTrimType trimType)
143155
{
144-
if (trimChars is SqlNull)
156+
if (trimChars is SqlNull) {
145157
return SqlDml.Trim(_this, trimType);
146-
if (!(trimChars is SqlContainer container))
147-
throw new NotSupportedException(Strings.ExStringTrimSupportedOnlyWithConstants);
148-
if (!(container.Value is char[] chars))
149-
throw new NotSupportedException(Strings.ExStringTrimSupportedOnlyWithConstants);
150-
return chars.Length==0
151-
? SqlDml.Trim(_this, trimType)
152-
: SqlDml.Trim(_this, trimType, new string(chars));
158+
}
159+
if (trimChars is SqlLiteral<char> oneChar) {
160+
return SqlDml.Trim(_this, trimType, oneChar.Value.ToString());
161+
}
162+
if (trimChars is SqlContainer container && container.Value is char[] chars) {
163+
if (chars.Length == 0) {
164+
return SqlDml.Trim(_this, trimType);
165+
}
166+
167+
var context = ExpressionTranslationContext.Current;
168+
var provider = context.ProviderInfo.ProviderName;
169+
return provider.Equals(WellKnown.Provider.Firebird, StringComparison.Ordinal)
170+
? chars.Aggregate(_this, (current, @char) => SqlDml.Trim(current, trimType, @char.ToString()))
171+
: SqlDml.Trim(_this, trimType, new string(chars));
172+
}
173+
throw new NotSupportedException(Strings.ExStringTrimSupportedOnlyWithConstants);
153174
}
154175

155176
[Compiler(typeof(string), nameof(string.Trim))]
@@ -159,20 +180,41 @@ public static SqlExpression StringTrim(SqlExpression _this,
159180
return GenericTrim(_this, trimChars, SqlTrimType.Both);
160181
}
161182

183+
[Compiler(typeof(string), nameof(string.Trim))]
184+
public static SqlExpression StringTrimOneChar(SqlExpression _this,
185+
[Type(typeof(char))] SqlExpression trimChar)
186+
{
187+
return GenericTrim(_this, trimChar, SqlTrimType.Both);
188+
}
189+
162190
[Compiler(typeof(string), nameof(string.TrimStart))]
163191
public static SqlExpression StringTrimStart(SqlExpression _this,
164192
[Type(typeof(char[]))] SqlExpression trimChars)
165193
{
166194
return GenericTrim(_this, trimChars, SqlTrimType.Leading);
167195
}
168196

197+
[Compiler(typeof(string), nameof(string.TrimStart))]
198+
public static SqlExpression StringTrimStartOneChar(SqlExpression _this,
199+
[Type(typeof(char))] SqlExpression trimChar)
200+
{
201+
return GenericTrim(_this, trimChar, SqlTrimType.Leading);
202+
}
203+
169204
[Compiler(typeof(string), nameof(string.TrimEnd))]
170205
public static SqlExpression StringTrimEnd(SqlExpression _this,
171206
[Type(typeof(char[]))] SqlExpression trimChars)
172207
{
173208
return GenericTrim(_this, trimChars, SqlTrimType.Trailing);
174209
}
175210

211+
[Compiler(typeof(string), nameof(string.TrimEnd))]
212+
public static SqlExpression StringTrimEndOneChar(SqlExpression _this,
213+
[Type(typeof(char))] SqlExpression trimChar)
214+
{
215+
return GenericTrim(_this, trimChar, SqlTrimType.Trailing);
216+
}
217+
176218
[Compiler(typeof(string), nameof(string.Length), TargetKind.PropertyGet)]
177219
public static SqlExpression StringLength(SqlExpression _this)
178220
{

0 commit comments

Comments
 (0)