1 // created on 4/3/2003 at 19:45
3 // Npgsql.NpgsqlBinaryRow.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 NpgsqlBinaryRow
44 // Logging related values
45 private static readonly String CLASSNAME = "NpgsqlBinaryRow";
47 private ArrayList data;
48 // private readonly Int16 READ_BUFFER_SIZE = 300; //[FIXME] Is this enough??
49 private NpgsqlRowDescription row_desc;
51 public NpgsqlBinaryRow(NpgsqlRowDescription rowDesc)
53 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
55 data = new ArrayList();
61 public void ReadFromStream(Stream inputStream, Encoding encoding)
63 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadFromStream");
65 //Byte[] input_buffer = new Byte[READ_BUFFER_SIZE];
66 Byte[] input_buffer = null;
67 Byte[] null_map_array = new Byte[(row_desc.NumFields + 7)/8];
69 null_map_array = new Byte[(row_desc.NumFields + 7)/8];
70 Array.Clear(null_map_array, 0, null_map_array.Length);
72 // Read the null fields bitmap.
73 inputStream.Read(null_map_array, 0, null_map_array.Length );
76 for (Int16 field_count = 0; field_count < row_desc.NumFields; field_count++)
79 // Check if this field isn't null
80 if (IsBackendNull(null_map_array, field_count))
82 // Field is null just keep next field.
84 data.Add(DBNull.Value);
88 // Read the first data of the first row.
90 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, 4);
92 Int32 field_value_size = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(input_buffer, 0));
94 Int32 bytes_left = field_value_size; //Size of data is the value read.
96 input_buffer = new Byte[bytes_left];
99 // Now, read just the field value.
100 PGUtil.CheckedStreamRead(inputStream, input_buffer, 0, bytes_left);
102 // Add them to the BinaryRow data.
103 data.Add(input_buffer);
109 // Using the given null field map (provided by the backend),
110 // determine if the given field index is mapped null by the backend.
111 // We only need to do this for version 2 protocol.
112 private static Boolean IsBackendNull(Byte[] null_map_array, Int32 index)
114 // Get the byte that holds the bit index position.
115 Byte test_byte = null_map_array[index/8];
117 // Now, check if index bit is set.
118 // To this, get its position in the byte, shift to
119 // MSB and test it with the byte 10000000.
120 return (((test_byte << (index%8)) & 0x80) == 0);
124 public Boolean IsDBNull(Int32 index)
126 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IsDBNull", index);
128 // Check valid index range.
129 if ((index < 0) || (index >= row_desc.NumFields))
130 throw new ArgumentOutOfRangeException("index");
132 return (this.data[index] == DBNull.Value);
135 public Object this[Int32 index]
139 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, index);
140 if ((index < 0) || (index >= row_desc.NumFields))
141 throw new ArgumentOutOfRangeException("this[] index value");