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 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;
55 public NpgsqlAsciiRow(NpgsqlRowDescription rowDesc, Hashtable oidToNameMapping, Int32 protocolVersion)
57 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
59 data = new ArrayList();
61 oid_to_name_mapping = oidToNameMapping;
62 protocol_version = protocolVersion;
66 public void ReadFromStream(Stream inputStream, Encoding encoding)
68 if (protocol_version == ProtocolVersion.Version2)
70 ReadFromStream_Ver_2(inputStream, encoding);
74 ReadFromStream_Ver_3(inputStream, encoding);
78 private void ReadFromStream_Ver_2(Stream inputStream, Encoding encoding)
80 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadFromStream_Ver_2()");
82 Byte[] input_buffer = new Byte[READ_BUFFER_SIZE];
83 Byte[] null_map_array = new Byte[(row_desc.NumFields + 7)/8];
85 Array.Clear(null_map_array, 0, null_map_array.Length);
87 // Read the null fields bitmap.
88 PGUtil.CheckedStreamRead(inputStream, null_map_array, 0, null_map_array.Length );
91 for (Int16 field_count = 0; field_count < row_desc.NumFields; field_count++)
94 // Check if this field isn't null
95 if (IsBackendNull(null_map_array, field_count))
97 // Field is null just keep next field.
99 data.Add(DBNull.Value);
103 // Read the first data of the first row.
105 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, 4);
107 Int32 field_value_size = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(input_buffer, 0));
108 field_value_size -= 4;
109 Int32 bytes_left = field_value_size;
111 StringBuilder result = new StringBuilder();
113 while (bytes_left > READ_BUFFER_SIZE)
115 // Now, read just the field value.
116 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, READ_BUFFER_SIZE);
118 // Read the bytes as string.
119 result.Append(new String(encoding.GetChars(input_buffer, 0, READ_BUFFER_SIZE)));
121 bytes_left -= READ_BUFFER_SIZE;
124 // Now, read just the field value.
125 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, bytes_left);
127 // Read the bytes as string.
128 result.Append(new String(encoding.GetChars(input_buffer, 0, bytes_left)));
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));
137 private void ReadFromStream_Ver_3(Stream inputStream, Encoding encoding)
139 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadFromStream_Ver_3()");
141 Byte[] input_buffer = new Byte[READ_BUFFER_SIZE];
143 PGUtil.ReadInt32(inputStream, input_buffer);
144 Int16 numCols = PGUtil.ReadInt16(inputStream, input_buffer);
146 for (Int16 field_count = 0; field_count < numCols; field_count++)
148 Int32 field_value_size = PGUtil.ReadInt32(inputStream, input_buffer);
150 if (field_value_size == -1) // Null value
152 // Field is null just keep next field.
154 data.Add(DBNull.Value);
158 Int32 bytes_left = field_value_size;
160 StringBuilder result = new StringBuilder();
162 while (bytes_left > READ_BUFFER_SIZE)
164 // Now, read just the field value.
165 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, READ_BUFFER_SIZE);
167 // Read the bytes as string.
168 result.Append(new String(encoding.GetChars(input_buffer, 0, READ_BUFFER_SIZE)));
170 bytes_left -= READ_BUFFER_SIZE;
173 // Now, read just the field value.
174 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, bytes_left);
176 if (row_desc[field_count].format_code == FormatCode.Text)
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));
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));
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)
194 // Get the byte that holds the bit index position.
195 Byte test_byte = null_map_array[index/8];
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);
204 public Boolean IsDBNull(Int32 index)
206 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IsDBNull", index);
208 // Check valid index range.
209 if ((index < 0) || (index >= row_desc.NumFields))
210 throw new IndexOutOfRangeException("index");
212 return (this.data[index] == DBNull.Value);
215 public Object this[Int32 index]
220 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, index);
222 if ((index < 0) || (index >= row_desc.NumFields))
223 throw new IndexOutOfRangeException("this[] index value");