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