Skip to content

Commit

Permalink
Fix dotnet#3110 - Move logic from ISqlStatementExecutor into IRelatio…
Browse files Browse the repository at this point in the history
…nalCommand

Allow caller to manually control connection Open / Close
Remove CreateDbCommand from IRelationalCommand for dotnet#3112
  • Loading branch information
mikary committed Oct 22, 2015
1 parent 6f82026 commit 3d74c12
Showing 33 changed files with 658 additions and 562 deletions.
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ public class SqlServerHistoryRepository : HistoryRepository
{
public SqlServerHistoryRepository(
[NotNull] IDatabaseCreator databaseCreator,
[NotNull] ISqlStatementExecutor executor,
[NotNull] ISqlCommandBuilder sqlCommandBuilder,
[NotNull] ISqlServerConnection connection,
[NotNull] IDbContextOptions options,
[NotNull] IMigrationsModelDiffer modelDiffer,
@@ -26,7 +26,7 @@ public SqlServerHistoryRepository(
[NotNull] ISqlGenerator sqlGenerator)
: base(
databaseCreator,
executor,
sqlCommandBuilder,
connection,
options,
modelDiffer,
Original file line number Diff line number Diff line change
@@ -9,31 +9,37 @@
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Migrations.Operations;
using Microsoft.Data.Entity.Utilities;

namespace Microsoft.Data.Entity.Storage.Internal
{
public class SqlServerDatabaseCreator : RelationalDatabaseCreator
{
private readonly ISqlServerConnection _connection;
private readonly IMigrationsSqlGenerator _migrationsSqlGenerator;
private readonly ISqlCommandBuilder _sqlCommandBuilder;

public SqlServerDatabaseCreator(
[NotNull] ISqlServerConnection connection,
[NotNull] IMigrationsModelDiffer modelDiffer,
[NotNull] IMigrationsSqlGenerator migrationsSqlGenerator,
[NotNull] ISqlStatementExecutor statementExecutor,
[NotNull] IModel model)
: base(model, connection, modelDiffer, migrationsSqlGenerator, statementExecutor)
[NotNull] IModel model,
[NotNull] ISqlCommandBuilder sqlCommandBuilder)
: base(model, connection, modelDiffer, migrationsSqlGenerator)
{
Check.NotNull(sqlCommandBuilder, nameof(sqlCommandBuilder));

_connection = connection;
_migrationsSqlGenerator = migrationsSqlGenerator;
_sqlCommandBuilder = sqlCommandBuilder;
}

public override void Create()
{
using (var masterConnection = _connection.CreateMasterConnection())
{
SqlStatementExecutor.ExecuteNonQuery(masterConnection, CreateCreateOperations());
CreateCreateOperations().ExecuteNonQuery(masterConnection);

ClearPool();
}

@@ -44,23 +50,23 @@ public override void Create()
{
using (var masterConnection = _connection.CreateMasterConnection())
{
await SqlStatementExecutor
.ExecuteNonQueryAsync(masterConnection, CreateCreateOperations(), cancellationToken);
await CreateCreateOperations().ExecuteNonQueryAsync(masterConnection, cancellationToken);

ClearPool();
}

await ExistsAsync(retryOnNotExists: true, cancellationToken: cancellationToken);
}

protected override bool HasTables()
=> (int)SqlStatementExecutor.ExecuteScalar(_connection, CreateHasTablesCommand()) != 0;
=> (int)CreateHasTablesCommand().ExecuteScalar(_connection) != 0;

protected override async Task<bool> HasTablesAsync(CancellationToken cancellationToken = default(CancellationToken))
=> (int)(await SqlStatementExecutor
.ExecuteScalarAsync(_connection, CreateHasTablesCommand(), cancellationToken)) != 0;
=> (int)(await CreateHasTablesCommand().ExecuteScalarAsync(_connection, cancellationToken)) != 0;

private string CreateHasTablesCommand()
=> "IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE') SELECT 1 ELSE SELECT 0";
private IRelationalCommand CreateHasTablesCommand()
=> _sqlCommandBuilder
.Build("IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE') SELECT 1 ELSE SELECT 0");

private IEnumerable<IRelationalCommand> CreateCreateOperations()
=> _migrationsSqlGenerator.Generate(new[] { new SqlServerCreateDatabaseOperation { Name = _connection.DbConnection.Database } });
@@ -161,8 +167,7 @@ public override void Delete()

using (var masterConnection = _connection.CreateMasterConnection())
{
SqlStatementExecutor
.ExecuteNonQuery(masterConnection, CreateDropCommands());
CreateDropCommands().ExecuteNonQuery(masterConnection);
}
}

@@ -172,7 +177,7 @@ public override void Delete()

using (var masterConnection = _connection.CreateMasterConnection())
{
await SqlStatementExecutor.ExecuteNonQueryAsync(masterConnection, CreateDropCommands(), cancellationToken);
await CreateDropCommands().ExecuteNonQueryAsync(masterConnection, cancellationToken);
}
}

Original file line number Diff line number Diff line change
@@ -7,41 +7,40 @@
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Storage;
using Microsoft.Data.Entity.Storage.Internal;
using Microsoft.Data.Entity.Update;
using Microsoft.Data.Entity.Update.Internal;
using Microsoft.Data.Entity.Utilities;

namespace Microsoft.Data.Entity.ValueGeneration.Internal
{
public class SqlServerSequenceHiLoValueGenerator<TValue> : HiLoValueGenerator<TValue>
{
private readonly ISqlStatementExecutor _executor;
private readonly ISqlCommandBuilder _sqlCommandBuilder;
private readonly ISqlServerUpdateSqlGenerator _sqlGenerator;
private readonly ISqlServerConnection _connection;
private readonly ISequence _sequence;

public SqlServerSequenceHiLoValueGenerator(
[NotNull] ISqlStatementExecutor executor,
[NotNull] ISqlCommandBuilder sqlCommandBuilder,
[NotNull] ISqlServerUpdateSqlGenerator sqlGenerator,
[NotNull] SqlServerSequenceValueGeneratorState generatorState,
[NotNull] ISqlServerConnection connection)
: base(generatorState)
{
Check.NotNull(executor, nameof(executor));
Check.NotNull(sqlCommandBuilder, nameof(sqlCommandBuilder));
Check.NotNull(sqlGenerator, nameof(sqlGenerator));
Check.NotNull(connection, nameof(connection));

_sequence = generatorState.Sequence;
_executor = executor;
_sqlCommandBuilder = sqlCommandBuilder;
_sqlGenerator = sqlGenerator;
_connection = connection;
}

protected override long GetNewLowValue()
=> (long)Convert.ChangeType(
_executor.ExecuteScalar(
_connection,
_sqlGenerator.GenerateNextSequenceValueOperation(_sequence.Name, _sequence.Schema)),
_sqlCommandBuilder
.Build(_sqlGenerator.GenerateNextSequenceValueOperation(_sequence.Name, _sequence.Schema))
.ExecuteScalar(_connection),
typeof(long),
CultureInfo.InvariantCulture);

Original file line number Diff line number Diff line change
@@ -14,17 +14,17 @@ namespace Microsoft.Data.Entity.ValueGeneration.Internal
{
public class SqlServerSequenceValueGeneratorFactory : ISqlServerSequenceValueGeneratorFactory
{
private readonly ISqlStatementExecutor _executor;
private readonly ISqlCommandBuilder _sqlCommandBuilder;
private readonly ISqlServerUpdateSqlGenerator _sqlGenerator;

public SqlServerSequenceValueGeneratorFactory(
[NotNull] ISqlStatementExecutor executor,
[NotNull] ISqlCommandBuilder sqlCommandBuilder,
[NotNull] ISqlServerUpdateSqlGenerator sqlGenerator)
{
Check.NotNull(executor, nameof(executor));
Check.NotNull(sqlCommandBuilder, nameof(sqlCommandBuilder));
Check.NotNull(sqlGenerator, nameof(sqlGenerator));

_executor = executor;
_sqlCommandBuilder = sqlCommandBuilder;
_sqlGenerator = sqlGenerator;
}

@@ -38,47 +38,47 @@ public virtual ValueGenerator Create(IProperty property, SqlServerSequenceValueG

if (type == typeof(long))
{
return new SqlServerSequenceHiLoValueGenerator<long>(_executor, _sqlGenerator, generatorState, connection);
return new SqlServerSequenceHiLoValueGenerator<long>(_sqlCommandBuilder, _sqlGenerator, generatorState, connection);
}

if (type == typeof(int))
{
return new SqlServerSequenceHiLoValueGenerator<int>(_executor, _sqlGenerator, generatorState, connection);
return new SqlServerSequenceHiLoValueGenerator<int>(_sqlCommandBuilder, _sqlGenerator, generatorState, connection);
}

if (type == typeof(short))
{
return new SqlServerSequenceHiLoValueGenerator<short>(_executor, _sqlGenerator, generatorState, connection);
return new SqlServerSequenceHiLoValueGenerator<short>(_sqlCommandBuilder, _sqlGenerator, generatorState, connection);
}

if (type == typeof(byte))
{
return new SqlServerSequenceHiLoValueGenerator<byte>(_executor, _sqlGenerator, generatorState, connection);
return new SqlServerSequenceHiLoValueGenerator<byte>(_sqlCommandBuilder, _sqlGenerator, generatorState, connection);
}

if (type == typeof(char))
{
return new SqlServerSequenceHiLoValueGenerator<char>(_executor, _sqlGenerator, generatorState, connection);
return new SqlServerSequenceHiLoValueGenerator<char>(_sqlCommandBuilder, _sqlGenerator, generatorState, connection);
}

if (type == typeof(ulong))
{
return new SqlServerSequenceHiLoValueGenerator<ulong>(_executor, _sqlGenerator, generatorState, connection);
return new SqlServerSequenceHiLoValueGenerator<ulong>(_sqlCommandBuilder, _sqlGenerator, generatorState, connection);
}

if (type == typeof(uint))
{
return new SqlServerSequenceHiLoValueGenerator<uint>(_executor, _sqlGenerator, generatorState, connection);
return new SqlServerSequenceHiLoValueGenerator<uint>(_sqlCommandBuilder, _sqlGenerator, generatorState, connection);
}

if (type == typeof(ushort))
{
return new SqlServerSequenceHiLoValueGenerator<ushort>(_executor, _sqlGenerator, generatorState, connection);
return new SqlServerSequenceHiLoValueGenerator<ushort>(_sqlCommandBuilder, _sqlGenerator, generatorState, connection);
}

if (type == typeof(sbyte))
{
return new SqlServerSequenceHiLoValueGenerator<sbyte>(_executor, _sqlGenerator, generatorState, connection);
return new SqlServerSequenceHiLoValueGenerator<sbyte>(_sqlCommandBuilder, _sqlGenerator, generatorState, connection);
}

throw new ArgumentException(CoreStrings.InvalidValueGeneratorFactoryProperty(
Original file line number Diff line number Diff line change
@@ -230,11 +230,11 @@
<Compile Include="Storage\IRelationalValueBufferFactoryFactory.cs" />
<Compile Include="Storage\ISqlCommandBuilder.cs" />
<Compile Include="Storage\ISqlGenerator.cs" />
<Compile Include="Storage\ISqlStatementExecutor.cs" />
<Compile Include="Storage\Internal\RelationalCommand.cs" />
<Compile Include="Storage\Internal\RelationalCommandBuilder.cs" />
<Compile Include="Storage\Internal\RelationalCommandBuilderFactory.cs" />
<Compile Include="Storage\RelationalCommandBuilderExtensions.cs" />
<Compile Include="Storage\RelationalCommandExtensions.cs" />
<Compile Include="Storage\RelationalConnection.cs" />
<Compile Include="Storage\RelationalDatabase.cs" />
<Compile Include="Storage\RelationalDatabaseCreator.cs" />
@@ -248,7 +248,6 @@
<Compile Include="Storage\RelationalTypeMapperExtensions.cs" />
<Compile Include="Storage\RelationalTypeMapping.cs" />
<Compile Include="Storage\Internal\SqlCommandBuilder.cs" />
<Compile Include="Storage\SqlStatementExecutor.cs" />
<Compile Include="Storage\TypedRelationalValueBufferFactoryFactory.cs" />
<Compile Include="Storage\UntypedRelationalValueBufferFactoryFactory.cs" />
<Compile Include="Query\AsyncQueryMethodProvider.cs" />
Original file line number Diff line number Diff line change
@@ -53,13 +53,11 @@ public static EntityFrameworkServicesBuilder AddRelational([NotNull] this Entity
.AddScoped<RelationalSqlExecutor>()
.AddScoped<IRelationalCommandBuilderFactory, RelationalCommandBuilderFactory>()
.AddScoped<ISqlCommandBuilder, SqlCommandBuilder>()
.AddScoped<SqlStatementExecutor>()
.AddScoped<CommandBatchPreparer>()
.AddScoped<IMigrationsModelDiffer, MigrationsModelDiffer>()
.AddScoped<MigrationsSqlGenerator>()
.AddScoped(typeof(ISensitiveDataLogger<>), typeof(SensitiveDataLogger<>))
.AddScoped(p => GetProviderServices(p).ParameterNameGeneratorFactory)
.AddScoped(p => GetProviderServices(p).SqlStatementExecutor)
.AddScoped(p => GetProviderServices(p).SqlGenerator)
.AddScoped(p => GetProviderServices(p).CompositeMethodCallTranslator)
.AddScoped(p => GetProviderServices(p).CompositeMemberTranslator)
Original file line number Diff line number Diff line change
@@ -12,45 +12,34 @@ namespace Microsoft.Data.Entity.Infrastructure
public class RelationalSqlExecutor
{
private readonly ISqlCommandBuilder _sqlCommandBuilder;
private readonly ISqlStatementExecutor _statementExecutor;
private readonly IRelationalConnection _connection;

public RelationalSqlExecutor(
[NotNull] ISqlCommandBuilder sqlCommandBuilder,
[NotNull] ISqlStatementExecutor statementExecutor,
[NotNull] IRelationalConnection connection)
{
Check.NotNull(sqlCommandBuilder, nameof(sqlCommandBuilder));
Check.NotNull(statementExecutor, nameof(statementExecutor));
Check.NotNull(connection, nameof(connection));

_sqlCommandBuilder = sqlCommandBuilder;
_statementExecutor = statementExecutor;
_connection = connection;
}

public virtual void ExecuteSqlCommand([NotNull] string sql, [NotNull] params object[] parameters)
=> _statementExecutor.ExecuteNonQuery(
_connection,
new[]
{
_sqlCommandBuilder.Build(
=> _sqlCommandBuilder
.Build(
Check.NotNull(sql, nameof(sql)),
Check.NotNull(parameters, nameof(parameters)))
});
.ExecuteNonQuery(_connection);

public virtual async Task ExecuteSqlCommandAsync(
[NotNull] string sql,
CancellationToken cancellationToken = default(CancellationToken),
[NotNull] params object[] parameters)
=> await _statementExecutor.ExecuteNonQueryAsync(
_connection,
new[]
{
_sqlCommandBuilder.Build(
Check.NotNull(sql, nameof(sql)),
Check.NotNull(parameters, nameof(parameters)))
},
cancellationToken);
=> await _sqlCommandBuilder
.Build(
Check.NotNull(sql, nameof(sql)),
Check.NotNull(parameters, nameof(parameters)))
.ExecuteNonQueryAsync(_connection, cancellationToken);
}
}
Loading

0 comments on commit 3d74c12

Please sign in to comment.