Skip to content

Commit 3a5f304

Browse files
committed
Round of feedback.
1 parent f6a9607 commit 3a5f304

File tree

7 files changed

+67
-40
lines changed

7 files changed

+67
-40
lines changed

Diff for: src/EFCore.Relational/Query/QuerySqlGenerator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ protected override Expression VisitSqlBinary(SqlBinaryExpression sqlBinaryExpres
628628
protected override Expression VisitSqlConstant(SqlConstantExpression sqlConstantExpression)
629629
{
630630
_relationalCommandBuilder
631-
.Append(sqlConstantExpression.TypeMapping!.GenerateSqlLiteral(sqlConstantExpression.Value), sqlConstantExpression.Sensitive);
631+
.Append(sqlConstantExpression.TypeMapping!.GenerateSqlLiteral(sqlConstantExpression.Value), sqlConstantExpression.IsSensitive);
632632

633633
return sqlConstantExpression;
634634
}

Diff for: src/EFCore.Relational/Query/SqlExpressions/SqlConstantExpression.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public SqlConstantExpression(object? value, Type type, bool sensitive, Relationa
5050
: base(type.UnwrapNullableType(), typeMapping)
5151
{
5252
Value = value;
53-
Sensitive = sensitive;
53+
IsSensitive = sensitive;
5454
}
5555

5656
/// <summary>
@@ -71,7 +71,7 @@ public SqlConstantExpression(object value, bool sensitive, RelationalTypeMapping
7171
/// <param name="typeMapping">The <see cref="RelationalTypeMapping" /> associated with the expression.</param>
7272
[Obsolete("Call the constructor accepting a value (and possibly a Type) instead")]
7373
public SqlConstantExpression(ConstantExpression constantExpression, RelationalTypeMapping? typeMapping)
74-
: this(constantExpression.Value, constantExpression.Type, false, typeMapping)
74+
: this(constantExpression.Value, constantExpression.Type, sensitive: false, typeMapping)
7575
{
7676
}
7777

@@ -83,15 +83,15 @@ public SqlConstantExpression(ConstantExpression constantExpression, RelationalTy
8383
/// <summary>
8484
/// Whether the expression contains sensitive values.
8585
/// </summary>
86-
public virtual bool Sensitive { get; }
86+
public virtual bool IsSensitive { get; }
8787

8888
/// <summary>
8989
/// Applies supplied type mapping to this expression.
9090
/// </summary>
9191
/// <param name="typeMapping">A relational type mapping to apply.</param>
9292
/// <returns>A new expression which has supplied type mapping.</returns>
9393
public virtual SqlExpression ApplyTypeMapping(RelationalTypeMapping? typeMapping)
94-
=> new SqlConstantExpression(Value, Type, Sensitive, typeMapping);
94+
=> new SqlConstantExpression(Value, Type, IsSensitive, typeMapping);
9595

9696
/// <inheritdoc />
9797
protected override Expression VisitChildren(ExpressionVisitor visitor)
@@ -107,7 +107,7 @@ public override Expression Quote()
107107
Constant(Value, Type), typeof(object))
108108
: Constant(Value, Type),
109109
Constant(Type),
110-
Constant(Sensitive),
110+
Constant(IsSensitive),
111111
RelationalExpressionQuotingUtilities.QuoteTypeMapping(TypeMapping));
112112

113113
/// <inheritdoc />

Diff for: src/EFCore.Relational/Storage/RelationalCommandBuilder.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ private void InitializeLogCommandTextBuilderIfNeeded(bool redact)
127127
&& _logCommandTextBuilder is null
128128
&& !Dependencies.LoggingOptions.IsSensitiveDataLoggingEnabled)
129129
{
130-
_logCommandTextBuilder = new();
131-
_logCommandTextBuilder.Append(_commandTextBuilder.ToString());
132-
_logCommandTextBuilder.IndentCount = _commandTextBuilder.IndentCount;
130+
_logCommandTextBuilder = _commandTextBuilder.Clone();
133131
}
134132
}
135133
}

Diff for: src/EFCore/Infrastructure/IndentedStringBuilder.cs

+14-5
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,11 @@ public class IndentedStringBuilder
2727
private readonly StringBuilder _stringBuilder = new();
2828

2929
/// <summary>
30-
/// Gets or sets the current indent level.
30+
/// Gets the current indent level.
3131
/// </summary>
3232
/// <value>The current indent level.</value>
3333
public virtual int IndentCount
34-
{
35-
get => _indent;
36-
set => _indent = value;
37-
}
34+
=> _indent;
3835

3936
/// <summary>
4037
/// The current length of the built string.
@@ -293,6 +290,18 @@ public virtual IDisposable Indent()
293290
public virtual IDisposable SuspendIndent()
294291
=> new IndentSuspender(this);
295292

293+
/// <summary>
294+
/// Clones this <see cref="IndentedStringBuilder" />, copying the built string and current indent level.
295+
/// </summary>
296+
/// <returns>New instance of <see cref="IndentedStringBuilder" />.</returns>
297+
public virtual IndentedStringBuilder Clone()
298+
{
299+
var result = new IndentedStringBuilder();
300+
result._stringBuilder.Append(_stringBuilder);
301+
result._indent = _indent;
302+
return result;
303+
}
304+
296305
/// <summary>
297306
/// Returns the built string.
298307
/// </summary>

Diff for: test/EFCore.Relational.Specification.Tests/Query/AdHocMiscellaneousQueryRelationalTestBase.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,25 @@ public static DateTime Modify(DateTime date)
241241

242242
#region Inlined redacting
243243

244+
protected abstract DbContextOptionsBuilder SetTranslateParameterizedCollectionsToConstants(DbContextOptionsBuilder optionsBuilder);
245+
244246
[ConditionalTheory]
245247
[MemberData(nameof(InlinedRedactingData))]
246248
public virtual async Task Check_inlined_constants_redacting(bool async, bool enableSensitiveDataLogging)
247249
{
248250
var contextFactory = await InitializeAsync<InlinedRedactingContext>(
249-
onConfiguring: o => o.EnableSensitiveDataLogging(enableSensitiveDataLogging));
251+
onConfiguring: o =>
252+
{
253+
SetTranslateParameterizedCollectionsToConstants(o);
254+
o.EnableSensitiveDataLogging(enableSensitiveDataLogging);
255+
});
250256
using var context = contextFactory.CreateContext();
251257

252258
var id = 1;
253259
var ids = new[] { id, 2, 3 };
254-
var query1 = context.TestEntities.Where(x => EF.Constant(ids).Contains(x.Id));
255-
var query2 = context.TestEntities.Where(x => EF.Constant(id) == x.Id);
256-
var query3 = context.TestEntities.Where(x => EF.Constant(ids).Where(y => y == x.Id).Any());
260+
var query1 = context.TestEntities.Where(x => ids.Contains(x.Id));
261+
var query2 = context.TestEntities.Where(x => ids.Where(y => y == x.Id).Any());
262+
var query3 = context.TestEntities.Where(x => EF.Constant(id) == x.Id);
257263

258264
if (async)
259265
{
@@ -280,7 +286,7 @@ public class TestEntity
280286
}
281287
}
282288

283-
public readonly static IEnumerable<object[]> InlinedRedactingData = [[true, true], [true, false], [false, true], [false, false]];
289+
public static readonly IEnumerable<object[]> InlinedRedactingData = [[true, true], [true, false], [false, true], [false, false]];
284290

285291
#endregion
286292
}

Diff for: test/EFCore.SqlServer.FunctionalTests/Query/AdHocMiscellaneousQuerySqlServerTest.cs

+18-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public class AdHocMiscellaneousQuerySqlServerTest : AdHocMiscellaneousQueryRelat
1919
protected override ITestStoreFactory TestStoreFactory
2020
=> SqlServerTestStoreFactory.Instance;
2121

22+
protected override DbContextOptionsBuilder SetTranslateParameterizedCollectionsToConstants(DbContextOptionsBuilder optionsBuilder)
23+
{
24+
new SqlServerDbContextOptionsBuilder(optionsBuilder).TranslateParameterizedCollectionsToConstants();
25+
26+
return optionsBuilder;
27+
}
28+
2229
protected override Task Seed2951(Context2951 context)
2330
=> context.Database.ExecuteSqlRawAsync(
2431
"""
@@ -2422,16 +2429,16 @@ WHERE [t].[Id] IN (?, ?, ?)
24222429
"""
24232430
SELECT [t].[Id], [t].[Name]
24242431
FROM [TestEntities] AS [t]
2425-
WHERE ? = [t].[Id]
2432+
WHERE EXISTS (
2433+
SELECT 1
2434+
FROM (VALUES (?), (?), (?)) AS [i]([Value])
2435+
WHERE [i].[Value] = [t].[Id])
24262436
""",
24272437
//
24282438
"""
24292439
SELECT [t].[Id], [t].[Name]
24302440
FROM [TestEntities] AS [t]
2431-
WHERE EXISTS (
2432-
SELECT 1
2433-
FROM (VALUES (?), (?), (?)) AS [i]([Value])
2434-
WHERE [i].[Value] = [t].[Id])
2441+
WHERE ? = [t].[Id]
24352442
""");
24362443
}
24372444
else
@@ -2446,16 +2453,16 @@ WHERE [t].[Id] IN (1, 2, 3)
24462453
"""
24472454
SELECT [t].[Id], [t].[Name]
24482455
FROM [TestEntities] AS [t]
2449-
WHERE 1 = [t].[Id]
2450-
""",
2451-
//
2452-
"""
2453-
SELECT [t].[Id], [t].[Name]
2454-
FROM [TestEntities] AS [t]
24552456
WHERE EXISTS (
24562457
SELECT 1
24572458
FROM (VALUES (1), (2), (3)) AS [i]([Value])
24582459
WHERE [i].[Value] = [t].[Id])
2460+
""",
2461+
//
2462+
"""
2463+
SELECT [t].[Id], [t].[Name]
2464+
FROM [TestEntities] AS [t]
2465+
WHERE 1 = [t].[Id]
24592466
""");
24602467
}
24612468
}

Diff for: test/EFCore.Sqlite.FunctionalTests/Query/AdHocMiscellaneousQuerySqliteTest.cs

+17-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ public class AdHocMiscellaneousQuerySqliteTest : AdHocMiscellaneousQueryRelation
1010
protected override ITestStoreFactory TestStoreFactory
1111
=> SqliteTestStoreFactory.Instance;
1212

13+
protected override DbContextOptionsBuilder SetTranslateParameterizedCollectionsToConstants(DbContextOptionsBuilder optionsBuilder)
14+
{
15+
new SqliteDbContextOptionsBuilder(optionsBuilder).TranslateParameterizedCollectionsToConstants();
16+
17+
return optionsBuilder;
18+
}
19+
1320
protected override Task Seed2951(Context2951 context)
1421
=> context.Database.ExecuteSqlRawAsync(
1522
"""
@@ -99,16 +106,16 @@ public override async Task Check_inlined_constants_redacting(bool async, bool en
99106
"""
100107
SELECT "t"."Id", "t"."Name"
101108
FROM "TestEntities" AS "t"
102-
WHERE ? = "t"."Id"
109+
WHERE EXISTS (
110+
SELECT 1
111+
FROM (SELECT ? AS "Value" UNION ALL VALUES (?), (?)) AS "i"
112+
WHERE "i"."Value" = "t"."Id")
103113
""",
104114
//
105115
"""
106116
SELECT "t"."Id", "t"."Name"
107117
FROM "TestEntities" AS "t"
108-
WHERE EXISTS (
109-
SELECT 1
110-
FROM (SELECT ? AS "Value" UNION ALL VALUES (?), (?)) AS "i"
111-
WHERE "i"."Value" = "t"."Id")
118+
WHERE ? = "t"."Id"
112119
""");
113120
}
114121
else
@@ -123,16 +130,16 @@ SELECT 1
123130
"""
124131
SELECT "t"."Id", "t"."Name"
125132
FROM "TestEntities" AS "t"
126-
WHERE 1 = "t"."Id"
133+
WHERE EXISTS (
134+
SELECT 1
135+
FROM (SELECT 1 AS "Value" UNION ALL VALUES (2), (3)) AS "i"
136+
WHERE "i"."Value" = "t"."Id")
127137
""",
128138
//
129139
"""
130140
SELECT "t"."Id", "t"."Name"
131141
FROM "TestEntities" AS "t"
132-
WHERE EXISTS (
133-
SELECT 1
134-
FROM (SELECT 1 AS "Value" UNION ALL VALUES (2), (3)) AS "i"
135-
WHERE "i"."Value" = "t"."Id")
142+
WHERE 1 = "t"."Id"
136143
""");
137144
}
138145
}

0 commit comments

Comments
 (0)