Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,45 @@ protected override SqlScalarExpression VisitImplicit(MethodCallExpression method
}
}

private class StringVisitTrim : SqlBuiltinFunctionVisitor
{
public StringVisitTrim()
: base("TRIM",
false,
null)
{
}

protected override SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
bool validInNet = false;
bool validInNetCore = false;

if (methodCallExpression.Arguments.Count == 1 &&
methodCallExpression.Arguments[0].NodeType == ExpressionType.Constant &&
methodCallExpression.Arguments[0].Type == typeof(char[]))
{
char[] argumentsExpressions = (char[])((ConstantExpression)methodCallExpression.Arguments[0]).Value;
if (argumentsExpressions.Length == 0)
{
validInNet = true;
}
}
else if (methodCallExpression.Arguments.Count == 0)
{
validInNetCore = true;
}

if (validInNet || validInNetCore)
{
SqlScalarExpression str = ExpressionToSql.VisitScalarExpression(methodCallExpression.Object, context);
return SqlFunctionCallScalarExpression.CreateBuiltin(SqlFunctionCallScalarExpression.Names.Trim, str);
}

return null;
}
}

static StringBuiltinFunctions()
{
StringBuiltinFunctionDefinitions = new Dictionary<string, BuiltinFunctionVisitor>
Expand Down Expand Up @@ -415,6 +454,10 @@ static StringBuiltinFunctions()
"TrimEnd",
new StringVisitTrimEnd()
},
{
"Trim",
new StringVisitTrim()
},
{
"StartsWith",
new SqlStringWithComparisonVisitor(SqlFunctionCallScalarExpression.Names.Startswith)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,45 +368,56 @@ FROM root]]></SqlQuery>
</Result>
<Result>
<Input>
<Description><![CDATA[ToLower]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.ToLower())]]></Expression>
<Description><![CDATA[Replace char]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.Replace(c, a))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE LOWER(root["StringField"])
SELECT VALUE REPLACE(root["StringField"], "c", "a")
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[TrimStart]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.TrimStart())]]></Expression>
<Description><![CDATA[Replace string]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.Replace("str", "str2"))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE LTRIM(root["StringField"])
SELECT VALUE REPLACE(root["StringField"], "str", "str2")
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Replace char]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.Replace(c, a))]]></Expression>
<Description><![CDATA[ToLower]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.ToLower())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE REPLACE(root["StringField"], "c", "a")
SELECT VALUE LOWER(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Replace string]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.Replace("str", "str2"))]]></Expression>
<Description><![CDATA[Trim]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.Trim())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE REPLACE(root["StringField"], "str", "str2")
SELECT VALUE TRIM(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Trim with Literal]]></Description>
<Expression><![CDATA[query.Select(doc => " abc ".Trim())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE "abc"
FROM root]]></SqlQuery>
</Output>
</Result>
Expand All @@ -418,6 +429,39 @@ FROM root]]></SqlQuery>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE RTRIM(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[TrimEnd with Literal]]></Description>
<Expression><![CDATA[query.Select(doc => " abc ".TrimEnd())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE " abc"
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[TrimStart]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.TrimStart())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE LTRIM(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[TrimStart with Literal]]></Description>
<Expression><![CDATA[query.Select(doc => " abc ".TrimStart())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE "abc "
FROM root]]></SqlQuery>
</Output>
</Result>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,15 +744,20 @@ public void TestStringFunctions()
new LinqTestInput("IndexOf string w/ startIndex", b => getQuery(b).Select(doc => doc.StringField.IndexOf("str", 0))),
// Count
new LinqTestInput("Count", b => getQuery(b).Select(doc => doc.StringField.Count())),
// ToLower
new LinqTestInput("ToLower", b => getQuery(b).Select(doc => doc.StringField.ToLower())),
// TrimStart
new LinqTestInput("TrimStart", b => getQuery(b).Select(doc => doc.StringField.TrimStart())),
// Replace
new LinqTestInput("Replace char", b => getQuery(b).Select(doc => doc.StringField.Replace('c', 'a'))),
new LinqTestInput("Replace string", b => getQuery(b).Select(doc => doc.StringField.Replace("str", "str2"))),
// ToLower
new LinqTestInput("ToLower", b => getQuery(b).Select(doc => doc.StringField.ToLower())),
// Trim
new LinqTestInput("Trim", b => getQuery(b).Select(doc => doc.StringField.Trim())),
new LinqTestInput("Trim with Literal", b => getQuery(b).Select(doc => " abc ".Trim())),
// TrimEnd
new LinqTestInput("TrimEnd", b => getQuery(b).Select(doc => doc.StringField.TrimEnd())),
new LinqTestInput("TrimEnd with Literal", b => getQuery(b).Select(doc => " abc ".TrimEnd())),
// TrimStart
new LinqTestInput("TrimStart", b => getQuery(b).Select(doc => doc.StringField.TrimStart())),
new LinqTestInput("TrimStart with Literal", b => getQuery(b).Select(doc => " abc ".TrimStart())),
//StartsWith
new LinqTestInput("StartsWith", b => getQuery(b).Select(doc => doc.StringField.StartsWith("str"))),
new LinqTestInput("String constant StartsWith", b => getQuery(b).Select(doc => "str".StartsWith(doc.StringField))),
Expand Down