-
Notifications
You must be signed in to change notification settings - Fork 3
/
YeSqlLoader.HelperMethods.cs
116 lines (102 loc) · 3.7 KB
/
YeSqlLoader.HelperMethods.cs
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
using System;
using System.Collections.Generic;
using System.IO;
namespace YeSql.Net;
public partial class YeSqlLoader
{
/// <summary>
/// Throws an exception if there are errors.
/// </summary>
/// <exception cref="AggregateException">
/// If the parser and/or loader encounters one or more errors.
/// </exception>
private void ThrowExceptionIfErrorsExist()
{
var exceptions = new List<Exception>();
if (_validationResult.HasError())
exceptions.Add(new YeSqlLoaderException(_validationResult.ErrorMessages));
if (_parser.ValidationResult.HasError())
exceptions.Add(new YeSqlParserException(_parser.ValidationResult.ErrorMessages));
if (exceptions.Count > 0)
throw new AggregateException(exceptions);
}
/// <summary>
/// Loads the SQL statements from a specified file.
/// </summary>
/// <param name="file">
/// The SQL file to load (can include your path or not).
/// </param>
private Result<SqlFile> LoadFromFile(string file)
{
var path = Path.IsPathRooted(file) ?
file :
Path.Combine(AppContext.BaseDirectory, file);
if (HasNotSqlExtension(path))
{
_validationResult.Add(string.Format(ExceptionMessages.FileHasNotSqlExtension, file));
return Result<SqlFile>.Failure();
}
if (!File.Exists(path))
{
_validationResult.Add(string.Format(ExceptionMessages.FileNotFound, file));
return Result<SqlFile>.Failure();
}
var sqlFile = new SqlFile
{
FileName = Path.GetFileName(file),
Content = File.ReadAllText(path)
};
return Result<SqlFile>.Success(sqlFile);
}
/// <summary>
/// Checks if the file name has not sql extension.
/// </summary>
/// <param name="fileName">
/// The file name to validate.
/// </param>
/// <returns>
/// <c>true</c> if the file has not sql extension, otherwise <c>false</c>.
/// </returns>
private bool HasNotSqlExtension(string fileName)
=> !Path.GetExtension(fileName).Equals(".sql", StringComparison.OrdinalIgnoreCase);
/// <summary>
/// Loads the SQL statements from all the SQL files in the specified directory.
/// </summary>
/// <param name="directoryName">
/// The name of the directory where the SQL files are located.
/// </param>
private Result<IEnumerable<SqlFile>> LoadFromDirectory(string directoryName)
{
var path = Path.IsPathRooted(directoryName) ?
directoryName :
Path.Combine(AppContext.BaseDirectory, directoryName);
if (!Directory.Exists(path))
{
_validationResult.Add(string.Format(ExceptionMessages.DirectoryNotFound, directoryName));
return Result<IEnumerable<SqlFile>>.Failure();
}
var sqlFiles = GetSqlFiles(path);
return Result<IEnumerable<SqlFile>>.Success(sqlFiles);
}
/// <summary>
/// Returns the details of the SQL files in a specified directory.
/// </summary>
/// <param name="directoryName">
/// The name of the directory where the SQL files are located.
/// </param>
/// <returns>
/// An enumerable of type <see cref="SqlFile"/> that contains the SQL file details.
/// </returns>
private IEnumerable<SqlFile> GetSqlFiles(string directoryName)
{
var files = Directory.GetFiles(directoryName, "*.sql", SearchOption.AllDirectories);
foreach (var file in files)
{
yield return new()
{
FileName = Path.GetFileName(file),
Content = File.ReadAllText(file)
};
}
}
}