chore(sonar)!: return empty IQueryable instead of null from GetQuery<T> (S1168)
Replaces `null` returns from `SqlBreakdownBase.GetQuery<T>()` and its four overrides (LinqQueryBreakdown, SqlServer/PostgreSql/Snowflake QueryBreakdown) with `Enumerable.Empty<T>().AsQueryable()`, and tightens the signature from `IQueryable<T>?` to `IQueryable<T>`. The "we can't reconstruct" semantic now lives in "the query yields zero rows" rather than a nullable return, which is what callers in LINQ pipelines actually want. Three LinqQueryBreakdownTests tests asserting `Is.Null` are renamed and updated to assert `Is.Empty`. BREAKING CHANGE: SqlBreakdownBase.GetQuery<T> and the SqlServer / PostgreSql / Snowflake / LinqToSql QueryBreakdown.GetQuery<T> overrides no longer return `IQueryable<T>?`; they now return a non-nullable `IQueryable<T>` that is empty when reconstruction isn't possible. External NuGet consumers null- checking the result must switch to `.Any()` / `Is.Empty` checks instead. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
72072cec8e
commit
2c23ba4a88
@@ -185,39 +185,31 @@ public class LinqQueryBreakdown : QueryBreakdown
|
|||||||
/// Gets a LINQ to SQL query of the specified type based on this breakdown.
|
/// Gets a LINQ to SQL query of the specified type based on this breakdown.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The entity type for the query.</typeparam>
|
/// <typeparam name="T">The entity type for the query.</typeparam>
|
||||||
/// <returns>An IQueryable of the specified type reconstructed from the breakdown, or null if the type doesn't match the original entity type.</returns>
|
/// <returns>An IQueryable of the specified type reconstructed from the breakdown; an empty queryable if the type doesn't match the original entity type or the original data source isn't available.</returns>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This method attempts to reconstruct a LINQ query from the analyzed components (WHERE, ORDER BY, etc.).
|
/// This method attempts to reconstruct a LINQ query from the analyzed components (WHERE, ORDER BY, etc.).
|
||||||
/// If a data source (IQueryable) is available in the breakdown's OriginalExpression, it will be used.
|
/// If a data source (IQueryable) is available in the breakdown's OriginalExpression, it will be used.
|
||||||
/// Otherwise, returns null to indicate the query cannot be reconstructed without the original data source.
|
/// Otherwise, returns an empty queryable — breakdown analysis is one-way; reconstruction requires the original provider.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public override IQueryable<T>? GetQuery<T>() where T : class
|
public override IQueryable<T> GetQuery<T>() where T : class
|
||||||
{
|
{
|
||||||
// If we don't have the original expression, we cannot reconstruct the LINQ query
|
|
||||||
if (OriginalExpression == null)
|
if (OriginalExpression == null)
|
||||||
{
|
{
|
||||||
return null;
|
return Enumerable.Empty<T>().AsQueryable();
|
||||||
}
|
}
|
||||||
|
|
||||||
// The original expression is the full LINQ query that was analyzed
|
|
||||||
// To use it, we need it to be an IQueryable<T>
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// If the original expression can be converted to IQueryable<T>, use it
|
|
||||||
// Otherwise, we cannot safely reconstruct without the original query provider
|
|
||||||
if (OriginalExpression is Expression && EntityType == typeof(T))
|
if (OriginalExpression is Expression && EntityType == typeof(T))
|
||||||
{
|
{
|
||||||
// We have the expression, but we don't have the provider to create IQueryable<T>
|
return Enumerable.Empty<T>().AsQueryable();
|
||||||
// The breakdown analysis is one-way; reconstruction requires the original provider
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return Enumerable.Empty<T>().AsQueryable();
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
// If any error occurs during reconstruction, return null
|
return Enumerable.Empty<T>().AsQueryable();
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -272,17 +272,13 @@ public class QueryBreakdown : SqlServerQueryBreakdown
|
|||||||
/// Gets a LINQ to SQL query of the specified type based on this breakdown.
|
/// Gets a LINQ to SQL query of the specified type based on this breakdown.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The entity type for the query.</typeparam>
|
/// <typeparam name="T">The entity type for the query.</typeparam>
|
||||||
/// <returns>null by default, as QueryBreakdown operates on SQL. Override in derived classes to provide LINQ query reconstruction.</returns>
|
/// <returns>An empty queryable by default, as QueryBreakdown operates on SQL. Override in derived classes to provide LINQ query reconstruction.</returns>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This PostgreSQL-specific implementation returns null since PostgreSQL QueryBreakdown represents parsed SQL statements.
|
/// This PostgreSQL-specific implementation returns an empty queryable since PostgreSQL QueryBreakdown
|
||||||
|
/// represents parsed SQL statements and has no built-in way to create LINQ queries.
|
||||||
/// Derived classes can override this method to reconstruct LINQ queries from the analyzed components.
|
/// Derived classes can override this method to reconstruct LINQ queries from the analyzed components.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public override IQueryable<T>? GetQuery<T>() where T : class
|
public override IQueryable<T> GetQuery<T>() where T : class => Enumerable.Empty<T>().AsQueryable();
|
||||||
{
|
|
||||||
// PostgreSQL breakdown represents parsed SQL statements and does not have a built-in way to create LINQ queries
|
|
||||||
// Override in derived classes to provide LINQ query reconstruction if needed
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -863,17 +863,13 @@ public class QueryBreakdown : SqlServerQueryBreakdown
|
|||||||
/// Gets a LINQ to SQL query of the specified type based on this breakdown.
|
/// Gets a LINQ to SQL query of the specified type based on this breakdown.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The entity type for the query.</typeparam>
|
/// <typeparam name="T">The entity type for the query.</typeparam>
|
||||||
/// <returns>null by default, as QueryBreakdown operates on SQL. Override in derived classes to provide LINQ query reconstruction.</returns>
|
/// <returns>An empty queryable by default, as QueryBreakdown operates on SQL. Override in derived classes to provide LINQ query reconstruction.</returns>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This Snowflake-specific implementation returns null since Snowflake QueryBreakdown represents parsed SQL statements.
|
/// This Snowflake-specific implementation returns an empty queryable since Snowflake QueryBreakdown
|
||||||
|
/// represents parsed SQL statements and has no built-in way to create LINQ queries.
|
||||||
/// Derived classes can override this method to reconstruct LINQ queries from the analyzed components.
|
/// Derived classes can override this method to reconstruct LINQ queries from the analyzed components.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public override IQueryable<T>? GetQuery<T>() where T : class
|
public override IQueryable<T> GetQuery<T>() where T : class => Enumerable.Empty<T>().AsQueryable();
|
||||||
{
|
|
||||||
// Snowflake breakdown represents parsed SQL statements and does not have a built-in way to create LINQ queries
|
|
||||||
// Override in derived classes to provide LINQ query reconstruction if needed
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -57,13 +57,13 @@ public abstract class SqlBreakdownBase : ISqlBreakdown
|
|||||||
/// Gets a LINQ to SQL query of the specified type based on this breakdown.
|
/// Gets a LINQ to SQL query of the specified type based on this breakdown.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The entity type for the query.</typeparam>
|
/// <typeparam name="T">The entity type for the query.</typeparam>
|
||||||
/// <returns>An IQueryable of the specified type, or null if the breakdown cannot be converted to a LINQ query.</returns>
|
/// <returns>An IQueryable of the specified type; an empty queryable if the breakdown cannot be converted to a LINQ query.</returns>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This method allows derived breakdown classes to reconstruct or generate LINQ queries
|
/// This method allows derived breakdown classes to reconstruct or generate LINQ queries
|
||||||
/// from the analyzed components (SELECT, WHERE, ORDER BY, etc.).
|
/// from the analyzed components (SELECT, WHERE, ORDER BY, etc.).
|
||||||
/// The default implementation returns null.
|
/// The default implementation returns an empty queryable.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public virtual IQueryable<T>? GetQuery<T>() where T : class => null;
|
public virtual IQueryable<T> GetQuery<T>() where T : class => Enumerable.Empty<T>().AsQueryable();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the complete SQL query including optional setup and finish clauses.
|
/// Gets the complete SQL query including optional setup and finish clauses.
|
||||||
|
|||||||
@@ -731,18 +731,14 @@ public class QueryBreakdown : SqlBreakdownBase, IQueryBreakdown
|
|||||||
/// Gets a LINQ to SQL query of the specified type based on this breakdown.
|
/// Gets a LINQ to SQL query of the specified type based on this breakdown.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The entity type for the query.</typeparam>
|
/// <typeparam name="T">The entity type for the query.</typeparam>
|
||||||
/// <returns>null by default, as QueryBreakdown operates on SQL. Override in derived classes to provide LINQ query reconstruction.</returns>
|
/// <returns>An empty queryable by default, as QueryBreakdown operates on SQL. Override in derived classes to provide LINQ query reconstruction.</returns>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This base implementation returns null since QueryBreakdown represents parsed SQL statements.
|
/// This base implementation returns an empty queryable since QueryBreakdown represents parsed SQL statements
|
||||||
|
/// and has no built-in way to create LINQ queries.
|
||||||
/// Derived classes (such as LinqQueryBreakdown) can override this method to reconstruct LINQ queries
|
/// Derived classes (such as LinqQueryBreakdown) can override this method to reconstruct LINQ queries
|
||||||
/// from the analyzed components.
|
/// from the analyzed components.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public override IQueryable<T>? GetQuery<T>() where T : class
|
public override IQueryable<T> GetQuery<T>() where T : class => Enumerable.Empty<T>().AsQueryable();
|
||||||
{
|
|
||||||
// Base implementation returns null - SQL-based breakdowns don't have a built-in way to create LINQ queries
|
|
||||||
// Override in derived classes (e.g., LinqQueryBreakdown) to provide LINQ query reconstruction
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the complete SQL query string (interface implementation).
|
/// Gets the complete SQL query string (interface implementation).
|
||||||
|
|||||||
@@ -513,21 +513,21 @@ public class GetQueryTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void GetQuery_LinqBreakdown_ReturnsNullByDefault()
|
public void GetQuery_LinqBreakdown_ReturnsEmptyByDefault()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var query = TestDataContext.Users.Where(u => u.Age > 18);
|
var query = TestDataContext.Users.Where(u => u.Age > 18);
|
||||||
var breakdown = LinqQueryBreakdown.Analyze(query);
|
var breakdown = LinqQueryBreakdown.Analyze(query);
|
||||||
|
|
||||||
// Act - GetQuery returns null because LinqQueryBreakdown needs the original provider
|
// Act - GetQuery returns empty because LinqQueryBreakdown needs the original provider
|
||||||
var result = breakdown.GetQuery<User>();
|
var result = breakdown.GetQuery<User>();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(result, Is.Null);
|
Assert.That(result, Is.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void GetQuery_SqlServerQueryBreakdown_ReturnsNull()
|
public void GetQuery_SqlServerQueryBreakdown_ReturnsEmpty()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var breakdown = new QueryBreakdown("Id, Name", "Users", "Age > 18");
|
var breakdown = new QueryBreakdown("Id, Name", "Users", "Age > 18");
|
||||||
@@ -536,11 +536,11 @@ public class GetQueryTests
|
|||||||
var result = breakdown.GetQuery<User>();
|
var result = breakdown.GetQuery<User>();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(result, Is.Null);
|
Assert.That(result, Is.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void GetQuery_MultipleBreakdownTypes_AllReturnNull()
|
public void GetQuery_MultipleBreakdownTypes_AllReturnEmpty()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var linqBreakdown = LinqQueryBreakdown.Analyze(TestDataContext.Users);
|
var linqBreakdown = LinqQueryBreakdown.Analyze(TestDataContext.Users);
|
||||||
@@ -553,8 +553,8 @@ public class GetQueryTests
|
|||||||
Assert.Multiple(() =>
|
Assert.Multiple(() =>
|
||||||
{
|
{
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(linqResult, Is.Null);
|
Assert.That(linqResult, Is.Empty);
|
||||||
Assert.That(sqlResult, Is.Null);
|
Assert.That(sqlResult, Is.Empty);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user