-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
155 lines (132 loc) · 5.35 KB
/
Program.cs
File metadata and controls
155 lines (132 loc) · 5.35 KB
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
using System;
using Spectre.Console;
public class Program
{
static string GetFileName(string filePath, int chunkCount, string destinationFolder)
{
string pth = filePath;
if (destinationFolder != null)
{
string fn = Path.GetFileName(filePath);
pth = Path.Combine(destinationFolder, fn);
}
string fileName = $"{pth}_chunk_{chunkCount}.sql";
return fileName;
}
/// <summary>
/// Splits a file into smaller chunks based on the specified maximum chunk size.
/// </summary>
/// <param name="filePath">The path to the file to be split.</param>
/// <param name="destinationFolder">The destination folder.</param>
/// <param name="maxChunkSize">The maximum size of each chunk in bytes.</param>
/// <param name="addGoAtTheEnd">if set to <c>true</c> the procedure add the GO command
/// at the end of each file.</param>
/// <returns></returns>
static void SplitFile(string filePath, string destinationFolder = "",
int maxChunkSize = 1024000, bool addGoAtTheEnd = true)
{
// Read all lines from the file
List<string> lines = new List<string>(File.ReadAllLines(filePath));
if (lines.Count == 0)
{
AnsiConsole.MarkupLine("[#ff0000]The file is empty.[/]");
throw new Exception("The file is empty.");
}
Console.WriteLine($"Found {lines.Count} line in the SQL script.");
List<string> chunk = new List<string>();
int chunkSize = 0;
int chunkCount = 1;
int currentRows = 0;
int totalRows = 0;
string? fName;
if (!string.IsNullOrEmpty(destinationFolder))
if (!Directory.Exists(destinationFolder))
{
try
{
Directory.CreateDirectory(destinationFolder);
Console.WriteLine($"Desctination fodler created.");
}
catch (Exception ex)
{
throw new Exception("The destination folder can't be created.");
}
}
AnsiConsole.Progress().Start(ctx =>
{
var table = new Table();
table.AddColumn("FileName");
table.AddColumn("Rows");
table.Border(TableBorder.MinimalHeavyHead);
var task = ctx.AddTask("[green]Split file[/]");
task.MaxValue = lines.Count();
// Iterate through each line in the file
foreach (string line in lines)
{
chunk.Add(line);
chunkSize += line.Length;
currentRows += 1;
totalRows += 1;
// Check if the current chunk size exceeds the maximum chunk size
if (chunkSize >= maxChunkSize)
{
if (addGoAtTheEnd)
{
chunk.Add("GO");
chunkSize += line.Length;
}
fName = GetFileName(filePath, chunkCount, destinationFolder);
table.AddRow(fName, currentRows.ToString());
// Write the current chunk to a new file
File.WriteAllLines(fName, chunk);
chunk.Clear();
chunkSize = 0;
chunkCount++;
//Console.WriteLine($"Created {fName}");
//Console.WriteLine($"Contains {currentRows} rows");
task.Increment(currentRows);
currentRows = 0;
}
}
fName = GetFileName(filePath, chunkCount, destinationFolder);
// Write any remaining lines in the last chunk
if (chunk.Count > 0)
{
File.WriteAllLines(fName, chunk);
}
task.Increment(currentRows);
table.ShowFooters();
table.AddRow("Total line read", lines.Count().ToString());
table.AddRow("Total line wrote", totalRows.ToString());
AnsiConsole.Write(table);
});
}
static void ShowHelp()
{
AnsiConsole.MarkupLine("See how to use this application adding [white]--help[/] as a parameter");
}
/// <summary>
/// Split SQL script in multiple files based on the required size.
/// </summary>
/// <param name="file">The SQL script file to split.</param>
/// <param name="destination">The destination folder. If this is empty or null, the new files will be created in the same directory as the original file.</param>
/// <param name="limit">The maximum bytes limit for the new files.</param>
/// <param name="addGo">if set to <c>true</c> the procedure will add the command GO at the end of each file.</param>
public static void Main(string file, string? destination,
int limit = 10240000, bool addGo = true)
{
if (string.IsNullOrEmpty(file))
{
AnsiConsole.MarkupLine("[#ff0000]The SQL script file must be passed![/]");
ShowHelp();
Environment.Exit(1);
}
if (!File.Exists(file))
{
AnsiConsole.MarkupLine("[#ff0000]The SQL script file doesn't not exists or accessible.[/]");
ShowHelp();
Environment.Exit(1);
}
SplitFile(file, destination ?? "", limit, addGo);
}
}