<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Transactions;
using Castle.Core.Internal;
using Dapper.SimpleSave.Impl;

namespace Dapper.SimpleSave
{
    public static class SimpleSaveExtensions
    {
        public static readonly DtoMetadataCache MetadataCache = new DtoMetadataCache();

        private static ISimpleSaveLogger _logger = new BasicSimpleSaveLogger();

        static SimpleSaveExtensions()
        {
            LogBuiltScripts = false;
            LogScriptsPreExecution = true;
            LogScriptsPostExecution = false;
            ExecutionTimeWarningEmitThresholdMilliseconds = 100;
            ThrowOnMultipleWriteablePropertiesAgainstSameColumn = true;
            IsRdbmsCaseSensitive = false;
        }

        public static ISimpleSaveLogger Logger
        {
            get { return _logger; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException(
                        "value",
                        "Specifying a null ISimpleSaveLogger is not permitted.");
                }
                _logger = value;
            }
        }

        public static bool LogBuiltScripts { get; set; }
        public static bool LogScriptsPreExecution { get; set; }
        public static bool LogScriptsPostExecution { get; set; }
        public static long ExecutionTimeWarningEmitThresholdMilliseconds { get; set; }
        public static bool ThrowOnMultipleWriteablePropertiesAgainstSameColumn { get; set; }
        public static bool IsRdbmsCaseSensitive { get; set; }

        public static event EventHandler<DifferenceEventArgs> DifferenceProcessed;

        internal static void OnDifferenceProcessed(DifferenceEventArgs args)
        {
            var handlers = DifferenceProcessed;
            if (handlers != null)
            {
                handlers(typeof(SimpleSaveExtensions), args);
            }
        }

        public static void UpdateAll<T>(
            this IDbConnection connection,
            IEnumerable<Tuple<T, T>> oldAndNewObjects,
            IDbTransaction transaction = null)
        {
            UpdateInternal(connection, oldAndNewObjects, false, transaction);
        }

        public static void UpdateAll<T>(
            this IDbConnection connection,
            IEnumerable<T> newObjects,
            Func<T, T> mapNewObjectToOldObject,
            IDbTransaction transaction = null)
        {
            UpdateAll(
                connection,
                newObjects.Select(
                    newObject => Tuple.Create(mapNewObjectToOldObject(newObject), newObject)),
                transaction);
        }

        public static void UpdateAllMappingFromOldObjects<T>(
            this IDbConnection connection,
            IEnumerable<T> oldObjects,
            Func<T, T> mapOldObjectToNewObject,
            IDbTransaction transaction = null)
        {
            UpdateAll(<commit_msg>Really naive zipping implementation of UpdateAll that takes two lists.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Transactions;
using Castle.Core.Internal;
using Dapper.SimpleSave.Impl;

namespace Dapper.SimpleSave
{
    public static class SimpleSaveExtensions
    {
        public static readonly DtoMetadataCache MetadataCache = new DtoMetadataCache();

        private static ISimpleSaveLogger _logger = new BasicSimpleSaveLogger();

        static SimpleSaveExtensions()
        {
            LogBuiltScripts = false;
            LogScriptsPreExecution = true;
            LogScriptsPostExecution = false;
            ExecutionTimeWarningEmitThresholdMilliseconds = 100;
            ThrowOnMultipleWriteablePropertiesAgainstSameColumn = true;
            IsRdbmsCaseSensitive = false;
        }

        public static ISimpleSaveLogger Logger
        {
            get { return _logger; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException(
                        "value",
                        "Specifying a null ISimpleSaveLogger is not permitted.");
                }
                _logger = value;
            }
        }

        public static bool LogBuiltScripts { get; set; }
        public static bool LogScriptsPreExecution { get; set; }
        public static bool LogScriptsPostExecution { get; set; }
        public static long ExecutionTimeWarningEmitThresholdMilliseconds { get; set; }
        public static bool ThrowOnMultipleWriteablePropertiesAgainstSameColumn { get; set; }
        public static bool IsRdbmsCaseSensitive { get; set; }

        public static event EventHandler<DifferenceEventArgs> DifferenceProcessed;

        internal static void OnDifferenceProcessed(DifferenceEventArgs args)
        {
            var handlers = DifferenceProcessed;
            if (handlers != null)
            {
                handlers(typeof(SimpleSaveExtensions), args);
            }
        }

        public static void UpdateAll<T>(
            this IDbConnection connection,
            IList<T> oldObjects,
            IList<T> newObjects,
            IDbTransaction transaction = null)
        {
            if (oldObjects.Count != newObjects.Count)
            {
                throw new ArgumentException(string.Format(
                    "Mismatch between length of old object list ({0}) and length of new object list."));
            }

            var target = oldObjects.Zip(newObjects, Tuple.Create);
            UpdateAll(connection, target, transaction);
        }

        public static void UpdateAll<T>(
            this IDbConnection connection,
            IEnumerable<Tuple<T, T>> oldAndNewObjects,
            IDbTransaction transaction = null)
        {
            UpdateInternal(connection, oldAndNewObjects, false, transaction);
        }

        public static void UpdateAll<T>(
            this IDbConnection connection,
            IEnumerable<T> newObjects,
            Func<T, T> mapNewObjectToOldObject,
            IDbTransaction transaction = null)
        {
            UpdateAll(
                connection,
                newObjects.Select(
                    newObject => Tuple.Create(mapNewObjectToOldObject(newObject), newObject)),
                transaction);
        }

        public static void UpdateAllMappingFromOldObjects<T>(
            this IDbConnection connection,
            IEnumerable<T> oldObjects,
            Func<T, T> mapOldObjectToNewObject,
            IDbTransaction transaction = null)
        {
            UpdateAll(
{'fraction_non_alphanumeric': 0.06201550387596899, 'fraction_numerical': 0.0010238408658768466, 'mean_word_length': 1.9873307121013544, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 28, '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': '7240423', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1568, 'n_words': 417}