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