1 // created on 29/6/2003 at 13:28
3 // Npgsql.NpgsqlBind.cs
6 // Francisco Jr. (fxjrlists@yahoo.com.br)
8 // Copyright (C) 2002 The Npgsql Development Team
9 // npgsql-general@gborg.postgresql.org
10 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 /// This class represents the Bind message sent to PostgreSQL
40 internal sealed class NpgsqlBind
43 // Logging related values
44 private static readonly String CLASSNAME
= "NpgsqlBind";
46 private String _portalName
;
47 private String _preparedStatementName
;
48 private Int16
[] _parameterFormatCodes
;
49 private Object
[] _parameterValues
;
50 private Int16
[] _resultFormatCodes
;
54 public NpgsqlBind(String portalName
,
55 String preparedStatementName
,
56 Int16
[] parameterFormatCodes
,
57 Object
[] parameterValues
,
58 Int16
[] resultFormatCodes
)
61 _portalName
= portalName
;
62 _preparedStatementName
= preparedStatementName
;
63 _parameterFormatCodes
= parameterFormatCodes
;
64 _parameterValues
= parameterValues
;
65 _resultFormatCodes
= resultFormatCodes
;
72 public String PortalName
80 public String PreparedStatementName
84 return _preparedStatementName
;
88 public Int16
[] ResultFormatCodes
92 return _resultFormatCodes
;
96 _resultFormatCodes
= value;
101 public Int16
[] ParameterFormatCodes
105 return _parameterFormatCodes
;
110 _parameterFormatCodes
= value;
115 public Object
[] ParameterValues
119 return _parameterValues
;
124 _parameterValues
= value;
129 public void WriteToStream(Stream outputStream
, Encoding encoding
)
134 Int32 messageLength
= 4 +
135 encoding
.GetByteCount(_portalName
) + 1 +
136 encoding
.GetByteCount(_preparedStatementName
) + 1 +
138 (_parameterFormatCodes
.Length
* 2) +
142 // Get size of parameter values.
145 if (_parameterValues
!= null)
146 for (i
= 0; i
< _parameterValues
.Length
; i
++)
149 if ( _parameterValues
[i
] != null)
150 if ( ((_parameterFormatCodes
.Length
== 1) && (_parameterFormatCodes
[0] == (Int16
) FormatCode
.Binary
)) ||
151 ((_parameterFormatCodes
.Length
!= 1) && (_parameterFormatCodes
[i
] == (Int16
) FormatCode
.Binary
)) )
152 messageLength
+= ((Byte
[])_parameterValues
[i
]).Length
;
154 messageLength
+= encoding
.GetByteCount((String
)_parameterValues
[i
]);
158 messageLength
+= 2 + (_resultFormatCodes
.Length
* 2);
161 outputStream
.WriteByte((Byte
)'B');
163 PGUtil
.WriteInt32(outputStream
, messageLength
);
164 PGUtil
.WriteString(_portalName
, outputStream
, encoding
);
165 PGUtil
.WriteString(_preparedStatementName
, outputStream
, encoding
);
167 PGUtil
.WriteInt16(outputStream
, (Int16
)_parameterFormatCodes
.Length
);
169 for (i
= 0; i
< _parameterFormatCodes
.Length
; i
++)
170 PGUtil
.WriteInt16(outputStream
, _parameterFormatCodes
[i
]);
172 if (_parameterValues
!= null)
174 PGUtil
.WriteInt16(outputStream
, (Int16
)_parameterValues
.Length
);
176 for (i
= 0; i
< _parameterValues
.Length
; i
++)
178 if ( ((_parameterFormatCodes
.Length
== 1) && (_parameterFormatCodes
[0] == (Int16
) FormatCode
.Binary
)) ||
179 ((_parameterFormatCodes
.Length
!= 1) && (_parameterFormatCodes
[i
] == (Int16
) FormatCode
.Binary
)) )
182 Byte
[] parameterValue
= (Byte
[])_parameterValues
[i
];
183 if (parameterValue
== null)
184 PGUtil
.WriteInt32(outputStream
, -1);
187 PGUtil
.WriteInt32(outputStream
, parameterValue
.Length
);
188 outputStream
.Write(parameterValue
, 0, parameterValue
.Length
);
193 if ((_parameterValues
[i
] == null))
194 PGUtil
.WriteInt32(outputStream
, -1);
197 Byte
[] parameterValueBytes
= encoding
.GetBytes((String
)_parameterValues
[i
]);
198 PGUtil
.WriteInt32(outputStream
, parameterValueBytes
.Length
);
199 outputStream
.Write(parameterValueBytes
, 0, parameterValueBytes
.Length
);
206 PGUtil
.WriteInt16(outputStream
, 0);
208 PGUtil
.WriteInt16(outputStream
, (Int16
)_resultFormatCodes
.Length
);
209 for (i
= 0; i
< _resultFormatCodes
.Length
; i
++)
210 PGUtil
.WriteInt16(outputStream
, _resultFormatCodes
[i
]);