Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14205 sql server translate bool to string #24793

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -72,10 +72,41 @@ public SqlServerObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFact
Check.NotNull(arguments, nameof(arguments));
Check.NotNull(logger, nameof(logger));

return method.Name == nameof(ToString)
&& arguments.Count == 0
&& instance != null
&& _typeMapping.TryGetValue(instance.Type, out var storeType)
if (instance == null || method.Name != nameof(ToString) || arguments.Count != 0)
{
return null;
}

if (instance.Type == typeof(bool))
michalczerwinski marked this conversation as resolved.
Show resolved Hide resolved
{
if (instance is ColumnExpression columnExpression && columnExpression.IsNullable)
{
return _sqlExpressionFactory.Case(
new[]
{
new CaseWhenClause(
_sqlExpressionFactory.Equal(instance, _sqlExpressionFactory.Constant(false)),
_sqlExpressionFactory.Constant(false.ToString())),
new CaseWhenClause(
_sqlExpressionFactory.Equal(instance, _sqlExpressionFactory.Constant(true)),
_sqlExpressionFactory.Constant(true.ToString()))
},
_sqlExpressionFactory.Constant(null));
}
else
{
return _sqlExpressionFactory.Case(
new[]
{
new CaseWhenClause(
_sqlExpressionFactory.Equal(instance, _sqlExpressionFactory.Constant(false)),
_sqlExpressionFactory.Constant(false.ToString()))
},
_sqlExpressionFactory.Constant(true.ToString()));
}
}

return _typeMapping.TryGetValue(instance.Type, out var storeType)
? _sqlExpressionFactory.Function(
"CONVERT",
new[] { _sqlExpressionFactory.Fragment(storeType), instance },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,41 @@ public SqliteObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFactory
Check.NotNull(method, nameof(method));
Check.NotNull(arguments, nameof(arguments));

return method.Name == nameof(ToString)
&& arguments.Count == 0
&& instance != null
&& _typeMapping.Contains(instance.Type)
if (instance == null || method.Name != nameof(ToString) || arguments.Count != 0)
{
return null;
}

if (instance.Type == typeof(bool))
{
if (instance is ColumnExpression columnExpression && columnExpression.IsNullable)
{
return _sqlExpressionFactory.Case(
new[]
{
new CaseWhenClause(
_sqlExpressionFactory.Equal(instance, _sqlExpressionFactory.Constant(false)),
_sqlExpressionFactory.Constant(false.ToString())),
new CaseWhenClause(
_sqlExpressionFactory.Equal(instance, _sqlExpressionFactory.Constant(true)),
_sqlExpressionFactory.Constant(true.ToString()))
},
_sqlExpressionFactory.Constant(null));
}
else
{
return _sqlExpressionFactory.Case(
new[]
{
new CaseWhenClause(
_sqlExpressionFactory.Equal(instance, _sqlExpressionFactory.Constant(false)),
_sqlExpressionFactory.Constant(false.ToString()))
},
_sqlExpressionFactory.Constant(true.ToString()));
}
}

return _typeMapping.Contains(instance.Type)
? _sqlExpressionFactory.Convert(instance, typeof(string))
: null;
}
Expand Down
18 changes: 18 additions & 0 deletions test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ public virtual Task ToString_guid_property_projection(bool async)
});
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task ToString_boolean_property_non_nullable(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Weapon>().Select(w => w.IsAutomatic.ToString()));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task ToString_boolean_property_nullable(bool async)
{
return AssertQuery(
async,
ss => ss.Set<LocustHorde>().Select(lh => lh.Eradicated.ToString()));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Include_multiple_one_to_one_and_one_to_many_self_reference(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3610,6 +3610,31 @@ FROM [Gears] AS [g]
ORDER BY [g].[SquadId], [g].[Nickname]");
}

public override async Task ToString_boolean_property_non_nullable(bool async)
{
await base.ToString_boolean_property_non_nullable(async);

AssertSql(
@"SELECT CASE
WHEN [w].[IsAutomatic] = CAST(0 AS bit) THEN N'False'
ELSE N'True'
END
FROM [Weapons] AS [w]");
}

public override async Task ToString_boolean_property_nullable(bool async)
{
await base.ToString_boolean_property_nullable(async);

AssertSql(
@"SELECT CASE
WHEN [f].[Eradicated] = CAST(0 AS bit) THEN N'False'
WHEN [f].[Eradicated] = CAST(1 AS bit) THEN N'True'
ELSE NULL
END
FROM [Factions] AS [f]");
}

public override async Task Correlated_collections_naked_navigation_with_ToList(bool async)
{
await base.Correlated_collections_naked_navigation_with_ToList(async);
Expand Down