New test.
[mono.git] / mcs / class / System.Data / Test / System.Data.Tests.Mainsoft / GHTUtils / DbTypeParameter.cs
1 // Authors:
2 //   Rafael Mizrahi   <rafim@mainsoft.com>
3 //   Erez Lotan       <erezl@mainsoft.com>
4 //   Oren Gurfinkel   <oreng@mainsoft.com>
5 //   Ofer Borstein
6 // 
7 // Copyright (c) 2004 Mainsoft Co.
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Data;
31 using System.Data.OleDb;
32
33 namespace GHTUtils.Data
34 {
35         /// <summary>
36         /// Represents a parameter type for use in tests of System.Data.
37         /// </summary>
38         public class DbTypeParameter
39         {
40                 #region Members
41                 //Name of the Database type of this parameter.
42                 private string m_sDbTypeName;
43                 //Value of this parameter.
44                 private object m_oValue;
45                 //Size of this parameter.
46                 private int m_iSize;
47                 //Indicates wheather the size of this DbTypeParameter was initialized.
48                 private bool m_bIsSizeSet = false;
49                 #endregion
50
51                 #region Constructors
52                 /// <summary>
53                 /// Default constructor.
54                 /// </summary>
55                 public DbTypeParameter()
56                 {
57                 }
58
59                 /// <summary>
60                 /// Constructor, Initializes the DbTypeParameter's properties according to specified values.
61                 /// </summary>
62                 /// <param name="a_sTypeName">Specifies the initial parameter type Name for the DbTypeParameter.</param>
63                 /// <param name="a_oValue">Specifies the initial value for the DbTypeParameter.</param>
64                 public DbTypeParameter(string a_sTypeName, object a_oValue)
65                 {
66                         DbTypeName = a_sTypeName;
67                         Value = a_oValue;
68                 }
69                 /// <summary>
70                 /// Constructor, Initializes the DbTypeParameter's properties according to specified values.
71                 /// </summary>
72                 /// <param name="a_sTypeName">Specifies the initial parameter type Name for the DbTypeParameter.</param>
73                 /// <param name="a_oValue">Specifies the initial value for the DbTypeParameter.</param>
74                 /// <param name="a_iSize">Specifies the initial size for the DbTypeParameter.</param>
75                 public DbTypeParameter(string a_sTypeName, object a_oValue, int a_iSize)
76                 {
77                         DbTypeName = a_sTypeName;
78                         Value = a_oValue;
79                         Size = a_iSize;
80                 }
81                 #endregion
82
83                 #region Properties
84                 public string DbColumnName
85                 {
86                         get
87                         {
88                                 return string.Format("T_{0}", m_sDbTypeName);
89                         }
90                 }
91
92                 public string ParameterName
93                 {
94                         get
95                         {
96                                 return String.Format("@T_{0}", m_sDbTypeName);
97                         }
98                 }
99                 public string DbTypeName
100                 {
101                         get
102                         {
103                                 return m_sDbTypeName;
104                         }
105                         set
106                         {
107                                 m_sDbTypeName = value;
108                         }
109                 }
110
111                 public object Value
112                 {
113                         get
114                         {
115                                 return m_oValue;
116                         }
117                         set
118                         {
119                                 m_oValue = value;
120                         }
121                 }
122
123                 public int Size
124                 {
125                         get
126                         {
127                                 if (IsSizeSet)
128                                 {
129                                         return m_iSize;
130                                 }
131                                 else
132                                 {
133                                         throw new InvalidOperationException("DbTypeParameter size was not set.");
134                                 }
135                         }
136                         set
137                         {
138                                 m_iSize = value;
139                                 m_bIsSizeSet = true;
140                         }
141                 }
142                 public bool IsSizeSet
143                 {
144                         get
145                         {
146                                 return m_bIsSizeSet;
147                         }
148                 }
149                 public DbType DbType
150                 {
151                         get
152                         {
153                                 return GetDefaultDbType(DbTypeName);
154                         }
155                 }
156                 #endregion
157
158                 #region Methods
159                 public static DbType GetDefaultDbType(string dbTypeName)
160                 {
161                         switch (dbTypeName.ToUpper())
162                         {
163                                 case "BIT":     //SQLServer.
164                                         return DbType.Boolean;
165                                 case "TINYINT": //SQLServer.
166                                         return DbType.Byte;
167                                 case "SMALLINT":        //SQLServer & DB2.
168                                         return DbType.Int16;
169                                 case "INT":     //SQLServer.
170                                         return DbType.Int32;
171                                 case "INTEGER": //DB2
172                                         return DbType.Int32;
173                                 case "BIGINT":  //MSSQLServer &DB2
174                                         return DbType.Int64;
175                                 case "NUMERIC": //MSSQLServer.
176                                         return DbType.Decimal;
177                                 case "NUMBER": //Oracle.
178                                         return DbType.VarNumeric;
179                         case "DECIMAL": //MSSQLServer & DB2
180                                 return DbType.Decimal;
181                                 case "FLOAT":   //MSSQLServer & Oracle
182                                                 return DbType.Double;
183                                 case "REAL": //MSSQLServer & DB2
184                                         return DbType.Single;
185                                 case "DOUBLE":
186                                         return DbType.Double;
187                                 case "CHAR":    //MSSQLServer & Oracle.
188                                         return DbType.AnsiStringFixedLength;
189                                 case "NCHAR": //MSSQLServer & Oracle.
190                                         return DbType.AnsiStringFixedLength;
191                                 case "VARCHAR": //MSSQLServer, Oracle & DB2.
192                                         return DbType.AnsiString;
193                                 case "NVARCHAR": //MSSQLServer & Oracle.
194                                         return DbType.AnsiString;
195                                 case "CHARACTER": //DB2
196                                         return DbType.AnsiStringFixedLength;
197                                 case "LONGVARCHAR": //DB2
198                                         return DbType.String;
199                                 case "LONG":    //Oracle.
200                                         return DbType.AnsiString;
201                                 default:
202                                         throw new ApplicationException(string.Format("Dont know the default DbType for {0}.", dbTypeName));
203                         }
204                 }
205                 public object ApplyDefaultDataTransformation()
206                 {
207                         if (Value == DBNull.Value)
208                         {
209                                 return DBNull.Value;
210                         }
211                         else if (Value.GetType() == typeof(bool))
212                         {
213                                 return DefaultBooleanTransformation((bool)Value);
214                         }
215                         else if (Value.GetType() == typeof(byte))
216                         {
217                                 return DefaultByteTransformation((byte)Value);
218                         }
219                         else if (Value.GetType() == typeof(Int16))
220                         {
221                                 return DefaultInt16Transformation((Int16)Value);
222                         }
223                         else if (Value.GetType() == typeof(int))
224                         {
225                                 return DefaultIntTransformation((int)Value);
226                         }
227                         else if (Value.GetType() == typeof(Int64))
228                         {
229                                 return DefaultInt64Transformation((Int64)Value);
230                         }
231                         else if (Value.GetType() == typeof(decimal))
232                         {
233                                 return DefaultDecimalTransformation((decimal)Value);
234                         }
235                         else if (Value.GetType() == typeof(double))
236                         {
237                                 return DefaultDoubleTransformation((double)Value);
238                         }
239                         else if (Value.GetType() == typeof(float))
240                         {
241                                 return DefaultFloatTransformation((float)Value);
242                         }
243                         else if (Value.GetType() == typeof(string))
244                         {
245                                 return DefaultStringTransformation((string)Value);
246                         }
247                         else
248                         {
249                                 throw new ApplicationException(string.Format("No default transformation for type {0}.", Value));
250                         }
251                 }
252                 public static bool DefaultBooleanTransformation(bool val)
253                 {
254                         return !val;
255                 }
256                 public static byte DefaultByteTransformation(byte val)
257                 {
258                         return (byte)(val*2);;
259                 }
260                 public static Int16 DefaultInt16Transformation(Int16 val)
261                 {
262                         return (Int16)(val*2);
263                 }
264                 public static int DefaultIntTransformation(int val)
265                 {
266                         return (int)(val*2);;
267                 }
268                 public static Int64 DefaultInt64Transformation(Int64 val)
269                 {
270                         return (Int64)(val*2);;
271                 }
272                 public static decimal DefaultDecimalTransformation(decimal val)
273                 {
274                         return (decimal)(val*2);;
275                 }
276                 public static double DefaultDoubleTransformation(double val)
277                 {
278                         return (double)(val*2);;
279                 }
280                 public static float DefaultFloatTransformation(float val)
281                 {
282                         return (float)(val*2);;
283                 }
284                 public static string DefaultStringTransformation(string val)
285                 {
286                         return val.ToUpper();;
287                 }
288                 /// <summary>
289                 /// Invalidates the size of this DbTypeParameter.
290                 /// </summary>
291                 public void InvalidateSize()
292                 {
293                         m_bIsSizeSet = false;
294                 }
295                 #endregion
296
297         }
298 }