forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitStringHandler.cs
More file actions
376 lines (328 loc) · 13.3 KB
/
BitStringHandler.cs
File metadata and controls
376 lines (328 loc) · 13.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#region License
// The PostgreSQL License
//
// Copyright (C) 2016 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 System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using Npgsql.BackendMessages;
using NpgsqlTypes;
using System.Data;
using JetBrains.Annotations;
namespace Npgsql.TypeHandlers
{
/// <summary>
/// Handler for the PostgreSQL bit string type.
/// Note that for BIT(1), this handler will return a bool by default, to align with SQLClient
/// (see discussion https://github.com/npgsql/npgsql/pull/362#issuecomment-59622101).
/// </summary>
/// <remarks>
/// http://www.postgresql.org/docs/current/static/datatype-bit.html
/// </remarks>
[TypeMapping("varbit", NpgsqlDbType.Varbit, typeof(BitArray))]
[TypeMapping("bit", NpgsqlDbType.Bit)]
internal class BitStringHandler : ChunkingTypeHandler<BitArray>, IChunkingTypeHandler<bool>
{
ReadBuffer _readBuf;
WriteBuffer _writeBuf;
int _len;
BitArray _bitArray;
object _value;
int _pos;
internal BitStringHandler(IBackendType backendType) : base(backendType) {}
internal override Type GetFieldType(FieldDescription fieldDescription)
{
return fieldDescription != null && fieldDescription.TypeModifier == 1 ? typeof (bool) : typeof(BitArray);
}
internal override Type GetProviderSpecificFieldType(FieldDescription fieldDescription)
{
return GetFieldType(fieldDescription);
}
internal override ArrayHandler CreateArrayHandler(IBackendType backendType)
{
// BitString requires a special array handler which returns bool or BitArray
return new BitStringArrayHandler(backendType, this);
}
internal override object ReadValueAsObjectFully(DataRowMessage row, FieldDescription fieldDescription = null)
{
return fieldDescription?.TypeModifier == 1
? (object)ReadFully<bool>(row, row.ColumnLen, fieldDescription)
: ReadFully<BitArray>(row, row.ColumnLen, fieldDescription);
}
internal override object ReadValueAsObjectFully(ReadBuffer buf, int len, FieldDescription fieldDescription = null)
{
return fieldDescription?.TypeModifier == 1
? (object)ReadFully<bool>(buf, len, fieldDescription)
: ReadFully<BitArray>(buf, len, fieldDescription);
}
#region Read
public override void PrepareRead(ReadBuffer buf, int len, FieldDescription fieldDescription)
{
_readBuf = buf;
_pos = -1;
_len = len - 4; // Subtract leading bit length field
}
bool IChunkingTypeHandler<bool>.Read(out bool result)
{
result = false;
if (_readBuf.ReadBytesLeft < 4) { return false; }
var bitLen = _readBuf.ReadInt32();
if (bitLen != 1)
{
// This isn't a single bit - error.
// Consume the rest of the value first so the connection is left in a good state.
_readBuf.Skip(_len);
throw new SafeReadException(new InvalidCastException("Can't convert a BIT(N) type to bool, only BIT(1)"));
}
var b = _readBuf.ReadByte();
result = (b & 128) != 0;
return true;
}
/// <summary>
/// Reads a BitArray from a binary PostgreSQL value. First 32-bit big endian length,
/// then the data in big-endian. Zero-padded low bits in the end if length is not multiple of 8.
/// </summary>
public override bool Read([CanBeNull] out BitArray result)
{
if (_pos == -1)
{
if (_readBuf.ReadBytesLeft < 4)
{
result = null;
return false;
}
var numBits = _readBuf.ReadInt32();
_bitArray = new BitArray(numBits);
_pos = 0;
}
var bitNo = _pos * 8;
var maxBytes = _pos + Math.Min(_len - _pos - 1, _readBuf.ReadBytesLeft);
for (; _pos < maxBytes; _pos++)
{
var chunk = _readBuf.ReadByte();
_bitArray[bitNo++] = (chunk & (1 << 7)) != 0;
_bitArray[bitNo++] = (chunk & (1 << 6)) != 0;
_bitArray[bitNo++] = (chunk & (1 << 5)) != 0;
_bitArray[bitNo++] = (chunk & (1 << 4)) != 0;
_bitArray[bitNo++] = (chunk & (1 << 3)) != 0;
_bitArray[bitNo++] = (chunk & (1 << 2)) != 0;
_bitArray[bitNo++] = (chunk & (1 << 1)) != 0;
_bitArray[bitNo++] = (chunk & (1 << 0)) != 0;
}
if (_pos < _len - 1)
{
result = null;
return false;
}
if (bitNo < _bitArray.Length)
{
var remainder = _bitArray.Length - bitNo;
var lastChunk = _readBuf.ReadByte();
for (var i = 7; i >= 8 - remainder; i--)
{
_bitArray[bitNo++] = (lastChunk & (1 << i)) != 0;
}
}
result = _bitArray;
return true;
}
#endregion
#region Write
public override int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter=null)
{
var asBitArray = value as BitArray;
if (asBitArray != null)
return 4 + (asBitArray.Length + 7) / 8;
if (value is bool)
return 5;
var asString = value as string;
if (asString != null)
{
if (asString.Any(c => c != '0' && c != '1'))
throw new FormatException("Cannot interpret as ASCII BitString: " + asString);
return 4 + (asString.Length + 7)/8;
}
throw CreateConversionException(value.GetType());
}
public override void PrepareWrite(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter=null)
{
_writeBuf = buf;
_pos = -1;
_value = value;
}
public override bool Write(ref DirectBuffer directBuf)
{
var bitArray = _value as BitArray;
if (bitArray != null) {
return WriteBitArray(bitArray);
}
if (_value is bool) {
return WriteBool((bool)_value);
}
var str = _value as string;
if (str != null) {
return WriteString(str);
}
throw PGUtil.ThrowIfReached($"Bad type {_value.GetType()} some made its way into BitStringHandler.Write()");
}
bool WriteBitArray(BitArray bitArray)
{
if (_pos < 0)
{
// Initial bitlength byte
if (_writeBuf.WriteSpaceLeft < 4) { return false; }
_writeBuf.WriteInt32(bitArray.Length);
_pos = 0;
}
var byteLen = (bitArray.Length + 7) / 8;
var endPos = _pos + Math.Min(byteLen - _pos, _writeBuf.WriteSpaceLeft);
for (; _pos < endPos; _pos++) {
var bitPos = _pos * 8;
var b = 0;
for (var i = 0; i < Math.Min(8, bitArray.Length - bitPos); i++)
b += (bitArray[bitPos + i] ? 1 : 0) << (8 - i - 1);
_writeBuf.WriteByte((byte)b);
}
if (_pos < byteLen) { return false; }
_writeBuf = null;
_value = null;
return true;
}
bool WriteBool(bool b)
{
if (_writeBuf.WriteSpaceLeft < 5)
return false;
_writeBuf.WriteInt32(1);
_writeBuf.WriteByte(b ? (byte)0x80 : (byte)0);
_writeBuf = null;
_value = null;
return true;
}
bool WriteString(string str)
{
if (_pos < 0)
{
// Initial bitlength byte
if (_writeBuf.WriteSpaceLeft < 4) { return false; }
_writeBuf.WriteInt32(str.Length);
_pos = 0;
}
var byteLen = (str.Length + 7) / 8;
var bytePos = (_pos + 7) / 8;
var endBytePos = bytePos + Math.Min(byteLen - bytePos - 1, _writeBuf.WriteSpaceLeft);
for (; bytePos < endBytePos; bytePos++)
{
var b = 0;
b += (str[_pos++] - '0') << 7;
b += (str[_pos++] - '0') << 6;
b += (str[_pos++] - '0') << 5;
b += (str[_pos++] - '0') << 4;
b += (str[_pos++] - '0') << 3;
b += (str[_pos++] - '0') << 2;
b += (str[_pos++] - '0') << 1;
b += (str[_pos++] - '0');
_writeBuf.WriteByte((byte)b);
}
if (bytePos < byteLen - 1) { return false; }
if (_pos < str.Length)
{
var remainder = str.Length - _pos;
var lastChunk = 0;
for (var i = 7; i >= 8 - remainder; i--)
{
lastChunk += (str[_pos++] - '0') << i;
}
_writeBuf.WriteByte((byte)lastChunk);
}
_writeBuf = null;
_value = null;
return true;
}
#endregion
}
/// <summary>
/// A special handler for arrays of bit strings.
/// Differs from the standard array handlers in that it returns arrays of bool for BIT(1) and arrays
/// of BitArray otherwise (just like the scalar BitStringHandler does).
/// </summary>
internal class BitStringArrayHandler : ArrayHandler<BitArray>
{
[CanBeNull]
FieldDescription _fieldDescription;
object _value;
internal override Type GetElementFieldType(FieldDescription fieldDescription = null)
{
return fieldDescription?.TypeModifier == 1 ? typeof(bool) : typeof(BitArray);
}
internal override Type GetElementPsvType(FieldDescription fieldDescription)
{
return GetElementFieldType(fieldDescription);
}
public BitStringArrayHandler(IBackendType backendType, BitStringHandler elementHandler)
: base(backendType, elementHandler) {}
public override void PrepareRead(ReadBuffer buf, int len, FieldDescription fieldDescription)
{
base.PrepareRead(buf, len, fieldDescription);
_fieldDescription = fieldDescription;
}
public override bool Read(out Array result)
{
return _fieldDescription?.TypeModifier == 1
? Read<bool>(out result)
: Read<BitArray>(out result);
}
public override void PrepareWrite(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter=null)
{
base.PrepareWrite(value, buf, lengthCache, parameter);
_value = value;
}
public override bool Write(ref DirectBuffer directBuf)
{
if (_value is BitArray[]) {
return base.Write<BitArray>(ref directBuf);
}
if (_value is bool[]) {
return base.Write<bool>(ref directBuf);
}
if (_value is string[]) {
return base.Write<string>(ref directBuf);
}
throw PGUtil.ThrowIfReached($"Can't write type {_value.GetType()} as an bitstring array");
}
public override int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter=null)
{
if (value is BitArray[]) {
return base.ValidateAndGetLength<BitArray>(value, ref lengthCache, parameter);
}
if (value is bool[]) {
return base.ValidateAndGetLength<bool>(value, ref lengthCache, parameter);
}
if (value is string[]) {
return base.ValidateAndGetLength<string>(value, ref lengthCache, parameter);
}
throw new InvalidCastException($"Can't write type {value.GetType()} as an bitstring array");
}
}
}