Merge branch 'master' of http://github.com/mono/mono
[mono.git] / mcs / class / System.Data / System.Data.ProviderBase.jvm / AbstractDbException.cs
1 //\r
2 // System.Data.ProviderBase.AbstractDbException\r
3 //\r
4 // Authors:\r
5 //      Konstantin Triger <kostat@mainsoft.com>\r
6 //      Boris Kirzner <borisk@mainsoft.com>\r
7 //      \r
8 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)\r
9 //\r
10 \r
11 //\r
12 // Permission is hereby granted, free of charge, to any person obtaining\r
13 // a copy of this software and associated documentation files (the\r
14 // "Software"), to deal in the Software without restriction, including\r
15 // without limitation the rights to use, copy, modify, merge, publish,\r
16 // distribute, sublicense, and/or sell copies of the Software, and to\r
17 // permit persons to whom the Software is furnished to do so, subject to\r
18 // the following conditions:\r
19 // \r
20 // The above copyright notice and this permission notice shall be\r
21 // included in all copies or substantial portions of the Software.\r
22 // \r
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
30 //\r
31 \r
32 \r
33 namespace System.Data.ProviderBase {\r
34 \r
35         using java.sql;\r
36         using System.Text;\r
37         using System.Data.Common;\r
38 \r
39         /*\r
40         * CURRENT LIMITATIONS\r
41         * 1. Constructor for serialization SqlException(SerializationInfo info, StreamingContext sc) \r
42         *    is not supported.\r
43         * 2. Method "void GetObjectData(...,...)" is not supported (serialization)\r
44         */\r
45 \r
46         public abstract class AbstractDbException :\r
47 #if NET_2_0\r
48                 DbException\r
49 #else\r
50                 System.SystemException\r
51 #endif\r
52         {\r
53                 protected SQLException _cause;\r
54                 protected AbstractDBConnection _connection;\r
55 \r
56                 protected AbstractDbException(Exception cause, AbstractDBConnection connection) : base(cause.Message, cause) {\r
57                         _connection = connection;\r
58                 }\r
59 \r
60                 protected AbstractDbException(SQLException cause, AbstractDBConnection connection) : this(String.Empty, cause, connection) {}\r
61 \r
62                 protected AbstractDbException(string message, SQLException cause, AbstractDBConnection connection) : base(message, cause) {\r
63                         _connection = connection;\r
64                         _cause = cause;\r
65                 }\r
66 \r
67                 abstract protected AbstractDbErrorCollection DbErrors { get; }\r
68 \r
69                 /**\r
70                  * Gets the error code of the error.\r
71                  * @return The error code of the error.\r
72                  */\r
73                 protected int DbErrorCode {\r
74                         get {\r
75                                 return _cause != null ? _cause.getErrorCode() : 0;\r
76                         }\r
77                 }\r
78 \r
79  \r
80                 /**\r
81                  * Gets the name of the OLE DB provider that generated the error.\r
82                  * @return the name of the OLE DB provider that generated the error. \r
83                  */\r
84                 public override String Source {\r
85                         get {\r
86                                 return _connection != null ? _connection.JdbcProvider : null;\r
87                         }\r
88                 }\r
89 \r
90                 public override string Message {\r
91                         get {\r
92                                 StringBuilder sb = new StringBuilder();\r
93                                 string message = base.Message;\r
94                                 bool addNewLine = false;\r
95                                 if (message != null && message.Length > 0) {\r
96                                         sb.Append(message);\r
97                                         addNewLine = true;\r
98                                 }\r
99 \r
100                                 foreach (AbstractDbError err in DbErrors) {\r
101                                         if (addNewLine)\r
102                                                 sb.Append(Environment.NewLine);\r
103 \r
104                                         addNewLine = true;\r
105                                         sb.Append(err.DbMessage);\r
106                                 }\r
107                                 return sb.ToString();\r
108                         }\r
109                 }\r
110 \r
111         }\r
112 }