-
Notifications
You must be signed in to change notification settings - Fork 0
/
RavenQueryableSpatialOperationHandlerBase.cs
169 lines (149 loc) · 5.5 KB
/
RavenQueryableSpatialOperationHandlerBase.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
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;
using HotChocolate.Configuration;
using HotChocolate.Data.Filters;
using HotChocolate.Data.Filters.Expressions;
using HotChocolate.Language;
using HotChocolate.Language.Visitors;
using HotChocolate.Raven.Spatial.Filtering.Convention;
using HotChocolate.Types;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
using Raven.Client.Documents.Queries.Spatial;
namespace HotChocolate.Raven.Spatial.Filtering.Expressions.Handlers;
public abstract class RavenQueryableSpatialOperationHandlerBase : FilterFieldHandler<QueryableFilterContext, Expression>
{
private readonly int _operation;
private readonly NameString _geometryFieldName;
private readonly NameString _locationFieldName;
private readonly NameString _bufferFieldName;
private readonly NameString _wktFieldName;
private readonly InputParser _inputParser;
public RavenQueryableSpatialOperationHandlerBase(
IFilterConvention convention,
InputParser inputParser,
int operation
)
{
_operation = operation;
_inputParser = inputParser;
_geometryFieldName = convention.GetOperationName(RavenSpatialFilterOperations.Geometry);
_bufferFieldName = convention.GetOperationName(RavenSpatialFilterOperations.Buffer);
_wktFieldName = convention.GetOperationName(RavenSpatialFilterOperations.Wkt);
_locationFieldName = convention.GetOperationName(RavenSpatialFilterOperations.Location);
}
public override bool CanHandle(
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition
)
{
return fieldDefinition is FilterOperationFieldDefinition op && op.Id == _operation;
}
public override bool TryHandleEnter(
QueryableFilterContext context,
IFilterField field,
ObjectFieldNode node,
[NotNullWhen(true)] out ISyntaxVisitorAction? action
)
{
if (field is IFilterOperationField filterOperationField)
{
if (node.Value.IsNull())
{
context.ReportError(ErrorHelper.CreateNonNullError(field, node.Value, context));
action = SyntaxVisitor.Skip;
return true;
}
if (!TryHandleOperation(context, filterOperationField, node, out var nestedProperty))
{
context.PushInstance(Expression.Constant(true));
action = SyntaxVisitor.SkipAndLeave;
return true;
}
context.PushInstance(nestedProperty);
action = SyntaxVisitor.SkipAndLeave;
}
else
{
action = SyntaxVisitor.Break;
}
return true;
}
public override bool TryHandleLeave(
QueryableFilterContext context,
IFilterField field,
ObjectFieldNode node,
[NotNullWhen(true)] out ISyntaxVisitorAction? action
)
{
// Dequeue last
var condition = context.PopInstance();
context.GetLevel().Enqueue(condition);
action = SyntaxVisitor.Continue;
return true;
}
protected bool TryGetParameter<T>(
IFilterField parentField,
IValueNode node,
string fieldName,
[NotNullWhen(true)] out T fieldNode
)
{
return RavenSpatialOperationHandlerHelper.TryGetParameter(
parentField,
node,
fieldName,
_inputParser,
out fieldNode
);
}
protected bool TryHandleOperation(
QueryableFilterContext context,
IFilterOperationField field,
ObjectFieldNode node,
[NotNullWhen(true)] out Expression? result
)
{
if (context is not RavenQueryableFilterContext ravenQueryableFilterContext)
{
result = null;
return false;
}
string? wkt = null;
double? buffer = null;
if (TryGetParameter(field, node.Value, _bufferFieldName, out double rawBuffer))
{
buffer = rawBuffer;
}
if (TryGetParameter(field, node.Value, _wktFieldName, out wkt))
{
}
if (wkt == null && TryGetParameter(field, node.Value, _locationFieldName, out LatLong ll))
{
wkt = ( (Point)ll ).AsText();
}
if (wkt == null && TryGetParameter(field, node.Value, _geometryFieldName, out Geometry g))
{
wkt = g.AsText();
}
if (!string.IsNullOrWhiteSpace(wkt) && context.GetScope().Instance.Peek() is MemberExpression memberExpression)
{
ravenQueryableFilterContext.SpatialFieldName =
memberExpression.Member switch
{
PropertyInfo pi => pi.PropertyType == typeof(LatLong)
? new PointField(pi.Name + "." + nameof(LatLong.Latitude), pi.Name + "." + nameof(LatLong.Longitude))
: new WktField(pi.Name),
_ => ravenQueryableFilterContext.SpatialFieldName
};
ravenQueryableFilterContext.FieldName = memberExpression.Member.Name;
ravenQueryableFilterContext.SpatialExpression = GetSpatialExpression(wkt, buffer);
ravenQueryableFilterContext.NodeToRemove = node;
}
result = null;
return false;
}
protected abstract Expression<Func<SpatialCriteriaFactory, SpatialCriteria>> GetSpatialExpression(string wkt, double? buffer);
}