<commit_before>﻿using Dibware.Helpers.Validation;
using Dibware.StoredProcedureFramework.Extensions;
using Dibware.StoredProcedureFramework.StoredProcedureAttributes;
using Microsoft.SqlServer.Server;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;

namespace Dibware.StoredProcedureFramework.Helpers
{
    /// <summary>
    /// Responsible for building Table Value Paramerters as a an Enumerable
    /// collection of SqlDataRecords from a strongly typed list of items.
    /// </summary>
    public class TableValuedParameterBuilder
    {
        #region Fields

        private const int DefaultDecimalParameterPrecision = 10;
        private const int DefaultDecimalParameterScale = 2;
        private const int DefaultSizeAttribute = 50;
        private const List<SqlDataRecord> DefaultValueForEmptyList = null;
        private readonly IList _tableValueParameterList;
        private readonly List<SqlMetaData> _columnList;
        private readonly Dictionary<String, String> _mapping;
        private readonly Type _listTypeUnderlyingType;
        private List<SqlDataRecord> _tableValueParameters;
        private PropertyInfo[] _mappedProperties;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TableValuedParameterBuilder"/> 
        /// class with a new list of .
        /// </summary>
        /// <param name="tableValueParameterList">The item list.</param>
        public TableValuedParameterBuilder(IList tableValueParameterList)
        {
            if (tableValueParameterList == null) throw new ArgumentNullException("tableValueParameterList");

            _tableValueParameterList = tableValueParameterList;
            _listTypeUnderlyingType = tableValueParameterList.GetUnderlyingType();
            _columnList = new List<SqlMetaData>();
            _mapping = new Dictionary<string, string>();
        }

        #endregion

        #region Public Members

        /// <summary>
        /// Builds the parameters.
        /// </summary>
        public TableValuedParameterBuilder BuildParameters()
        {
            if (HasEmptyItemList)
            {
                _tableValueParameters = DefaultValueForEmptyList;
            }
            else
            {
                BuildMappedProperties();
                BuildMappingsFromMappedProperties();
                BuildTableValueParameters();
            }
            return this;
        }

        /// <summary>
        /// Gets the table value parameters.
        /// </summary>
        /// <value>
        /// The table value parameters.
        /// </value>
        public IEnumerable<SqlDataRecord> TableValueParameters
        {
            get { return _tableValueParameters; }
        }

        #endregion

        #region Private Members

        private void BuildMappedProperties()
        {
            _mappedProperties = _listTypeUnderlyingType.GetMappedProperties();
        }

        private void BuildMappingsFromMappedProperties()
        {
            ClearMappings();
            ClearColumnList();

            foreach (PropertyInfo propertyInfo in _mappedProperties)
            {
                AddPropertyNameMapping(propertyInfo);
                CreateAndAddSqlMetaDataColumn(propertyInfo);
            }
        }

        private void AddPropertyNameMapping(PropertyInfo propertyInfo)
        {
            var name = propertyInfo.GetNamefromAttributeOrPropertyName();
            _mapping.Add(name, propertyInfo.Name);
        }

        private void BuildTableValueParameters()
        {
            ClearParametersIfNotNullAndNotEmpty();
            InstantiateParametersIfNull();
            CreateAndAddSqlDataRecords();
        }

        private void ClearColumnList()
        {
            _columnList.Clear();
        }

        private void ClearMappings()
        {
            _mapping.Clear();
        }

        private void ClearParametersIfNotNullAndNotEmpty()
        {
            if (_tableValueParameters != null && _tableValueParameters.Count > 0)
            {
                _tableValueParameters.Clear();
            }
        }

        private void CreateAndAddSqlDataRecords()
        {
            foreach (object item in _tableValueParameterList)
            {
                CreateAndAddSqlDataRecord(item);
            }
        }

        private void CreateAndAddSqlMetaDataColumn(PropertyInfo propertyInfo)
        {
            var columnMetaData = CreateColumnMetaData(propertyInfo);
            _columnList.Add(columnMetaData);
        }

        private static SqlMetaData CreateColumnMetaData(PropertyInfo propertyInfo)
        {
            SqlMetaData columnMetaData;
            var name = propertyInfo.GetNamefromAttributeOrPropertyName();<commit_msg>20151218_0755_DWDT Removed unused variable decalaration in `TableValuedParameterBuilder`<commit_after>﻿using Dibware.Helpers.Validation;
using Dibware.StoredProcedureFramework.Extensions;
using Dibware.StoredProcedureFramework.StoredProcedureAttributes;
using Microsoft.SqlServer.Server;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;

namespace Dibware.StoredProcedureFramework.Helpers
{
    /// <summary>
    /// Responsible for building Table Value Paramerters as a an Enumerable
    /// collection of SqlDataRecords from a strongly typed list of items.
    /// </summary>
    public class TableValuedParameterBuilder
    {
        #region Fields

        private const int DefaultDecimalParameterPrecision = 10;
        private const int DefaultDecimalParameterScale = 2;
        private const int DefaultSizeAttribute = 50;
        private const List<SqlDataRecord> DefaultValueForEmptyList = null;
        private readonly IList _tableValueParameterList;
        private readonly List<SqlMetaData> _columnList;
        private readonly Dictionary<String, String> _mapping;
        private readonly Type _listTypeUnderlyingType;
        private List<SqlDataRecord> _tableValueParameters;
        private PropertyInfo[] _mappedProperties;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TableValuedParameterBuilder"/> 
        /// class with a new list of .
        /// </summary>
        /// <param name="tableValueParameterList">The item list.</param>
        public TableValuedParameterBuilder(IList tableValueParameterList)
        {
            if (tableValueParameterList == null) throw new ArgumentNullException("tableValueParameterList");

            _tableValueParameterList = tableValueParameterList;
            _listTypeUnderlyingType = tableValueParameterList.GetUnderlyingType();
            _columnList = new List<SqlMetaData>();
            _mapping = new Dictionary<string, string>();
        }

        #endregion

        #region Public Members

        /// <summary>
        /// Builds the parameters.
        /// </summary>
        public TableValuedParameterBuilder BuildParameters()
        {
            if (HasEmptyItemList)
            {
                _tableValueParameters = DefaultValueForEmptyList;
            }
            else
            {
                BuildMappedProperties();
                BuildMappingsFromMappedProperties();
                BuildTableValueParameters();
            }
            return this;
        }

        /// <summary>
        /// Gets the table value parameters.
        /// </summary>
        /// <value>
        /// The table value parameters.
        /// </value>
        public IEnumerable<SqlDataRecord> TableValueParameters
        {
            get { return _tableValueParameters; }
        }

        #endregion

        #region Private Members

        private void BuildMappedProperties()
        {
            _mappedProperties = _listTypeUnderlyingType.GetMappedProperties();
        }

        private void BuildMappingsFromMappedProperties()
        {
            ClearMappings();
            ClearColumnList();

            foreach (PropertyInfo propertyInfo in _mappedProperties)
            {
                AddPropertyNameMapping(propertyInfo);
                CreateAndAddSqlMetaDataColumn(propertyInfo);
            }
        }

        private void AddPropertyNameMapping(PropertyInfo propertyInfo)
        {
            var name = propertyInfo.GetNamefromAttributeOrPropertyName();
            _mapping.Add(name, propertyInfo.Name);
        }

        private void BuildTableValueParameters()
        {
            ClearParametersIfNotNullAndNotEmpty();
            InstantiateParametersIfNull();
            CreateAndAddSqlDataRecords();
        }

        private void ClearColumnList()
        {
            _columnList.Clear();
        }

        private void ClearMappings()
        {
            _mapping.Clear();
        }

        private void ClearParametersIfNotNullAndNotEmpty()
        {
            if (_tableValueParameters != null && _tableValueParameters.Count > 0)
            {
                _tableValueParameters.Clear();
            }
        }

        private void CreateAndAddSqlDataRecords()
        {
            foreach (object item in _tableValueParameterList)
            {
                CreateAndAddSqlDataRecord(item);
            }
        }

        private void CreateAndAddSqlMetaDataColumn(PropertyInfo propertyInfo)
        {
            var columnMetaData = CreateColumnMetaData(propertyInfo);
            _columnList.Add(columnMetaData);
        }

        private static SqlMetaData CreateColumnMetaData(PropertyInfo propertyInfo)
        {
            SqlMetaData columnMetaData;
{'fraction_non_alphanumeric': 0.07085165116041305, 'fraction_numerical': 0.0024537368367242613, 'mean_word_length': 2.097530082330589, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4377785', 'n_tokens_mistral': 2286, 'n_tokens_neox': 2229, 'n_words': 514}