<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Management.Automation;
using static System.FormattableString;

namespace PSql
{
    internal static class SqlCommandExtensions
    {
        public static IEnumerable<PSObject> ExecuteAndProjectToPSObjects(this SqlCommand command)
        {
            if (command is null)
                throw new ArgumentNullException(nameof(command));

            if (command.Connection.State == ConnectionState.Closed)
                command.Connection.Open();

            using (var reader = command.ExecuteReader())
            {
                // Visit each result set
                do
                {
                    var names = null as string[];

                    // Visit each row in result set
                    while (reader.Read())
                    {
                        // If first row in result set, get column names
                        if (names == null)
                            names = GetColumnNames(reader);

                        // Return the row as a PowerShell object
                        yield return Project(reader, names);
                    }
                }
                while (reader.NextResult());
            }
        }

        private static string[] GetColumnNames(SqlDataReader reader)
        {
            var names = new string[reader.FieldCount];

            for (var i = 0; i < names.Length; i++)
                names[i] = reader.GetName(i) ?? Invariant($"Col{i}");

            return names;
        }

        private static PSObject Project(SqlDataReader reader, string[] names)
        {
            var obj = new PSObject();

            for (var i = 0; i < names.Length; i++)
            {
                obj.Properties.Add(new PSNoteProperty(
                    name:  names[i],
                    value: reader.IsDBNull(i) ? null : reader.GetValue(i)
                ));<commit_msg>Fix error on empty column name.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Management.Automation;
using static System.FormattableString;

namespace PSql
{
    internal static class SqlCommandExtensions
    {
        public static IEnumerable<PSObject> ExecuteAndProjectToPSObjects(this SqlCommand command)
        {
            if (command is null)
                throw new ArgumentNullException(nameof(command));

            if (command.Connection.State == ConnectionState.Closed)
                command.Connection.Open();

            using (var reader = command.ExecuteReader())
            {
                // Visit each result set
                do
                {
                    var names = null as string[];

                    // Visit each row in result set
                    while (reader.Read())
                    {
                        // If first row in result set, get column names
                        if (names == null)
                            names = GetColumnNames(reader);

                        // Return the row as a PowerShell object
                        yield return Project(reader, names);
                    }
                }
                while (reader.NextResult());
            }
        }

        private static string[] GetColumnNames(SqlDataReader reader)
        {
            var names = new string[reader.FieldCount];

            for (var i = 0; i < names.Length; i++)
                names[i] = reader.GetName(i).NullIfEmpty() ?? Invariant($"Col{i}");

            return names;
        }

        private static PSObject Project(SqlDataReader reader, string[] names)
        {
            var obj = new PSObject();

            for (var i = 0; i < names.Length; i++)
            {
                obj.Properties.Add(new PSNoteProperty(
                    name:  names[i],
                    value: reader.IsDBNull(i) ? null : reader.GetValue(i)
                ));
{'fraction_non_alphanumeric': 0.085785536159601, 'fraction_numerical': 0.000997506234413965, 'mean_word_length': 1.3875, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '6709685', 'n_tokens_mistral': 972, 'n_tokens_neox': 909, 'n_words': 269}