2 // Rafael Mizrahi <rafim@mainsoft.com>
3 // Erez Lotan <erezl@mainsoft.com>
4 // Oren Gurfinkel <oreng@mainsoft.com>
7 // Copyright (c) 2004 Mainsoft Co.
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Data.OleDb;
33 namespace GHTUtils.Data
36 /// Represents a parameter type for use in tests of System.Data.
38 public class DbTypeParameter
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.
47 //Indicates wheather the size of this DbTypeParameter was initialized.
48 private bool m_bIsSizeSet = false;
53 /// Default constructor.
55 public DbTypeParameter()
60 /// Constructor, Initializes the DbTypeParameter's properties according to specified values.
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)
66 DbTypeName = a_sTypeName;
70 /// Constructor, Initializes the DbTypeParameter's properties according to specified values.
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)
77 DbTypeName = a_sTypeName;
84 public string DbColumnName
88 return string.Format("T_{0}", m_sDbTypeName);
92 public string ParameterName
96 return String.Format("@T_{0}", m_sDbTypeName);
99 public string DbTypeName
103 return m_sDbTypeName;
107 m_sDbTypeName = value;
133 throw new InvalidOperationException("DbTypeParameter size was not set.");
142 public bool IsSizeSet
153 return GetDefaultDbType(DbTypeName);
159 public static DbType GetDefaultDbType(string dbTypeName)
161 switch (dbTypeName.ToUpper())
163 case "BIT": //SQLServer.
164 return DbType.Boolean;
165 case "TINYINT": //SQLServer.
167 case "SMALLINT": //SQLServer & DB2.
169 case "INT": //SQLServer.
171 case "INTEGER": //DB2
173 case "BIGINT": //MSSQLServer &DB2
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;
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;
202 throw new ApplicationException(string.Format("Dont know the default DbType for {0}.", dbTypeName));
205 public object ApplyDefaultDataTransformation()
207 if (Value == DBNull.Value)
211 else if (Value.GetType() == typeof(bool))
213 return DefaultBooleanTransformation((bool)Value);
215 else if (Value.GetType() == typeof(byte))
217 return DefaultByteTransformation((byte)Value);
219 else if (Value.GetType() == typeof(Int16))
221 return DefaultInt16Transformation((Int16)Value);
223 else if (Value.GetType() == typeof(int))
225 return DefaultIntTransformation((int)Value);
227 else if (Value.GetType() == typeof(Int64))
229 return DefaultInt64Transformation((Int64)Value);
231 else if (Value.GetType() == typeof(decimal))
233 return DefaultDecimalTransformation((decimal)Value);
235 else if (Value.GetType() == typeof(double))
237 return DefaultDoubleTransformation((double)Value);
239 else if (Value.GetType() == typeof(float))
241 return DefaultFloatTransformation((float)Value);
243 else if (Value.GetType() == typeof(string))
245 return DefaultStringTransformation((string)Value);
249 throw new ApplicationException(string.Format("No default transformation for type {0}.", Value));
252 public static bool DefaultBooleanTransformation(bool val)
256 public static byte DefaultByteTransformation(byte val)
258 return (byte)(val*2);;
260 public static Int16 DefaultInt16Transformation(Int16 val)
262 return (Int16)(val*2);
264 public static int DefaultIntTransformation(int val)
266 return (int)(val*2);;
268 public static Int64 DefaultInt64Transformation(Int64 val)
270 return (Int64)(val*2);;
272 public static decimal DefaultDecimalTransformation(decimal val)
274 return (decimal)(val*2);;
276 public static double DefaultDoubleTransformation(double val)
278 return (double)(val*2);;
280 public static float DefaultFloatTransformation(float val)
282 return (float)(val*2);;
284 public static string DefaultStringTransformation(string val)
286 return val.ToUpper();;
289 /// Invalidates the size of this DbTypeParameter.
291 public void InvalidateSize()
293 m_bIsSizeSet = false;