Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Apr 21, 2019
1 parent a6d351c commit 31c48ff
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 76 deletions.
5 changes: 2 additions & 3 deletions src/EFCore.PG/Query/Sql/Internal/NpgsqlQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public NpgsqlQuerySqlGenerator(
[NotNull] QuerySqlGeneratorDependencies dependencies,
[NotNull] SelectExpression selectExpression,
bool reverseNullOrderingEnabled,
Version postgresVersion,
DiagnosticsLoggers loggers)
: base(dependencies, selectExpression, loggers)
Version postgresVersion)
: base(dependencies, selectExpression)
{
_reverseNullOrderingEnabled = reverseNullOrderingEnabled;
_postgresVersion = postgresVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ public NpgsqlQuerySqlGeneratorFactory(
=> _npgsqlOptions = Check.NotNull(npgsqlOptions, nameof(npgsqlOptions));

/// <inheritdoc />
public override IQuerySqlGenerator CreateDefault(SelectExpression selectExpression, DiagnosticsLoggers loggers)
public override IQuerySqlGenerator CreateDefault(SelectExpression selectExpression)
=> new NpgsqlQuerySqlGenerator(
Dependencies,
Check.NotNull(selectExpression, nameof(selectExpression)),
_npgsqlOptions.ReverseNullOrderingEnabled,
_npgsqlOptions.PostgresVersion,
loggers);
_npgsqlOptions.PostgresVersion);
}
}
4 changes: 2 additions & 2 deletions src/EFCore.PG/Storage/Internal/NpgsqlDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ private Task<bool> ExistsAsync(bool retryOnNotExists, CancellationToken cancella
}, cancellationToken);

protected override bool HasTables()
=> (bool)CreateHasTablesCommand().ExecuteScalar(_connection);
=> (bool)CreateHasTablesCommand().ExecuteScalar(_connection, null, Dependencies.CommandLogger);

protected override async Task<bool> HasTablesAsync(CancellationToken cancellationToken = default)
=> (bool)(await CreateHasTablesCommand().ExecuteScalarAsync(_connection, cancellationToken: cancellationToken));
=> (bool)(await CreateHasTablesCommand().ExecuteScalarAsync(_connection, null, Dependencies.CommandLogger, cancellationToken));

IRelationalCommand CreateHasTablesCommand()
=> _rawSqlCommandBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;

Expand All @@ -18,6 +21,8 @@ public interface INpgsqlSequenceValueGeneratorFactory
ValueGenerator Create(
[NotNull] IProperty property,
[NotNull] NpgsqlSequenceValueGeneratorState generatorState,
[NotNull] INpgsqlRelationalConnection connection);
[NotNull] INpgsqlRelationalConnection connection,
[NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder,
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Database.Command> commandLogger);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.ValueGeneration;

namespace Npgsql.EntityFrameworkCore.PostgreSQL.ValueGeneration.Internal
Expand All @@ -14,6 +15,8 @@ public interface INpgsqlValueGeneratorCache : IValueGeneratorCache
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
NpgsqlSequenceValueGeneratorState GetOrAddSequenceState([NotNull] IProperty property);
NpgsqlSequenceValueGeneratorState GetOrAddSequenceState(
[NotNull] IProperty property,
[NotNull] IRelationalConnection connection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Update;
Expand All @@ -22,6 +24,7 @@ public class NpgsqlSequenceHiLoValueGenerator<TValue> : HiLoValueGenerator<TValu
readonly IUpdateSqlGenerator _sqlGenerator;
readonly INpgsqlRelationalConnection _connection;
readonly ISequence _sequence;
readonly IDiagnosticsLogger<DbLoggerCategory.Database.Command> _commandLogger;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
Expand All @@ -31,17 +34,15 @@ public NpgsqlSequenceHiLoValueGenerator(
[NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder,
[NotNull] IUpdateSqlGenerator sqlGenerator,
[NotNull] NpgsqlSequenceValueGeneratorState generatorState,
[NotNull] INpgsqlRelationalConnection connection)
[NotNull] INpgsqlRelationalConnection connection,
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Database.Command> commandLogger)
: base(generatorState)
{
Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder));
Check.NotNull(sqlGenerator, nameof(sqlGenerator));
Check.NotNull(connection, nameof(connection));

_sequence = generatorState.Sequence;
_rawSqlCommandBuilder = rawSqlCommandBuilder;
_sqlGenerator = sqlGenerator;
_connection = connection;
_commandLogger = commandLogger;
}

/// <summary>
Expand All @@ -52,7 +53,7 @@ protected override long GetNewLowValue()
=> (long)Convert.ChangeType(
_rawSqlCommandBuilder
.Build(_sqlGenerator.GenerateNextSequenceValueOperation(_sequence.Name, _sequence.Schema))
.ExecuteScalar(_connection),
.ExecuteScalar(_connection, null, _commandLogger),
typeof(long),
CultureInfo.InvariantCulture);

Expand All @@ -64,7 +65,7 @@ protected override long GetNewLowValue()
=> (long)Convert.ChangeType(
await _rawSqlCommandBuilder
.Build(_sqlGenerator.GenerateNextSequenceValueOperation(_sequence.Name, _sequence.Schema))
.ExecuteScalarAsync(_connection, cancellationToken: cancellationToken),
.ExecuteScalarAsync(_connection, null, _commandLogger, cancellationToken: cancellationToken),
typeof(long),
CultureInfo.InvariantCulture);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,79 +19,76 @@ namespace Npgsql.EntityFrameworkCore.PostgreSQL.ValueGeneration.Internal
/// </summary>
public class NpgsqlSequenceValueGeneratorFactory : INpgsqlSequenceValueGeneratorFactory
{
private readonly IRawSqlCommandBuilder _rawSqlCommandBuilder;
private readonly IUpdateSqlGenerator _sqlGenerator;
readonly IUpdateSqlGenerator _sqlGenerator;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public NpgsqlSequenceValueGeneratorFactory(
[NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder,
[NotNull] IUpdateSqlGenerator sqlGenerator)
{
Check.NotNull(rawSqlCommandBuilder, nameof(rawSqlCommandBuilder));
Check.NotNull(sqlGenerator, nameof(sqlGenerator));

_rawSqlCommandBuilder = rawSqlCommandBuilder;
_sqlGenerator = sqlGenerator;
}

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual ValueGenerator Create(IProperty property, NpgsqlSequenceValueGeneratorState generatorState, INpgsqlRelationalConnection connection)
public virtual ValueGenerator Create(
IProperty property,
NpgsqlSequenceValueGeneratorState generatorState,
INpgsqlRelationalConnection connection,
IRawSqlCommandBuilder rawSqlCommandBuilder,
IDiagnosticsLogger<DbLoggerCategory.Database.Command> commandLogger)
{
Check.NotNull(property, nameof(property));
Check.NotNull(generatorState, nameof(generatorState));
Check.NotNull(connection, nameof(connection));

var type = property.ClrType.UnwrapNullableType().UnwrapEnumType();

if (type == typeof(long))
{
return new NpgsqlSequenceHiLoValueGenerator<long>(_rawSqlCommandBuilder, _sqlGenerator, generatorState, connection);
return new NpgsqlSequenceHiLoValueGenerator<long>(rawSqlCommandBuilder, _sqlGenerator, generatorState, connection, commandLogger);
}

if (type == typeof(int))
{
return new NpgsqlSequenceHiLoValueGenerator<int>(_rawSqlCommandBuilder, _sqlGenerator, generatorState, connection);
return new NpgsqlSequenceHiLoValueGenerator<int>(rawSqlCommandBuilder, _sqlGenerator, generatorState, connection, commandLogger);
}

if (type == typeof(short))
{
return new NpgsqlSequenceHiLoValueGenerator<short>(_rawSqlCommandBuilder, _sqlGenerator, generatorState, connection);
return new NpgsqlSequenceHiLoValueGenerator<short>(rawSqlCommandBuilder, _sqlGenerator, generatorState, connection, commandLogger);
}

if (type == typeof(byte))
{
return new NpgsqlSequenceHiLoValueGenerator<byte>(_rawSqlCommandBuilder, _sqlGenerator, generatorState, connection);
return new NpgsqlSequenceHiLoValueGenerator<byte>(rawSqlCommandBuilder, _sqlGenerator, generatorState, connection, commandLogger);
}

if (type == typeof(char))
{
return new NpgsqlSequenceHiLoValueGenerator<char>(_rawSqlCommandBuilder, _sqlGenerator, generatorState, connection);
return new NpgsqlSequenceHiLoValueGenerator<char>(rawSqlCommandBuilder, _sqlGenerator, generatorState, connection, commandLogger);
}

if (type == typeof(ulong))
{
return new NpgsqlSequenceHiLoValueGenerator<ulong>(_rawSqlCommandBuilder, _sqlGenerator, generatorState, connection);
return new NpgsqlSequenceHiLoValueGenerator<ulong>(rawSqlCommandBuilder, _sqlGenerator, generatorState, connection, commandLogger);
}

if (type == typeof(uint))
{
return new NpgsqlSequenceHiLoValueGenerator<uint>(_rawSqlCommandBuilder, _sqlGenerator, generatorState, connection);
return new NpgsqlSequenceHiLoValueGenerator<uint>(rawSqlCommandBuilder, _sqlGenerator, generatorState, connection, commandLogger);
}

if (type == typeof(ushort))
{
return new NpgsqlSequenceHiLoValueGenerator<ushort>(_rawSqlCommandBuilder, _sqlGenerator, generatorState, connection);
return new NpgsqlSequenceHiLoValueGenerator<ushort>(rawSqlCommandBuilder, _sqlGenerator, generatorState, connection, commandLogger);
}

if (type == typeof(sbyte))
{
return new NpgsqlSequenceHiLoValueGenerator<sbyte>(_rawSqlCommandBuilder, _sqlGenerator, generatorState, connection);
return new NpgsqlSequenceHiLoValueGenerator<sbyte>(rawSqlCommandBuilder, _sqlGenerator, generatorState, connection, commandLogger);
}

throw new ArgumentException(CoreStrings.InvalidValueGeneratorFactoryProperty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using Npgsql.EntityFrameworkCore.PostgreSQL.Utilities;

Expand All @@ -14,7 +15,7 @@ namespace Npgsql.EntityFrameworkCore.PostgreSQL.ValueGeneration.Internal
/// </summary>
public class NpgsqlValueGeneratorCache : ValueGeneratorCache, INpgsqlValueGeneratorCache
{
private readonly ConcurrentDictionary<string, NpgsqlSequenceValueGeneratorState> _sequenceGeneratorCache
readonly ConcurrentDictionary<string, NpgsqlSequenceValueGeneratorState> _sequenceGeneratorCache
= new ConcurrentDictionary<string, NpgsqlSequenceValueGeneratorState>();

/// <summary>
Expand All @@ -30,10 +31,10 @@ public NpgsqlValueGeneratorCache([NotNull] ValueGeneratorCacheDependencies depen
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual NpgsqlSequenceValueGeneratorState GetOrAddSequenceState(IProperty property)
public virtual NpgsqlSequenceValueGeneratorState GetOrAddSequenceState(
IProperty property,
IRelationalConnection connection)
{
Check.NotNull(property, nameof(property));

var sequence = property.Npgsql().FindHiLoSequence();

Debug.Assert(sequence != null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;
Expand All @@ -12,17 +14,22 @@ namespace Npgsql.EntityFrameworkCore.PostgreSQL.ValueGeneration.Internal
public class NpgsqlValueGeneratorSelector : RelationalValueGeneratorSelector
{
readonly INpgsqlSequenceValueGeneratorFactory _sequenceFactory;

readonly INpgsqlRelationalConnection _connection;
readonly IRawSqlCommandBuilder _rawSqlCommandBuilder;
readonly IDiagnosticsLogger<DbLoggerCategory.Database.Command> _commandLogger;

public NpgsqlValueGeneratorSelector(
[NotNull] ValueGeneratorSelectorDependencies dependencies,
[NotNull] INpgsqlSequenceValueGeneratorFactory sequenceFactory,
[NotNull] INpgsqlRelationalConnection connection)
[NotNull] INpgsqlRelationalConnection connection,
[NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder,
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Database.Command> commandLogger)
: base(dependencies)
{
_sequenceFactory = sequenceFactory;
_connection = connection;
_rawSqlCommandBuilder = rawSqlCommandBuilder;
_commandLogger = commandLogger;
}

/// <summary>
Expand All @@ -38,7 +45,12 @@ public override ValueGenerator Select(IProperty property, IEntityType entityType

return property.GetValueGeneratorFactory() == null
&& property.Npgsql().ValueGenerationStrategy == NpgsqlValueGenerationStrategy.SequenceHiLo
? _sequenceFactory.Create(property, Cache.GetOrAddSequenceState(property), _connection)
? _sequenceFactory.Create(
property,
Cache.GetOrAddSequenceState(property, _connection),
_connection,
_rawSqlCommandBuilder,
_commandLogger)
: base.Select(property, entityType);
}

Expand Down
Loading

0 comments on commit 31c48ff

Please sign in to comment.