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