**** Merged from MCS ****
[mono-project.git] / mcs / class / Npgsql / Npgsql / Design / NpgsqlParameterConverter.cs
blob53520b97a9f8adada56792f2a93d862c64b34044
1 using System;
2 using System.ComponentModel;
3 using System.ComponentModel.Design.Serialization;
4 using System.Data;
5 using System.Reflection;
7 namespace Npgsql.Design
9 /// <summary>
10 /// Zusammenfassung fr NpgsqlParameterConverter.
11 /// </summary>
12 internal class NpgsqlParameterConverter : ExpandableObjectConverter
14 public NpgsqlParameterConverter()
17 // TODO: Fgen Sie hier die Konstruktorlogik hinzu
21 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
22 if (destinationType == typeof(InstanceDescriptor))
23 return true;
24 return base.CanConvertTo (context, destinationType);
27 public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
28 if(destinationType == null){
29 throw new ArgumentNullException("destinationType");
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;
41 if(param.Direction != ParameterDirection.Input || param.Precision != 0 || param.Scale != 0 || param.SourceVersion != DataRowVersion.Current || param.IsNullable == true){
42 OtherChanged = true;
44 if(param.Size != 0){
45 SizeChanged = true;
47 if(param.SourceColumn == null || param.SourceColumn.Trim() != String.Empty){
48 SourceColumnChanged = true;
50 if(param.Value != null){
51 ValueChanged = true;
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});
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});
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});
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});
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});
81 return base.ConvertTo (context, culture, value, destinationType);