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;
53 public NpgsqlBind(String portalName,
54 String preparedStatementName,
55 Int16[] parameterFormatCodes,
56 Object[] parameterValues,
57 Int16[] resultFormatCodes)
60 _portalName = portalName;
61 _preparedStatementName = preparedStatementName;
62 _parameterFormatCodes = parameterFormatCodes;
63 _parameterValues = parameterValues;
64 _resultFormatCodes = resultFormatCodes;
69 public String PortalName
77 public Int16[] ParameterFormatCodes
81 return _parameterFormatCodes;
86 _parameterFormatCodes = value;
90 public Object[] ParameterValues
94 return _parameterValues;
99 _parameterValues = value;
104 public void WriteToStream(Stream outputStream, Encoding encoding)
109 Int32 messageLength = 4 +
110 encoding.GetByteCount(_portalName) + 1 +
111 encoding.GetByteCount(_preparedStatementName) + 1 +
113 (_parameterFormatCodes.Length * 2) +
117 // Get size of parameter values.
120 if (_parameterValues != null)
121 for (i = 0; i < _parameterValues.Length; i++)
124 if ( _parameterValues[i] != null)
125 if ( ((_parameterFormatCodes.Length == 1) && (_parameterFormatCodes[0] == (Int16) FormatCode.Binary)) ||
126 ((_parameterFormatCodes.Length != 1) && (_parameterFormatCodes[i] == (Int16) FormatCode.Binary)) )
127 messageLength += ((Byte[])_parameterValues[i]).Length;
129 messageLength += encoding.GetByteCount((String)_parameterValues[i]);
133 messageLength += 2 + (_resultFormatCodes.Length * 2);
136 outputStream.WriteByte((Byte)'B');
138 PGUtil.WriteInt32(outputStream, messageLength);
139 PGUtil.WriteString(_portalName, outputStream, encoding);
140 PGUtil.WriteString(_preparedStatementName, outputStream, encoding);
142 PGUtil.WriteInt16(outputStream, (Int16)_parameterFormatCodes.Length);
144 for (i = 0; i < _parameterFormatCodes.Length; i++)
145 PGUtil.WriteInt16(outputStream, _parameterFormatCodes[i]);
147 if (_parameterValues != null)
149 PGUtil.WriteInt16(outputStream, (Int16)_parameterValues.Length);
151 for (i = 0; i < _parameterValues.Length; i++)
153 if ( ((_parameterFormatCodes.Length == 1) && (_parameterFormatCodes[0] == (Int16) FormatCode.Binary)) ||
154 ((_parameterFormatCodes.Length != 1) && (_parameterFormatCodes[i] == (Int16) FormatCode.Binary)) )
157 Byte[] parameterValue = (Byte[])_parameterValues[i];
158 if (parameterValue == null)
159 PGUtil.WriteInt32(outputStream, -1);
162 PGUtil.WriteInt32(outputStream, parameterValue.Length);
163 outputStream.Write(parameterValue, 0, parameterValue.Length);
168 if ((_parameterValues[i] == null))
169 PGUtil.WriteInt32(outputStream, -1);
172 String parameterValue = (String)_parameterValues[i];
173 PGUtil.WriteInt32(outputStream, encoding.GetByteCount(parameterValue));
174 outputStream.Write(encoding.GetBytes(parameterValue), 0, encoding.GetByteCount(parameterValue));
181 PGUtil.WriteInt16(outputStream, 0);
183 PGUtil.WriteInt16(outputStream, (Int16)_resultFormatCodes.Length);
184 for (i = 0; i < _resultFormatCodes.Length; i++)
185 PGUtil.WriteInt16(outputStream, _resultFormatCodes[i]);