Skip to content

Commit ac5bd7b

Browse files
authored
Allow SqlFragmentExpression to have a type/type mapping (#34995)
1 parent aed754c commit ac5bd7b

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

src/EFCore.Relational/Query/ISqlExpressionFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ SqlExpression NiladicFunction(
435435
/// Creates a new <see cref="SqlExpression" /> which represents a SQL token.
436436
/// </summary>
437437
/// <param name="sql">A string token to print in SQL tree.</param>
438+
/// <param name="type">The <see cref="Type" /> of the expression. Defaults to <see langword="void" />. </param>
439+
/// <param name="typeMapping">The <see cref="RelationalTypeMapping" /> associated with the expression.</param>
438440
/// <returns>An expression representing a SQL token.</returns>
439-
SqlExpression Fragment(string sql);
441+
SqlExpression Fragment(string sql, Type? type = null, RelationalTypeMapping? typeMapping = null);
440442
}

src/EFCore.Relational/Query/SqlExpressionFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,8 @@ public virtual SqlExpression Like(SqlExpression match, SqlExpression pattern, Sq
956956
=> ApplyDefaultTypeMapping(new LikeExpression(match, pattern, escapeChar, null));
957957

958958
/// <inheritdoc />
959-
public virtual SqlExpression Fragment(string sql)
960-
=> new SqlFragmentExpression(sql);
959+
public virtual SqlExpression Fragment(string sql, Type? type = null, RelationalTypeMapping? typeMapping = null)
960+
=> new SqlFragmentExpression(sql, type, typeMapping);
961961

962962
/// <inheritdoc />
963963
public virtual SqlExpression Constant(object value, RelationalTypeMapping? typeMapping = null)

src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2913,7 +2913,7 @@ private void AddJoin(
29132913
? innerSelect.Orderings
29142914
: innerSelect._identifier.Count > 0
29152915
? innerSelect._identifier.Select(e => new OrderingExpression(e.Column, true))
2916-
: new[] { new OrderingExpression(new SqlFragmentExpression("(SELECT 1)"), true) };
2916+
: new[] { new OrderingExpression(new SqlFragmentExpression("(SELECT 1)", typeof(int)), true) };
29172917

29182918
var rowNumberExpression = new RowNumberExpression(
29192919
partitions, orderings.ToList(), (limit ?? offset)!.TypeMapping);

src/EFCore.Relational/Query/SqlExpressions/SqlFragmentExpression.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ public class SqlFragmentExpression : SqlExpression
2020
/// Creates a new instance of the <see cref="SqlFragmentExpression" /> class.
2121
/// </summary>
2222
/// <param name="sql">A string token to print in SQL tree.</param>
23-
public SqlFragmentExpression(string sql)
24-
: base(typeof(string), null)
23+
/// <param name="type">The <see cref="Type" /> of the expression. Defaults to <see langword="object" />. </param>
24+
/// <param name="typeMapping">The <see cref="RelationalTypeMapping" /> associated with the expression.</param>
25+
public SqlFragmentExpression(string sql, Type? type = null, RelationalTypeMapping? typeMapping = null)
26+
: base(type ?? typeof(object), typeMapping)
2527
=> Sql = sql;
2628

2729
/// <summary>
@@ -36,8 +38,11 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
3638
/// <inheritdoc />
3739
public override Expression Quote()
3840
=> New(
39-
_quotingConstructor ??= typeof(SqlFragmentExpression).GetConstructor([typeof(string)])!,
40-
Constant(Sql)); // TODO: The new type mapping once that's merged
41+
_quotingConstructor ??=
42+
typeof(SqlFragmentExpression).GetConstructor([typeof(string), typeof(Type), typeof(RelationalTypeMapping)])!,
43+
Constant(Sql),
44+
Constant(Type),
45+
RelationalExpressionQuotingUtilities.QuoteTypeMapping(TypeMapping));
4146

4247
/// <inheritdoc />
4348
protected override void Print(ExpressionPrinter expressionPrinter)

0 commit comments

Comments
 (0)