Skip to content

Commit da285c0

Browse files
committed
.
1 parent 48b9d4f commit da285c0

File tree

2 files changed

+81
-48
lines changed

2 files changed

+81
-48
lines changed

readme.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ An optional string suffix that is dynamically calculated at runtime based on the
9494
var app = builder.Build();
9595
app.UseDelta(suffix: httpContext => "MySuffix");
9696
```
97-
<sup><a href='/src/DeltaTests/Usage.cs#L6-L11' title='Snippet source file'>snippet source</a> | <a href='#snippet-Suffix' title='Start of snippet'>anchor</a></sup>
97+
<sup><a href='/src/DeltaTests/Usage.cs#L8-L13' title='Snippet source file'>snippet source</a> | <a href='#snippet-Suffix' title='Start of snippet'>anchor</a></sup>
9898
<!-- endSnippet -->
9999

100100

@@ -274,7 +274,7 @@ app.UseDelta(
274274
return path.Contains("match");
275275
});
276276
```
277-
<sup><a href='/src/DeltaTests/Usage.cs#L16-L26' title='Snippet source file'>snippet source</a> | <a href='#snippet-ShouldExecute' title='Start of snippet'>anchor</a></sup>
277+
<sup><a href='/src/DeltaTests/Usage.cs#L18-L28' title='Snippet source file'>snippet source</a> | <a href='#snippet-ShouldExecute' title='Start of snippet'>anchor</a></sup>
278278
<!-- endSnippet -->
279279

280280

@@ -325,7 +325,7 @@ var application = webApplicationBuilder.Build();
325325
application.UseDelta(
326326
getConnection: httpContext => httpContext.RequestServices.GetRequiredService<SqlConnection>());
327327
```
328-
<sup><a href='/src/DeltaTests/Usage.cs#L295-L301' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDiscoveryConnection' title='Start of snippet'>anchor</a></sup>
328+
<sup><a href='/src/DeltaTests/Usage.cs#L329-L335' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDiscoveryConnection' title='Start of snippet'>anchor</a></sup>
329329
<!-- endSnippet -->
330330

331331
To use custom connection and transaction discovery:
@@ -338,12 +338,12 @@ webApplication.UseDelta(
338338
getConnection: httpContext =>
339339
{
340340
var provider = httpContext.RequestServices;
341-
var sqlConnection = provider.GetRequiredService<SqlConnection>();
342-
var sqlTransaction = provider.GetService<SqlTransaction>();
343-
return new(sqlConnection, sqlTransaction);
341+
var connection = provider.GetRequiredService<SqlConnection>();
342+
var transaction = provider.GetService<SqlTransaction>();
343+
return new(connection, transaction);
344344
});
345345
```
346-
<sup><a href='/src/DeltaTests/Usage.cs#L306-L318' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDiscoveryConnectionAndTransaction' title='Start of snippet'>anchor</a></sup>
346+
<sup><a href='/src/DeltaTests/Usage.cs#L340-L352' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDiscoveryConnectionAndTransaction' title='Start of snippet'>anchor</a></sup>
347347
<!-- endSnippet -->
348348

349349

@@ -530,7 +530,7 @@ Nuget: [Delta.SqlServer](https://www.nuget.org/packages/Delta.SqlServer)
530530
```cs
531531
var timeStamp = await sqlConnection.GetLastTimeStamp();
532532
```
533-
<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>
533+
<sup><a href='/src/DeltaTests/Usage.cs#L165-L169' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetLastTimeStampSqlConnection' title='Start of snippet'>anchor</a></sup>
534534
<!-- endSnippet -->
535535

536536

@@ -558,7 +558,7 @@ foreach (var db in trackedDatabases)
558558
Trace.WriteLine(db);
559559
}
560560
```
561-
<sup><a href='/src/DeltaTests/Usage.cs#L193-L201' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetDatabasesWithTracking' title='Start of snippet'>anchor</a></sup>
561+
<sup><a href='/src/DeltaTests/Usage.cs#L227-L235' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetDatabasesWithTracking' title='Start of snippet'>anchor</a></sup>
562562
<!-- endSnippet -->
563563

564564
Uses the following SQL:
@@ -588,7 +588,7 @@ foreach (var db in trackedTables)
588588
Trace.WriteLine(db);
589589
}
590590
```
591-
<sup><a href='/src/DeltaTests/Usage.cs#L219-L227' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetTrackedTables' title='Start of snippet'>anchor</a></sup>
591+
<sup><a href='/src/DeltaTests/Usage.cs#L253-L261' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetTrackedTables' title='Start of snippet'>anchor</a></sup>
592592
<!-- endSnippet -->
593593

594594
Uses the following SQL:
@@ -613,7 +613,7 @@ Determine if change tracking is enabled for a database.
613613
```cs
614614
var isTrackingEnabled = await sqlConnection.IsTrackingEnabled();
615615
```
616-
<sup><a href='/src/DeltaTests/Usage.cs#L284-L288' title='Snippet source file'>snippet source</a> | <a href='#snippet-IsTrackingEnabled' title='Start of snippet'>anchor</a></sup>
616+
<sup><a href='/src/DeltaTests/Usage.cs#L318-L322' title='Snippet source file'>snippet source</a> | <a href='#snippet-IsTrackingEnabled' title='Start of snippet'>anchor</a></sup>
617617
<!-- endSnippet -->
618618

619619
Uses the following SQL:
@@ -640,7 +640,7 @@ Enable change tracking for a database.
640640
```cs
641641
await sqlConnection.EnableTracking();
642642
```
643-
<sup><a href='/src/DeltaTests/Usage.cs#L278-L282' title='Snippet source file'>snippet source</a> | <a href='#snippet-EnableTracking' title='Start of snippet'>anchor</a></sup>
643+
<sup><a href='/src/DeltaTests/Usage.cs#L312-L316' title='Snippet source file'>snippet source</a> | <a href='#snippet-EnableTracking' title='Start of snippet'>anchor</a></sup>
644644
<!-- endSnippet -->
645645

646646
Uses the following SQL:
@@ -668,7 +668,7 @@ Disable change tracking for a database and all tables within that database.
668668
```cs
669669
await sqlConnection.DisableTracking();
670670
```
671-
<sup><a href='/src/DeltaTests/Usage.cs#L263-L267' title='Snippet source file'>snippet source</a> | <a href='#snippet-DisableTracking' title='Start of snippet'>anchor</a></sup>
671+
<sup><a href='/src/DeltaTests/Usage.cs#L297-L301' title='Snippet source file'>snippet source</a> | <a href='#snippet-DisableTracking' title='Start of snippet'>anchor</a></sup>
672672
<!-- endSnippet -->
673673

674674
Uses the following SQL:
@@ -705,7 +705,7 @@ Enables change tracking for all tables listed, and disables change tracking for
705705
```cs
706706
await sqlConnection.SetTrackedTables(["Companies"]);
707707
```
708-
<sup><a href='/src/DeltaTests/Usage.cs#L213-L217' title='Snippet source file'>snippet source</a> | <a href='#snippet-SetTrackedTables' title='Start of snippet'>anchor</a></sup>
708+
<sup><a href='/src/DeltaTests/Usage.cs#L247-L251' title='Snippet source file'>snippet source</a> | <a href='#snippet-SetTrackedTables' title='Start of snippet'>anchor</a></sup>
709709
<!-- endSnippet -->
710710

711711
Uses the following SQL:
@@ -779,7 +779,6 @@ If disable cache is checked, the browser will not send the `if-none-match` heade
779779
Chromium, and hence the Chrome and Edge browsers, are very sensitive to certificate problems when determining if an item should be cached. Specifically, if a request is done dynamically (type: xhr) and the server is using a self-signed certificate, then the browser will not send the `if-none-match` header. [Reference]( https://issues.chromium.org/issues/40666473). If self-signed certificates are required during development in lower environment, then use FireFox to test the caching behavior.
780780

781781

782-
783782
## Programmatic client usage
784783

785784
Delta is primarily designed to support web browsers as a client. All web browsers have the necessary 304 and caching functionally required.

src/DeltaTests/Usage.cs

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
public class Usage :
1+
using Npgsql;
2+
3+
public class Usage :
24
LocalDbTestBase
35
{
46
public static void Suffix(WebApplicationBuilder builder)
@@ -35,13 +37,16 @@ public async Task LastTimeStampRowVersion()
3537
IsNotEmpty(timeStamp);
3638
IsNotNull(timeStamp);
3739
Recording.Start();
38-
await using var command = database.Connection.CreateCommand();
39-
command.CommandText =
40-
$"""
41-
insert into [Companies] (Id, Content)
42-
values ('{Guid.NewGuid()}', 'The company')
43-
""";
44-
await command.ExecuteNonQueryAsync();
40+
await using (var command = database.Connection.CreateCommand())
41+
{
42+
command.CommandText =
43+
$"""
44+
insert into [Companies] (Id, Content)
45+
values ('{Guid.NewGuid()}', 'The company')
46+
""";
47+
await command.ExecuteNonQueryAsync();
48+
}
49+
4550
var newTimeStamp = await DeltaExtensions.GetLastTimeStamp(database.Connection, null);
4651
IsNotEmpty(newTimeStamp);
4752
IsNotNull(newTimeStamp);
@@ -68,14 +73,17 @@ insert into [Companies] (Id, Content)
6873
IsNotEmpty(timeStamp);
6974
IsNotNull(timeStamp);
7075

71-
await using var updateCommand = connection.CreateCommand();
72-
updateCommand.CommandText = $"""
73-
UPDATE [Companies]
74-
SET [Content] = 'New Content Value'
75-
WHERE [Id] = @Id;
76-
""";
77-
updateCommand.Parameters.AddWithValue("@Id", companyGuid);
78-
await updateCommand.ExecuteNonQueryAsync();
76+
await using (var updateCommand = connection.CreateCommand())
77+
{
78+
updateCommand.CommandText =
79+
$"""
80+
UPDATE [Companies]
81+
SET [Content] = 'New Content Value'
82+
WHERE [Id] = @Id;
83+
""";
84+
updateCommand.Parameters.AddWithValue("@Id", companyGuid);
85+
await updateCommand.ExecuteNonQueryAsync();
86+
}
7987

8088
var newTimeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
8189
IsNotEmpty(newTimeStamp);
@@ -103,10 +111,12 @@ insert into [Companies] (Id, Content)
103111
IsNotEmpty(timeStamp);
104112
IsNotNull(timeStamp);
105113

106-
await using var deleteCommand = connection.CreateCommand();
107-
deleteCommand.CommandText = $"delete From Companies where Id=@Id";
108-
deleteCommand.Parameters.AddWithValue("@Id", companyGuid);
109-
await deleteCommand.ExecuteNonQueryAsync();
114+
await using (var deleteCommand = connection.CreateCommand())
115+
{
116+
deleteCommand.CommandText = $"delete From Companies where Id=@Id";
117+
deleteCommand.Parameters.AddWithValue("@Id", companyGuid);
118+
await deleteCommand.ExecuteNonQueryAsync();
119+
}
110120

111121
var newTimeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
112122
IsNotEmpty(newTimeStamp);
@@ -133,9 +143,11 @@ insert into [Companies] (Id, Content)
133143
IsNotEmpty(timeStamp);
134144
IsNotNull(timeStamp);
135145

136-
await using var truncateCommand = connection.CreateCommand();
137-
truncateCommand.CommandText = "truncate table Companies";
138-
await truncateCommand.ExecuteNonQueryAsync();
146+
await using (var truncateCommand = connection.CreateCommand())
147+
{
148+
truncateCommand.CommandText = "truncate table Companies";
149+
await truncateCommand.ExecuteNonQueryAsync();
150+
}
139151

140152
var newTimeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
141153
IsNotEmpty(newTimeStamp);
@@ -156,6 +168,25 @@ public async Task GetLastTimeStampSqlServer()
156168

157169
#endregion
158170

171+
IsNotNull(timeStamp);
172+
}
173+
174+
[Test]
175+
public async Task GetLastTimeStampPostgres()
176+
{
177+
await using var connection = new NpgsqlConnection(PostgresConnection.ConnectionString);
178+
await connection.OpenAsync();
179+
await PostgresDbBuilder.Create(connection);
180+
await using var command = connection.CreateCommand();
181+
command.CommandText =
182+
$"""
183+
insert into "Companies"("Id", "Content")
184+
values ('{Guid.NewGuid()}', 'The company')
185+
""";
186+
await command.ExecuteNonQueryAsync();
187+
188+
var timeStamp = await connection.GetLastTimeStamp();
189+
159190
IsNotNull(timeStamp);
160191
IsNotEmpty(timeStamp);
161192
}
@@ -170,13 +201,16 @@ public async Task LastTimeStampRowVersionAndTracking()
170201
var timeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
171202
IsNotEmpty(timeStamp);
172203
IsNotNull(timeStamp);
173-
await using var command = connection.CreateCommand();
174-
command.CommandText =
175-
$"""
176-
insert into [Companies] (Id, Content)
177-
values ('{Guid.NewGuid()}', 'The company')
178-
""";
179-
await command.ExecuteNonQueryAsync();
204+
await using (var command = connection.CreateCommand())
205+
{
206+
command.CommandText =
207+
$"""
208+
insert into [Companies] (Id, Content)
209+
values ('{Guid.NewGuid()}', 'The company')
210+
""";
211+
await command.ExecuteNonQueryAsync();
212+
}
213+
180214
var newTimeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
181215
IsNotEmpty(newTimeStamp);
182216
IsNotNull(newTimeStamp);
@@ -310,9 +344,9 @@ static void CustomDiscoveryConnectionAndTransaction(WebApplicationBuilder webApp
310344
getConnection: httpContext =>
311345
{
312346
var provider = httpContext.RequestServices;
313-
var sqlConnection = provider.GetRequiredService<SqlConnection>();
314-
var sqlTransaction = provider.GetService<SqlTransaction>();
315-
return new(sqlConnection, sqlTransaction);
347+
var connection = provider.GetRequiredService<SqlConnection>();
348+
var transaction = provider.GetService<SqlTransaction>();
349+
return new(connection, transaction);
316350
});
317351

318352
#endregion

0 commit comments

Comments
 (0)