Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WebControlParameterProxy.cs « WebControls « Data « System « System.Web.Entity « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1df13685dd3fb36d6d3fdfaf3e30e0e9d9f6b4b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//---------------------------------------------------------------------
// <copyright file="WebControlParameterProxy.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner       [....]
// @backupOwner objsdev
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Data;
using System.Globalization;

namespace System.Web.UI.WebControls
{
    internal class WebControlParameterProxy
    {
        ParameterCollection _collection;
        EntityDataSource _entityDataSource;
        Parameter _parameter; // Can be null, that's why this class doesn't subclass Parameter

        internal WebControlParameterProxy(string propertyName, ParameterCollection parameterCollection, EntityDataSource entityDataSource)
        {
            Debug.Assert(null != entityDataSource);
            Debug.Assert(!String.IsNullOrEmpty(propertyName));

            _parameter = EntityDataSourceUtil.GetParameter(propertyName, parameterCollection);
            _collection = parameterCollection;
            _entityDataSource = entityDataSource;
            VerifyUniqueType(_parameter);
        }
        internal WebControlParameterProxy(Parameter parameter, ParameterCollection parameterCollection, EntityDataSource entityDataSource)
        {
            Debug.Assert(null != entityDataSource);
            _parameter = parameter;
            _collection = parameterCollection;
            _entityDataSource = entityDataSource;
            VerifyUniqueType(_parameter);
        }
        internal string Name
        {
            get 
            {
                if (null != _parameter)
                {
                    return _parameter.Name;
                }
                return null;
            }
        }
        internal bool HasValue
        {
            get 
            { 
                return null != _parameter && 
                    null != Value; 
            }
        }
        internal bool ConvertEmptyStringToNull
        {
            get
            {
                if (null != _parameter)
                {
                    return _parameter.ConvertEmptyStringToNull;
                }
                return false;
            }
        }
        internal TypeCode TypeCode
        {
            get
            {
                if (null != _parameter)
                {
                    return _parameter.Type;
                }
                return TypeCode.Empty;
            }
        }
        internal DbType DbType
        {
            get
            {
                if (null != _parameter)
                {
                    return _parameter.DbType;
                }
                return DbType.Object;
            }
        }
        internal Type ClrType
        {
            get
            {
                Debug.Assert(this.TypeCode != TypeCode.Empty || this.DbType != DbType.Object, "Need to have TypeCode or DbType to get a ClrType");
                if (this.TypeCode != TypeCode.Empty)
                {
                    return EntityDataSourceUtil.ConvertTypeCodeToType(this.TypeCode);
                }
                return EntityDataSourceUtil.ConvertDbTypeToType(this.DbType);
            }
        }

        internal object Value
        {
            get 
            {
                if (_parameter != null)
                {
                    object paramValue = EntityDataSourceUtil.GetParameterValue(_parameter.Name, _collection, _entityDataSource);

                    if (paramValue != null)
                    {
                        if (this.DbType == DbType.DateTimeOffset)
                        {
                            object value = (paramValue is DateTimeOffset)
                                ? paramValue
                                : DateTimeOffset.Parse(this.Value.ToString(), CultureInfo.CurrentCulture);
                            return value;
                        }
                        else if (this.DbType == DbType.Time)
                        {
                            object value = (paramValue is TimeSpan)
                                ? paramValue
                                : TimeSpan.Parse(paramValue.ToString(), CultureInfo.CurrentCulture);
                            return value;
                        }
                        else if (this.DbType == DbType.Guid)
                        {
                            object value = (paramValue is Guid)
                                ? paramValue
                                : new Guid(paramValue.ToString());
                            return value;
                        }
                    }

                    return paramValue;
                }

                return null;
            }
        }

        private static void VerifyUniqueType(Parameter parameter)
        {
            if (parameter != null && parameter.Type == TypeCode.Empty && parameter.DbType == DbType.Object)
            {
                throw new InvalidOperationException(Strings.WebControlParameterProxy_TypeDbTypeMutuallyExclusive);
            }

            if (parameter != null && parameter.DbType != DbType.Object && parameter.Type != TypeCode.Empty)
            {
                throw new InvalidOperationException(Strings.WebControlParameterProxy_TypeDbTypeMutuallyExclusive);
            }
        }

    }

}