File: IDbSpatialValue.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (208 lines) | stat: -rw-r--r-- 7,248 bytes parent folder | download | duplicates (6)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//------------------------------------------------------------------------------
// <copyright file="IDbSpatialValue.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner  willa
// @backupOwner Microsoft
//------------------------------------------------------------------------------

using System.Data.Spatial;
using System.Data.Metadata.Edm;

namespace System.Data.SqlClient.Internal
{
    /// <summary>
    /// Adapter interface to make working with instances of <see cref="DbGeometry"/> or <see cref="DbGeography"/> easier.  
    /// Implementing types wrap instances of DbGeography/DbGeometry and allow them to be consumed in a common way. 
    /// This interface is implemented by wrapping types for two reasons:
    /// 1. The DbGeography/DbGeometry classes cannot directly implement internal interfaces because their members are virtual (behavior is not guaranteed).
    /// 2. The wrapping types ensure that instances of IDbSpatialValue handle the <see cref="NotImplementedException"/>s thrown
    ///    by any unimplemented members of derived DbGeography/DbGeometry types that correspond to the properties and methods declared in the interface.
    /// </summary>
    internal interface IDbSpatialValue
    {
        bool IsGeography { get; }
        PrimitiveTypeKind PrimitiveType { get; }
        object ProviderValue { get; }
        int? CoordinateSystemId { get; }
        string WellKnownText { get; }
        byte[] WellKnownBinary { get; }
        string GmlString { get; }
        
        Exception NotSqlCompatible();
    }

    internal static class IDbSpatialValueExtensionMethods
    {
        /// <summary>
        /// Returns an instance of <see cref="IDbSpatialValue"/> that wraps the specified <see cref="DbGeography"/> value.
        /// IDbSpatialValue members are guaranteed not to throw the <see cref="NotImplementedException"/>s caused by unimplemented members of their wrapped values.
        /// </summary>
        /// <param name="geographyValue">The geography instance to wrap</param>
        /// <returns>An instance of <see cref="IDbSpatialValue"/> that wraps the specified geography value</returns>
        internal static IDbSpatialValue AsSpatialValue(this DbGeography geographyValue)
        {
            if (geographyValue == null)
            {
                return null;
            }
            return new DbGeographyAdapter(geographyValue);
        }

        /// <summary>
        /// Returns an instance of <see cref="IDbSpatialValue"/> that wraps the specified <see cref="DbGeometry"/> value.
        /// IDbSpatialValue members are guaranteed not to throw the <see cref="NotImplementedException"/>s caused by unimplemented members of their wrapped values.
        /// </summary>
        /// <param name="geometryValue">The geometry instance to wrap</param>
        /// <returns>An instance of <see cref="IDbSpatialValue"/> that wraps the specified geometry value</returns>
        internal static IDbSpatialValue AsSpatialValue(this DbGeometry geometryValue)
        {
            if (geometryValue == null)
            {
                return null;
            }
            return new DbGeometryAdapter(geometryValue);
        }
    }

    internal struct DbGeographyAdapter : IDbSpatialValue
    {
        private readonly DbGeography value;

        internal DbGeographyAdapter(DbGeography geomValue)
        {
            this.value = geomValue;
        }

        private TResult NullIfNotImplemented<TResult>(Func<DbGeography, TResult> accessor)
            where TResult : class
        {
            try
            {
                return accessor(this.value);
            }
            catch (NotImplementedException)
            {
                return null;
            }
        }

        private int? NullIfNotImplemented(Func<DbGeography, int> accessor)
        {
            try
            {
                return accessor(this.value);
            }
            catch (NotImplementedException)
            {
                return null;
            }
        }

        public bool IsGeography { get { return true; } }

        public PrimitiveTypeKind PrimitiveType { get { return PrimitiveTypeKind.Geography; } }

        public object ProviderValue
        {
            get { return NullIfNotImplemented(geog => geog.ProviderValue); }
        }

        public int? CoordinateSystemId
        {
            get { return NullIfNotImplemented(geog => geog.CoordinateSystemId); }
        }

        public string WellKnownText
        {
            get
            {
                return NullIfNotImplemented(geog => geog.AsTextIncludingElevationAndMeasure())
                    ?? NullIfNotImplemented(geog => geog.AsText()); // better than nothing if the provider doesn't support AsTextIncludingElevationAndMeasure
            }
        }

        public byte[] WellKnownBinary
        {
            get { return NullIfNotImplemented(geog => geog.AsBinary()); }
        }

        public string GmlString
        {
            get { return NullIfNotImplemented(geog => geog.AsGml()); }
        }

        public Exception NotSqlCompatible() { return EntityUtil.GeographyValueNotSqlCompatible(); }
    }

    internal struct DbGeometryAdapter : IDbSpatialValue
    {
        private readonly DbGeometry value;

        internal DbGeometryAdapter(DbGeometry geomValue)
        {
            this.value = geomValue;
        }

        private TResult NullIfNotImplemented<TResult>(Func<DbGeometry, TResult> accessor)
            where TResult : class
        {
            try
            {
                return accessor(this.value);
            }
            catch (NotImplementedException)
            {
                return null;
            }
        }

        private int? NullIfNotImplemented(Func<DbGeometry, int> accessor)
        {
            try
            {
                return accessor(this.value);
            }
            catch (NotImplementedException)
            {
                return null;
            }
        }

        public bool IsGeography { get { return false; } }

        public PrimitiveTypeKind PrimitiveType { get { return PrimitiveTypeKind.Geometry; } }

        public object ProviderValue
        {
            get { return NullIfNotImplemented(geom => geom.ProviderValue); }
        }

        public int? CoordinateSystemId
        {
            get { return NullIfNotImplemented(geom => geom.CoordinateSystemId); }
        }

        public string WellKnownText
        {
            get 
            {
                return NullIfNotImplemented(geom => geom.AsTextIncludingElevationAndMeasure())
                    ?? NullIfNotImplemented(geom => geom.AsText()); // better than nothing if the provider doesn't support AsTextIncludingElevationAndMeasure
            }
        }

        public byte[] WellKnownBinary
        {
            get { return NullIfNotImplemented(geom => geom.AsBinary()); }
        }

        public string GmlString
        {
            get { return NullIfNotImplemented(geom => geom.AsGml()); }
        }

        public Exception NotSqlCompatible() { return EntityUtil.GeometryValueNotSqlCompatible(); }
    }
}