using System; using System.Collections.Generic; using System.Data.Common; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Gestor.Application.Helpers; using Gestor.Infrastructure.UnitOfWork.Generic; using Gestor.Infrastructure.UnitOfWork.Logic; using Gestor.Model.Helper; namespace Gestor.Application.Migration; public class Migrator { private readonly Assembly _executingAssembly; private static string _connection { get; set; } public Migrator() { _executingAssembly = Assembly.GetExecutingAssembly(); } public async Task Execute() { _ = 1; try { return await ExecuteFiles(await GetAllFiles()); } catch (Exception) { } return false; } private async Task GetAllFiles() { string folderName = _executingAssembly.GetName().Name + ".Migration.Files"; long lastUpdate = await LastUpdate(); return _executingAssembly.GetManifestResourceNames().Where(delegate(string r) { long num = 0L; if (r.StartsWith(folderName) && r.EndsWith(".SQL")) { num = ValidationHelper.ToInt(ValidationHelper.Clear(ValidationHelper.Index(r.Split(new char[1] { '_' }), 1))); } return num > lastUpdate; }).ToArray(); } private async Task ExecuteFiles(IEnumerable filesToExecute) { foreach (string fileName in filesToExecute.OrderBy((string x) => x)) { using Stream resourceStream = _executingAssembly.GetManifestResourceStream(fileName); if (resourceStream == null) { continue; } Encoding encoding = Encoding.GetEncoding("ISO-8859-1"); Encoding uTF = Encoding.UTF8; using StreamReader streamReader = new StreamReader(resourceStream); string s = streamReader.ReadToEnd(); byte[] bytes = uTF.GetBytes(s); byte[] bytes2 = Encoding.Convert(uTF, encoding, bytes); s = encoding.GetString(bytes2); if (!(await ExecuteScript(s))) { return false; } if (!(await SaveUpdate(ValidationHelper.ToInt(ValidationHelper.Clear(ValidationHelper.Index(fileName.Split(new char[1] { '_' }), 1)))))) { return false; } } return true; } private static async Task ExecuteScript(string scriptText) { return await Task.Run(delegate { //IL_002e: Unknown result type (might be due to invalid IL or missing references) //IL_0034: Expected O, but got Unknown IEnumerable enumerable = Regex.Split(scriptText, "^\\s*GO\\s*$", RegexOptions.IgnoreCase | RegexOptions.Multiline); try { if (string.IsNullOrEmpty(_connection)) { _connection = Connection.GetConnection(); } SqlConnection val = new SqlConnection(_connection); try { ((DbConnection)(object)val).Open(); SqlCommand val2 = val.CreateCommand(); try { foreach (string item in enumerable) { if (!(item.Trim() == "")) { ((DbCommand)(object)val2).CommandText = item; ((DbCommand)(object)val2).ExecuteNonQuery(); } } } finally { ((IDisposable)val2)?.Dispose(); } } finally { ((IDisposable)val)?.Dispose(); } return true; } catch (Exception) { return false; } }); } private static async Task SaveUpdate(long fileId) { return await Task.Run(delegate { UnitOfWork commited = Instancia.Commited; try { commited.AtualizacaoRepository.Save(fileId); ((GenericUnitOfWork)commited).Commit(); } catch (Exception) { ((GenericUnitOfWork)commited).Rollback(); return false; } finally { ((IDisposable)commited)?.Dispose(); } return true; }); } private static async Task LastUpdate() { return await Task.Run(delegate { UnitOfWork commited = Instancia.Commited; try { return commited.AtualizacaoRepository.FindLastUpdate(); } finally { ((IDisposable)commited)?.Dispose(); } }); } }