* OdbcParameterTest.cs: Fixed compilation on 1.0 profile.
[mono.git] / mcs / class / System.Data / System.Data.Common / ExceptionHelper.cs
1 //\r
2 // System.Data.Common.ExceptionHelper\r
3 //\r
4 // Author:\r
5 //   Boris Kirzner (borisk@mainsoft.com)\r
6 //\r
7 \r
8 using System;\r
9 using System.Globalization;\r
10 \r
11 namespace System.Data.Common\r
12 {\r
13         internal sealed class ExceptionHelper\r
14         {\r
15                 internal static ArgumentException InvalidSizeValue (int value)\r
16                 {\r
17                         string [] args = new string [] {value.ToString ()};\r
18                         return new ArgumentException  (GetExceptionMessage ("Invalid parameter Size value '{0}'. The value must be greater than or equal to 0.",args));\r
19                 }\r
20 \r
21                 internal static void CheckEnumValue (Type enumType, object value)\r
22                 {\r
23                         if (!Enum.IsDefined (enumType, value))\r
24                                 throw InvalidEnumValueException (enumType.Name, value);\r
25                 }\r
26 \r
27                 internal static ArgumentException InvalidEnumValueException (String enumeration, object value)\r
28                 {\r
29                         string msg = string.Format (CultureInfo.InvariantCulture,\r
30                                 "The {0} enumeration value, {1}, is invalid.",\r
31                                 enumeration, value);\r
32 #if NET_2_0\r
33                         return new ArgumentOutOfRangeException (enumeration, msg);\r
34 #else\r
35                         return new ArgumentException (msg);\r
36 #endif\r
37                 }\r
38 \r
39                 internal static ArgumentOutOfRangeException InvalidDataRowVersion (DataRowVersion value)\r
40                 {\r
41                         object [] args = new object [] { "DataRowVersion", value.ToString () } ;\r
42                         return new ArgumentOutOfRangeException  (GetExceptionMessage ("{0}: Invalid DataRow Version enumeration value: {1}",args));\r
43                 }\r
44 \r
45                 internal static ArgumentOutOfRangeException InvalidParameterDirection (ParameterDirection value)\r
46                 {\r
47                         object [] args = new object [] { "ParameterDirection", value.ToString () } ;\r
48                         return new ArgumentOutOfRangeException  (GetExceptionMessage ("Invalid direction '{0}' for '{1}' parameter.",args));\r
49                 }\r
50 \r
51                 internal static InvalidOperationException NoStoredProcedureExists (string procedureName) {\r
52                         object [] args = new object [1] { procedureName } ;\r
53                         return new InvalidOperationException  (GetExceptionMessage ("The stored procedure '{0}' doesn't exist.", args));\r
54                 }\r
55 \r
56                 internal static ArgumentNullException ArgumentNull (string parameter)\r
57                 {\r
58                         return new ArgumentNullException (parameter);\r
59                 }\r
60 \r
61                 internal static InvalidOperationException TransactionRequired ()\r
62                 {\r
63                         return new InvalidOperationException  (GetExceptionMessage ("Execute requires the command to have a transaction object when the connection assigned to the command is in a pending local transaction.  The Transaction property of the command has not been initialized."));\r
64                 }\r
65 \r
66                 internal static ArgumentOutOfRangeException InvalidOleDbType (int value)\r
67                 {\r
68                         string [] args = new string [] { value.ToString() };\r
69                         return new ArgumentOutOfRangeException  (GetExceptionMessage ("Invalid OleDbType enumeration value: {0}",args));\r
70                 }\r
71  \r
72                 internal static ArgumentException InvalidDbType(int value)\r
73                 {\r
74                         string [] args = new string [] { value.ToString () };\r
75                         return new ArgumentException  (GetExceptionMessage ("No mapping exists from DbType {0} to a known {1}.",args));\r
76                 }\r
77 \r
78                 internal static InvalidOperationException DeriveParametersNotSupported(Type type,CommandType commandType)\r
79                 {\r
80                         string [] args = new string [] { type.ToString(),commandType.ToString() };\r
81                         return new InvalidOperationException  (GetExceptionMessage ("{0} DeriveParameters only supports CommandType.StoredProcedure, not CommandType.{1}.",args));\r
82                 }\r
83 \r
84                 internal static InvalidOperationException ReaderClosed (string mehodName)\r
85                 {\r
86                         string [] args = new string [] { mehodName };\r
87                         return new InvalidOperationException  (GetExceptionMessage ("Invalid attempt to {0} when reader is closed.",args));\r
88                 }\r
89 \r
90                 internal static ArgumentOutOfRangeException InvalidSqlDbType (int value)\r
91                 {\r
92                         string [] args = new string [] { value.ToString () };\r
93                         return new ArgumentOutOfRangeException  (GetExceptionMessage ("{0}: Invalid SqlDbType enumeration value: {1}.",args));\r
94                 }\r
95 \r
96                 internal static ArgumentException UnknownDataType (string type1, string type2)\r
97                 {\r
98                         string [] args = new string [] { type1, type2 };\r
99                         return new ArgumentException  (GetExceptionMessage ("No mapping exists from DbType {0} to a known {1}.",args));\r
100                 }\r
101 \r
102                 internal static InvalidOperationException TransactionNotInitialized ()\r
103                 {\r
104                         return new InvalidOperationException  (GetExceptionMessage ("Execute requires the command to have a transaction object when the connection assigned to the command is in a pending local transaction.  The Transaction property of the command has not been initialized."));\r
105                 }\r
106 \r
107                 internal static InvalidOperationException TransactionNotUsable (Type type)\r
108                 {\r
109                         return new InvalidOperationException (string.Format (\r
110                                 CultureInfo.InvariantCulture,\r
111                                 "This {0} has completed; it is no longer usable.",\r
112                                 type.Name));\r
113                 }\r
114 \r
115                 internal static InvalidOperationException ParametersNotInitialized (int parameterPosition,string parameterName,string parameterType)\r
116                 {\r
117                         object [] args = new object [] { parameterPosition, parameterName, parameterType };\r
118                         return new InvalidOperationException  (GetExceptionMessage ("Parameter {0}: '{1}', the property DbType is uninitialized: OleDbType.{2}.",args));\r
119                 }\r
120 \r
121                 internal static InvalidOperationException WrongParameterSize(string provider)\r
122                 {\r
123                         string [] args = new string [] { provider };\r
124                         return new InvalidOperationException  (GetExceptionMessage ("{0}.Prepare method requires all variable length parameters to have an explicitly set non-zero Size.",args));\r
125                 }\r
126 \r
127                 internal static InvalidOperationException ConnectionNotOpened (string operationName, string connectionState)\r
128                 {\r
129                         object [] args = new object [] { operationName, connectionState };\r
130                         return new InvalidOperationException  (GetExceptionMessage ("{0} requires an open and available Connection. The connection's current state is {1}.",args));\r
131                 }\r
132 \r
133                 internal static InvalidOperationException ConnectionNotInitialized (string methodName)\r
134                 {\r
135                         object [] args = new object [] { methodName };\r
136                         return new InvalidOperationException (GetExceptionMessage ("{0}: Connection property has not been initialized.",args));\r
137                 }\r
138 \r
139                 internal static InvalidOperationException OpenConnectionRequired (string methodName, object connectionState)\r
140                 {\r
141                         object [] args = new object [] { methodName, connectionState };\r
142                         return new InvalidOperationException (GetExceptionMessage ("{0} requires an open and available Connection. The connection's current state is {1}.",args));\r
143                 }\r
144 \r
145                 internal static InvalidOperationException OpenedReaderExists ()\r
146                 {\r
147                         return new InvalidOperationException (GetExceptionMessage ("There is already an open DataReader associated with this Connection which must be closed first."));\r
148                 }\r
149 \r
150                 internal static InvalidOperationException ConnectionAlreadyOpen (object connectionState)\r
151                 {\r
152                         object [] args = new object [] { connectionState };\r
153                         return new InvalidOperationException (GetExceptionMessage ("The connection is already Open (state={0}).",args));\r
154                 }\r
155 \r
156                 internal static InvalidOperationException ConnectionClosed ()\r
157                 {\r
158                         return new InvalidOperationException ("Invalid operation. The Connection is closed.");\r
159                 }\r
160 \r
161                 internal static InvalidOperationException ConnectionStringNotInitialized ()\r
162                 {\r
163                         return new InvalidOperationException (GetExceptionMessage ("The ConnectionString property has not been initialized."));\r
164                 }\r
165 \r
166                 internal static InvalidOperationException ConnectionIsBusy (object commandType,object connectionState)\r
167                 {\r
168                         object [] args = new object [] { commandType.ToString (), connectionState.ToString () };\r
169                         return new InvalidOperationException (GetExceptionMessage ("The {0} is currently busy {1}.",args));\r
170                 }\r
171 \r
172                 internal static InvalidOperationException NotAllowedWhileConnectionOpen (string propertyName, object connectionState)\r
173                 {\r
174                         object [] args = new object [] { propertyName, connectionState };\r
175                         return new InvalidOperationException (GetExceptionMessage ("Not allowed to change the '{0}' property while the connection (state={1}).",args));\r
176                 }\r
177 \r
178                 internal static ArgumentException OleDbNoProviderSpecified ()\r
179                 {\r
180                         return new ArgumentException (GetExceptionMessage ("An OLE DB Provider was not specified in the ConnectionString.  An example would be, 'Provider=SQLOLEDB;'."));\r
181                 }\r
182 \r
183                 internal static ArgumentException InvalidValueForKey (string key)\r
184                 {\r
185                         string [] args = new string [] { key };\r
186                         return new ArgumentException (String.Format ("Invalid value for key {0}",args));\r
187                 }\r
188 \r
189                 internal static InvalidOperationException ParameterSizeNotInitialized( int parameterIndex, string parameterName,string parameterType,int parameterSize)\r
190                 {\r
191                         object [] args = new object [] { parameterIndex.ToString (), parameterName, parameterType, parameterSize.ToString () };\r
192                         return new InvalidOperationException (GetExceptionMessage ("Parameter {0}: '{1}' of type: {2}, the property Size has an invalid size: {3}",args));\r
193                 }\r
194 \r
195                 internal static ArgumentException InvalidUpdateStatus (UpdateStatus status)\r
196                 {\r
197                         object [] args = new object [] { status };\r
198                         return new ArgumentException (GetExceptionMessage ("Invalid UpdateStatus: {0}",args));\r
199                 }\r
200 \r
201                 internal static InvalidOperationException UpdateRequiresCommand (string command)\r
202                 {\r
203                         object [] args = new object [] { command };\r
204                         return new InvalidOperationException (GetExceptionMessage ("Auto SQL generation during {0} requires a valid SelectCommand.",args));\r
205                 }\r
206 \r
207                 internal static DataException RowUpdatedError ()\r
208                 {\r
209                         return new DataException (GetExceptionMessage ("RowUpdatedEvent: Errors occurred; no additional is information available."));\r
210                 }\r
211 \r
212                 internal static ArgumentNullException CollectionNoNullsAllowed (object collection, object objectsType)\r
213                 {\r
214                         object [] args = new object [] {collection.GetType ().ToString (), objectsType.ToString ()};\r
215                         return new ArgumentNullException (GetExceptionMessage ("The {0} only accepts non-null {1} type objects."));\r
216                 }\r
217 \r
218                 internal static ArgumentException CollectionAlreadyContains(object objectType,string propertyName, object propertyValue, object collection)\r
219                 {\r
220                         object [] args = new object [] {objectType.ToString (), propertyName, propertyValue, collection.GetType ().ToString ()};\r
221                         return new ArgumentException (GetExceptionMessage ("The {0} with {1} '{2}' is already contained by this {3}.",args));\r
222                 }\r
223 \r
224                 internal static string GetExceptionMessage (string exceptionMessage,object [] args)\r
225                 {\r
226                         if ((args == null) || (args.Length == 0)) {\r
227                                 return exceptionMessage;\r
228                         } else {\r
229                                 return String.Format (exceptionMessage,args);\r
230                         }\r
231                 }\r
232 \r
233                 internal static string GetExceptionMessage (string exceptionMessage)\r
234                 {\r
235                         return GetExceptionMessage (exceptionMessage, null);\r
236                 }\r
237         }\r
238 }\r