|
| 1 | +// Copyright 2007 - Ricardo Stuven ([email protected]) |
| 2 | +// |
| 3 | +// This file is part of NHibernate.Spatial. |
| 4 | +// NHibernate.Spatial is free software; you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation; either version 2 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// NHibernate.Spatial is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with NHibernate.Spatial; if not, write to the Free Software |
| 16 | +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | + |
| 18 | +using System; |
| 19 | +using System.Data.Common; |
| 20 | +using NetTopologySuite.Geometries; |
| 21 | +using NHibernate.Engine; |
| 22 | +using NHibernate.SqlTypes; |
| 23 | +using NHibernate.Type; |
| 24 | +using Npgsql; |
| 25 | +using NpgsqlTypes; |
| 26 | + |
| 27 | +namespace NHibernate.Spatial.Type |
| 28 | +{ |
| 29 | + [Serializable] |
| 30 | + public class PostGisGeographyType : GeometryTypeBase<Geometry> |
| 31 | + { |
| 32 | + private static readonly NullableType GeometryType = new CustomGeometryType(); |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Initializes a new instance of the <see cref="PostGisGeographyType"/> class. |
| 36 | + /// </summary> |
| 37 | + public PostGisGeographyType() |
| 38 | + : base(GeometryType) |
| 39 | + { } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Converts from GeoAPI geometry type to database geography type. |
| 43 | + /// </summary> |
| 44 | + /// <param name="value">The GeoAPI geometry value.</param> |
| 45 | + /// <returns></returns> |
| 46 | + protected override Geometry FromGeometry(object value) |
| 47 | + { |
| 48 | + var geometry = value as Geometry; |
| 49 | + if (geometry == null) |
| 50 | + { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + SetDefaultSRID(geometry); |
| 55 | + |
| 56 | + return geometry; |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Converts to GeoAPI geometry type from database geography type. |
| 61 | + /// </summary> |
| 62 | + /// <param name="value">The database geography value.</param> |
| 63 | + /// <returns></returns> |
| 64 | + protected override Geometry ToGeometry(object value) |
| 65 | + { |
| 66 | + var geometry = value as Geometry; |
| 67 | + if (geometry == null) |
| 68 | + { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + SetDefaultSRID(geometry); |
| 73 | + |
| 74 | + return geometry; |
| 75 | + } |
| 76 | + |
| 77 | + [Serializable] |
| 78 | + private class CustomGeometryType : MutableType |
| 79 | + { |
| 80 | + internal CustomGeometryType() : base(new BinarySqlType()) |
| 81 | + { } |
| 82 | + |
| 83 | + public override System.Type ReturnedClass => typeof(Geometry); |
| 84 | + |
| 85 | + public override string Name => "Geography"; |
| 86 | + |
| 87 | + public override object Get(DbDataReader rs, int index, ISessionImplementor session) |
| 88 | + { |
| 89 | + return (Geometry) rs.GetValue(index); |
| 90 | + } |
| 91 | + |
| 92 | + public override object Get(DbDataReader rs, string name, ISessionImplementor session) |
| 93 | + { |
| 94 | + return Get(rs, rs.GetOrdinal(name), session); |
| 95 | + } |
| 96 | + |
| 97 | + public override void Set(DbCommand cmd, object value, int index, ISessionImplementor session) |
| 98 | + { |
| 99 | + var parameter = (NpgsqlParameter) cmd.Parameters[index]; |
| 100 | + parameter.NpgsqlDbType = NpgsqlDbType.Geography; |
| 101 | + parameter.Value = value; |
| 102 | + } |
| 103 | + |
| 104 | + public override object DeepCopyNotNull(object value) |
| 105 | + { |
| 106 | + var obj = (Geometry) value; |
| 107 | + return obj.Copy(); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments