Skip to content

Commit e5c908b

Browse files
committed
.
1 parent ee78c32 commit e5c908b

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

readme.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,21 @@ AssemblyWriteTime = File.GetLastWriteTime(webAssemblyLocation).Ticks.ToString();
7272
<!-- snippet: SqlServerTimestamp -->
7373
<a id='snippet-SqlServerTimestamp'></a>
7474
```cs
75-
select top 1 [End Time], [Current LSN]
75+
declare @changeTracking bigint = change_tracking_current_version();
76+
declare @timeStamp bigint = convert(bigint, @@dbts);
77+
78+
declare @timeResult varchar;
79+
if (@changeTracking is null)
80+
set @timeResult = cast(@timeStamp as varchar)
81+
else
82+
set @timeResult = cast(@timeStamp as varchar) + '-' + cast(@changeTracking as varchar)
83+
84+
select top 1 [End Time] + '-' + @timeResult, [Current LSN]
7685
from fn_dblog({lsnString}, null)
7786
where Operation = 'LOP_COMMIT_XACT'
7887
order by [End Time] desc;
7988
```
80-
<sup><a href='/src/Delta/DeltaExtensions_Sql.cs#L51-L56' title='Snippet source file'>snippet source</a> | <a href='#snippet-SqlServerTimestamp' title='Start of snippet'>anchor</a></sup>
89+
<sup><a href='/src/Delta/DeltaExtensions_Sql.cs#L51-L65' title='Snippet source file'>snippet source</a> | <a href='#snippet-SqlServerTimestamp' title='Start of snippet'>anchor</a></sup>
8190
<!-- endSnippet -->
8291

8392

@@ -320,7 +329,7 @@ var application = webApplicationBuilder.Build();
320329
application.UseDelta(
321330
getConnection: httpContext => httpContext.RequestServices.GetRequiredService<SqlConnection>());
322331
```
323-
<sup><a href='/src/DeltaTests/Usage.cs#L293-L299' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDiscoveryConnection' title='Start of snippet'>anchor</a></sup>
332+
<sup><a href='/src/DeltaTests/Usage.cs#L291-L297' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDiscoveryConnection' title='Start of snippet'>anchor</a></sup>
324333
<!-- endSnippet -->
325334

326335
To use custom connection and transaction discovery:
@@ -338,7 +347,7 @@ webApplication.UseDelta(
338347
return new(connection, transaction);
339348
});
340349
```
341-
<sup><a href='/src/DeltaTests/Usage.cs#L304-L316' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDiscoveryConnectionAndTransaction' title='Start of snippet'>anchor</a></sup>
350+
<sup><a href='/src/DeltaTests/Usage.cs#L302-L314' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDiscoveryConnectionAndTransaction' title='Start of snippet'>anchor</a></sup>
342351
<!-- endSnippet -->
343352

344353

@@ -525,7 +534,7 @@ Nuget: [Delta.SqlServer](https://www.nuget.org/packages/Delta.SqlServer)
525534
```cs
526535
var timeStamp = await sqlConnection.GetLastTimeStamp();
527536
```
528-
<sup><a href='/src/DeltaTests/Usage.cs#L155-L159' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetLastTimeStampSqlConnection' title='Start of snippet'>anchor</a></sup>
537+
<sup><a href='/src/DeltaTests/Usage.cs#L153-L157' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetLastTimeStampSqlConnection' title='Start of snippet'>anchor</a></sup>
529538
<!-- endSnippet -->
530539

531540

@@ -553,7 +562,7 @@ foreach (var db in trackedDatabases)
553562
Trace.WriteLine(db);
554563
}
555564
```
556-
<sup><a href='/src/DeltaTests/Usage.cs#L191-L199' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetDatabasesWithTracking' title='Start of snippet'>anchor</a></sup>
565+
<sup><a href='/src/DeltaTests/Usage.cs#L189-L197' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetDatabasesWithTracking' title='Start of snippet'>anchor</a></sup>
557566
<!-- endSnippet -->
558567

559568
Uses the following SQL:
@@ -583,7 +592,7 @@ foreach (var db in trackedTables)
583592
Trace.WriteLine(db);
584593
}
585594
```
586-
<sup><a href='/src/DeltaTests/Usage.cs#L217-L225' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetTrackedTables' title='Start of snippet'>anchor</a></sup>
595+
<sup><a href='/src/DeltaTests/Usage.cs#L215-L223' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetTrackedTables' title='Start of snippet'>anchor</a></sup>
587596
<!-- endSnippet -->
588597

589598
Uses the following SQL:
@@ -608,7 +617,7 @@ Determine if change tracking is enabled for a database.
608617
```cs
609618
var isTrackingEnabled = await sqlConnection.IsTrackingEnabled();
610619
```
611-
<sup><a href='/src/DeltaTests/Usage.cs#L282-L286' title='Snippet source file'>snippet source</a> | <a href='#snippet-IsTrackingEnabled' title='Start of snippet'>anchor</a></sup>
620+
<sup><a href='/src/DeltaTests/Usage.cs#L280-L284' title='Snippet source file'>snippet source</a> | <a href='#snippet-IsTrackingEnabled' title='Start of snippet'>anchor</a></sup>
612621
<!-- endSnippet -->
613622

614623
Uses the following SQL:
@@ -635,7 +644,7 @@ Enable change tracking for a database.
635644
```cs
636645
await sqlConnection.EnableTracking();
637646
```
638-
<sup><a href='/src/DeltaTests/Usage.cs#L276-L280' title='Snippet source file'>snippet source</a> | <a href='#snippet-EnableTracking' title='Start of snippet'>anchor</a></sup>
647+
<sup><a href='/src/DeltaTests/Usage.cs#L274-L278' title='Snippet source file'>snippet source</a> | <a href='#snippet-EnableTracking' title='Start of snippet'>anchor</a></sup>
639648
<!-- endSnippet -->
640649

641650
Uses the following SQL:
@@ -663,7 +672,7 @@ Disable change tracking for a database and all tables within that database.
663672
```cs
664673
await sqlConnection.DisableTracking();
665674
```
666-
<sup><a href='/src/DeltaTests/Usage.cs#L261-L265' title='Snippet source file'>snippet source</a> | <a href='#snippet-DisableTracking' title='Start of snippet'>anchor</a></sup>
675+
<sup><a href='/src/DeltaTests/Usage.cs#L259-L263' title='Snippet source file'>snippet source</a> | <a href='#snippet-DisableTracking' title='Start of snippet'>anchor</a></sup>
667676
<!-- endSnippet -->
668677

669678
Uses the following SQL:
@@ -700,7 +709,7 @@ Enables change tracking for all tables listed, and disables change tracking for
700709
```cs
701710
await sqlConnection.SetTrackedTables(["Companies"]);
702711
```
703-
<sup><a href='/src/DeltaTests/Usage.cs#L211-L215' title='Snippet source file'>snippet source</a> | <a href='#snippet-SetTrackedTables' title='Start of snippet'>anchor</a></sup>
712+
<sup><a href='/src/DeltaTests/Usage.cs#L209-L213' title='Snippet source file'>snippet source</a> | <a href='#snippet-SetTrackedTables' title='Start of snippet'>anchor</a></sup>
704713
<!-- endSnippet -->
705714

706715
Uses the following SQL:

src/Delta/DeltaExtensions_Sql.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,23 @@ static async Task<string> ExecuteTimestampQuery(DbCommand command, Cancel cancel
4949
var lsnString = lsn is null ? "null" : $"'0x{lsn}'";
5050
command.CommandText = $@"
5151
-- begin-snippet: SqlServerTimestamp
52-
select top 1 [End Time], [Current LSN]
52+
declare @changeTracking bigint = change_tracking_current_version();
53+
declare @timeStamp bigint = convert(bigint, @@dbts);
54+
55+
declare @timeResult varchar;
56+
if (@changeTracking is null)
57+
set @timeResult = cast(@timeStamp as varchar)
58+
else
59+
set @timeResult = cast(@timeStamp as varchar) + '-' + cast(@changeTracking as varchar)
60+
61+
select top 1 [End Time] + '-' + @timeResult, [Current LSN]
5362
from fn_dblog({lsnString}, null)
5463
where Operation = 'LOP_COMMIT_XACT'
5564
order by [End Time] desc;
5665
-- end-snippet
5766
";
5867

68+
var startNew = Stopwatch.StartNew();
5969
await using var reader = await command.ExecuteReaderAsync(CommandBehavior.SingleRow, cancel);
6070
var readAsync = await reader.ReadAsync(cancel);
6171
// for empty transaction log
@@ -66,6 +76,7 @@ from fn_dblog({lsnString}, null)
6676

6777
var endTime = (string)reader[0];
6878
lsn = (string)reader[1];
79+
Debug.WriteLine(startNew.ElapsedMilliseconds);
6980
return endTime;
7081
}
7182

src/DeltaTests/Usage.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ public async Task LastTimeStamp()
3434
await using var database = await LocalDb();
3535

3636
var timeStamp = await DeltaExtensions.GetLastTimeStamp(database.Connection, null);
37-
//seed with an entity so there is something in transaction log
38-
await AddEntity(database.Connection);
3937
IsNotEmpty(timeStamp);
4038
IsNotNull(timeStamp);
4139
Recording.Start();
@@ -133,18 +131,21 @@ public async Task LastTimeStampOnTruncate()
133131
IsNotEmpty(timeStamp);
134132
IsNotNull(timeStamp);
135133

136-
await using (var truncateCommand = connection.CreateCommand())
137-
{
138-
truncateCommand.CommandText = "truncate table Companies";
139-
await truncateCommand.ExecuteNonQueryAsync();
140-
}
134+
await TruncateTable(connection);
141135

142136
var newTimeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
143137
IsNotEmpty(newTimeStamp);
144138
IsNotNull(newTimeStamp);
145139
AreNotEqual(newTimeStamp, timeStamp);
146140
}
147141

142+
static async Task TruncateTable(SqlConnection connection)
143+
{
144+
await using var command = connection.CreateCommand();
145+
command.CommandText = "truncate table Companies";
146+
await command.ExecuteNonQueryAsync();
147+
}
148+
148149
[Test]
149150
public async Task GetLastTimeStampSqlServer()
150151
{

0 commit comments

Comments
 (0)