forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBindMessage.cs
More file actions
executable file
·225 lines (196 loc) · 9 KB
/
BindMessage.cs
File metadata and controls
executable file
·225 lines (196 loc) · 9 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
#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 System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
namespace Npgsql.FrontendMessages
{
class BindMessage : ChunkingFrontendMessage
{
/// <summary>
/// The name of the destination portal (an empty string selects the unnamed portal).
/// </summary>
string Portal { get; set; }
/// <summary>
/// The name of the source prepared statement (an empty string selects the unnamed prepared statement).
/// </summary>
string Statement { get; set; }
List<NpgsqlParameter> InputParameters { get; set; }
internal List<FormatCode> ResultFormatCodes { get; private set; }
internal bool AllResultTypesAreUnknown { get; set; }
internal bool[] UnknownResultTypeList { get; set; }
State _state;
int _paramIndex;
bool _wroteParamLen;
const byte Code = (byte)'B';
internal BindMessage Populate(TypeHandlerRegistry typeHandlerRegistry, List<NpgsqlParameter> inputParameters,
string portal="", string statement="")
{
Contract.Requires(typeHandlerRegistry != null);
Contract.Requires(inputParameters != null && inputParameters.All(p => p.IsInputDirection));
Contract.Requires(portal != null);
Contract.Requires(statement != null);
AllResultTypesAreUnknown = false;
UnknownResultTypeList = null;
Portal = portal;
Statement = statement;
InputParameters = inputParameters;
_state = State.WroteNothing;
_paramIndex = 0;
_wroteParamLen = false;
return this;
}
internal override bool Write(NpgsqlBuffer buf, ref DirectBuffer directBuf)
{
Contract.Requires(Statement != null && Statement.All(c => c < 128));
Contract.Requires(Portal != null && Portal.All(c => c < 128));
switch (_state)
{
case State.WroteNothing:
var formatCodesSum = InputParameters.Select(p => p.FormatCode).Sum(c => (int)c);
var formatCodeListLength = formatCodesSum == 0 ? 0 : formatCodesSum == InputParameters.Count ? 1 : InputParameters.Count;
var headerLength =
1 + // Message code
4 + // Message length
Portal.Length + 1 +
Statement.Length + 1 +
2 + // Number of parameter format codes that follow
2 * formatCodeListLength + // List of format codes
2; // Number of parameters
if (buf.WriteSpaceLeft < headerLength)
{
Contract.Assume(buf.Size >= headerLength, "Buffer too small for Bind header");
return false;
}
foreach (var c in InputParameters.Select(p => p.LengthCache).Where(c => c != null)) { c.Rewind(); }
var messageLength = headerLength +
4 * InputParameters.Count + // Parameter lengths
InputParameters.Select(p => p.ValidateAndGetLength()).Sum() + // Parameter values
2 + // Number of result format codes
2 * (UnknownResultTypeList == null ? 1 : UnknownResultTypeList.Length); // Result format codes
buf.WriteByte(Code);
buf.WriteInt32(messageLength-1);
buf.WriteBytesNullTerminated(Encoding.ASCII.GetBytes(Portal));
buf.WriteBytesNullTerminated(Encoding.ASCII.GetBytes(Statement));
// 0 implicitly means all-text, 1 means all binary, >1 means mix-and-match
buf.WriteInt16(formatCodeListLength);
if (formatCodeListLength == 1)
{
buf.WriteInt16((short)FormatCode.Binary);
}
else if (formatCodeListLength > 1)
{
foreach (var code in InputParameters.Select(p => p.FormatCode))
buf.WriteInt16((short)code);
}
buf.WriteInt16(InputParameters.Count);
_state = State.WroteHeader;
goto case State.WroteHeader;
case State.WroteHeader:
if (!WriteParameters(buf, ref directBuf)) { return false; }
_state = State.WroteParameters;
goto case State.WroteParameters;
case State.WroteParameters:
if (UnknownResultTypeList != null)
{
if (buf.WriteSpaceLeft < 2 + UnknownResultTypeList.Length * 2) { return false; }
buf.WriteInt16(UnknownResultTypeList.Length);
foreach (var t in UnknownResultTypeList) {
buf.WriteInt16(t ? 0 : 1);
}
}
else
{
if (buf.WriteSpaceLeft < 4) { return false; }
buf.WriteInt16(1);
buf.WriteInt16(AllResultTypesAreUnknown ? 0 : 1);
}
_state = State.Done;
return true;
default:
throw PGUtil.ThrowIfReached();
}
}
bool WriteParameters(NpgsqlBuffer buf, ref DirectBuffer directBuf)
{
for (; _paramIndex < InputParameters.Count; _paramIndex++)
{
var param = InputParameters[_paramIndex];
if (!_wroteParamLen)
{
if (param.Value is DBNull)
{
if (buf.WriteSpaceLeft < 4) { return false; }
buf.WriteInt32(-1);
continue;
}
if (param.LengthCache != null) {
param.LengthCache.Rewind();
}
}
var handler = param.Handler;
var asChunkingWriter = handler as IChunkingTypeWriter;
if (asChunkingWriter != null)
{
if (!_wroteParamLen)
{
if (buf.WriteSpaceLeft < 4) { return false; }
buf.WriteInt32(param.ValidateAndGetLength());
asChunkingWriter.PrepareWrite(param.Value, buf, param.LengthCache, param);
_wroteParamLen = true;
}
if (!asChunkingWriter.Write(ref directBuf)) {
return false;
}
_wroteParamLen = false;
continue;
}
var len = param.ValidateAndGetLength();
var asSimpleWriter = (ISimpleTypeWriter)handler;
if (buf.WriteSpaceLeft < len + 4)
{
Contract.Assume(buf.Size >= len + 4);
return false;
}
buf.WriteInt32(len);
asSimpleWriter.Write(param.Value, buf, param);
}
return true;
}
public override string ToString()
{
return String.Format("[Bind(Portal={0},Statement={1},NumParams={2}]", Portal, Statement, InputParameters.Count);
}
private enum State
{
WroteNothing,
WroteHeader,
WroteParameters,
Done
}
}
}