2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Npgsql / Npgsql / NpgsqlBind.cs
blob91056afc587c815442eacc67d4baf8145846061b
1 // created on 29/6/2003 at 13:28
3 // Npgsql.NpgsqlBind.cs
4 //
5 // Author:
6 // Francisco Jr. (fxjrlists@yahoo.com.br)
7 //
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
26 using System;
27 using System.IO;
28 using System.Text;
29 using System.Data;
32 namespace Npgsql
35 /// <summary>
36 /// This class represents the Bind message sent to PostgreSQL
37 /// server.
38 /// </summary>
39 ///
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
74 get
76 return _portalName;
80 public String PreparedStatementName
82 get
84 return _preparedStatementName;
88 public Int16[] ResultFormatCodes
90 get
92 return _resultFormatCodes;
94 set
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.
143 Int32 i;
145 if (_parameterValues != null)
146 for (i = 0; i < _parameterValues.Length; i++)
148 messageLength += 4;
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;
153 else
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);
185 else
187 PGUtil.WriteInt32(outputStream, parameterValue.Length);
188 outputStream.Write(parameterValue, 0, parameterValue.Length);
191 else
193 if ((_parameterValues[i] == null))
194 PGUtil.WriteInt32(outputStream, -1);
195 else
197 Byte[] parameterValueBytes = encoding.GetBytes((String)_parameterValues[i]);
198 PGUtil.WriteInt32(outputStream, parameterValueBytes.Length);
199 outputStream.Write(parameterValueBytes, 0, parameterValueBytes.Length);
205 else
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]);