forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRangeHandler.cs
More file actions
332 lines (291 loc) · 11.3 KB
/
RangeHandler.cs
File metadata and controls
332 lines (291 loc) · 11.3 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#region License
// The PostgreSQL License
//
// Copyright (C) 2015 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion
using Npgsql.BackendMessages;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NpgsqlTypes;
using System.Diagnostics.Contracts;
using JetBrains.Annotations;
namespace Npgsql.TypeHandlers
{
/// <summary>
/// Type handler for PostgreSQL range types
/// </summary>
/// <remarks>
/// Introduced in PostgreSQL 9.2.
/// http://www.postgresql.org/docs/current/static/rangetypes.html
/// </remarks>
/// <typeparam name="TElement">the range subtype</typeparam>
internal class RangeHandler<TElement> : ChunkingTypeHandler<NpgsqlRange<TElement>>
{
/// <summary>
/// The type handler for the element that this range type holds
/// </summary>
public TypeHandler ElementHandler { get; private set; }
public RangeHandler(TypeHandler<TElement> elementHandler, string name)
{
ElementHandler = elementHandler;
PgName = name;
}
#region State
NpgsqlBuffer _buf;
LengthCache _lengthCache;
NpgsqlRange<TElement> _value;
State _state;
FieldDescription _fieldDescription;
int _elementLen;
bool _wroteElementLen;
bool _preparedRead;
void CleanupState()
{
_buf = null;
_value = default(NpgsqlRange<TElement>);
_fieldDescription = null;
_state = State.Done;
}
#endregion
#region Read
public override void PrepareRead(NpgsqlBuffer buf, int len, FieldDescription fieldDescription)
{
_buf = buf;
_state = State.Flags;
_elementLen = -1;
}
public override bool Read(out NpgsqlRange<TElement> result)
{
switch (_state) {
case State.Flags:
if (_buf.ReadBytesLeft < 1)
{
result = default(NpgsqlRange<TElement>);
return false;
}
var flags = (RangeFlags)_buf.ReadByte();
_value = new NpgsqlRange<TElement>(flags);
if (_value.IsEmpty) {
result = _value;
CleanupState();
return true;
}
if (_value.LowerBoundInfinite) {
goto case State.UpperBound;
}
_state = State.LowerBound;
goto case State.LowerBound;
case State.LowerBound:
_state = State.LowerBound;
TElement lowerBound;
if (!ReadSingleElement(out lowerBound))
{
result = default(NpgsqlRange<TElement>);
return false;
}
_value.LowerBound = lowerBound;
goto case State.UpperBound;
case State.UpperBound:
_state = State.UpperBound;
if (_value.UpperBoundInfinite) {
result = _value;
CleanupState();
return true;
}
TElement upperBound;
if (!ReadSingleElement(out upperBound))
{
result = default(NpgsqlRange<TElement>);
return false;
}
_value.UpperBound = upperBound;
result = _value;
CleanupState();
return true;
default:
throw new ArgumentOutOfRangeException();
}
}
bool ReadSingleElement(out TElement element)
{
try {
if (_elementLen == -1) {
if (_buf.ReadBytesLeft < 4) {
element = default(TElement);
return false;
}
_elementLen = _buf.ReadInt32();
Contract.Assume(_elementLen != -1);
}
var asSimpleReader = ElementHandler as ISimpleTypeHandler<TElement>;
if (asSimpleReader != null) {
if (_buf.ReadBytesLeft < _elementLen) {
element = default(TElement);
return false;
}
element = asSimpleReader.Read(_buf, _elementLen, _fieldDescription);
_elementLen = -1;
return true;
}
var asChunkingReader = ElementHandler as IChunkingTypeHandler<TElement>;
if (asChunkingReader != null) {
if (!_preparedRead)
{
asChunkingReader.PrepareRead(_buf, _elementLen, _fieldDescription);
_preparedRead = true;
}
if (!asChunkingReader.Read(out element)) {
return false;
}
_elementLen = -1;
_preparedRead = false;
return true;
}
throw PGUtil.ThrowIfReached();
} catch (SafeReadException e) {
// TODO: Implement safe reading. For now, translate the safe exception to an unsafe one
// to break the connector.
throw e.InnerException;
}
}
#endregion
#region Write
public override int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null)
{
if (!(value is NpgsqlRange<TElement>))
throw CreateConversionException(value.GetType());
var range = (NpgsqlRange<TElement>)value;
var totalLen = 1;
var lengthCachePos = lengthCache != null ? lengthCache.Position : 0;
if (!range.IsEmpty)
{
var asChunkingWriter = ElementHandler as IChunkingTypeHandler;
if (!range.LowerBoundInfinite) {
totalLen += 4 + (asChunkingWriter != null
? asChunkingWriter.ValidateAndGetLength(range.LowerBound, ref lengthCache, null)
: ((ISimpleTypeHandler)ElementHandler).ValidateAndGetLength(range.LowerBound, null));
}
if (!range.UpperBoundInfinite) {
totalLen += 4 + (asChunkingWriter != null
? asChunkingWriter.ValidateAndGetLength(range.UpperBound, ref lengthCache, null)
: ((ISimpleTypeHandler)ElementHandler).ValidateAndGetLength(range.UpperBound, null));
}
}
// If we're traversing an already-populated length cache, rewind to first element slot so that
// the elements' handlers can access their length cache values
if (lengthCache != null && lengthCache.IsPopulated) {
lengthCache.Position = lengthCachePos;
}
return totalLen;
}
public override void PrepareWrite(object value, NpgsqlBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter)
{
_buf = buf;
_lengthCache = lengthCache;
_value = (NpgsqlRange<TElement>)value;
_state = State.Flags;
}
public override bool Write(ref DirectBuffer directBuf)
{
switch (_state)
{
case State.Flags:
if (_buf.WriteSpaceLeft < 1) { return false; }
_buf.WriteByte((byte)_value.Flags);
if (_value.IsEmpty)
{
CleanupState();
return true;
}
goto case State.LowerBound;
case State.LowerBound:
_state = State.LowerBound;
if (_value.LowerBoundInfinite) {
goto case State.UpperBound;
}
if (!WriteSingleElement(_value.LowerBound, ref directBuf)) { return false; }
goto case State.UpperBound;
case State.UpperBound:
_state = State.UpperBound;
if (_value.UpperBoundInfinite)
{
CleanupState();
return true;
}
if (!WriteSingleElement(_value.UpperBound, ref directBuf)) { return false; }
CleanupState();
return true;
default:
throw PGUtil.ThrowIfReached();
}
}
// TODO: Duplicated from ArrayHandler... Refactor...
bool WriteSingleElement([CanBeNull] object element, ref DirectBuffer directBuf)
{
if (element == null || element is DBNull)
{
if (_buf.WriteSpaceLeft < 4)
{
return false;
}
_buf.WriteInt32(-1);
return true;
}
var asSimpleWriter = ElementHandler as ISimpleTypeHandler;
if (asSimpleWriter != null)
{
var elementLen = asSimpleWriter.ValidateAndGetLength(element, null);
if (_buf.WriteSpaceLeft < 4 + elementLen) { return false; }
_buf.WriteInt32(elementLen);
asSimpleWriter.Write(element, _buf, null);
return true;
}
var asChunkedWriter = ElementHandler as IChunkingTypeHandler;
if (asChunkedWriter != null)
{
if (!_wroteElementLen)
{
if (_buf.WriteSpaceLeft < 4) { return false; }
_buf.WriteInt32(asChunkedWriter.ValidateAndGetLength(element, ref _lengthCache, null));
asChunkedWriter.PrepareWrite(element, _buf, _lengthCache, null);
_wroteElementLen = true;
}
if (!asChunkedWriter.Write(ref directBuf))
{
return false;
}
_wroteElementLen = false;
return true;
}
throw PGUtil.ThrowIfReached();
}
#endregion
enum State
{
Flags,
LowerBound,
UpperBound,
Done
}
}
}