<commit_before>﻿using System;
using System.Data.SqlClient;

namespace tSQLt.Client.Net.Gateways
{
    public interface ISqlServerGateway
    {
        string RunWithXmlResult(string query);
        void RunWithNoResult(string query);
    }

    public class SqlServerGateway : ISqlServerGateway
    {
        
        private readonly string _connectionString;
        private readonly int _runTimeout;

        public SqlServerGateway(string connectionString, int runTimeout)
        {
            _connectionString = connectionString;
            _runTimeout = runTimeout;
        }

        public void RunWithNoResult(string query)
        {

            using (var con = new SqlConnection(_connectionString))
                {
                    con.Open();

                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = query;
                        cmd.CommandTimeout = _runTimeout;
                        cmd.ExecuteNonQuery();
                    }
                }
        }
        
        public string RunWithXmlResult(string query)
        {
            using (var con = new SqlConnection(_connectionString))
                {
                    con.Open();

                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = query;
                        cmd.CommandTimeout = _runTimeout;

                        SqlDataReader reader = cmd.ExecuteReader();
                        if (!reader.Read())
                        {
                            throw new InvalidOperationException(
                                string.Format("Expecting to get a data reader with the response to: \"{0}\" ", query));
                        }

                        return reader[0] as string;
                    }
                }            
        }

    }
}<commit_msg>Newer versions of tSQLt now return xml in second result set with RunWithXmlResults - handling both cases<commit_after>﻿using System;
using System.Data.SqlClient;

namespace tSQLt.Client.Net.Gateways
{
    public interface ISqlServerGateway
    {
        string RunWithXmlResult(string query);
        void RunWithNoResult(string query);
    }

    public class SqlServerGateway : ISqlServerGateway
    {
        private readonly string _connectionString;
        private readonly int _runTimeout;

        public SqlServerGateway(string connectionString, int runTimeout)
        {
            _connectionString = connectionString;
            _runTimeout = runTimeout;
        }

        public void RunWithNoResult(string query)
        {
            using (var con = new SqlConnection(_connectionString))
            {
                con.Open();

                using (var cmd = con.CreateCommand())
                {
                    cmd.CommandText = query;
                    cmd.CommandTimeout = _runTimeout;
                    cmd.ExecuteNonQuery();
                }
            }
        }

        public string RunWithXmlResult(string query)
        {
            using (var con = new SqlConnection(_connectionString))
            {
                con.Open();

                using (var cmd = con.CreateCommand())
                {
                    cmd.CommandText = query;
                    cmd.CommandTimeout = _runTimeout;

                    var reader = cmd.ExecuteReader();
                    if (!reader.Read())
                    {
                        throw new InvalidOperationException(
                            string.Format("Expecting to get a data reader with the response to: \"{0}\" ", query));
                    }

                    if (reader[0] is int?)
                    {
                        reader.NextResult();
                        if (reader.Read())
                        {
                            return reader[0] as string;
                        }
                    }

                    return reader[0] as string;
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.06872852233676977, 'fraction_numerical': 0.0014727540500736377, 'mean_word_length': 1.1756540309663641, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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': '4885312', 'n_tokens_mistral': 929, 'n_tokens_neox': 868, 'n_words': 220}