2004-05-09 Francisco Figueiredo Jr. <fxjrlists@yahoo.com.br>
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlAsciiRow.cs
1 // created on 13/6/2002 at 21:06
2
3 // Npgsql.NpgsqlAsciiRow.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.Collections;
28 using System.IO;
29 using System.Text;
30 using System.Net;
31 using NpgsqlTypes;
32
33
34 namespace Npgsql
35 {
36
37     /// <summary>
38     /// This class represents the AsciiRow message sent from PostgreSQL
39     /// server.
40     /// </summary>
41     ///
42     internal sealed class NpgsqlAsciiRow
43     {
44         // Logging related values
45         private static readonly String CLASSNAME = "NpgsqlAsciiRow";
46
47         private ArrayList                                                       data;
48         private readonly Int16  READ_BUFFER_SIZE = 300; //[FIXME] Is this enough??
49         private NpgsqlRowDescription row_desc;
50         private Hashtable                                                       oid_to_name_mapping;
51         private Int32                 protocol_version;
52
53
54
55         public NpgsqlAsciiRow(NpgsqlRowDescription rowDesc, Hashtable oidToNameMapping, Int32 protocolVersion)
56         {
57             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
58
59             data = new ArrayList();
60             row_desc = rowDesc;
61             oid_to_name_mapping = oidToNameMapping;
62             protocol_version = protocolVersion;
63
64         }
65
66         public void ReadFromStream(Stream inputStream, Encoding encoding)
67         {
68             if (protocol_version == ProtocolVersion.Version2)
69             {
70                 ReadFromStream_Ver_2(inputStream, encoding);
71             }
72             else
73             {
74                 ReadFromStream_Ver_3(inputStream, encoding);
75             }
76         }
77
78         private void ReadFromStream_Ver_2(Stream inputStream, Encoding encoding)
79         {
80             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadFromStream_Ver_2()");
81
82             Byte[]       input_buffer = new Byte[READ_BUFFER_SIZE];
83             Byte[]       null_map_array = new Byte[(row_desc.NumFields + 7)/8];
84
85             Array.Clear(null_map_array, 0, null_map_array.Length);
86
87             // Read the null fields bitmap.
88             PGUtil.CheckedStreamRead(inputStream, null_map_array, 0, null_map_array.Length );
89
90             // Get the data.
91             for (Int16 field_count = 0; field_count < row_desc.NumFields; field_count++)
92             {
93
94                 // Check if this field isn't null
95                 if (IsBackendNull(null_map_array, field_count))
96                 {
97                     // Field is null just keep next field.
98
99                     data.Add(DBNull.Value);
100                     continue;
101                 }
102
103                 // Read the first data of the first row.
104
105                 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, 4);
106
107                 Int32 field_value_size = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(input_buffer, 0));
108                 field_value_size -= 4;
109                 Int32 bytes_left = field_value_size;
110
111                 StringBuilder result = new StringBuilder();
112
113                 while (bytes_left > READ_BUFFER_SIZE)
114                 {
115                     // Now, read just the field value.
116                     PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, READ_BUFFER_SIZE);
117
118                     // Read the bytes as string.
119                     result.Append(new String(encoding.GetChars(input_buffer, 0, READ_BUFFER_SIZE)));
120
121                     bytes_left -= READ_BUFFER_SIZE;
122                 }
123
124                 // Now, read just the field value.
125                 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, bytes_left);
126
127                 // Read the bytes as string.
128                 result.Append(new String(encoding.GetChars(input_buffer, 0, bytes_left)));
129
130
131                 // Add them to the AsciiRow data.
132                 data.Add(NpgsqlTypesHelper.ConvertBackendStringToSystemType(oid_to_name_mapping, result.ToString(), row_desc[field_count].type_oid, row_desc[field_count].type_modifier));
133
134             }
135         }
136
137         private void ReadFromStream_Ver_3(Stream inputStream, Encoding encoding)
138         {
139             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadFromStream_Ver_3()");
140
141             Byte[] input_buffer = new Byte[READ_BUFFER_SIZE];
142
143             PGUtil.ReadInt32(inputStream, input_buffer);
144             Int16 numCols = PGUtil.ReadInt16(inputStream, input_buffer);
145
146             for (Int16 field_count = 0; field_count < numCols; field_count++)
147             {
148                 Int32 field_value_size = PGUtil.ReadInt32(inputStream, input_buffer);
149
150                 if (field_value_size == -1) // Null value
151                 {
152                     // Field is null just keep next field.
153
154                     data.Add(DBNull.Value);
155                     continue;
156
157                 }
158                 Int32 bytes_left = field_value_size;
159
160                 StringBuilder result = new StringBuilder();
161
162                 while (bytes_left > READ_BUFFER_SIZE)
163                 {
164                     // Now, read just the field value.
165                     PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, READ_BUFFER_SIZE);
166
167                     // Read the bytes as string.
168                     result.Append(new String(encoding.GetChars(input_buffer, 0, READ_BUFFER_SIZE)));
169
170                     bytes_left -= READ_BUFFER_SIZE;
171                 }
172
173                 // Now, read just the field value.
174                 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, bytes_left);
175
176                 if (row_desc[field_count].format_code == FormatCode.Text)
177                 {
178                     // Read the bytes as string.
179                     result.Append(new String(encoding.GetChars(input_buffer, 0, bytes_left)));
180                     // Add them to the AsciiRow data.
181                     data.Add(NpgsqlTypesHelper.ConvertBackendStringToSystemType(oid_to_name_mapping, result.ToString(), row_desc[field_count].type_oid, row_desc[field_count].type_modifier));
182                 }
183                 else
184                     data.Add(NpgsqlTypesHelper.ConvertBackendBytesToStytemType(oid_to_name_mapping, input_buffer, encoding, field_value_size, row_desc[field_count].type_oid, row_desc[field_count].type_modifier));
185             }
186         }
187
188         // Using the given null field map (provided by the backend),
189         // determine if the given field index is mapped null by the backend.
190         // We only need to do this for version 2 protocol.
191         private static Boolean IsBackendNull(Byte[] null_map_array, Int32 index)
192         {
193
194             // Get the byte that holds the bit index position.
195             Byte test_byte = null_map_array[index/8];
196
197             // Now, check if index bit is set.
198             // To this, get its position in the byte, shift to
199             // MSB and test it with the byte 10000000.
200             return (((test_byte << (index%8)) & 0x80) == 0);
201         }
202
203
204         public Boolean IsDBNull(Int32 index)
205         {
206             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IsDBNull", index);
207
208             // Check valid index range.
209             if ((index < 0) || (index >= row_desc.NumFields))
210                 throw new IndexOutOfRangeException("index");
211
212             return (this.data[index] == DBNull.Value);
213         }
214
215         public Object this[Int32 index]
216         {
217             get
218             {
219
220                 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, index);
221
222                 if ((index < 0) || (index >= row_desc.NumFields))
223                     throw new IndexOutOfRangeException("this[] index value");
224
225                 return data[index];
226
227
228
229             }
230         }
231     }
232
233 }