1 // created on 13/6/2002 at 21:06
3 // Npgsql.NpgsqlAsciiRow.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
27 using System.Collections;
38 /// This class represents the AsciiRow message sent from PostgreSQL
42 internal sealed class NpgsqlAsciiRow
44 // Logging related values
45 private static readonly String CLASSNAME = "NpgsqlAsciiRow";
47 private ArrayList data;
48 private Byte[] null_map_array;
49 private Int16 num_fields;
50 private readonly Int16 READ_BUFFER_SIZE = 300; //[FIXME] Is this enough??
51 private NpgsqlRowDescription row_desc;
52 private Hashtable oid_to_name_mapping;
53 private Int32 protocol_version;
57 public NpgsqlAsciiRow(NpgsqlRowDescription rowDesc, Hashtable oidToNameMapping, Int32 protocolVersion)
59 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
61 data = new ArrayList();
63 null_map_array = new Byte[(row_desc.NumFields + 7)/8];
64 oid_to_name_mapping = oidToNameMapping;
65 protocol_version = protocolVersion;
67 //num_fields = numFields;
73 public void ReadFromStream(Stream inputStream, Encoding encoding)
75 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadFromStream()");
77 Byte[] input_buffer = new Byte[READ_BUFFER_SIZE];
80 if (protocol_version == ProtocolVersion.Version2)
83 Array.Clear(null_map_array, 0, null_map_array.Length);
85 // Read the null fields bitmap.
86 PGUtil.CheckedStreamRead(inputStream, null_map_array, 0, null_map_array.Length );
89 for (Int16 field_count = 0; field_count < row_desc.NumFields; field_count++)
92 // Check if this field isn't null
93 if (IsNull(field_count))
95 // Field is null just keep next field.
97 //[FIXME] See this[] method.
102 // Read the first data of the first row.
104 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, 4);
106 Int32 field_value_size = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(input_buffer, 0));
107 field_value_size -= 4;
108 Int32 bytes_left = field_value_size;
110 StringBuilder result = new StringBuilder();
112 while (bytes_left > READ_BUFFER_SIZE)
114 // Now, read just the field value.
115 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, READ_BUFFER_SIZE);
117 // Read the bytes as string.
118 result.Append(new String(encoding.GetChars(input_buffer, 0, READ_BUFFER_SIZE)));
120 bytes_left -= READ_BUFFER_SIZE;
123 // Now, read just the field value.
124 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, bytes_left);
126 // Read the bytes as string.
127 result.Append(new String(encoding.GetChars(input_buffer, 0, bytes_left)));
130 // Add them to the AsciiRow data.
131 data.Add(NpgsqlTypesHelper.ConvertBackendStringToSystemType(oid_to_name_mapping, result.ToString(), row_desc[field_count].type_oid, row_desc[field_count].type_modifier));
138 PGUtil.ReadInt32(inputStream, input_buffer);
139 Int16 numCols = PGUtil.ReadInt16(inputStream, input_buffer);
141 for (Int16 field_count = 0; field_count < numCols; field_count++)
143 Int32 field_value_size = PGUtil.ReadInt32(inputStream, input_buffer);
145 if (field_value_size == -1) // Null value
147 // Field is null just keep next field.
149 //[FIXME] See this[] method.
154 Int32 bytes_left = field_value_size;
156 StringBuilder result = new StringBuilder();
158 while (bytes_left > READ_BUFFER_SIZE)
160 // Now, read just the field value.
161 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, READ_BUFFER_SIZE);
163 // Read the bytes as string.
164 result.Append(new String(encoding.GetChars(input_buffer, 0, READ_BUFFER_SIZE)));
166 bytes_left -= READ_BUFFER_SIZE;
169 // Now, read just the field value.
170 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, bytes_left);
172 if (row_desc[field_count].format_code == FormatCode.Text)
174 // Read the bytes as string.
175 result.Append(new String(encoding.GetChars(input_buffer, 0, bytes_left)));
176 // Add them to the AsciiRow data.
177 data.Add(NpgsqlTypesHelper.ConvertBackendStringToSystemType(oid_to_name_mapping, result.ToString(), row_desc[field_count].type_oid, row_desc[field_count].type_modifier));
180 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));
188 public Boolean IsNull(Int32 index)
191 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IsNull", index);
192 // [FIXME] Check more optimized way of doing this.
193 // Should this be public or internal?
195 // Check valid index range.
196 if ((index < 0) || (index >= row_desc.NumFields))
197 throw new ArgumentOutOfRangeException("index");
199 // Check if the value (index) of the field is null
201 // Get the byte that holds the bit index position.
202 Byte test_byte = null_map_array[index/8];
204 // Now, check if index bit is set.
205 // To this, get its position in the byte, shift to
206 // MSB and test it with the byte 10000000.
207 return (((test_byte << (index%8)) & 0x80) == 0);
211 public Object this[Int32 index]
216 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, index);
218 if ((index < 0) || (index >= row_desc.NumFields))
219 throw new ArgumentOutOfRangeException("this[] index value");
220 // [FIXME] Should return null or something else
223 //[FIXME] This code assumes that the data arraylist has the null and non null values
224 // in order, but just the non-null values are added.
225 // It is necessary to map the index value with the elements in the array list.
226 // For now, the workaround is to insert the null values in the array list.
227 // But this is a hack. :)
229 //return (IsNull(index) ? null : data[index]);