Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -65,8 +65,11 @@ public static SqlScalarExpression VisitBuiltinFunctionCall(MethodCallExpression
return MathBuiltinFunctions.Visit(methodCallExpression, context);
}

// String functions
if (declaringType == typeof(string))
// String functions or ToString with Objects (String and Guid only)
if ((declaringType == typeof(string)) ||
(methodCallExpression.Method.Name == "ToString" &&
methodCallExpression.Arguments.Count == 0 &&
methodCallExpression.Object != null))
{
return StringBuiltinFunctions.Visit(methodCallExpression, context);
}
Expand All @@ -83,14 +86,6 @@ public static SqlScalarExpression VisitBuiltinFunctionCall(MethodCallExpression
return SpatialBuiltinFunctions.Visit(methodCallExpression, context);
}

// ToString with Objects (String and Guid only)
if (methodCallExpression.Method.Name == "ToString" &&
methodCallExpression.Arguments.Count == 0 &&
methodCallExpression.Object != null)
{
return ExpressionToSql.VisitNonSubqueryScalarExpression(methodCallExpression.Object, context);
}

throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, methodCallExpression.Method.Name));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,27 @@ protected override SqlScalarExpression VisitExplicit(MethodCallExpression method
}
}

private class StringVisitToString : SqlBuiltinFunctionVisitor
{
public StringVisitToString()
: base("ToString",
true,
null)
{
}

protected override SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
if ((methodCallExpression.Arguments.Count == 0) && (methodCallExpression.Object != null))
{
SqlScalarExpression str = ExpressionToSql.VisitNonSubqueryScalarExpression(methodCallExpression.Object, context);
return SqlFunctionCallScalarExpression.CreateBuiltin(SqlFunctionCallScalarExpression.Names.ToString, str);
}

return null;
}
}

static StringBuiltinFunctions()
{
StringBuiltinFunctionDefinitions = new Dictionary<string, BuiltinFunctionVisitor>
Expand Down Expand Up @@ -386,6 +407,10 @@ static StringBuiltinFunctions()
"Reverse",
new StringVisitReverse()
},
{
"ToString",
new StringVisitToString()
},
{
"TrimEnd",
new StringVisitTrimEnd()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ JOIN (
SELECT VALUE MAX(t0["Amount"])
FROM root
JOIN t0 IN root["Records"]["Transactions"])) AS v0
ORDER BY (root["IsRegistered"] ? root["FamilyId"] : root["Int"]) DESC, (v0[0] % 1000) ASC
ORDER BY (root["IsRegistered"] ? root["FamilyId"] : ToString(root["Int"])) DESC, (v0[0] % 1000) ASC
]]></SqlQuery>
<ErrorMessage><![CDATA[Status Code: BadRequest,{"errors":[{"severity":"Error","code":2206,"message":"Unsupported ORDER BY clause. ORDER BY item expression could not be mapped to a document path."}]},0x800A0B00]]></ErrorMessage>
</Output>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ WHERE (root["id"] = "098aa945-7ed8-4c50-b7b8-bd99eddb54bc")]]></SqlQuery>
<SqlQuery><![CDATA[
SELECT VALUE root
FROM root
WHERE (root["id"] = "098aa945-7ed8-4c50-b7b8-bd99eddb54bc")]]></SqlQuery>
WHERE (ToString(root["id"]) = "098aa945-7ed8-4c50-b7b8-bd99eddb54bc")]]></SqlQuery>
</Output>
</Result>
<Result>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ FROM root]]></SqlQuery>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE root["StringField"]
SELECT VALUE ToString(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,17 @@ FROM root]]></SqlQuery>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE SUBSTRING(root["StringField"], 0, 1)
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Substring]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.ToString())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE ToString(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,8 @@ public void TestStringFunctions()
new LinqTestInput("String constant StartsWith (case-insensitive)", b => getQuery(b).Select(doc => "sTr".StartsWith(doc.StringField, StringComparison.OrdinalIgnoreCase))),
// Substring
new LinqTestInput("Substring", b => getQuery(b).Select(doc => doc.StringField.Substring(0, 1))),
// ToString
new LinqTestInput("Substring", b => getQuery(b).Select(doc => doc.StringField.ToString())),
// ToUpper
new LinqTestInput("ToUpper", b => getQuery(b).Select(doc => doc.StringField.ToUpper()))
};
Expand Down