forked from samus/mongodb-csharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MongoExpressionExtensions.cs
64 lines (56 loc) · 2.24 KB
/
MongoExpressionExtensions.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace MongoDB.Linq.Expressions
{
internal static class MongoExpressionExtensions
{
public static bool HasSelectAllField(this IEnumerable<FieldDeclaration> fields)
{
if (fields == null)
return true;
return fields.Any(f => f.Name == "*");
}
public static SelectExpression AddField(this SelectExpression select, FieldDeclaration field)
{
List<FieldDeclaration> fields = new List<FieldDeclaration>(select.Fields);
fields.Add(field);
return select.SetFields(fields);
}
public static string GetAvailableFieldName(this SelectExpression select, string baseName)
{
string name = baseName;
int n = 0;
while (!IsUniqueName(select, name))
{
name = baseName + (n++);
}
return name;
}
public static SelectExpression RemoveField(this SelectExpression select, FieldDeclaration field)
{
List<FieldDeclaration> fields = new List<FieldDeclaration>(select.Fields);
fields.Remove(field);
return select.SetFields(fields);
}
public static SelectExpression SetFields(this SelectExpression select, IEnumerable<FieldDeclaration> fields)
{
return new SelectExpression(select.Alias, fields.OrderBy(f => f.Name), select.From, select.Where, select.OrderBy, select.GroupBy, select.IsDistinct, select.Skip, select.Take);
}
public static SelectExpression SetWhere(this SelectExpression select, Expression where)
{
return new SelectExpression(select.Alias, select.Fields, select.From, where, select.OrderBy, select.GroupBy, select.IsDistinct, select.Skip, select.Take);
}
private static bool IsUniqueName(SelectExpression select, string name)
{
foreach (var field in select.Fields)
{
if (field.Name == name)
return false;
}
return true;
}
}
}