2009-05-13 Atsushi Enomoto <atsushi@ximian.com>
[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
54         public NpgsqlBind(String portalName,
55                           String preparedStatementName,
56                           Int16[] parameterFormatCodes,
57                           Object[] parameterValues,
58                           Int16[] resultFormatCodes)
59         {
60
61             _portalName = portalName;
62             _preparedStatementName = preparedStatementName;
63             _parameterFormatCodes = parameterFormatCodes;
64             _parameterValues = parameterValues;
65             _resultFormatCodes = resultFormatCodes;
66
67             
68
69
70         }
71
72         public String PortalName
73         {
74             get
75             {
76                 return _portalName;
77             }
78         }
79         
80         public String PreparedStatementName
81         {
82             get
83             {
84                 return _preparedStatementName;
85             }
86         }
87
88         public Int16[] ResultFormatCodes
89         {
90             get
91             {
92                 return _resultFormatCodes;
93             }
94             set
95             {
96                 _resultFormatCodes = value;
97                 
98             }
99         }
100
101         public Int16[] ParameterFormatCodes
102         {
103             get
104             {
105                 return _parameterFormatCodes;
106             }
107
108             set
109             {
110                 _parameterFormatCodes = value;
111                 
112             }
113         }
114
115         public Object[] ParameterValues
116         {
117             get
118             {
119                 return _parameterValues;
120             }
121
122             set
123             {
124                 _parameterValues = value;
125             }
126         }
127
128
129         public void WriteToStream(Stream outputStream, Encoding encoding)
130         {
131
132
133
134             Int32 messageLength = 4 +
135                     encoding.GetByteCount(_portalName) + 1 +
136                     encoding.GetByteCount(_preparedStatementName) + 1 +
137                     2 +
138                     (_parameterFormatCodes.Length * 2) +
139                     2;
140
141
142             // Get size of parameter values.
143             Int32 i;
144
145             if (_parameterValues != null)
146                 for (i = 0; i < _parameterValues.Length; i++)
147                 {
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]);
155
156                 }
157
158             messageLength += 2 + (_resultFormatCodes.Length * 2);
159
160
161             outputStream.WriteByte((Byte)'B');
162
163             PGUtil.WriteInt32(outputStream, messageLength);
164             PGUtil.WriteString(_portalName, outputStream, encoding);
165             PGUtil.WriteString(_preparedStatementName, outputStream, encoding);
166
167             PGUtil.WriteInt16(outputStream, (Int16)_parameterFormatCodes.Length);
168
169             for (i = 0; i < _parameterFormatCodes.Length; i++)
170                 PGUtil.WriteInt16(outputStream, _parameterFormatCodes[i]);
171
172             if (_parameterValues != null)
173             {
174                 PGUtil.WriteInt16(outputStream, (Int16)_parameterValues.Length);
175
176                 for (i = 0; i < _parameterValues.Length; i++)
177                 {
178                     if ( ((_parameterFormatCodes.Length == 1) && (_parameterFormatCodes[0] == (Int16) FormatCode.Binary)) ||
179                             ((_parameterFormatCodes.Length != 1) && (_parameterFormatCodes[i] == (Int16) FormatCode.Binary)) )
180                     {
181
182                         Byte[] parameterValue = (Byte[])_parameterValues[i];
183                         if (parameterValue == null)
184                             PGUtil.WriteInt32(outputStream, -1);
185                         else
186                         {
187                             PGUtil.WriteInt32(outputStream, parameterValue.Length);
188                             outputStream.Write(parameterValue, 0, parameterValue.Length);
189                         }
190                     }
191                     else
192                     {
193                         if ((_parameterValues[i] == null))
194                             PGUtil.WriteInt32(outputStream, -1);
195                         else
196                         {
197                             Byte[] parameterValueBytes = encoding.GetBytes((String)_parameterValues[i]);
198                             PGUtil.WriteInt32(outputStream, parameterValueBytes.Length);
199                             outputStream.Write(parameterValueBytes, 0, parameterValueBytes.Length);
200                         }
201                     }
202
203                 }
204             }
205             else
206                 PGUtil.WriteInt16(outputStream, 0);
207
208             PGUtil.WriteInt16(outputStream, (Int16)_resultFormatCodes.Length);
209             for (i = 0; i < _resultFormatCodes.Length; i++)
210                 PGUtil.WriteInt16(outputStream, _resultFormatCodes[i]);
211
212
213
214         }
215
216     }
217 }
218