Flush
[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 System;
37 using System.ComponentModel;
38 using System.Data;
39 using System.Data.Common;
40 using System.Runtime.Serialization;
41 using System.Text;
42
43 using Mono.Data.Tds.Protocol;
44
45 namespace System.Data.SqlClient
46 {
47         [Serializable]
48         public sealed class SqlException
49 #if NET_2_0
50                 : DbException
51 #else
52                 : SystemException
53 #endif //NET_1_1
54         {
55                 #region Fields
56
57                 private readonly SqlErrorCollection errors;
58                 private const string DEF_MESSAGE = "SQL Exception has occured.";
59
60                 #endregion // Fields
61
62                 #region Constructors
63
64                 internal SqlException ()
65                         : this (DEF_MESSAGE, null, null)
66                 {
67                 }
68                 
69                 internal SqlException (string message, Exception inner)
70                         : this (message, inner, null)
71                 {
72                 }
73
74                 internal SqlException (string message, Exception inner, SqlError sqlError)
75                         : base (message == null ? DEF_MESSAGE : message, inner)
76                 {
77                         HResult = -2146232060;
78                         errors = new SqlErrorCollection ();
79                         if (sqlError != null)
80                                 errors.Add (sqlError);
81                 }
82
83                 internal SqlException (byte theClass, int lineNumber, string message, int number, string procedure, string server, string source, byte state) 
84                         : this (null, 
85                                 null, 
86                                 new SqlError (theClass, lineNumber, message, 
87                                               number, procedure, server, source, 
88                                               state)) 
89                 {
90                 }
91                 
92                 private SqlException(SerializationInfo si, StreamingContext sc)
93                 {
94                         HResult = -2146232060;
95                         errors = (SqlErrorCollection) si.GetValue ("Errors", typeof (SqlErrorCollection));
96                 }
97
98                 #endregion // Constructors
99
100                 #region Properties
101
102                 public byte Class {
103                         get { return Errors [0].Class; }
104                 }
105
106                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
107                 public SqlErrorCollection Errors {
108                         get { return errors; }
109                 }
110
111                 public int LineNumber {
112                         get { return Errors [0].LineNumber; }
113                 }
114                 
115                 public override string Message {
116                         get {
117                                 if (Errors.Count == 0)
118                                         return base.Message;
119                                 StringBuilder result = new StringBuilder ();
120                                 if (base.Message != DEF_MESSAGE) {
121                                         result.Append (base.Message);
122                                         result.Append ("\n");
123                                 }
124                                 for (int i = 0; i < Errors.Count -1; i++) {
125                                         result.Append (Errors [i].Message);
126                                         result.Append ("\n");
127                                 }
128                                 result.Append (Errors [Errors.Count - 1].Message);
129                                 return result.ToString ();
130                         }
131                 }
132                 
133                 public int Number {
134                         get { return Errors [0].Number; }
135                 }
136                 
137                 public string Procedure {
138                         get { return Errors [0].Procedure; }
139                 }
140
141                 public string Server {
142                         get { return Errors [0].Server; }
143                 }
144                 
145                 public override string Source {
146                         get { return Errors [0].Source; }
147                 }
148
149                 public byte State {
150                         get { return Errors [0].State; }
151                 }
152
153                 #endregion // Properties
154
155                 #region Methods
156
157                 internal static SqlException FromTdsInternalException (TdsInternalException e)
158                 {
159                         SqlError sqlError = new SqlError (e.Class, e.LineNumber, e.Message,
160                                                           e.Number, e.Procedure, e.Server,
161                                                           "Mono SqlClient Data Provider", e.State);
162                         return new SqlException (null, e, sqlError);
163                 }
164
165                 public override void GetObjectData (SerializationInfo si, StreamingContext context) 
166                 {
167                         if (si == null)
168                                 throw new ArgumentNullException ("si");
169
170                         si.AddValue ("Errors", errors, typeof(SqlErrorCollection));
171                         base.GetObjectData (si, context);
172                 }
173
174                 #endregion // Methods
175         }
176 }