<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using Aurum.SQL.Data;

using static Aurum.SQL.Helpers.SqlHelpers;

namespace Aurum.SQL.Readers
{
	/// <summary>Provides Query Metadata for SQL2012 and Newer</summary>
	public class SqlQueryReader2012 : IDisposable, ISqlValidator
	{
		SqlConnection _cnn;

		public SqlQueryReader2012(string connectionString)
		{
			_cnn = new SqlConnection(connectionString);
			_cnn.Open();
		}

		public bool Validate(string query, out IList<SqlError> errors)
		{
			var result = RunAndGetErrors(() => RunParameterQuery(query).ToList(), out errors);
			return errors == null;
		}

		public IList<Data.SqlParameter> GetParameters(string query, out IList<SqlError> errors)
		{
			return RunAndGetErrors(() => RunParameterQuery(query).ToList(), out errors);
		}

		public IList<Data.SqlColumn> GetResultSet(string query, out IList<SqlError> errors, params string[] parameters)
		{
			return RunAndGetErrors(() => RunResultSetQuery(query, parameters).ToList(), out errors);
		}

		private IEnumerable<Data.SqlParameter> RunParameterQuery(string sql)
		{
			var query = "sys.sp_describe_undeclared_parameters";
			var command = new SqlCommand(query, _cnn) { CommandType = CommandType.StoredProcedure }
				.AddParam("tsql", sql);

			using (var reader = command.ExecuteReader())
			{
				while (reader.Read()) yield return new Data.SqlParameter
				{
					Name = Convert.ToString(reader["name"]),
					Order = Convert.ToInt32(reader["parameter_ordinal"]),
					SQLType = (SqlDbType)Convert.ToInt32(reader["system_type_id"]),
					//Nullable = Convert.ToBoolean(reader["is_nullable"]),<commit_msg>#15: Fixed broken type mappings<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using Aurum.SQL.Data;

using static Aurum.SQL.Helpers.SqlHelpers;

namespace Aurum.SQL.Readers
{
	/// <summary>Provides Query Metadata for SQL2012 and Newer</summary>
	public class SqlQueryReader2012 : IDisposable, ISqlValidator
	{
		SqlConnection _cnn;

		public SqlQueryReader2012(string connectionString)
		{
			_cnn = new SqlConnection(connectionString);
			_cnn.Open();
		}

		public bool Validate(string query, out IList<SqlError> errors)
		{
			var result = RunAndGetErrors(() => RunParameterQuery(query).ToList(), out errors);
			return errors == null;
		}

		public IList<Data.SqlParameter> GetParameters(string query, out IList<SqlError> errors)
		{
			return RunAndGetErrors(() => RunParameterQuery(query).ToList(), out errors);
		}

		public IList<Data.SqlColumn> GetResultSet(string query, out IList<SqlError> errors, params string[] parameters)
		{
			return RunAndGetErrors(() => RunResultSetQuery(query, parameters).ToList(), out errors);
		}

		private IEnumerable<Data.SqlParameter> RunParameterQuery(string sql)
		{
			var query = "sys.sp_describe_undeclared_parameters";
			var command = new SqlCommand(query, _cnn) { CommandType = CommandType.StoredProcedure }
				.AddParam("tsql", sql);

			using (var reader = command.ExecuteReader())
			{
				while (reader.Read()) yield return new Data.SqlParameter
				{
					Name = Convert.ToString(reader["name"]),
					Order = Convert.ToInt32(reader["parameter_ordinal"]),
					SQLType = (SqlDbType)Convert.ToInt32(reader["suggested_system_type_id"]),
					//Nullable = Convert.ToBoolean(reader["is_nullable"]),
{'fraction_non_alphanumeric': 0.11807909604519774, 'fraction_numerical': 0.0096045197740113, 'mean_word_length': 5.01188455008489, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, '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': '4221545', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1077, 'n_words': 243}