-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathQueryEngineExamples.cs
197 lines (172 loc) · 6.58 KB
/
QueryEngineExamples.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using NUnit.Framework;
using Realms.Sync;
using MongoDB.Bson;
using Realms;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Examples
{
public class QueryEngineExamples
{
public QueryEngineExamples()
{
}
App app;
User user;
PartitionSyncConfiguration config;
const string myRealmAppId = Config.AppId;
[OneTimeSetUp]
public async Task Setup()
{
app = App.Create(myRealmAppId);
user = app.LogInAsync(Config.EPCreds).Result;
config = new PartitionSyncConfiguration("foo", user);
//:remove-start:
config.Schema = new[]
{
typeof(UserTask),
typeof(UserProject)
};
//:remove-end:
var realm = await Realm.GetInstanceAsync(config);
var synchronousRealm = await Realm.GetInstanceAsync(config);
var t = new UserTask() { Priority = 100, ProgressMinutes = 5, Assignee = "Jamie", Name = "Jamie_Task" };
var t2 = new UserTask() { Priority = 1, ProgressMinutes = 500, Assignee = "Elvis", Name = "Elvis_Task" };
var up = new UserProject() { Name = "A Big Project" };
up.Items.Add(t);
up.Items.Add(t2);
realm.Write(() =>
{
realm.Add(t);
realm.Add(t2);
realm.Add(up);
});
return;
}
[Test]
public async Task Comparisons()
{
var realm = await Realm.GetInstanceAsync(config);
var items = realm.All<UserTask>();
// :snippet-start: comparisons
var highPri = items.Where(i => i.Priority > 5);
var quickItems = items.Where(i =>
i.ProgressMinutes >= 1 &&
i.ProgressMinutes < 15);
var unassignedItems = items.Where(i =>
i.Assignee == null);
var AliOrJamieItems = items.Where(i =>
i.Assignee == "Ali" ||
i.Assignee == "Jamie");
// :snippet-end:
Assert.AreEqual(1, highPri.Count());
Assert.AreEqual(1, quickItems.Count());
Assert.AreEqual(0, unassignedItems.Count());
Assert.AreEqual(1, AliOrJamieItems.Count());
// :snippet-start: logical
var completedItemsForAli = items
.Where(i => i.Assignee == "Ali" && i.IsComplete);
// :snippet-end:
// :snippet-start: strings
// Note: In each of the following examples, you can replace the
// Where() method with First(), FirstOrDefault(),
// Single(), SingleOrDefault(),
// Last(), or LastOrDefault().
// Get all items where the Assignee's name starts with "E" or "e"
var ItemssStartWithE = items.Where(i => i.Assignee.StartsWith("E",
StringComparison.OrdinalIgnoreCase));
// Get all items where the Assignee's name ends wth "is"
// (lower case only)
var endsWith = items.Where(t =>
t.Assignee.EndsWith("is", StringComparison.Ordinal));
// Get all items where the Assignee's name contains the
// letters "ami" in any casing
var itemsContains = items.Where(i => i.Assignee.Contains("ami",
StringComparison.OrdinalIgnoreCase));
// Get all items that have no assignee
var null_or_empty = items.Where(i => string.IsNullOrEmpty(i.Assignee));
// :snippet-end:
Assert.AreEqual(1, ItemssStartWithE.Count());
Assert.AreEqual(1, itemsContains.Count());
Assert.AreEqual(0, null_or_empty.Count());
// :snippet-start: read_all
// :replace-start: {
// "terms": {
// "UserTask": "Items",
// "useritems": "items",
// "UserProject": "Project"}
// }
var projects = realm.All<UserProject>();
var useritems = realm.All<UserTask>();
// :replace-end:
// :snippet-end:
var projectId = projects.First().ID;
// :snippet-start: get_by_id
// :replace-start: {
// "terms": {
// "UserProject": "Project"}
// }
var myProject = realm.Find<UserProject>(projectId);
// :replace-end:
// :snippet-end:
// :snippet-start: aggregate
// Get all projects with an average Item priorty > 5:
var avgPriority = projects.Filter(
"Items.@avg.Priority > $0", 5);
// Get all projects where all Items are high-priority:
var highPriProjects = projects.Filter(
"Items.@min.Priority > $0", 5);
// Get all projects with long-running Items:
var longRunningProjects = projects.Filter(
"Items.@sum.ProgressMinutes > $0", 100);
// :snippet-end:
// :snippet-start: rql
var elvisProjects = projects.Filter("Items.Assignee == $0", "Elvis");
// :snippet-end:
Assert.AreEqual(1, avgPriority.Count());
Assert.AreEqual(0, highPriProjects.Count()); // 0 because the project has one lower than 5
Assert.AreEqual(1, longRunningProjects.Count());
Assert.AreEqual(1, elvisProjects.Count());
return;
}
[OneTimeTearDown]
public void Teardown()
{
var realm = Realm.GetInstance(config);
realm.Write(() =>
{
realm.RemoveAll<UserTask>();
realm.RemoveAll<UserProject>();
});
}
}
// :snippet-start: classes
// :replace-start: {
// "terms": {
// "UserTask": "Items",
// "UserProject": "Project"}
// }
public partial class UserTask : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
public string Name { get; set; }
public string Assignee { get; set; }
public bool IsComplete { get; set; }
public int Priority { get; set; }
public int ProgressMinutes { get; set; }
}
public partial class UserProject : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId ID { get; set; } = ObjectId.GenerateNewId();
public string Name { get; set; }
public IList<UserTask> Items { get; }
}
// :replace-end:
// :snippet-end:
}