<commit_before>using Ecos.DBInit.Core;
using System.Collections.Generic;
using Ecos.DBInit.Core.ScriptHelpers;
using Ecos.DBInit.Core.Model;
using MySql.Data.MySqlClient;
using Ecos.DBInit.MySql.ScriptHelpers;
using System.Data;
using System.Linq;

namespace Ecos.DBInit.MySql
{
    public class MySqlDBInit:IDBInit
	{
        readonly string _assemblyName;
        readonly MySqlConnection _connection;
        readonly List<Script> _scripts = new List<Script>();
        readonly MySqlScriptHelper _helper;

        public MySqlDBInit(string connectionString,string assemblyName){
            _assemblyName = assemblyName;
            //TODO: WHAT ABOUT THE INJECTION!!!!
            _connection = new MySqlConnection(connectionString);
            //TODO: WHAT ABOUT THE INJECTION!!!!
            _helper = new MySqlScriptHelper(connectionString);
        }

        public void InitData()
        {
            CleanData();
            AddData();
            ExecuteFromUoW();
        }

        void CleanData()
        {
            DeactivateReferentialIntegrity();
            CleanEachTable();
        }

        void CleanEachTable()
        {
            var scripts = new List<Script>();

            scripts.AddRange(GetDeleteEachTableScripts());

            AddToUoW(scripts);
        }

        IEnumerable<Script> GetDeleteEachTableScripts()
        {
            IEnumerable<Script> enumerable = GetEachTableName().
                Select(tableName => Script.From(string.Format("DELETE FROM {0};", tableName)));
            return enumerable;
        }

        IEnumerable<string> GetEachTableName()
        {
            var container = ScriptFinderFluentFactory.
                FromEmbeddedResource.
                InitWith(GetType().Assembly.GetName().Name, ScriptType.Data).
                GetContainer();

            var scriptsWithTableNames = ScriptLoaderFluentFactory.
                FromEmbeddedResource.
                InitWith(GetType().Assembly.GetName().Name, container).
                GetScripts("_DATABASE_NAME_",_connection.Database);

            IDictionary<int, Script> scriptMap = scriptsWithTableNames.Select((s, i) => new { s, i }).ToDictionary(x => x.i, x => x.s);
            IDictionary<int,ICollection<string>> tableNames = new Dictionary<int,ICollection<string>>();
            _helper.ExecuteAndProcess(scriptMap,tableNames,AddFirstFieldIntoStringCollectionField);
            return tableNames.Values.SelectMany(v=>v);
        }
            
        ICollection<string> AddFirstFieldIntoStringCollectionField(IDataRecord reader,int index,ICollection<string> tableNames){
            tableNames.Add(reader.GetString(0));
            return tableNames;
        }

        void AddData()
        {
            LoadDataScripts();
            ActivateReferentialIntegrity();
        }

        void LoadDataScripts()
        {
            var container = 
                ScriptFinderFluentFactory.
                FromEmbeddedResource.
                InitWith(_assemblyName, ScriptType.Data).
                GetContainer();

            var scripts = 
                ScriptLoaderFluentFactory.
                FromEmbeddedResource.
                InitWith(_assemblyName, container).
                GetScripts();

            AddToUoW(scripts);
        }

        public void InitSchema()
        {
            CleanDB();
            InitializeDB();
            ExecuteFromUoW();
        }
            
        private void CleanDB()
        {
            DeactivateReferentialIntegrity();
            DropDataBaseObjects();
        }

        private void InitializeDB()
        {
            CreateDataBaseObjects();
            ActivateReferentialIntegrity();
        }

        void DeactivateReferentialIntegrity()
        {
            var scripts = new []{ Script.From("SET @@foreign_key_checks = 0;") };

            AddToUoW(scripts);
        }

        void DropDataBaseObjects()
        {
            var scripts = new List<Script>();

            scripts.AddRange(LoadDropAllSchemaObjectsScripts());

            AddToUoW(scripts);
        }
                 
        IEnumerable<Script> LoadDropAllSchemaObjectsScripts()
        {
            IEnumerable<Script> scriptsWithDropFunction = ComposeScriptsWithDroptAllSchemaObjects();
            IDictionary<int, Script> dictionary = scriptsWithDropFunction.Select((s, i) => new { s, i }).ToDictionary(x => x.i, x => x.s);
            IDictionary<int,ICollection<Script>> dropAllSchemaObjectsScripts = new Dictionary<int,ICollection<Script>>();

            _helper.ExecuteAndProcess(dictionary,dropAllSchemaObjectsScripts,AddFirstFieldIntoScriptMap);
            return dropAllSchemaObjectsScripts.Values.SelectMany(v => v);
        }

        ICollection<Script> AddFirstFieldIntoScriptMap(IDataRecord reader, int index,ICollection<Script> dropAllSchemaObjectsScripts)
        {
            dropAllSchemaObjectsScripts.Add(Script.From(reader.GetString(0)));
            return dropAllSchemaObjectsScripts;
        }

        IEnumerable<Script> ComposeScriptsWithDroptAllSchemaObjects()
        {
            var container = ScriptFinderFluentFactory.
                FromEmbeddedResource.
                    InitWith(GetType().Assembly.GetName().Name, ScriptType.Schema).
                GetContainer();

            return ScriptLoaderFluentFactory.
                FromEmbeddedResource.<commit_msg>Change the method name for more descriptive one Formatting changes Wrapping some "freak code" into private method<commit_after>using Ecos.DBInit.Core;
using System.Collections.Generic;
using Ecos.DBInit.Core.ScriptHelpers;
using Ecos.DBInit.Core.Model;
using MySql.Data.MySqlClient;
using Ecos.DBInit.MySql.ScriptHelpers;
using System.Data;
using System.Linq;

namespace Ecos.DBInit.MySql
{
    public class MySqlDBInit:IDBInit
	{
        readonly string _assemblyName;
        readonly MySqlConnection _connection;
        readonly List<Script> _scripts = new List<Script>();
        readonly MySqlScriptHelper _helper;

        public MySqlDBInit(string connectionString,string assemblyName){
            _assemblyName = assemblyName;
            //TODO: WHAT ABOUT THE INJECTION!!!!
            _connection = new MySqlConnection(connectionString);
            //TODO: WHAT ABOUT THE INJECTION!!!!
            _helper = new MySqlScriptHelper(connectionString);
        }

        public void InitData()
        {
            CleanData();
            AddData();
            ExecuteFromUoW();
        }

        void CleanData()
        {
            DeactivateReferentialIntegrity();
            CleanEachTable();
        }

        void CleanEachTable()
        {
            var scripts = new List<Script>();

            scripts.AddRange(ComposeScriptsDeleteEachTable());

            AddToUoW(scripts);
        }

        IEnumerable<Script> ComposeScriptsDeleteEachTable()
        {
            return GetEachTableName().
                Select(tableName => 
                    Script.From(string.Format("DELETE FROM {0};", tableName))
                );
        }

        IEnumerable<string> GetEachTableName()
        {
            var container = ScriptFinderFluentFactory.
                FromEmbeddedResource.
                    InitWith(GetType().Assembly.GetName().Name, ScriptType.Data).
                GetContainer();

            var scriptsWithTableNames = ScriptLoaderFluentFactory.
                FromEmbeddedResource.
                    InitWith(GetType().Assembly.GetName().Name, container).
                GetScripts("_DATABASE_NAME_",_connection.Database);

            IDictionary<int, Script> scriptMap = FromCollectionToDictionary(scriptsWithTableNames);
            IDictionary<int,ICollection<string>> tableNames = new Dictionary<int,ICollection<string>>();

            _helper.ExecuteAndProcess(scriptMap,tableNames,AddFirstFieldIntoStringCollectionField);

            return tableNames.Values.SelectMany(v=>v);
        }

        static Dictionary<int, Script> FromCollectionToDictionary(IEnumerable<Script> collection)
        {
            return collection.
                Select((element, index) => 
                    new {
                        element,
                        index
                    }).
                ToDictionary(keySelector => keySelector.index, valueSelector => valueSelector.element);
        }
            
        ICollection<string> AddFirstFieldIntoStringCollectionField(IDataRecord reader,int index,ICollection<string> tableNames){
            tableNames.Add(reader.GetString(0));
            return tableNames;
        }

        void AddData()
        {
            LoadDataScripts();
            ActivateReferentialIntegrity();
        }

        void LoadDataScripts()
        {
            var container = 
                ScriptFinderFluentFactory.
                FromEmbeddedResource.
                InitWith(_assemblyName, ScriptType.Data).
                GetContainer();

            var scripts = 
                ScriptLoaderFluentFactory.
                FromEmbeddedResource.
                InitWith(_assemblyName, container).
                GetScripts();

            AddToUoW(scripts);
        }

        public void InitSchema()
        {
            CleanDB();
            InitializeDB();
            ExecuteFromUoW();
        }
            
        private void CleanDB()
        {
            DeactivateReferentialIntegrity();
            DropDataBaseObjects();
        }

        private void InitializeDB()
        {
            CreateDataBaseObjects();
            ActivateReferentialIntegrity();
        }

        void DeactivateReferentialIntegrity()
        {
            var scripts = new []{ Script.From("SET @@foreign_key_checks = 0;") };

            AddToUoW(scripts);
        }

        void DropDataBaseObjects()
        {
            var scripts = new List<Script>();

            scripts.AddRange(LoadDropAllSchemaObjectsScripts());

            AddToUoW(scripts);
        }
                 
        IEnumerable<Script> LoadDropAllSchemaObjectsScripts()
        {
            IEnumerable<Script> scriptsWithDropFunction = ComposeScriptsWithDroptAllSchemaObjects();
            IDictionary<int, Script> indexedScriptsWithDropFunction = FromCollectionToDictionary(scriptsWithDropFunction);
            IDictionary<int,ICollection<Script>> indexedDropAllSchemaObjectsScripts = new Dictionary<int,ICollection<Script>>();

            _helper.ExecuteAndProcess(indexedScriptsWithDropFunction,indexedDropAllSchemaObjectsScripts,AddFirstFieldIntoCollection);
            return indexedDropAllSchemaObjectsScripts.Values.SelectMany(script => script);
        }

        static ICollection<Script> AddFirstFieldIntoCollection(IDataRecord reader, int index,ICollection<Script> collection)
        {
            collection.Add(Script.From(reader.GetString(0)));
            return collection;
        }

        IEnumerable<Script> ComposeScriptsWithDroptAllSchemaObjects()
        {
            var container = ScriptFinderFluentFactory.
                FromEmbeddedResource.
                    InitWith(GetType().Assembly.GetName().Name, ScriptType.Schema).
                GetContainer();

            return ScriptLoaderFluentFactory.
                FromEmbeddedResource.
{'fraction_non_alphanumeric': 0.08479971703952605, 'fraction_numerical': 0.0007074011848969847, 'mean_word_length': 2.0378726833199035, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 67, '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': '4017820', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2720, 'n_words': 473}