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

Allow SqlFragmentExpression to have a type/type mapping #34995

Merged
merged 1 commit into from
Dec 4, 2024
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
4 changes: 3 additions & 1 deletion src/EFCore.Relational/Query/ISqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ SqlExpression NiladicFunction(
/// Creates a new <see cref="SqlExpression" /> which represents a SQL token.
/// </summary>
/// <param name="sql">A string token to print in SQL tree.</param>
/// <param name="type">The <see cref="Type" /> of the expression. Defaults to <see langword="void" />. </param>
/// <param name="typeMapping">The <see cref="RelationalTypeMapping" /> associated with the expression.</param>
/// <returns>An expression representing a SQL token.</returns>
SqlExpression Fragment(string sql);
SqlExpression Fragment(string sql, Type? type = null, RelationalTypeMapping? typeMapping = null);
}
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Query/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -956,8 +956,8 @@ public virtual SqlExpression Like(SqlExpression match, SqlExpression pattern, Sq
=> ApplyDefaultTypeMapping(new LikeExpression(match, pattern, escapeChar, null));

/// <inheritdoc />
public virtual SqlExpression Fragment(string sql)
=> new SqlFragmentExpression(sql);
public virtual SqlExpression Fragment(string sql, Type? type = null, RelationalTypeMapping? typeMapping = null)
=> new SqlFragmentExpression(sql, type, typeMapping);

/// <inheritdoc />
public virtual SqlExpression Constant(object value, RelationalTypeMapping? typeMapping = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2915,7 +2915,7 @@ private void AddJoin(
? innerSelect.Orderings
: innerSelect._identifier.Count > 0
? innerSelect._identifier.Select(e => new OrderingExpression(e.Column, true))
: new[] { new OrderingExpression(new SqlFragmentExpression("(SELECT 1)"), true) };
: new[] { new OrderingExpression(new SqlFragmentExpression("(SELECT 1)", typeof(int)), true) };

var rowNumberExpression = new RowNumberExpression(
partitions, orderings.ToList(), (limit ?? offset)!.TypeMapping);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public class SqlFragmentExpression : SqlExpression
/// Creates a new instance of the <see cref="SqlFragmentExpression" /> class.
/// </summary>
/// <param name="sql">A string token to print in SQL tree.</param>
public SqlFragmentExpression(string sql)
: base(typeof(string), null)
/// <param name="type">The <see cref="Type" /> of the expression. Defaults to <see langword="object" />. </param>
/// <param name="typeMapping">The <see cref="RelationalTypeMapping" /> associated with the expression.</param>
public SqlFragmentExpression(string sql, Type? type = null, RelationalTypeMapping? typeMapping = null)
: base(type ?? typeof(object), typeMapping)
=> Sql = sql;

/// <summary>
Expand All @@ -36,8 +38,11 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
/// <inheritdoc />
public override Expression Quote()
=> New(
_quotingConstructor ??= typeof(SqlFragmentExpression).GetConstructor([typeof(string)])!,
Constant(Sql)); // TODO: The new type mapping once that's merged
_quotingConstructor ??=
typeof(SqlFragmentExpression).GetConstructor([typeof(string), typeof(Type), typeof(RelationalTypeMapping)])!,
Constant(Sql),
Constant(Type),
RelationalExpressionQuotingUtilities.QuoteTypeMapping(TypeMapping));

/// <inheritdoc />
protected override void Print(ExpressionPrinter expressionPrinter)
Expand Down