forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextHandler.cs
More file actions
244 lines (208 loc) · 10.2 KB
/
TextHandler.cs
File metadata and controls
244 lines (208 loc) · 10.2 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
#region License
// The PostgreSQL License
//
// Copyright (C) 2017 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.IO;
using Npgsql.BackendMessages;
using NpgsqlTypes;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Npgsql.TypeHandling;
using Npgsql.TypeMapping;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
namespace Npgsql.TypeHandlers
{
[TypeMapping("text", NpgsqlDbType.Text,
new[] { DbType.String, DbType.StringFixedLength, DbType.AnsiString, DbType.AnsiStringFixedLength },
new[] { typeof(string), typeof(char[]), typeof(char), typeof(ArraySegment<char>) },
DbType.String
)]
[TypeMapping("xml", NpgsqlDbType.Xml, dbType: DbType.Xml)]
[TypeMapping("varchar", NpgsqlDbType.Varchar, inferredDbType: DbType.String)]
[TypeMapping("bpchar", NpgsqlDbType.Char, inferredDbType: DbType.String)]
[TypeMapping("name", NpgsqlDbType.Name, inferredDbType: DbType.String)]
[TypeMapping("json", NpgsqlDbType.Json, inferredDbType: DbType.String)]
[TypeMapping("refcursor", NpgsqlDbType.Refcursor, inferredDbType: DbType.String)]
[TypeMapping("citext", NpgsqlDbType.Citext, inferredDbType: DbType.String)]
[TypeMapping("unknown")]
public class TextHandlerFactory : NpgsqlTypeHandlerFactory<string>
{
protected override NpgsqlTypeHandler<string> Create(NpgsqlConnection conn)
=> new TextHandler(conn);
}
public class TextHandler : NpgsqlTypeHandler<string>, INpgsqlTypeHandler<char[]>, INpgsqlTypeHandler<ArraySegment<char>>,
INpgsqlTypeHandler<char>, ITextReaderHandler
{
// Text types are handled a bit more efficiently when sent as text than as binary
// see https://github.com/npgsql/npgsql/issues/1210#issuecomment-235641670
internal override bool PreferTextWrite => true;
readonly Encoding _encoding;
#region State
readonly char[] _singleCharArray = new char[1];
#endregion
protected internal TextHandler(NpgsqlConnection connection)
{
_encoding = connection.Connector.TextEncoding;
}
#region Read
public override ValueTask<string> Read(NpgsqlReadBuffer buf, int byteLen, bool async, FieldDescription fieldDescription = null)
{
if (buf.ReadBytesLeft >= byteLen)
return new ValueTask<string>(buf.ReadString(byteLen));
return ReadLong(buf, byteLen, async);
}
async ValueTask<string> ReadLong(NpgsqlReadBuffer buf, int byteLen, bool async)
{
if (byteLen <= buf.Size)
{
// The string's byte representation can fit in our read buffer, read it.
while (buf.ReadBytesLeft < byteLen)
await buf.ReadMore(async);
return buf.ReadString(byteLen);
}
// Bad case: the string's byte representation doesn't fit in our buffer.
// This is rare - will only happen in CommandBehavior.Sequential mode (otherwise the
// entire row is in memory). Tweaking the buffer length via the connection string can
// help avoid this.
// Allocate a temporary byte buffer to hold the entire string and read it in chunks.
var tempBuf = new byte[byteLen];
var pos = 0;
while (true)
{
var len = Math.Min(buf.ReadBytesLeft, byteLen - pos);
buf.ReadBytes(tempBuf, pos, len);
pos += len;
if (pos < byteLen)
{
await buf.ReadMore(async);
continue;
}
break;
}
return buf.TextEncoding.GetString(tempBuf);
}
async ValueTask<char[]> INpgsqlTypeHandler<char[]>.Read(NpgsqlReadBuffer buf, int byteLen, bool async, FieldDescription fieldDescription)
{
if (byteLen <= buf.Size)
{
// The string's byte representation can fit in our read buffer, read it.
while (buf.ReadBytesLeft < byteLen)
await buf.ReadMore(async);
return buf.ReadChars(byteLen);
}
// TODO: The following can be optimized with Decoder - no need to allocate a byte[]
var tempBuf = new byte[byteLen];
var pos = 0;
while (true)
{
var len = Math.Min(buf.ReadBytesLeft, byteLen - pos);
buf.ReadBytes(tempBuf, pos, len);
pos += len;
if (pos < byteLen)
{
await buf.ReadMore(async);
continue;
}
break;
}
return buf.TextEncoding.GetChars(tempBuf);
}
ValueTask<ArraySegment<char>> INpgsqlTypeHandler<ArraySegment<char>>.Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription)
{
buf.Skip(len);
throw new NpgsqlSafeReadException(new NotSupportedException("Only writing ArraySegment<char> to PostgreSQL text is supported, no reading."));
}
ValueTask<char> INpgsqlTypeHandler<char>.Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription)
{
buf.Skip(len);
throw new NpgsqlSafeReadException(new NotSupportedException("Only writing char to PostgreSQL text is supported, no reading."));
}
#endregion
#region Write
public override unsafe int ValidateAndGetLength(string value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
{
if (lengthCache == null)
lengthCache = new NpgsqlLengthCache(1);
if (lengthCache.IsPopulated)
return lengthCache.Get();
if (parameter == null || parameter.Size <= 0 || parameter.Size >= value.Length)
return lengthCache.Set(_encoding.GetByteCount(value));
fixed (char* p = value)
return lengthCache.Set(_encoding.GetByteCount(p, parameter.Size));
}
public virtual int ValidateAndGetLength(char[] value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
{
if (lengthCache == null)
lengthCache = new NpgsqlLengthCache(1);
if (lengthCache.IsPopulated)
return lengthCache.Get();
return lengthCache.Set(
parameter == null || parameter.Size <= 0 || parameter.Size >= value.Length
? _encoding.GetByteCount(value)
: _encoding.GetByteCount(value, 0, parameter.Size)
);
}
public int ValidateAndGetLength(ArraySegment<char> value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
{
if (lengthCache == null)
lengthCache = new NpgsqlLengthCache(1);
if (lengthCache.IsPopulated)
return lengthCache.Get();
if (parameter?.Size > 0)
throw new ArgumentException($"Parameter {parameter.ParameterName} is of type ArraySegment<char> and should not have its Size set", parameter.ParameterName);
return lengthCache.Set(_encoding.GetByteCount(value.Array, value.Offset, value.Count));
}
public int ValidateAndGetLength(char value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
{
_singleCharArray[0] = value;
return _encoding.GetByteCount(_singleCharArray);
}
public override Task Write(string value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
=> WriteString(value, buf, lengthCache, parameter, async);
public virtual Task Write(char[] value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
{
var charLen = parameter == null || parameter.Size <= 0 || parameter.Size >= value.Length
? value.Length
: parameter.Size;
return buf.WriteChars(value, 0, charLen, lengthCache.GetLast(), async);
}
public Task Write(ArraySegment<char> value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
=> buf.WriteChars(value.Array, value.Offset, value.Count, lengthCache.GetLast(), async);
Task WriteString(string str, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, [CanBeNull] NpgsqlParameter parameter, bool async)
{
var charLen = parameter == null || parameter.Size <= 0 || parameter.Size >= str.Length
? str.Length
: parameter.Size;
return buf.WriteString(str, charLen, lengthCache.GetLast(), async);
}
public Task Write(char value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
{
_singleCharArray[0] = value;
var len = _encoding.GetByteCount(_singleCharArray);
return buf.WriteChars(_singleCharArray, 0, 1, len, async);
}
#endregion
public virtual TextReader GetTextReader(Stream stream) => new StreamReader(stream);
}
}