NpgsqlArrayTypeMapping.cs 2.6 KB
Newer Older
S
License  
Shay Rojansky 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#region License
// The PostgreSQL License
//
// Copyright (C) 2016 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion

using System;
S
Shay Rojansky 已提交
25
using System.Text;
26

27
namespace Microsoft.EntityFrameworkCore.Storage.Internal
28
{
S
Shay Rojansky 已提交
29
    public sealed class NpgsqlArrayTypeMapping : NpgsqlTypeMapping
30
    {
S
Shay Rojansky 已提交
31 32 33
        public RelationalTypeMapping ElementMapping { get; private set; }

        internal NpgsqlArrayTypeMapping(Type arrayClrType, RelationalTypeMapping elementMapping)
S
Shay Rojansky 已提交
34
            : base(elementMapping.StoreType + "[]", arrayClrType)
35
        {
S
Shay Rojansky 已提交
36
            ElementMapping = elementMapping;
37

S
Shay Rojansky 已提交
38 39
            if (elementMapping is NpgsqlTypeMapping m && m.NpgsqlDbType.HasValue)
                NpgsqlDbType = m.NpgsqlDbType.Value | NpgsqlTypes.NpgsqlDbType.Array;
40
        }
S
Shay Rojansky 已提交
41

S
Shay Rojansky 已提交
42 43 44 45
        public override RelationalTypeMapping Clone(string storeType, int? size)
            => new NpgsqlTypeMapping(storeType, ClrType, NpgsqlDbType);

        protected override string GenerateNonNullSqlLiteral(object value)
S
Shay Rojansky 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        {
            // Only support one-dimensional arrays (at least for now)
            var arr = (Array)value;

            if (arr.Rank != 1)
                throw new NotSupportedException("Multidimensional array literals aren't supported yet");

            var sb = new StringBuilder();
            sb.Append("ARRAY[");
            for (var i = 0; i < arr.Length; i++)
            {
                sb.Append(ElementMapping.GenerateSqlLiteral(arr.GetValue(i)));
                if (i < arr.Length - 1)
                    sb.Append(",");
            }
            sb.Append("]");
            return sb.ToString();
        }
64 65
    }
}