2004-05-22 Francisco Figueiredo Jr. <fxjrlists@yahoo.com.br>
[mono.git] / mcs / class / Npgsql / Npgsql / Design / NpgsqlParameterConverter.cs
1 using System;
2 using System.ComponentModel;
3 using System.ComponentModel.Design.Serialization;
4 using System.Data;
5 using System.Reflection;
6
7 namespace Npgsql.Design
8 {
9         /// <summary>
10         /// Zusammenfassung fr NpgsqlParameterConverter.
11         /// </summary>
12         internal class NpgsqlParameterConverter : ExpandableObjectConverter
13         {
14                 public NpgsqlParameterConverter()
15                 {
16                         //
17                         // TODO: Fgen Sie hier die Konstruktorlogik hinzu
18                         //
19                 }
20         
21                 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
22                         if (destinationType == typeof(InstanceDescriptor))
23                                 return true;
24                         return base.CanConvertTo (context, destinationType);
25                 }
26         
27                 public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
28                         if(destinationType == null){
29                                 throw new ArgumentNullException("destinationType");
30                         }
31                         if(destinationType == typeof(InstanceDescriptor) && value as NpgsqlParameter != null){
32                                 NpgsqlParameter param = (NpgsqlParameter)value;
33                                 bool DbTypeChanged = false;
34                                 bool OtherChanged = false;
35                                 bool SizeChanged = false;
36                                 bool SourceColumnChanged = false;
37                                 bool ValueChanged = false;
38                                 if(param.DbType != DbType.String){
39                                         DbTypeChanged= true;
40                                 }
41                                 if(param.Direction != ParameterDirection.Input || param.Precision != 0 || param.Scale != 0 || param.SourceVersion != DataRowVersion.Current || param.IsNullable == true){
42                                         OtherChanged = true;
43                                 }
44                                 if(param.Size != 0){
45                                         SizeChanged = true;
46                                 }
47                                 if(param.SourceColumn == null || param.SourceColumn.Trim() != String.Empty){
48                                         SourceColumnChanged = true;
49                                 }
50                                 if(param.Value != null){
51                                         ValueChanged = true;
52                                 }
53
54                                 if(!(OtherChanged || SizeChanged || SourceColumnChanged || ValueChanged)){
55                                         ConstructorInfo ci = typeof(NpgsqlParameter).GetConstructor(new Type[]{typeof(String), typeof(DbType)});
56                                         if (ci != null){
57                                                 return new InstanceDescriptor(ci, new Object[]{param.ParameterName, param.DbType});
58                                         }
59                                 }else if(!(OtherChanged || SourceColumnChanged || ValueChanged)){
60                                         ConstructorInfo ci = typeof(NpgsqlParameter).GetConstructor(new Type[]{typeof(String), typeof(DbType), typeof(Int32)});
61                                         if (ci != null){
62                                                 return new InstanceDescriptor(ci, new Object[]{param.ParameterName, param.DbType, param.Size});
63                                         }
64                                 }else if(!(OtherChanged || ValueChanged)){
65                                         ConstructorInfo ci = typeof(NpgsqlParameter).GetConstructor(new Type[]{typeof(String), typeof(DbType), typeof(Int32), typeof(String)});
66                                         if (ci != null){
67                                                 return new InstanceDescriptor(ci, new Object[]{param.ParameterName, param.DbType, param.Size, param.SourceColumn});
68                                         }
69                                 }else if(ValueChanged && !DbTypeChanged){
70                                         ConstructorInfo ci = typeof(NpgsqlParameter).GetConstructor(new Type[]{typeof(String), typeof(Object)});
71                                         if (ci != null){
72                                                 return new InstanceDescriptor(ci, new Object[]{param.ParameterName, param.Value});
73                                         }
74                                 }else{
75                                         ConstructorInfo ci = typeof(NpgsqlParameter).GetConstructor(new Type[]{typeof(String), typeof(DbType), typeof(Int32), typeof(String), typeof(ParameterDirection), typeof(Boolean), typeof(Byte), typeof(Byte), typeof(DataRowVersion), typeof(Object)});
76                                         if (ci != null){
77                                                 return new InstanceDescriptor(ci, new Object[]{param.ParameterName, param.DbType, param.Size, param.SourceColumn, param.Direction, param.IsNullable, param.Precision, param.Scale, param.SourceVersion, param.Value});
78                                         }
79                                 }
80                         }
81                         return base.ConvertTo (context, culture, value, destinationType);
82                 }
83         }
84 }