<commit_before>﻿namespace Swan.Data.SqlBulkOps;

using Swan.Data.Schema;
using Swan.Reflection;
using System.Globalization;
using System.Text;

internal static class HelperExtensions
{
    private static readonly CultureInfo Invariant = CultureInfo.InvariantCulture;

    public static bool TryGetRowsCopied(this SqlBulkCopy bulkOperation, out int rowsCopied)
    {
        const string RowsCopiedFieldName = "_rowsCopied";

        rowsCopied = 0;
        var rowsCopiedField = typeof(SqlBulkCopy).TypeInfo().Fields
            .FirstOrDefault(c => !c.IsPublic && c.FieldType == typeof(int) && c.Name.Equals(RowsCopiedFieldName, StringComparison.Ordinal));

        if (rowsCopiedField?.GetValue(bulkOperation) is int actualRowsCopied)
        {
            rowsCopied = actualRowsCopied;
            return true;
        }

        return false;
    }

    public static string BuildBulkUpdateCommandText(IDbTableSchema tempTable, IDbTableSchema targetTable, DbProvider provider)
    {
        var updateCommandText = new StringBuilder(4096);

        var columnNames = targetTable.UpdateableColumns.Select(c => c.Name).ToArray();
        var keyColumnNames = targetTable.KeyColumns.Select(c => c.Name).ToArray();

        var sourceTableName = provider.QuoteTable(tempTable);
        var targetTableName = provider.QuoteTable(targetTable);
        var sourceAlias = provider.QuoteTable("s");
        var targetAlias = provider.QuoteTable("t");

        updateCommandText.AppendLine(Invariant, $"UPDATE {targetAlias} SET");
        for (var i = 0; i < columnNames.Length; i++)
        {
            var colName = provider.QuoteField(columnNames[i]);
            var isLast = i >= columnNames.Length - 1;
            updateCommandText.AppendLine(Invariant,
                $"    {targetAlias}.{colName} = {sourceAlias}.{colName}{(isLast ? string.Empty : ",")}");
        }

        updateCommandText.AppendLine(Invariant, <commit_msg>minor update to bulk update command generator<commit_after>﻿namespace Swan.Data.SqlBulkOps;

using Swan.Data.Schema;
using Swan.Reflection;
using System.Globalization;
using System.Text;

internal static class HelperExtensions
{
    private static readonly CultureInfo Invariant = CultureInfo.InvariantCulture;

    public static bool TryGetRowsCopied(this SqlBulkCopy bulkOperation, out int rowsCopied)
    {
        const string RowsCopiedFieldName = "_rowsCopied";

        rowsCopied = 0;
        var rowsCopiedField = typeof(SqlBulkCopy).TypeInfo().Fields
            .FirstOrDefault(c => !c.IsPublic && c.FieldType == typeof(int) && c.Name.Equals(RowsCopiedFieldName, StringComparison.Ordinal));

        if (rowsCopiedField?.GetValue(bulkOperation) is int actualRowsCopied)
        {
            rowsCopied = actualRowsCopied;
            return true;
        }

        return false;
    }

    public static string BuildBulkUpdateCommandText(IDbTableSchema tempTable, IDbTableSchema targetTable, DbProvider provider)
    {
        var updateCommandText = new StringBuilder(4096);

        // Get intersection of updateable columns
        var columnNames = targetTable.UpdateableColumns.Select(c => c.Name)
            .Intersect(tempTable.UpdateableColumns.Select(c => c.Name), StringComparer.OrdinalIgnoreCase)
            .ToArray();

        var keyColumnNames = targetTable.KeyColumns.Select(c => c.Name).ToArray();
        var sourceTableName = provider.QuoteTable(tempTable);
        var targetTableName = provider.QuoteTable(targetTable);
        var sourceAlias = provider.QuoteTable("s");
        var targetAlias = provider.QuoteTable("t");

        updateCommandText.AppendLine(Invariant, $"UPDATE {targetAlias} SET");
        for (var i = 0; i < columnNames.Length; i++)
        {
            var colName = provider.QuoteField(columnNames[i]);
            var isLast = i >= columnNames.Length - 1;
            updateCommandText.AppendLine(Invariant,
                $"    {targetAlias}.{colName} = {sourceAlias}.{colName}{(isLast ? string.Empty : ",")}");
        }

        updateCommandText.AppendLine(Invariant, 
{'fraction_non_alphanumeric': 0.09972979611888971, 'fraction_numerical': 0.0034389584868582657, 'mean_word_length': 3.117290192113246, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, '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': '3621562', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1095, 'n_words': 239}