<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using FirebirdSql.Data.FirebirdClient;
using Npgsql;
using NUnit.Framework;

namespace NHibernate.TestDatabaseSetup
{
    [TestFixture]
    public class DatabaseSetup
    {
		private static IDictionary<string, Action<Cfg.Configuration>> SetupMethods;

		static DatabaseSetup()
		{
			SetupMethods = new Dictionary<string, Action<Cfg.Configuration>>();
			SetupMethods.Add("NHibernate.Driver.SqlClientDriver", SetupSqlServer);
			SetupMethods.Add("NHibernate.Driver.FirebirdClientDriver", SetupFirebird);
			SetupMethods.Add("NHibernate.Driver.SQLite20Driver", SetupNoop);
			SetupMethods.Add("NHibernate.Driver.NpgsqlDriver", SetupNpgsql);
		}

		[Test]
		public void SetupDatabase()
		{
            var cfg = new Cfg.Configuration();
			var driver = cfg.Properties[Cfg.Environment.ConnectionDriver];

			Assert.That(SetupMethods.ContainsKey(driver), "No setup method found for " + driver);

			var setupMethod = SetupMethods[driver];
			setupMethod(cfg);
		}

        private static void SetupSqlServer(Cfg.Configuration cfg)
        {
            var connStr = cfg.Properties[Cfg.Environment.ConnectionString];
			
            using (var conn = new SqlConnection(connStr))
            {
                conn.Open();

                using (var cmd = new System.Data.SqlClient.SqlCommand("use master", conn))
                {
                    cmd.ExecuteNonQuery();

                    cmd.CommandText = "drop database nhibernate";

                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch(Exception e)
                    {
						Console.WriteLine(e);
                    }

                    cmd.CommandText = "create database nhibernate";
                    cmd.ExecuteNonQuery();
                }
            }
        }

		private static void SetupFirebird(Cfg.Configuration cfg)
		{
			try
			{
				if (File.Exists("NHibernate.fdb"))
					File.Delete("NHibernate.fdb");
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}

			FbConnection.CreateDatabase("Database=NHibernate.fdb;ServerType=1");
		}

		private static void SetupNpgsql(Cfg.Configuration cfg)
		{
			var connStr = cfg.Properties[Cfg.Environment.ConnectionString];

			using (var conn = new NpgsqlConnection(connStr.Replace("Database=nhibernate", "Database=postgres")))
			{
				conn.Open();

				using (var cmd = conn.CreateCommand())
				{
					cmd.CommandText = "drop database nhibernate";

					try
					{
						cmd.ExecuteNonQuery();
					}
					catch (Exception e)
					{
						Console.WriteLine(e);
					}

					cmd.CommandText = "create database nhibernate";
					cmd.ExecuteNonQuery();
				}
			}
		}

		private static void SetupNoop(Cfg.Configuration cfg)
		{
		}
	}<commit_msg>Tests: Made SQLite test database setup more like other database setups to see if it improves our build server situation.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using FirebirdSql.Data.FirebirdClient;
using Npgsql;
using NUnit.Framework;

namespace NHibernate.TestDatabaseSetup
{
    [TestFixture]
    public class DatabaseSetup
    {
		private static IDictionary<string, Action<Cfg.Configuration>> SetupMethods;

		static DatabaseSetup()
		{
			SetupMethods = new Dictionary<string, Action<Cfg.Configuration>>();
			SetupMethods.Add("NHibernate.Driver.SqlClientDriver", SetupSqlServer);
			SetupMethods.Add("NHibernate.Driver.FirebirdClientDriver", SetupFirebird);
			SetupMethods.Add("NHibernate.Driver.SQLite20Driver", SetupSQLite);
			SetupMethods.Add("NHibernate.Driver.NpgsqlDriver", SetupNpgsql);
		}

		[Test]
		public void SetupDatabase()
		{
            var cfg = new Cfg.Configuration();
			var driver = cfg.Properties[Cfg.Environment.ConnectionDriver];

			Assert.That(SetupMethods.ContainsKey(driver), "No setup method found for " + driver);

			var setupMethod = SetupMethods[driver];
			setupMethod(cfg);
		}

        private static void SetupSqlServer(Cfg.Configuration cfg)
        {
            var connStr = cfg.Properties[Cfg.Environment.ConnectionString];
			
            using (var conn = new SqlConnection(connStr))
            {
                conn.Open();

                using (var cmd = new System.Data.SqlClient.SqlCommand("use master", conn))
                {
                    cmd.ExecuteNonQuery();

                    cmd.CommandText = "drop database nhibernate";

                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch(Exception e)
                    {
						Console.WriteLine(e);
                    }

                    cmd.CommandText = "create database nhibernate";
                    cmd.ExecuteNonQuery();
                }
            }
        }

		private static void SetupFirebird(Cfg.Configuration cfg)
		{
			try
			{
				if (File.Exists("NHibernate.fdb"))
					File.Delete("NHibernate.fdb");
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}

			FbConnection.CreateDatabase("Database=NHibernate.fdb;ServerType=1");
		}

		private static void SetupNpgsql(Cfg.Configuration cfg)
		{
			var connStr = cfg.Properties[Cfg.Environment.ConnectionString];

			using (var conn = new NpgsqlConnection(connStr.Replace("Database=nhibernate", "Database=postgres")))
			{
				conn.Open();

				using (var cmd = conn.CreateCommand())
				{
					cmd.CommandText = "drop database nhibernate";

					try
					{
						cmd.ExecuteNonQuery();
					}
					catch (Exception e)
					{
						Console.WriteLine(e);
					}

					cmd.CommandText = "create database nhibernate";
					cmd.ExecuteNonQuery();
				}
			}
		}

		private static void SetupSQLite(Cfg.Configuration cfg)
		{
			if (File.Exists("NHibernate.db"))
				File.Delete("NHibernate.db");
		}
	}
{'fraction_non_alphanumeric': 0.10408232118758434, 'fraction_numerical': 0.0010121457489878543, 'mean_word_length': 2.389937106918239, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4247518', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1593, 'n_words': 322}