New test.
[mono.git] / mcs / class / System.Data / System.Data.SqlClient / SqlException.cs
1 //
2 // System.Data.SqlClient.SqlException.cs
3 //
4 // Author:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Daniel Morgan (danmorg@sc.rr.com)
7 //   Tim Coleman (tim@timcoleman.com)
8 //
9 // (C) Ximian, Inc
10 // Copyright (C) Tim Coleman, 2002
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using Mono.Data.Tds.Protocol;
37 using System;
38 using System.ComponentModel;
39 using System.Data;
40 using System.Data.Common;
41 using System.Runtime.Serialization;
42 using System.Text;
43
44 namespace System.Data.SqlClient {
45         [SerializableAttribute()]
46
47         public sealed class SqlException
48 #if NET_2_0
49          : DbException
50 #else
51          : SystemException
52 #endif //NET_1_1
53         {
54                 #region Fields
55
56                 private SqlErrorCollection errors; 
57                 private const string DEF_MESSAGE        = "SQL Exception has occured.";
58
59                 #endregion // Fields
60
61                 #region Constructors
62
63                 internal SqlException () 
64                         : this (DEF_MESSAGE, null, null) 
65                 {
66                 }
67                 
68                 internal SqlException (string message, Exception inner)
69                         : this (message, inner, null)
70                 {
71                 }
72
73                 internal SqlException (string message, Exception inner, SqlError sqlError)
74                         : base (message == null ? DEF_MESSAGE : message, inner)
75                 {
76                         errors = new SqlErrorCollection ();
77                         if (sqlError != null)
78                                 errors.Add (sqlError);
79                 }
80
81                 internal SqlException (byte theClass, int lineNumber, string message, int number, string procedure, string server, string source, byte state) 
82                         : this (null, 
83                                 null, 
84                                 new SqlError (theClass, lineNumber, message, 
85                                               number, procedure, server, source, 
86                                               state)) 
87                 {
88                 }
89                 
90                 private SqlException(SerializationInfo si, StreamingContext sc)
91                 {
92                         errors = (SqlErrorCollection)si.GetValue("Errors", typeof(SqlErrorCollection));
93                 }
94
95                 #endregion // Constructors
96
97                 #region Properties
98
99                 public byte Class {
100                         get { return Errors [0].Class; }
101                 }
102
103                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
104                 public SqlErrorCollection Errors {
105                         get { return errors; }
106                 }
107
108                 public int LineNumber {
109                         get { return Errors [0].LineNumber; }
110                 }
111                 
112                 public override string Message  {
113                         get {
114                                 if (Errors.Count == 0)
115                                         return base.Message;
116                                 StringBuilder result = new StringBuilder ();
117                                 if (base.Message != DEF_MESSAGE) {
118                                         result.Append (base.Message);
119                                         result.Append ("\n");
120                                 }
121                                 for (int i=0; i < Errors.Count -1; i++) {
122                                         result.Append (Errors [i].Message);
123                                         result.Append ("\n");
124                                 }
125                                 if (Errors.Count > 0)
126                                         result.Append (Errors [Errors.Count - 1].Message);
127                                 return result.ToString ();
128                         }
129                 }
130                 
131                 public int Number {
132                         get { return Errors [0].Number; }
133                 }
134                 
135                 public string Procedure {
136                         get { return Errors [0].Procedure; }
137                 }
138
139                 public string Server {
140                         get { return Errors [0].Server; }
141                 }
142                 
143                 public override string Source {
144                         get { return Errors [0].Source; }
145                 }
146
147                 public byte State {
148                         get { return Errors [0].State; }
149                 }
150
151                 #endregion // Properties
152
153                 #region Methods
154
155
156                 internal static SqlException FromTdsInternalException (TdsInternalException e)
157                 {
158                         return FromTdsInternalException (null, e);
159                 }
160
161                 internal static SqlException FromTdsInternalException (string message, 
162                                                                        TdsInternalException e)
163                 {
164                         SqlError sqlError = new SqlError (e.Class, e.LineNumber, e.Message, 
165                                                           e.Number, e.Procedure, e.Server, 
166                                                           "Mono SqlClient Data Provider", e.State);
167                         return new SqlException (message, e, sqlError);
168                 }
169
170                 public override void GetObjectData (SerializationInfo si, StreamingContext context) 
171                 {
172                         if (si == null)
173                                 throw new ArgumentNullException ("si");
174
175                         si.AddValue ("Errors", errors, typeof(SqlErrorCollection));
176                         base.GetObjectData (si, context);
177                 }
178
179                 #endregion // Methods
180         }
181 }