forked from Emill/Npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitStringHandler.cs
More file actions
executable file
·341 lines (294 loc) · 11.5 KB
/
BitStringHandler.cs
File metadata and controls
executable file
·341 lines (294 loc) · 11.5 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
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;
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 : TypeHandler,
IChunkingTypeReader<BitArray>, IChunkingTypeWriter,
ISimpleTypeReader<bool>
{
NpgsqlBuffer _buf;
int _len;
BitArray _bitArray;
object _value;
int _pos;
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 object ReadValueAsObject(DataRowMessage row, FieldDescription fieldDescription)
{
var result = fieldDescription.TypeModifier == 1
? (object)((ISimpleTypeReader<bool>) this).Read(row.Buffer, row.ColumnLen, fieldDescription)
: Read<BitArray>(row, row.ColumnLen, fieldDescription);
row.PosInColumn += row.ColumnLen;
return result;
}
internal override object ReadPsvAsObject(DataRowMessage row, FieldDescription fieldDescription)
{
return ReadValueAsObject(row, fieldDescription);
}
#region Read
bool ISimpleTypeReader<bool>.Read(NpgsqlBuffer buf, int len, FieldDescription fieldDescription)
{
if (fieldDescription != null && fieldDescription.TypeModifier != 1) {
throw new InvalidCastException(String.Format("Can't convert a BIT({0}) type to bool, only BIT(1)", fieldDescription.TypeModifier));
}
var bitLen = buf.ReadInt32();
Contract.Assume(bitLen == 1);
var b = buf.ReadByte();
return (b & 128) != 0;
}
public void PrepareRead(NpgsqlBuffer buf, int len, FieldDescription fieldDescription)
{
_buf = buf;
_pos = -1;
_len = len - 4; // Subtract leading bit length field
}
/// <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 bool Read(out BitArray result)
{
if (_pos == -1)
{
if (_buf.ReadBytesLeft < 4)
{
result = null;
return false;
}
var numBits = _buf.ReadInt32();
_bitArray = new BitArray(numBits);
_pos = 0;
}
var bitNo = _pos * 8;
var maxBytes = _pos + Math.Min(_len - _pos - 1, _buf.ReadBytesLeft);
for (; _pos < maxBytes; _pos++)
{
var chunk = _buf.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 = _buf.ReadByte();
for (var i = 7; i >= 8 - remainder; i--)
{
_bitArray[bitNo++] = (lastChunk & (1 << i)) != 0;
}
}
result = _bitArray;
return true;
}
#endregion
#region Write
public 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 new InvalidCastException("Expected BitArray, bool or string");
}
public void PrepareWrite(object value, NpgsqlBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter=null)
{
_buf = buf;
_pos = -1;
_value = value;
}
public 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(String.Format("Bad type {0} some made its way into BitStringHandler.Write()", _value.GetType()));
}
bool WriteBitArray(BitArray bitArray)
{
if (_pos < 0)
{
// Initial bitlength byte
if (_buf.WriteSpaceLeft < 4) { return false; }
_buf.WriteInt32(bitArray.Length);
_pos = 0;
}
var byteLen = (bitArray.Length + 7) / 8;
var endPos = _pos + Math.Min(byteLen - _pos, _buf.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);
_buf.WriteByte((byte)b);
}
if (_pos < byteLen) { return false; }
_buf = null;
_value = null;
return true;
}
bool WriteBool(bool b)
{
if (_buf.WriteSpaceLeft < 5)
return false;
_buf.WriteInt32(1);
_buf.WriteByte(b ? (byte)0x80 : (byte)0);
_buf = null;
_value = null;
return true;
}
bool WriteString(string str)
{
if (_pos < 0)
{
// Initial bitlength byte
if (_buf.WriteSpaceLeft < 4) { return false; }
_buf.WriteInt32(str.Length);
_pos = 0;
}
var byteLen = (str.Length + 7) / 8;
var bytePos = (_pos + 7) / 8;
var endBytePos = bytePos + Math.Min(byteLen - bytePos - 1, _buf.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');
_buf.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;
}
_buf.WriteByte((byte)lastChunk);
}
_buf = 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,
IChunkingTypeReader<Array>, IChunkingTypeWriter
{
FieldDescription _fieldDescription;
object _value;
internal override Type GetElementFieldType(FieldDescription fieldDescription)
{
return fieldDescription.TypeModifier == 1 ? typeof(bool) : typeof(BitArray);
}
internal override Type GetElementPsvType(FieldDescription fieldDescription)
{
return GetElementFieldType(fieldDescription);
}
public BitStringArrayHandler(BitStringHandler elementHandler)
: base(elementHandler) {}
public void PrepareRead(NpgsqlBuffer buf, int len, FieldDescription fieldDescription)
{
base.PrepareRead(buf, fieldDescription, len);
_fieldDescription = fieldDescription;
}
public bool Read(out Array result)
{
return _fieldDescription.TypeModifier == 1
? Read<bool>(out result)
: Read<BitArray>(out result);
}
public override void PrepareWrite(object value, NpgsqlBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter=null)
{
base.PrepareWrite(value, buf, lengthCache, parameter);
_value = value;
}
public 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(String.Format("Can't write type {0} as an bitstring array", _value.GetType()));
}
public 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(String.Format("Can't write type {0} as an bitstring array", value.GetType()));
}
}
}