<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Data.Linq.Mapping;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using DbExtensions;

namespace Samples {

   class Program {

      static void Main() {
         new Program().Run();
      }

      void Run() {

         Console.WriteLine("DbExtensions Sample Runner");
         Console.WriteLine("==========================");

         var connectionStrings = ConfigurationManager.ConnectionStrings
            .Cast<ConnectionStringSettings>()
            // Only connection strings defined in this application config file (not in machine.config)
            .Where(c => c.ElementInformation.Source != null && c.ElementInformation.Source.EndsWith("exe.config", StringComparison.OrdinalIgnoreCase))
            .ToArray();

         int connIndex = GetArrayOption(connectionStrings.Select(c => c.Name).ToArray(), "Select a connection string (or Enter to select the first one):");
         ConnectionStringSettings connSettings = connectionStrings[connIndex];
         DbProviderFactory provider = DbFactory.GetProviderFactory(connSettings.ProviderName);
         string connectionString = "name=" + connSettings.Name;

         Console.WriteLine();
         Console.WriteLine("Provider: {0}", provider.GetType().AssemblyQualifiedName);
         Console.WriteLine();
         Console.WriteLine("Connecting...");

         try {
            DbConnection conn = DbFactory.CreateConnection(connectionString);
            using (conn.EnsureOpen())
               Console.WriteLine("Server Version: {0}", conn.ServerVersion);
         } catch (Exception ex) {

            WriteError(ex, fatal: true);
            return;
         }

         string[] samplesLangs = { "C#", "VB" };
         int samplesLangIndex = GetArrayOption(samplesLangs, "Select the samples language (or Enter):");
         string samplesLanguage = samplesLangs[samplesLangIndex];

         MappingSource[] mappingSources = { new AttributeMappingSource(), XmlMappingSource.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("Samples.Northwind.Northwind.xml")) };
         int mappingSourceIndex = GetArrayOption(mappingSources, "Select the mapping source (or Enter):");
         MappingSource mappingSource = mappingSources[mappingSourceIndex];

         object[] samples = GetSamples(samplesLanguage, connectionString, mappingSource, Console.Out).ToArray();
         string[] samplesOptions = samples.Select(o => o.GetType().Name).Concat(new[] { "All" }).ToArray();

         int samplesIndex = GetArrayOption(samplesOptions, "Select the samples category (or Enter to run all):", samplesOptions.Length - 1);

         object[] selectedSamples = (samplesIndex == samplesOptions.Length - 1) ?
            samples
            : new[] { samples[samplesIndex] };

         string[] continueOnErrorOptions = { "Yes", "No" };
         bool continueOnError = GetArrayOption(continueOnErrorOptions, "Continue on Error:") == 0;

         Console.WriteLine();
         Console.WriteLine("Press key to begin...");
         Console.ReadKey();

         for (int i = 0; i < selectedSamples.Length; i++) {

            RunSamples(selectedSamples[i], continueOnError);
            Console.WriteLine();
            Console.WriteLine((i == selectedSamples.Length - 1) ? "Press key to continue..." : "Press key to exit...");
            Console.ReadKey();
         }
      }

      IEnumerable<object> GetSamples(string language, string connectionString, MappingSource mappingSource, TextWriter log) {

         MetaModel mapping;

         switch (language) {
            case "C#":
               mapping = (mappingSource is AttributeMappingSource) ?
                  mappingSource.GetModel(typeof(Samples.CSharp.Northwind.NorthwindDatabase))
                  : mappingSource.GetModel(typeof(Samples.CSharp.Northwind.ForXmlMappingSourceOnlyDataContext));

               yield return new Samples.CSharp.ExtensionMethodsSamples(connectionString, log);
               yield return new Samples.CSharp.SqlBuilderSamples();
               yield return new Samples.CSharp.SqlSetSamples(connectionString, log);
               yield return new Samples.CSharp.DatabaseSamples(connectionString, mapping, log);
               break;

            case "VB":
               mapping = (mappingSource is AttributeMappingSource) ?
                  mappingSource.GetModel(typeof(Samples.VisualBasic.Northwind.NorthwindDatabase))
                  : mappingSource.GetModel(typeof(Samples.VisualBasic.Northwind.ForXmlMappingSourceOnlyDataContext));

               yield return new Samples.VisualBasic.ExtensionMethodsSamples(connectionString, log);
               yield return new Samples.VisualBasic.SqlBuilderSamples();
               yield return new Samples.VisualBasic.SqlSetSamples(connectionString, log);
               yield return new Samples.VisualBasic.DatabaseSamples(connectionString, mapping, log);
               break;

            default:
               throw new ArgumentOutOfRangeException("language", "Only C# and VB are accepted.");
         }
      }

      void RunSamples(object samples, bool continueOnError) {

         Type samplesType = samples.GetType();

         List<MethodInfo> methods = samplesType
            .GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
            .ToList();

         for (int i = 0; i < methods.Count; i++) {
            MethodInfo method = methods[i];

            Console.WriteLine();
            Console.WriteLine(method.Name);
            Array.ForEach<char>(method.Name.ToCharArray(), c => Console.Write("="));
            Console.WriteLine();

            object returnValue = null;

            if (method.ReturnType == typeof(void)) {

               var runSample = (Action)Delegate.CreateDelegate(typeof(Action), samples, method);

               if (continueOnError) {

                  try {
                     runSample();
                  } catch (Exception ex) {
                     WriteError(ex);
                  }

               } else {
                  runSample();
               }

            } else {

               Action runSample = () => {
                  returnValue = Expression.Lambda<Func<object>>(
                     Expression.Convert(
                        Expression.Call(Expression.Constant(samples), method)
                        , typeof(object)
                     )
                  ).Compile()();

                  if (returnValue is IEnumerable)
                     returnValue = ((IEnumerable)returnValue).Cast<object>().ToArray();
               };

               if (continueOnError) {

                  try {
                     runSample();
                  } catch (Exception ex) {
                     WriteError(ex);
                  }

               } else {
                  runSample();
               }
            }

            if (returnValue != null) {

               Console.WriteLine();

               var sqlbuilder = returnValue as SqlBuilder;

               if (sqlbuilder != null) {

                  Console.WriteLine(returnValue);

                  for (int j = 0; j < sqlbuilder.ParameterValues.Count; j++) {

                     object value = sqlbuilder.ParameterValues[j];
                     Type type = (value != null) ? value.GetType() : null;<commit_msg>continue on error fix<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Data.Linq.Mapping;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using DbExtensions;

namespace Samples {

   class Program {

      static void Main() {
         new Program().Run();
      }

      void Run() {

         Console.WriteLine("DbExtensions Sample Runner");
         Console.WriteLine("==========================");

         var connectionStrings = ConfigurationManager.ConnectionStrings
            .Cast<ConnectionStringSettings>()
            // Only connection strings defined in this application config file (not in machine.config)
            .Where(c => c.ElementInformation.Source != null && c.ElementInformation.Source.EndsWith("exe.config", StringComparison.OrdinalIgnoreCase))
            .ToArray();

         int connIndex = GetArrayOption(connectionStrings.Select(c => c.Name).ToArray(), "Select a connection string (or Enter to select the first one):");
         ConnectionStringSettings connSettings = connectionStrings[connIndex];
         DbProviderFactory provider = DbFactory.GetProviderFactory(connSettings.ProviderName);
         string connectionString = "name=" + connSettings.Name;

         Console.WriteLine();
         Console.WriteLine("Provider: {0}", provider.GetType().AssemblyQualifiedName);
         Console.WriteLine();
         Console.WriteLine("Connecting...");

         try {
            DbConnection conn = DbFactory.CreateConnection(connectionString);
            using (conn.EnsureOpen())
               Console.WriteLine("Server Version: {0}", conn.ServerVersion);
         } catch (Exception ex) {

            WriteError(ex, fatal: true);
            return;
         }

         string[] samplesLangs = { "C#", "VB" };
         int samplesLangIndex = GetArrayOption(samplesLangs, "Select the samples language (or Enter):");
         string samplesLanguage = samplesLangs[samplesLangIndex];

         MappingSource[] mappingSources = { new AttributeMappingSource(), XmlMappingSource.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("Samples.Northwind.Northwind.xml")) };
         int mappingSourceIndex = GetArrayOption(mappingSources, "Select the mapping source (or Enter):");
         MappingSource mappingSource = mappingSources[mappingSourceIndex];

         object[] samples = GetSamples(samplesLanguage, connectionString, mappingSource, Console.Out).ToArray();
         string[] samplesOptions = samples.Select(o => o.GetType().Name).Concat(new[] { "All" }).ToArray();

         int samplesIndex = GetArrayOption(samplesOptions, "Select the samples category (or Enter to run all):", samplesOptions.Length - 1);

         object[] selectedSamples = (samplesIndex == samplesOptions.Length - 1) ?
            samples
            : new[] { samples[samplesIndex] };

         string[] continueOnErrorOptions = { "Yes", "No" };
         bool continueOnError = GetArrayOption(continueOnErrorOptions, "Continue on Error:") == 0;

         Console.WriteLine();
         Console.WriteLine("Press key to begin...");
         Console.ReadKey();

         for (int i = 0; i < selectedSamples.Length; i++) {

            RunSamples(selectedSamples[i], continueOnError);
            Console.WriteLine();
            Console.WriteLine((i == selectedSamples.Length - 1) ? "Press key to continue..." : "Press key to exit...");
            Console.ReadKey();
         }
      }

      IEnumerable<object> GetSamples(string language, string connectionString, MappingSource mappingSource, TextWriter log) {

         MetaModel mapping;

         switch (language) {
            case "C#":
               mapping = (mappingSource is AttributeMappingSource) ?
                  mappingSource.GetModel(typeof(Samples.CSharp.Northwind.NorthwindDatabase))
                  : mappingSource.GetModel(typeof(Samples.CSharp.Northwind.ForXmlMappingSourceOnlyDataContext));

               yield return new Samples.CSharp.ExtensionMethodsSamples(connectionString, log);
               yield return new Samples.CSharp.SqlBuilderSamples();
               yield return new Samples.CSharp.SqlSetSamples(connectionString, log);
               yield return new Samples.CSharp.DatabaseSamples(connectionString, mapping, log);
               break;

            case "VB":
               mapping = (mappingSource is AttributeMappingSource) ?
                  mappingSource.GetModel(typeof(Samples.VisualBasic.Northwind.NorthwindDatabase))
                  : mappingSource.GetModel(typeof(Samples.VisualBasic.Northwind.ForXmlMappingSourceOnlyDataContext));

               yield return new Samples.VisualBasic.ExtensionMethodsSamples(connectionString, log);
               yield return new Samples.VisualBasic.SqlBuilderSamples();
               yield return new Samples.VisualBasic.SqlSetSamples(connectionString, log);
               yield return new Samples.VisualBasic.DatabaseSamples(connectionString, mapping, log);
               break;

            default:
               throw new ArgumentOutOfRangeException("language", "Only C# and VB are accepted.");
         }
      }

      void RunSamples(object samples, bool continueOnError) {

         Type samplesType = samples.GetType();

         List<MethodInfo> methods = samplesType
            .GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
            .ToList();

         for (int i = 0; i < methods.Count; i++) {
            MethodInfo method = methods[i];

            Console.WriteLine();
            Console.WriteLine(method.Name);
            Array.ForEach<char>(method.Name.ToCharArray(), c => Console.Write("="));
            Console.WriteLine();

            object returnValue = null;

            if (method.ReturnType == typeof(void)) {

               var runSample = (Action)Delegate.CreateDelegate(typeof(Action), samples, method);

               if (continueOnError) {

                  try {
                     runSample();
                  } catch (Exception ex) {
                     WriteError(ex);
                     continue;
                  }

               } else {
                  runSample();
               }

            } else {

               Action runSample = () => {
                  returnValue = Expression.Lambda<Func<object>>(
                     Expression.Convert(
                        Expression.Call(Expression.Constant(samples), method)
                        , typeof(object)
                     )
                  ).Compile()();

                  if (returnValue is IEnumerable)
                     returnValue = ((IEnumerable)returnValue).Cast<object>().ToArray();
               };

               if (continueOnError) {

                  try {
                     runSample();
                  } catch (Exception ex) {
                     WriteError(ex);
                     continue;
                  }

               } else {
                  runSample();
               }
            }

            if (returnValue != null) {

               Console.WriteLine();

               var sqlbuilder = returnValue as SqlBuilder;

               if (sqlbuilder != null) {

                  Console.WriteLine(returnValue);

                  for (int j = 0; j < sqlbuilder.ParameterValues.Count; j++) {

                     object value = sqlbuilder.ParameterValues[j];
                     Type type = (value != null) ? value.GetType() : null;
{'fraction_non_alphanumeric': 0.10561143462149285, 'fraction_numerical': 0.0011911064055055585, 'mean_word_length': 2.1315789473684212, 'pattern_counts': {'":': 4, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1732332', 'n_tokens_mistral': 3664, 'n_tokens_neox': 3382, 'n_words': 806}