Skip to content
Closed
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
12 changes: 12 additions & 0 deletions src/Delta.EF/Delta.EF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,17 @@
<Compile Include="..\Delta\DeltaExtensions_Shared.cs">
<Link>DeltaExtensions_Shared.cs</Link>
</Compile>
<Compile Include="..\Delta/TimestampQueries/NpgsqlTimestampQueryExecutor.cs">
<Link>TimestampQueries/NpgsqlTimestampQueryExecutor.cs</Link>
</Compile>
<Compile Include="..\Delta/TimestampQueries/SqlServerTimestampQueryExecutor.cs">
<Link>TimestampQueries/SqlServerTimestampQueryExecutor.cs</Link>
</Compile>
<Compile Include="..\Delta/TimestampQueries/TimestampQueryExecutorFactory.cs">
<Link>TimestampQueries/TimestampQueryExecutorFactory.cs</Link>
</Compile>
<Compile Include="..\Delta/TimestampQueries/ITimestampQueryExecutor.cs">
<Link>TimestampQueries/ITimestampQueryExecutor.cs</Link>
</Compile>
</ItemGroup>
</Project>
40 changes: 2 additions & 38 deletions src/Delta/DeltaExtensions_Sql.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// ReSharper disable UseRawString

namespace Delta;

public static partial class DeltaExtensions
Expand Down Expand Up @@ -39,41 +37,7 @@ public static async Task<string> GetLastTimeStamp(this DbConnection connection,

static async Task<string> ExecuteTimestampQuery(DbCommand command, Cancel cancel = default)
{
var name = command.GetType().Name;
if (name == "SqlCommand")
{
command.CommandText = @"
-- begin-snippet: SqlServerTimestamp
declare @changeTracking bigint = change_tracking_current_version();
declare @timeStamp bigint = convert(bigint, @@dbts);

if (@changeTracking is null)
select cast(@timeStamp as varchar)
else
select cast(@timeStamp as varchar) + '-' + cast(@changeTracking as varchar)
-- end-snippet
";
return (string) (await command.ExecuteScalarAsync(cancel))!;
}

if (name == "NpgsqlCommand")
{
command.CommandText = @"
-- begin-snippet: PostgresTimestamp
select pg_last_committed_xact();
-- end-snippet
";
var result = (object[]?) await command.ExecuteScalarAsync(cancel);
// null on first run after SET track_commit_timestamp to 'on'
if (result is null)
{
return string.Empty;
}

var xid = (uint) result[0];
return xid.ToString();
}

throw new("Unsupported type " + name);
var timestampQueryExecutor = TimestampQueryExecutorFactory.Create(command);
return await timestampQueryExecutor.Execute(command, cancel);
}
}
6 changes: 6 additions & 0 deletions src/Delta/TimestampQueries/ITimestampQueryExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Delta;

interface ITimestampQueryExecutor
{
Task<string> Execute(DbCommand command, Cancel cancel);
}
23 changes: 23 additions & 0 deletions src/Delta/TimestampQueries/NpgsqlTimestampQueryExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ReSharper disable UseRawString

namespace Delta;
class NpgsqlTimestampQueryExecutor : ITimestampQueryExecutor
{
public async Task<string> Execute(DbCommand command, Cancel cancel)
{
command.CommandText = @"
-- begin-snippet: PostgresTimestamp
select pg_last_committed_xact();
-- end-snippet
";
var result = (object[]?)await command.ExecuteScalarAsync(cancel);
// null on first run after SET track_commit_timestamp to 'on'
if (result is null)
{
return string.Empty;
}

var xid = (uint)result[0];
return xid.ToString();
}
}
21 changes: 21 additions & 0 deletions src/Delta/TimestampQueries/SqlServerTimestampQueryExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// ReSharper disable UseRawString

namespace Delta;
class SqlServerTimestampQueryExecutor : ITimestampQueryExecutor
{
public async Task<string> Execute(DbCommand command, Cancel cancel)
{
command.CommandText = @"
-- begin-snippet: SqlServerTimestamp
declare @changeTracking bigint = change_tracking_current_version();
declare @timeStamp bigint = convert(bigint, @@dbts);

if (@changeTracking is null)
select cast(@timeStamp as varchar)
else
select cast(@timeStamp as varchar) + '-' + cast(@changeTracking as varchar)
-- end-snippet
";
return (string)(await command.ExecuteScalarAsync(cancel))!;
}
}
12 changes: 12 additions & 0 deletions src/Delta/TimestampQueries/TimestampQueryExecutorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Delta;

static class TimestampQueryExecutorFactory
{
public static ITimestampQueryExecutor Create(DbCommand command) =>
command.GetType().Name switch
{
"SqlCommand" => new SqlServerTimestampQueryExecutor(),
"NpgsqlCommand" => new NpgsqlTimestampQueryExecutor(),
_ => throw new NotSupportedException("Unsupported type " + command.GetType().Name)
};
}