summaryrefslogtreecommitdiff
path: root/Decompiler/Gestor.Application.Migration/Migrator.cs
blob: 1d7984f0c426310c0319b7bdb6f0e64eb1857a88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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<bool> Execute()
	{
		_ = 1;
		try
		{
			return await ExecuteFiles(await GetAllFiles());
		}
		catch (Exception)
		{
		}
		return false;
	}

	private async Task<string[]> 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<bool> ExecuteFiles(IEnumerable<string> 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<bool> 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<string> 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<bool> 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<long> LastUpdate()
	{
		return await Task.Run(delegate
		{
			UnitOfWork commited = Instancia.Commited;
			try
			{
				return commited.AtualizacaoRepository.FindLastUpdate();
			}
			finally
			{
				((IDisposable)commited)?.Dispose();
			}
		});
	}
}