2004-11-25 Francisco Figueiredo Jr. <fxjrlists@yahoo.com.br>
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlBind.cs
1 // created on 29/6/2003 at 13:28
2
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
11 //
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.
16 //
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.
21 //
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
25
26 using System;
27 using System.IO;
28 using System.Text;
29 using System.Data;
30
31
32 namespace Npgsql
33 {
34
35     /// <summary>
36     /// This class represents the Bind message sent to PostgreSQL
37     /// server.
38     /// </summary>
39     ///
40     internal sealed class NpgsqlBind
41     {
42
43         // Logging related values
44         private static readonly String CLASSNAME = "NpgsqlBind";
45
46         private String _portalName;
47         private String _preparedStatementName;
48         private Int16[] _parameterFormatCodes;
49         private Object[] _parameterValues;
50         private Int16[] _resultFormatCodes;
51
52
53         public NpgsqlBind(String portalName,
54                           String preparedStatementName,
55                           Int16[] parameterFormatCodes,
56                           Object[] parameterValues,
57                           Int16[] resultFormatCodes)
58         {
59
60             _portalName = portalName;
61             _preparedStatementName = preparedStatementName;
62             _parameterFormatCodes = parameterFormatCodes;
63             _parameterValues = parameterValues;
64             _resultFormatCodes = resultFormatCodes;
65
66
67         }
68
69         public String PortalName
70         {
71             get
72             {
73                 return _portalName;
74             }
75         }
76
77         public Int16[] ParameterFormatCodes
78         {
79             get
80             {
81                 return _parameterFormatCodes;
82             }
83
84             set
85             {
86                 _parameterFormatCodes = value;
87             }
88         }
89
90         public Object[] ParameterValues
91         {
92             get
93             {
94                 return _parameterValues;
95             }
96
97             set
98             {
99                 _parameterValues = value;
100             }
101         }
102
103
104         public void WriteToStream(Stream outputStream, Encoding encoding)
105         {
106
107
108
109             Int32 messageLength = 4 +
110                                   encoding.GetByteCount(_portalName) + 1 +
111                                   encoding.GetByteCount(_preparedStatementName) + 1 +
112                                   2 +
113                                   (_parameterFormatCodes.Length * 2) +
114                                   2;
115
116
117             // Get size of parameter values.
118             Int32 i;
119
120             if (_parameterValues != null)
121                 for (i = 0; i < _parameterValues.Length; i++)
122                 {
123                     messageLength += 4;
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;
128                         else
129                             messageLength += encoding.GetByteCount((String)_parameterValues[i]);
130
131                 }
132
133             messageLength += 2 + (_resultFormatCodes.Length * 2);
134
135
136             outputStream.WriteByte((Byte)'B');
137
138             PGUtil.WriteInt32(outputStream, messageLength);
139             PGUtil.WriteString(_portalName, outputStream, encoding);
140             PGUtil.WriteString(_preparedStatementName, outputStream, encoding);
141
142             PGUtil.WriteInt16(outputStream, (Int16)_parameterFormatCodes.Length);
143
144             for (i = 0; i < _parameterFormatCodes.Length; i++)
145                 PGUtil.WriteInt16(outputStream, _parameterFormatCodes[i]);
146
147             if (_parameterValues != null)
148             {
149                 PGUtil.WriteInt16(outputStream, (Int16)_parameterValues.Length);
150
151                 for (i = 0; i < _parameterValues.Length; i++)
152                 {
153                     if ( ((_parameterFormatCodes.Length == 1) && (_parameterFormatCodes[0] == (Int16) FormatCode.Binary)) ||
154                             ((_parameterFormatCodes.Length != 1) && (_parameterFormatCodes[i] == (Int16) FormatCode.Binary)) )
155                     {
156
157                         Byte[] parameterValue = (Byte[])_parameterValues[i];
158                         if (parameterValue == null)
159                             PGUtil.WriteInt32(outputStream, -1);
160                         else
161                         {
162                             PGUtil.WriteInt32(outputStream, parameterValue.Length);
163                             outputStream.Write(parameterValue, 0, parameterValue.Length);
164                         }
165                     }
166                     else
167                     {
168                         if ((_parameterValues[i] == null))
169                             PGUtil.WriteInt32(outputStream, -1);
170                         else
171                         {
172                             String parameterValue = (String)_parameterValues[i];
173                             PGUtil.WriteInt32(outputStream, encoding.GetByteCount(parameterValue));
174                             outputStream.Write(encoding.GetBytes(parameterValue), 0, encoding.GetByteCount(parameterValue));
175                         }
176                     }
177
178                 }
179             }
180             else
181                 PGUtil.WriteInt16(outputStream, 0);
182
183             PGUtil.WriteInt16(outputStream, (Int16)_resultFormatCodes.Length);
184             for (i = 0; i < _resultFormatCodes.Length; i++)
185                 PGUtil.WriteInt16(outputStream, _resultFormatCodes[i]);
186
187
188
189         }
190
191     }
192 }
193