Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
namespace EntityFrameworkCore.Extensions.Sequences.PostgreSQL.Internal
{
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal;
using DbFunctionsExtensions = EntityFrameworkCore.Extensions.Sequences.DbFunctionsExtensions;

internal class NpgsqlSequenceTranslator : IMethodCallTranslator
{
private static readonly MethodInfo MethodLong = typeof(DbFunctionsExtensions)
.GetMethod(nameof(DbFunctionsExtensions.NextValLong), new[] {typeof(DbFunctions), typeof(string)});

private readonly NpgsqlSqlExpressionFactory factory;

public NpgsqlSequenceTranslator([NotNull] NpgsqlSqlExpressionFactory factory)
{
this.factory = factory;
}
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Npgsql.EntityFrameworkCore.PostgreSQL.Query;
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal;
using DbFunctionsExtensions = EntityFrameworkCore.Extensions.Sequences.DbFunctionsExtensions;

public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments)
internal class NpgsqlSequenceTranslator : IMethodCallTranslator
{

if (method.Equals(NpgsqlSequenceTranslator.MethodLong))
{
return this.factory.Function("nextval",
arguments.Skip(1),
method.ReturnType);
}

return null;
private static readonly MethodInfo MethodLong = typeof(DbFunctionsExtensions)
.GetMethod(nameof(DbFunctionsExtensions.NextValLong), new[] { typeof(DbFunctions), typeof(string) });

private readonly NpgsqlSqlExpressionFactory factory;

public NpgsqlSequenceTranslator([NotNull] NpgsqlSqlExpressionFactory factory)
{
this.factory = factory;
}

public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments)
{

if (method.Equals(NpgsqlSequenceTranslator.MethodLong))
{
return this.factory.Function("nextval",
arguments.Skip(1),
method.ReturnType);
}

return null;
}

public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments, IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
if (method.Equals(NpgsqlSequenceTranslator.MethodLong))
{
return this.factory.Function("nextval",
arguments.Skip(1),
method.ReturnType);
}

return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
Expand All @@ -8,15 +8,15 @@
<RepositoryUrl>https://github.com/moritzrinow/efcore-sequences</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>github</RepositoryType>
<PackageVersion>0.0.2</PackageVersion>
<PackageVersion>5.0.0.1</PackageVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\EntityFrameworkCore.Extensions.Sequences\EntityFrameworkCore.Extensions.Sequences.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public T GetNextSequenceValue<T>(string name) where T : struct
};

var sql =
$"SELECT @result = (NEXT VALUE FOR {name})";
$"SELECT @result = (NEXT VALUE FOR [{name}])";

var res = this.context.Database.ExecuteSqlRaw(sql, new object[] {result});

Expand All @@ -104,7 +104,7 @@ public bool CreateSequence<T>(DbSequence<T> sequence) where T : struct
connection.Open();
}

var name = sequence.Schema != null ? $"{sequence.Schema}.{sequence.Name} " : sequence.Name;
var name = sequence.Schema != null ? $"[{sequence.Schema}].[{sequence.Name}] " : $"[{sequence.Name}]";
var start = sequence.StartValue != null ? $"START WITH {sequence.StartValue} " : "";
var increment = sequence.IncrementBy != null ? $"INCREMENT BY {sequence.IncrementBy} " : "";
var min = sequence.MinValue != null ? $"MINVALUE {sequence.MinValue} " : "NO MINVALUE ";
Expand Down Expand Up @@ -171,7 +171,7 @@ public bool UpdateSequence<T>(string name, DbSequenceUpdate<T> update) where T :
var cache = update.CacheSize != null ? (update.CacheSize.Value > 0 ? $"CACHE {update.CacheSize}" : "NO CACHE") : "";

var sql =
$"ALTER SEQUENCE {name} " +
$"ALTER SEQUENCE [{name}] " +
$"{restart} " +
$"{increment}" +
$"{min} " +
Expand All @@ -196,7 +196,7 @@ public bool DropSequence(string name, bool conditionally)
var sql =
$"DROP SEQUENCE " +
(conditionally ? $"IF EXISTS " : "") +
$"{name}";
$"[{name}]";

var result = this.context.Database.ExecuteSqlRaw(sql);

Expand Down Expand Up @@ -246,7 +246,7 @@ public async Task<T> GetNextSequenceValueAsync<T>(string name, CancellationToken
};

var sql =
$"SELECT @result = (NEXT VALUE FOR {name})";
$"SELECT @result = (NEXT VALUE FOR [{name}])";

var res = await this.context.Database.ExecuteSqlRawAsync(sql, new object[] {result}, ct);

Expand All @@ -271,7 +271,7 @@ public async Task<bool> UpdateSequenceAsync<T>(string name, DbSequenceUpdate<T>
var cache = update.CacheSize != null ? (update.CacheSize.Value > 0 ? $"CACHE {update.CacheSize}" : "NO CACHE") : "";

var sql =
$"ALTER SEQUENCE {name} " +
$"ALTER SEQUENCE [{name}] " +
$"{restart} " +
$"{increment}" +
$"{min} " +
Expand Down
Loading