Skip to content

Commit fa0bd56

Browse files
author
jmd
committed
adding intial changelog SQL
1 parent 662e2be commit fa0bd56

File tree

7 files changed

+109
-42
lines changed

7 files changed

+109
-42
lines changed

src/DotJEM.Json.Storage2.Test/SqlServerStorageAreaFactoryTest.cs

+18
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ public async Task EnsureLogTable_NoTableExists_ShouldCreateTables()
3636

3737
StorageObject? so5 = await area.GetAsync(so.Id);
3838
Console.WriteLine(so5);
39+
}
40+
41+
[Test]
42+
public async Task GetAsync_NoTableExists_ShouldCreateTables()
43+
{
44+
SqlServerStorageContext context = await SqlServerStorageContext
45+
.Create(TestSqlConnectionFactory.ConnectionString, "fox");
46+
IStorageArea area = await context.AreaAsync("test");
47+
48+
await area.InsertAsync("na", JObject.FromObject(new { track="T-01"}));
49+
await area.InsertAsync("na", JObject.FromObject(new { track= "T-02" }));
50+
await area.InsertAsync("na", JObject.FromObject(new { track= "T-03" }));
3951

52+
53+
await foreach (StorageObject obj in area.GetAsync())
54+
{
55+
Console.WriteLine(obj);
56+
}
4057
}
58+
4159
}

src/DotJEM.Json.Storage2/IStorageArea.cs

+5-10
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@ public interface IStorageArea
88
{
99
string Name { get; }
1010

11-
#if NETSTANDARD2_0
12-
//Task<IEnumerable<StorageObject>> GetAsync();
13-
//Task<IEnumerable<StorageObject>> GetAsync(long skip, int take = 100);
14-
#else
15-
//IAsyncEnumerable<StorageObject> GetAsync();
16-
//IAsyncEnumerable<StorageObject> GetAsync(CancellationToken cancellation);
17-
//IAsyncEnumerable<StorageObject> GetAsync(long skip);
18-
//IAsyncEnumerable<StorageObject> GetAsync(long skip, CancellationToken cancellation);
19-
//IAsyncEnumerable<StorageObject> GetAsync(long skip, int take, CancellationToken cancellation);
20-
#endif
11+
IAsyncEnumerable<StorageObject> GetAsync();
12+
IAsyncEnumerable<StorageObject> GetAsync(CancellationToken cancellation);
13+
IAsyncEnumerable<StorageObject> GetAsync(long skip);
14+
IAsyncEnumerable<StorageObject> GetAsync(long skip, CancellationToken cancellation);
15+
IAsyncEnumerable<StorageObject> GetAsync(long skip, int take, CancellationToken cancellation);
2116

2217
Task<StorageObject?> GetAsync(Guid id);
2318
Task<StorageObject?> GetAsync(Guid id, CancellationToken cancellation);

src/DotJEM.Json.Storage2/SqlServer/SqlServerStorageArea.cs

+38-19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ namespace DotJEM.Json.Storage2.SqlServer;
1111

1212
public class SqlServerStorageArea : IStorageArea
1313
{
14+
private const long DEFAULT_SKIP = 0;
15+
private const int DEFAULT_TAKE = 100;
16+
1417
private readonly SqlServerStorageContext context;
1518
private readonly SqlServerAreaStateManager stateManager;
1619

@@ -22,32 +25,48 @@ public SqlServerStorageArea(SqlServerStorageContext context, SqlServerAreaStateM
2225
this.stateManager = stateManager;
2326
}
2427

28+
public IAsyncEnumerable<StorageObject> GetAsync()
29+
=> GetAsync(DEFAULT_SKIP, DEFAULT_TAKE, CancellationToken.None);
2530

26-
//private const long DEFAULT_SKIP = 0;
27-
//private const int DEFAULT_TAKE = 100;
31+
public IAsyncEnumerable<StorageObject> GetAsync(CancellationToken cancellation)
32+
=> GetAsync(DEFAULT_SKIP, DEFAULT_TAKE, cancellation);
2833

29-
//public async IAsyncEnumerable<StorageObject> GetAsync()
30-
// => GetAsync(DEFAULT_SKIP, DEFAULT_TAKE, CancellationToken.None);
34+
public IAsyncEnumerable<StorageObject> GetAsync(long skip)
35+
=> GetAsync(skip, DEFAULT_TAKE, CancellationToken.None);
3136

32-
//public async IAsyncEnumerable<StorageObject> GetAsync(CancellationToken cancellation)
33-
// => GetAsync(DEFAULT_SKIP, DEFAULT_TAKE, cancellation);
37+
public IAsyncEnumerable<StorageObject> GetAsync(long skip, CancellationToken cancellation)
38+
=> GetAsync(skip, DEFAULT_TAKE, cancellation);
3439

35-
//public async IAsyncEnumerable<StorageObject> GetAsync(long skip)
36-
// => GetAsync(skip, DEFAULT_TAKE, CancellationToken.None);
40+
public IAsyncEnumerable<StorageObject> GetAsync(long skip, int take)
41+
=> GetAsync(skip, take, CancellationToken.None);
3742

38-
//public async IAsyncEnumerable<StorageObject> GetAsync(long skip, CancellationToken cancellation)
39-
// => GetAsync(skip, DEFAULT_TAKE, cancellation);
43+
public async IAsyncEnumerable<StorageObject> GetAsync(long skip, int take, CancellationToken cancellation)
44+
{
45+
if (!stateManager.Exists)
46+
yield break;
4047

41-
//public async IAsyncEnumerable<StorageObject> GetAsync(long skip, int take)
42-
// => GetAsync(skip, take, CancellationToken.None);
48+
using ISqlServerCommand cmd = context.CommandFactory.Create(
49+
SqlTemplates.SelectFromDataTable_Paged(stateManager.Schema, stateManager.AreaName),
50+
("take", take),
51+
("skip", skip));
4352

44-
//public async IAsyncEnumerable<StorageObject> GetAsync(long skip, int take, CancellationToken cancellation)
45-
//{
46-
// if (!stateManager.Exists)
47-
// yield break;
53+
ISqlServerDataReader<StorageObject> read = await cmd
54+
.ExecuteReaderAsync(
55+
["Id", "ContentType", "Version", "Created", "Updated", "CreatedBy", "UpdatedBy", "Data"],
56+
values => new StorageObject(
57+
(string)values[1],
58+
(Guid)values[0],
59+
(int)values[2],
60+
(DateTime)values[3],
61+
(DateTime)values[4],
62+
(string)values[5],
63+
(string)values[6],
64+
JObject.Parse((string)values[7])),
65+
CancellationToken.None);
4866

49-
// throw new NotImplementedException();
50-
//}
67+
await foreach (StorageObject obj in read)
68+
yield return obj;
69+
}
5170

5271

5372

@@ -58,11 +77,11 @@ public SqlServerStorageArea(SqlServerStorageContext context, SqlServerAreaStateM
5877
{
5978
if (!stateManager.Exists)
6079
return null;
80+
6181
using ISqlServerCommand cmd = context.CommandFactory.Create(
6282
SqlTemplates.SelectFromDataTable_Byid(stateManager.Schema, stateManager.AreaName),
6383
("id", id));
6484

65-
6685
using ISqlServerDataReader<StorageObject> read = await cmd
6786
.ExecuteReaderAsync(
6887
["Id", "ContentType", "Version", "Created", "Updated", "CreatedBy", "UpdatedBy", "Data"],

src/DotJEM.Json.Storage2/SqlServer/SqlServerStorageContext.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public static implicit operator Parameter((string name, byte[] value) tuple)
154154
}
155155

156156

157-
public interface ISqlServerDataReader<out T> : IDisposable, IEnumerable<T>
157+
public interface ISqlServerDataReader<out T> : IDisposable, IEnumerable<T>, IAsyncEnumerable<T>
158158
{
159159
}
160160

@@ -184,6 +184,18 @@ public IEnumerator<T> GetEnumerator()
184184
}
185185
}
186186

187+
public async IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = new CancellationToken())
188+
{
189+
int[] c = columns.Select(name => reader.GetOrdinal(name)).ToArray();
190+
while (await reader.ReadAsync(cancellationToken))
191+
{
192+
//TODO: Better way of handling column specs as this is surely horrible performance, but function first!.
193+
object[] values = new object[c.Length];
194+
for (int i = 0; i < c.Length; i++)
195+
values[i] = reader.GetValue(c[i]);
196+
yield return factory(values);
197+
}
198+
}
187199

188200
/// <inheritdoc />
189201
public void Dispose() => reader.Dispose();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--start:byid
2+
SELECT TOP (@count)
3+
[Revision]
4+
,[Id]
5+
,[Event]
6+
,[Time]
7+
,[User]
8+
,[Version]
9+
,[Data]
10+
FROM (
11+
SELECT MAX([Revision]) as LatestRevision
12+
FROM [@{schema}].[@{area_name}.log]
13+
WHERE [Revision] > @start
14+
GROUP BY [Id]
15+
) temp
16+
JOIN [@{schema}].[@{area_name}.log] cl ON cl.[Revision] = temp.[LatestRevision]
17+
ORDER BY [Revision];
18+
--end:byid
19+
20+
--start:paged
21+
SELECT [Id]
22+
,[ContentType]
23+
,[Version]
24+
,[Created]
25+
,[Updated]
26+
,[CreatedBy]
27+
,[UpdatedBy]
28+
,[Data]
29+
,[RV]
30+
FROM [@{schema}].[@{area_name}.data]
31+
ORDER BY [Created]
32+
OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY;
33+
--end:paged

src/DotJEM.Json.Storage2/SqlServer/Statements/SelectFromDataTable.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--start:byid
2-
SELECT [Id]
2+
SELECT [Id]
33
,[ContentType]
44
,[Version]
55
,[Created]
@@ -14,6 +14,7 @@
1414

1515
--start:paged
1616
SELECT [Id]
17+
,[ContentType]
1718
,[Version]
1819
,[Created]
1920
,[Updated]

src/DotJEM.Json.Storage2/SqlServer/Statements/TempTemp.sql

-11
This file was deleted.

0 commit comments

Comments
 (0)