* FileSystemInfo.cs: corrected COM visibility of UTC properties
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlError.cs
1 // created on 12/7/2003 at 18:36
2
3 // Npgsql.NpgsqlError.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 ErrorResponse message sent from PostgreSQL
35     /// server.
36     /// </summary>
37     ///
38     internal sealed class NpgsqlError
39     {
40         // Logging related values
41         private static readonly String CLASSNAME = "NpgsqlError";
42
43         private Int32 _protocolVersion;
44         private String _severity;
45         private String _code;
46         private String _message;
47         private String _detail;
48         private String _hint;
49         private String _position;
50         private String _where;
51         private String _file;
52         private String _line;
53         private String _routine;
54
55
56
57         public String Message
58         {
59             get
60             {
61                 return _message;
62             }
63         }
64
65
66
67
68         public NpgsqlError(Int32 protocolVersion)
69         {
70             _protocolVersion = protocolVersion;
71
72         }
73
74         public void ReadFromStream(Stream inputStream, Encoding encoding)
75         {
76
77             if (_protocolVersion == ProtocolVersion.Version2)
78             {
79                 _message = PGUtil.ReadString(inputStream, encoding);
80
81             }
82             else
83             {
84                 Int32 messageLength = PGUtil.ReadInt32(inputStream, new Byte[4]);
85
86                 //[TODO] Would this be the right way to do?
87                 // Check the messageLength value. If it is 1178686529, this would be the
88                 // "FATA" string, which would mean a protocol 2.0 error string.
89
90                 if (messageLength == 1178686529)
91                 {
92                     _message = "FATA" + PGUtil.ReadString(inputStream, encoding);
93                     return;
94                 }
95
96                 Char field;
97                 String fieldValue;
98
99                 field = (Char) inputStream.ReadByte();
100
101                 // Now start to read fields.
102                 while (field != 0)
103                 {
104
105                     fieldValue = PGUtil.ReadString(inputStream, encoding);
106
107                     switch (field)
108                     {
109                     case 'S':
110                         _severity = fieldValue;
111                         break;
112                     case 'C':
113                         _code = fieldValue;
114                         break;
115                     case 'M':
116                         _message = fieldValue;
117                         break;
118                     case 'D':
119                         _detail = fieldValue;
120                         break;
121                     case 'H':
122                         _hint = fieldValue;
123                         break;
124                     case 'P':
125                         _position = fieldValue;
126                         break;
127                     case 'W':
128                         _where = fieldValue;
129                         break;
130                     case 'F':
131                         _file = fieldValue;
132                         break;
133                     case 'L':
134                         _line = fieldValue;
135                         break;
136                     case 'R':
137                         _routine = fieldValue;
138                         break;
139
140                     }
141
142                     field = (Char) inputStream.ReadByte();
143
144                 }
145
146                 // Read 0 byte terminator.
147                 //inputStream.ReadByte();
148             }
149
150         }
151     }
152 }