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
295 lines (255 loc) · 10.3 KB
/
RangeHandler.cs
File metadata and controls
295 lines (255 loc) · 10.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
#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;
namespace Npgsql.TypeHandlers
{
internal class RangeHandler<TElement> : TypeHandler<NpgsqlRange<TElement>>,
IChunkingTypeReader<NpgsqlRange<TElement>>, IChunkingTypeWriter
{
/// <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;
void CleanupState()
{
_buf = null;
_value = default(NpgsqlRange<TElement>);
_fieldDescription = null;
_state = State.Done;
}
#endregion
#region Read
public void PrepareRead(NpgsqlBuffer buf, int len, FieldDescription fieldDescription)
{
_buf = buf;
_state = State.Start;
_elementLen = -1;
}
public bool Read(out NpgsqlRange<TElement> result)
{
switch (_state) {
case State.Start:
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:
TElement lowerBound;
if (!ReadSingleElement(out lowerBound))
{
result = default(NpgsqlRange<TElement>);
return false;
}
_value.LowerBound = lowerBound;
goto case State.UpperBound;
case 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 ISimpleTypeReader<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 IChunkingTypeReader<TElement>;
if (asChunkingReader != null) {
asChunkingReader.PrepareRead(_buf, _elementLen, _fieldDescription);
if (!asChunkingReader.Read(out element)) {
return false;
}
_elementLen = -1;
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 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;
if (!range.IsEmpty)
{
var asChunkingWriter = ElementHandler as IChunkingTypeWriter;
if (!range.LowerBoundInfinite) {
totalLen += 4 + (asChunkingWriter != null
? asChunkingWriter.ValidateAndGetLength(range.LowerBound, ref lengthCache, null)
: ((ISimpleTypeWriter)ElementHandler).ValidateAndGetLength(range.LowerBound, null));
}
if (!range.UpperBoundInfinite) {
totalLen += 4 + (asChunkingWriter != null
? asChunkingWriter.ValidateAndGetLength(range.UpperBound, ref lengthCache, null)
: ((ISimpleTypeWriter)ElementHandler).ValidateAndGetLength(range.UpperBound, null));
}
}
return totalLen;
}
public void PrepareWrite(object value, NpgsqlBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter)
{
_buf = buf;
_lengthCache = lengthCache;
_value = (NpgsqlRange<TElement>)value;
_state = State.Start;
}
public bool Write(ref DirectBuffer directBuf)
{
var asChunkingWriter = ElementHandler as IChunkingTypeWriter;
switch (_state)
{
case State.Start:
if (_buf.WriteSpaceLeft < 1) { return false; }
_buf.WriteByte((byte)_value.Flags);
if (_value.IsEmpty)
{
CleanupState();
return true;
}
if (_value.LowerBoundInfinite) {
goto case State.BeforeUpperBound;
}
if (asChunkingWriter != null) {
asChunkingWriter.PrepareWrite(_value.LowerBound, _buf, _lengthCache, null);
}
_state = State.LowerBound;
goto case State.LowerBound;
case State.LowerBound:
if (asChunkingWriter == null)
{
var asSimpleWriter = (ISimpleTypeWriter)ElementHandler;
// TODO: Cache length
var len = asSimpleWriter.ValidateAndGetLength(_value.LowerBound, null);
if (_buf.WriteSpaceLeft < len + 4) { return false; }
_buf.WriteInt32(len);
asSimpleWriter.Write(_value.LowerBound, _buf, null);
}
else if (!asChunkingWriter.Write(ref directBuf)) { return false; }
goto case State.BeforeUpperBound;
case State.BeforeUpperBound:
if (_value.UpperBoundInfinite) {
CleanupState();
return true;
}
if (asChunkingWriter != null) {
asChunkingWriter.PrepareWrite(_value.UpperBound, _buf, _lengthCache, null);
}
_state = State.UpperBound;
goto case State.UpperBound;
case State.UpperBound:
if (asChunkingWriter == null)
{
var asSimpleWriter = (ISimpleTypeWriter)ElementHandler;
// TODO: Cache length
var len = asSimpleWriter.ValidateAndGetLength(_value.UpperBound, null);
if (_buf.WriteSpaceLeft < len + 4) { return false; }
_buf.WriteInt32(len);
asSimpleWriter.Write(_value.UpperBound, _buf, null);
}
else if (!asChunkingWriter.Write(ref directBuf)) { return false; }
CleanupState();
return true;
default:
throw PGUtil.ThrowIfReached();
}
}
#endregion
enum State
{
Start,
LowerBound,
BeforeUpperBound,
UpperBound,
Done
}
}
}