// Copyright (C) 2008-2024 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Dmitri Maximov
// Created:    2008.09.26

using System;
using System.Collections.Generic;
using System.Linq;
using Xtensive.Core;
using Xtensive.Sql;
using Xtensive.Sql.Dml;

namespace Xtensive.Orm.Providers
{
  /// <summary>
  /// Describes SQL parameter binding.
  /// </summary>
  public abstract class ParameterBinding
  {
    public TypeMapping TypeMapping { get; private set; }

    public ParameterTransmissionType TransmissionType { get; private set; }

    public SqlExpression ParameterReference { get; private set; }

    public static IEnumerable<T> NormalizeBindings<T>(IEnumerable<T> bindings)
      where T : ParameterBinding
    {
      if (bindings is null)
        return Array.Empty<T>();
      var normalized = bindings.ToArray();
      if (normalized.Length == 0)
        return Array.Empty<T>();
      return normalized;
    }


    // Constructors

    protected ParameterBinding(TypeMapping typeMapping, ParameterTransmissionType transmissionType)
    {
      TypeMapping = typeMapping;
      TransmissionType = transmissionType;
      ParameterReference = SqlDml.Placeholder(this);
    }
  }
}