5
5
using static Sensemaking . Query . Cosmos . Querying ;
6
6
using Database = Sensemaking . Cosmos . Database ;
7
7
8
- namespace Sensemaking . Query . Cosmos
8
+ namespace Sensemaking . Query . Cosmos ;
9
+
10
+ public abstract class Query < T , U > : IQuery < T , U >
9
11
{
10
- public abstract class Query < T , U > : IQuery < T , U >
12
+ public async Task < IEnumerable < U > > GetResultsAsync ( T parameters )
11
13
{
12
- public async Task < IEnumerable < U > > GetResultsAsync ( T parameters )
13
- {
14
- var spec = GetQuerySpecification ( parameters ) ;
15
- var results = new List < U > ( ) ;
16
- var iterator = Database . GetClient ( ) . GetDatabase ( Database . DatabaseName ! ) . GetContainer ( spec . Container ) . GetItemQueryIterator < U > ( new QueryDefinition ( spec . Query ) ) ;
14
+ var spec = GetQuerySpecification ( parameters ) ;
15
+ var results = new List < U > ( ) ;
16
+ var iterator = Database . GetClient ( ) . GetDatabase ( Database . DatabaseName ! ) . GetContainer ( spec . Container ) . GetItemQueryIterator < U > ( new QueryDefinition ( spec . Query ) ) ;
17
17
18
- while ( iterator . HasMoreResults )
19
- results . AddRange ( await iterator . ReadNextAsync ( ) . ConfigureAwait ( false ) ) ;
18
+ while ( iterator . HasMoreResults )
19
+ results . AddRange ( await iterator . ReadNextAsync ( ) . ConfigureAwait ( false ) ) ;
20
20
21
- return results ;
22
- }
21
+ return results ;
22
+ }
23
23
24
- protected abstract QuerySpecification GetQuerySpecification ( T parameters ) ;
24
+ protected abstract QuerySpecification GetQuerySpecification ( T parameters ) ;
25
25
26
- protected class QuerySpecification
26
+ protected class QuerySpecification
27
+ {
28
+ public QuerySpecification ( string container , string query )
27
29
{
28
- public QuerySpecification ( string container , string query )
29
- {
30
- Container = container ;
31
- Query = query ;
32
- }
33
-
34
- public string Container { get ; }
35
- public string Query { get ; }
30
+ Container = container ;
31
+ Query = query ;
36
32
}
33
+
34
+ public string Container { get ; }
35
+ public string Query { get ; }
37
36
}
37
+ }
38
38
39
- internal static class Querying
39
+ internal static class Querying
40
+ {
41
+ internal static async Task < IEnumerable < T > > GetResults < T > ( ( string Container , string Query ) specification )
40
42
{
41
- internal static async Task < IEnumerable < T > > GetResults < T > ( ( string Container , string Query ) specification )
42
- {
43
- var results = new List < T > ( ) ;
44
- var iterator = Database . GetClient ( ) . GetDatabase ( Database . DatabaseName ! )
45
- . GetContainer ( specification . Container )
46
- . GetItemQueryIterator < T > ( new QueryDefinition ( specification . Query ) ) ;
43
+ var results = new List < T > ( ) ;
44
+ var iterator = Database . GetClient ( ) . GetDatabase ( Database . DatabaseName ! )
45
+ . GetContainer ( specification . Container )
46
+ . GetItemQueryIterator < T > ( new QueryDefinition ( specification . Query ) ) ;
47
47
48
- while ( iterator . HasMoreResults )
49
- results . AddRange ( await iterator . ReadNextAsync ( ) . ConfigureAwait ( false ) ) ;
48
+ while ( iterator . HasMoreResults )
49
+ results . AddRange ( await iterator . ReadNextAsync ( ) . ConfigureAwait ( false ) ) ;
50
50
51
- return results ;
52
- }
51
+ return results ;
53
52
}
53
+ }
54
54
55
- public class Query < T >
55
+ public class Query < T >
56
+ {
57
+ private readonly ( string Container , string Query ) specification ;
58
+
59
+ public Query ( string container , string query ) : this ( new ( container , query ) )
56
60
{
57
- private readonly ( string Container , string Query ) specification ;
61
+ }
58
62
59
- public Query ( string container , string query ) : this ( new ( container , query ) ) { }
60
- public Query ( ( string Container , string Query ) specification )
61
- {
62
- this . specification = specification ;
63
- }
63
+ public Query ( ( string Container , string Query ) specification )
64
+ {
65
+ this . specification = specification ;
66
+ }
64
67
65
- public async Task < IEnumerable < T > > GetResults ( )
66
- {
67
- return await GetResults < T > ( specification ) ;
68
- }
68
+ public async Task < IEnumerable < T > > GetResults ( )
69
+ {
70
+ return await GetResults < T > ( specification ) ;
69
71
}
72
+ }
73
+
74
+ public class ParameterisedQuery < T , U >
75
+ {
76
+ private readonly ( string Container , Func < T , string > BuildQuery ) specification ;
70
77
71
- public class ParameterisedQuery < T , U >
78
+ public ParameterisedQuery ( string container , Func < T , string > buildQuery ) : this ( new ( container , buildQuery ) )
72
79
{
73
- private readonly ( string Container , Func < T , string > BuildQuery ) specification ;
80
+ }
74
81
75
- public ParameterisedQuery ( string container , Func < T , string > buildQuery ) : this ( new ( container , buildQuery ) ) { }
76
- public ParameterisedQuery ( ( string Container , Func < T , string > BuildQuery ) specification )
77
- {
78
- this . specification = specification ;
79
- }
82
+ public ParameterisedQuery ( ( string Container , Func < T , string > BuildQuery ) specification )
83
+ {
84
+ this . specification = specification ;
85
+ }
80
86
81
- public async Task < IEnumerable < U > > GetResults ( T parameters )
82
- {
83
- return await GetResults < U > ( ( specification . Container , specification . BuildQuery ( parameters ) ) ) ;
84
- }
87
+ public async Task < IEnumerable < U > > GetResults ( T parameters )
88
+ {
89
+ return await GetResults < U > ( ( specification . Container , specification . BuildQuery ( parameters ) ) ) ;
85
90
}
86
91
}
92
+
93
+ public interface IRunQueries
94
+ {
95
+ Task < IEnumerable < T > > GetResults < T > ( string container , string query ) ;
96
+ Task < IEnumerable < T > > GetResults < T > ( ( string Container , string Query ) definition ) ;
97
+ }
98
+
99
+ public class QueryRunner : IRunQueries
100
+ {
101
+ public Task < IEnumerable < T > > GetResults < T > ( string container , string query ) => GetResults < T > ( ( container , query ) ) ;
102
+
103
+ public async Task < IEnumerable < T > > GetResults < T > ( ( string Container , string Query ) definition )
104
+ {
105
+ var results = new List < T > ( ) ;
106
+ var iterator = Database . GetClient ( ) . GetDatabase ( Database . DatabaseName ! )
107
+ . GetContainer ( definition . Container )
108
+ . GetItemQueryIterator < T > ( new QueryDefinition ( definition . Query ) ) ;
109
+
110
+ while ( iterator . HasMoreResults )
111
+ results . AddRange ( await iterator . ReadNextAsync ( ) . ConfigureAwait ( false ) ) ;
112
+
113
+ return results ;
114
+ }
115
+ }
0 commit comments