Skip to content

Commit

Permalink
Evolve null-checking approach in SQLite provider
Browse files Browse the repository at this point in the history
Part of #19233
  • Loading branch information
ajcvickers committed Nov 4, 2021
1 parent 74f4e5e commit 2314a1d
Show file tree
Hide file tree
Showing 26 changed files with 13 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public static DbContextOptionsBuilder UseSqlite(
this DbContextOptionsBuilder optionsBuilder,
Action<SqliteDbContextOptionsBuilder>? sqliteOptionsAction = null)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));

((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(GetOrCreateExtension(optionsBuilder));

ConfigureWarnings(optionsBuilder);
Expand All @@ -68,7 +66,6 @@ public static DbContextOptionsBuilder UseSqlite(
string connectionString,
Action<SqliteDbContextOptionsBuilder>? sqliteOptionsAction = null)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
Check.NotEmpty(connectionString, nameof(connectionString));

var extension = (SqliteOptionsExtension)GetOrCreateExtension(optionsBuilder).WithConnectionString(connectionString);
Expand Down Expand Up @@ -101,7 +98,6 @@ public static DbContextOptionsBuilder UseSqlite(
DbConnection connection,
Action<SqliteDbContextOptionsBuilder>? sqliteOptionsAction = null)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
Check.NotNull(connection, nameof(connection));

var extension = (SqliteOptionsExtension)GetOrCreateExtension(optionsBuilder).WithConnection(connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Sqlite.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore
Expand All @@ -29,8 +28,6 @@ public static class SqlitePropertyBuilderExtensions
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder HasSrid(this PropertyBuilder propertyBuilder, int srid)
{
Check.NotNull(propertyBuilder, nameof(propertyBuilder));

propertyBuilder.Metadata.SetSrid(srid);

return propertyBuilder;
Expand Down Expand Up @@ -95,7 +92,7 @@ public static bool CanSetSrid(
this IConventionPropertyBuilder propertyBuilder,
int? srid,
bool fromDataAnnotation = false)
=> Check.NotNull(propertyBuilder, nameof(propertyBuilder)).CanSetAnnotation(
=> propertyBuilder.CanSetAnnotation(
SqliteAnnotationNames.Srid,
srid,
fromDataAnnotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public static IServiceCollection AddSqlite<TContext>(
Action<DbContextOptionsBuilder>? optionsAction = null)
where TContext : DbContext
{
Check.NotNull(serviceCollection, nameof(serviceCollection));
Check.NotEmpty(connectionString, nameof(connectionString));

return serviceCollection.AddDbContext<TContext>(
Expand Down Expand Up @@ -107,8 +106,6 @@ public static IServiceCollection AddSqlite<TContext>(
[EditorBrowsable(EditorBrowsableState.Never)]
public static IServiceCollection AddEntityFrameworkSqlite(this IServiceCollection serviceCollection)
{
Check.NotNull(serviceCollection, nameof(serviceCollection));

var builder = new EntityFrameworkRelationalServicesBuilder(serviceCollection)
.TryAdd<LoggingDefinitions, SqliteLoggingDefinitions>()
.TryAdd<IDatabaseProvider, DatabaseProvider<SqliteOptionsExtension>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,6 @@ protected override void Generate(
MigrationCommandListBuilder builder,
bool terminate)
{
Check.NotNull(operation, nameof(operation));
Check.NotNull(builder, nameof(builder));

builder
.Append("DROP INDEX ")
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name));
Expand Down Expand Up @@ -606,9 +603,6 @@ protected override void Generate(RenameIndexOperation operation, IModel? model,
/// <param name="builder">The command builder to use to build the commands.</param>
protected override void Generate(RenameTableOperation operation, IModel? model, MigrationCommandListBuilder builder)
{
Check.NotNull(operation, nameof(operation));
Check.NotNull(builder, nameof(builder));

if (operation.NewName != null
&& operation.NewName != operation.Name)
{
Expand All @@ -630,11 +624,7 @@ protected override void Generate(RenameTableOperation operation, IModel? model,
/// <param name="model">The target model which may be <see langword="null" /> if the operations exist without a model.</param>
/// <param name="builder">The command builder to use to build the commands.</param>
protected override void Generate(RenameColumnOperation operation, IModel? model, MigrationCommandListBuilder builder)
{
Check.NotNull(operation, nameof(operation));
Check.NotNull(builder, nameof(builder));

builder
=> builder
.Append("ALTER TABLE ")
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table))
.Append(" RENAME COLUMN ")
Expand All @@ -643,7 +633,6 @@ protected override void Generate(RenameColumnOperation operation, IModel? model,
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.NewName))
.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator)
.EndCommand();
}

/// <summary>
/// Builds commands for the given <see cref="CreateTableOperation" /> by making calls on the given
Expand All @@ -659,9 +648,6 @@ protected override void Generate(
MigrationCommandListBuilder builder,
bool terminate = true)
{
Check.NotNull(operation, nameof(operation));
Check.NotNull(builder, nameof(builder));

var spatialiteColumns = new Stack<AddColumnOperation>();
for (var i = operation.Columns.Count - 1; i >= 0; i--)
{
Expand Down Expand Up @@ -739,9 +725,6 @@ protected override void CreateTableColumns(
IModel? model,
MigrationCommandListBuilder builder)
{
Check.NotNull(operation, nameof(operation));
Check.NotNull(builder, nameof(builder));

if (!operation.Columns.Any(c => !string.IsNullOrEmpty(c.Comment)))
{
base.CreateTableColumns(operation, model, builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal
{
Expand Down Expand Up @@ -48,10 +47,6 @@ public SqliteByteArrayMethodTranslator(
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(method, nameof(method));
Check.NotNull(arguments, nameof(arguments));
Check.NotNull(logger, nameof(logger));

if (method.IsGenericMethod
&& method.GetGenericMethodDefinition().Equals(EnumerableMethods.Contains)
&& arguments[0].Type == typeof(byte[]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal
{
Expand Down Expand Up @@ -51,10 +50,6 @@ public SqliteCharMethodTranslator(ISqlExpressionFactory sqlExpressionFactory)
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(method, nameof(method));
Check.NotNull(arguments, nameof(arguments));
Check.NotNull(logger, nameof(logger));

if (_supportedMethods.TryGetValue(method, out var sqlFunctionName))
{
return _sqlExpressionFactory.Function(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal
{
Expand Down Expand Up @@ -53,12 +52,7 @@ public SqliteDateOnlyMemberTranslator(ISqlExpressionFactory sqlExpressionFactory
MemberInfo member,
Type returnType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(member, nameof(member));
Check.NotNull(returnType, nameof(returnType));
Check.NotNull(logger, nameof(logger));

return member.DeclaringType == typeof(DateOnly) && _datePartMapping.TryGetValue(member.Name, out var datePart)
=> member.DeclaringType == typeof(DateOnly) && _datePartMapping.TryGetValue(member.Name, out var datePart)
? _sqlExpressionFactory.Convert(
SqliteExpression.Strftime(
_sqlExpressionFactory,
Expand All @@ -67,6 +61,5 @@ public SqliteDateOnlyMemberTranslator(ISqlExpressionFactory sqlExpressionFactory
instance!),
returnType)
: null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal
{
Expand Down Expand Up @@ -63,17 +62,11 @@ public SqliteDateTimeAddTranslator(ISqlExpressionFactory sqlExpressionFactory)
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(method, nameof(method));
Check.NotNull(arguments, nameof(arguments));
Check.NotNull(logger, nameof(logger));

return method.DeclaringType == typeof(DateTime)
=> method.DeclaringType == typeof(DateTime)
? TranslateDateTime(instance, method, arguments)
: method.DeclaringType == typeof(DateOnly)
? TranslateDateOnly(instance, method, arguments)
: null;
}

private SqlExpression? TranslateDateTime(
SqlExpression? instance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ public SqliteDateTimeMemberTranslator(ISqlExpressionFactory sqlExpressionFactory
Type returnType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(member, nameof(member));
Check.NotNull(returnType, nameof(returnType));
Check.NotNull(logger, nameof(logger));

if (member.DeclaringType == typeof(DateTime))
{
var memberName = member.Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal
{
Expand All @@ -31,7 +30,7 @@ public class SqliteGlobMethodTranslator : IMethodCallTranslator
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public SqliteGlobMethodTranslator(ISqlExpressionFactory sqlExpressionFactory)
=> _sqlExpressionFactory = Check.NotNull(sqlExpressionFactory, nameof(sqlExpressionFactory));
=> _sqlExpressionFactory = sqlExpressionFactory;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -45,10 +44,6 @@ public SqliteGlobMethodTranslator(ISqlExpressionFactory sqlExpressionFactory)
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(method, nameof(method));
Check.NotNull(arguments, nameof(arguments));
Check.NotNull(logger, nameof(logger));

if (method.Equals(_methodInfo))
{
var matchExpression = arguments[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal
{
Expand All @@ -31,7 +30,7 @@ public class SqliteHexMethodTranslator : IMethodCallTranslator
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public SqliteHexMethodTranslator(ISqlExpressionFactory sqlExpressionFactory)
=> _sqlExpressionFactory = Check.NotNull(sqlExpressionFactory, nameof(sqlExpressionFactory));
=> _sqlExpressionFactory = sqlExpressionFactory;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -45,10 +44,6 @@ public SqliteHexMethodTranslator(ISqlExpressionFactory sqlExpressionFactory)
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(method, nameof(method));
Check.NotNull(arguments, nameof(arguments));
Check.NotNull(logger, nameof(logger));

if (method.Equals(_methodInfo))
{
return _sqlExpressionFactory.Function(
Expand Down
5 changes: 0 additions & 5 deletions src/EFCore.Sqlite.Core/Query/Internal/SqliteMathTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal
{
Expand Down Expand Up @@ -81,10 +80,6 @@ public SqliteMathTranslator(ISqlExpressionFactory sqlExpressionFactory)
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(method, nameof(method));
Check.NotNull(arguments, nameof(arguments));
Check.NotNull(logger, nameof(logger));

if (_supportedMethods.TryGetValue(method, out var sqlFunctionName))
{
RelationalTypeMapping? typeMapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal
{
Expand Down Expand Up @@ -63,9 +62,6 @@ public SqliteObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFactory
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(method, nameof(method));
Check.NotNull(arguments, nameof(arguments));

if (instance == null || method.Name != nameof(ToString) || arguments.Count != 0)
{
return null;
Expand Down
16 changes: 2 additions & 14 deletions src/EFCore.Sqlite.Core/Query/Internal/SqliteQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal
{
Expand Down Expand Up @@ -34,14 +33,10 @@ public SqliteQuerySqlGenerator(QuerySqlGeneratorDependencies dependencies)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override string GetOperator(SqlBinaryExpression binaryExpression)
{
Check.NotNull(binaryExpression, nameof(binaryExpression));

return binaryExpression.OperatorType == ExpressionType.Add
=> binaryExpression.OperatorType == ExpressionType.Add
&& binaryExpression.Type == typeof(string)
? " || "
: base.GetOperator(binaryExpression);
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -51,8 +46,6 @@ protected override string GetOperator(SqlBinaryExpression binaryExpression)
/// </summary>
protected override void GenerateLimitOffset(SelectExpression selectExpression)
{
Check.NotNull(selectExpression, nameof(selectExpression));

if (selectExpression.Limit != null
|| selectExpression.Offset != null)
{
Expand All @@ -79,12 +72,7 @@ protected override void GenerateLimitOffset(SelectExpression selectExpression)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override void GenerateSetOperationOperand(SetOperationBase setOperation, SelectExpression operand)
{
Check.NotNull(setOperation, nameof(setOperation));
Check.NotNull(operand, nameof(operand));

// Sqlite doesn't support parentheses around set operation operands
Visit(operand);
}
=> Visit(operand);
}
}
Loading

0 comments on commit 2314a1d

Please sign in to comment.