<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using Dapper;



namespace DeclarativeSql.Dapper
{
    /// <summary>
    /// Provides the database operations for SQLite.
    /// </summary>
    internal class SqliteOperation : DbOperation
    {
        #region Constructors
        /// <summary>
        /// Creates instanse.
        /// </summary>
        /// <param name="connection">Database connection</param>
        /// <param name="transaction">Database transaction</param>
        /// <param name="provider">Database provider</param>
        /// <param name="timeout">Timeout</param>
        protected SqliteOperation(IDbConnection connection, IDbTransaction transaction, DbProvider provider, int? timeout)
            : base(connection, transaction, provider, timeout)
        {}
        #endregion


        #region BulkInsert
        /// <summary>
        /// Inserts the specified record into the table by bulk method.
        /// </summary>
        /// <typeparam name="T">Mapped type to table.</typeparam>
        /// <param name="data">Inserting target data.</param>
        /// <returns>Affected row count.</returns>
        public override int BulkInsert<T>(IEnumerable<T> data)
        {
            //--- 挿入処理本体
            Func<IEnumerable<T>, IDbTransaction, int> insert = (collection, transaction) =>
            {
                var result = 0;
                var sql = this.DbProvider.Insert<T>(false, true).ToString();
                foreach (var x in collection)
                {
                    var value = this.Connection.Execute(sql, x, transaction, this.Timeout);
                    Interlocked.Add(ref result, value);
                }
                return result;
            };

            //--- トランザクションが外部から指定されている場合はそれを利用
            if (this.Transaction != null)
                return insert(data, this.Transaction);

            //--- トランザクションが外部から指定されていない場合は新規に作成
            //--- SQLiteにおけるバルクインサートの魔法
            using (var transaction = this.Connection.StartTransaction())
            {
                var result = insert(data, transaction.Raw);
                transaction.Complete();
                return result;
            }
        }


        /// <summary>
        /// Asynchronously inserts the specified record into the table by bulk method.
        /// </summary>
        /// <typeparam name="T">Mapped type to table.</typeparam>
        /// <param name="data">Inserting target data.</param>
        /// <returns>Affected row count.</returns>
        public override async Task<int> BulkInsertAsync<T>(IEnumerable<T> data)
        {
            //--- 挿入処理本体
            Func<IEnumerable<T>, IDbTransaction, Task<int>> insert = async (collection, transaction) =>
            {
                var result = 0;
                var sql = this.DbProvider.Insert<T>(false, true).ToString();
                foreach (var x in collection)
                {
                    var value = await this.Connection.ExecuteAsync(sql, x, transaction, this.Timeout).ConfigureAwait(false);
                    Interlocked.Add(ref result, value);
                }
                return result;
            };

            //--- トランザクションが外部から指定されている場合はそれを利用
            if (this.Transaction != null)
                return await insert(data, this.Transaction).ConfigureAwait(false);

            //--- トランザクションが外部から指定されていない場合は新規に作成
            //--- SQLiteにおけるバルクインサートの魔法
            using (var transaction = this.Connection.StartTransaction())
            {<commit_msg>Use local function<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using Dapper;



namespace DeclarativeSql.Dapper
{
    /// <summary>
    /// Provides the database operations for SQLite.
    /// </summary>
    internal class SqliteOperation : DbOperation
    {
        #region Constructors
        /// <summary>
        /// Creates instanse.
        /// </summary>
        /// <param name="connection">Database connection</param>
        /// <param name="transaction">Database transaction</param>
        /// <param name="provider">Database provider</param>
        /// <param name="timeout">Timeout</param>
        protected SqliteOperation(IDbConnection connection, IDbTransaction transaction, DbProvider provider, int? timeout)
            : base(connection, transaction, provider, timeout)
        {}
        #endregion


        #region BulkInsert
        /// <summary>
        /// Inserts the specified record into the table by bulk method.
        /// </summary>
        /// <typeparam name="T">Mapped type to table.</typeparam>
        /// <param name="data">Inserting target data.</param>
        /// <returns>Affected row count.</returns>
        public override int BulkInsert<T>(IEnumerable<T> data)
        {
            //--- 挿入処理本体
            int insert(IEnumerable<T> collection, IDbTransaction transaction)
            {
                var result = 0;
                var sql = this.DbProvider.Insert<T>(false, true).ToString();
                foreach (var x in collection)
                {
                    var value = this.Connection.Execute(sql, x, transaction, this.Timeout);
                    Interlocked.Add(ref result, value);
                }
                return result;
            };

            //--- トランザクションが外部から指定されている場合はそれを利用
            if (this.Transaction != null)
                return insert(data, this.Transaction);

            //--- トランザクションが外部から指定されていない場合は新規に作成
            //--- SQLiteにおけるバルクインサートの魔法
            using (var transaction = this.Connection.StartTransaction())
            {
                var result = insert(data, transaction.Raw);
                transaction.Complete();
                return result;
            }
        }


        /// <summary>
        /// Asynchronously inserts the specified record into the table by bulk method.
        /// </summary>
        /// <typeparam name="T">Mapped type to table.</typeparam>
        /// <param name="data">Inserting target data.</param>
        /// <returns>Affected row count.</returns>
        public override async Task<int> BulkInsertAsync<T>(IEnumerable<T> data)
        {
            //--- 挿入処理本体
            async Task<int> insert(IEnumerable<T> collection, IDbTransaction transaction)
            {
                var result = 0;
                var sql = this.DbProvider.Insert<T>(false, true).ToString();
                foreach (var x in collection)
                {
                    var value = await this.Connection.ExecuteAsync(sql, x, transaction, this.Timeout).ConfigureAwait(false);
                    Interlocked.Add(ref result, value);
                }
                return result;
            };

            //--- トランザクションが外部から指定されている場合はそれを利用
            if (this.Transaction != null)
                return await insert(data, this.Transaction).ConfigureAwait(false);

            //--- トランザクションが外部から指定されていない場合は新規に作成
            //--- SQLiteにおけるバルクインサートの魔法
            using (var transaction = this.Connection.StartTransaction())
            {
{'fraction_non_alphanumeric': 0.11421425593997499, 'fraction_numerical': 0.0005557871335278589, 'mean_word_length': 1.7296169890026545, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 83, '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': '4667114', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1915, 'n_words': 430}