-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pump.cs
271 lines (243 loc) · 9.95 KB
/
Pump.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// -----------------------------------------------------------------------
// <copyright file="Pump.cs" company="Conglomo">
// Copyright 2019-2024 Conglomo Limited. Please see LICENSE for license details.
// </copyright>
// -----------------------------------------------------------------------
namespace Conglomo.DataPump;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FirebirdSql.Data.FirebirdClient;
using Microsoft.Data.SqlClient;
using MySqlConnector;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
/// <summary>
/// The main data pump.
/// </summary>
public static class Pump
{
/// <summary>
/// Executes the specified configuration.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <returns>The asynchronous task.</returns>
/// <exception cref="ArgumentException">Thrown when the configuration is invalid.</exception>
public static async Task ExecuteAsync(PumpConfiguration configuration)
{
if (configuration.IsValid())
{
switch (configuration.FileType)
{
case FileType.CSV:
{
// Execute the query
await using StreamWriter writer = File.CreateText(configuration.OutputFile);
bool firstRow = true;
await foreach (
object[] values in ExecuteQueryAsync(
configuration.Database,
configuration.ConnectionString,
await File.ReadAllTextAsync(configuration.SqlFile)))
{
bool firstColumn = true;
for (int i = 0; i < values.Length; i++)
{
string? text = values[i].ToString();
if (text != default)
{
// Check for SYLK workaround
if (firstRow && firstColumn && text == "ID")
{
await writer.WriteAsync("\"ID\"");
}
else
{
await writer.WriteAsync(text.EncodeCsvField());
}
// If this is not the last column, write the comma
if (i < values.Length - 1)
{
await writer.WriteAsync(',');
}
}
// Clear the first column flag
firstColumn = false;
}
// Write and flush
await writer.WriteLineAsync();
await writer.FlushAsync();
// Clear the first row flag
firstRow = false;
}
writer.Close();
return;
}
case FileType.XLS:
{
using IWorkbook workbook = new HSSFWorkbook();
await WriteSpreadsheet(configuration, workbook);
return;
}
case FileType.XLSX:
{
using IWorkbook workbook = new XSSFWorkbook();
await WriteSpreadsheet(configuration, workbook);
return;
}
}
}
throw new ArgumentException(Properties.Resources.InvalidConfiguration);
}
/// <summary>
/// Executes the Firebird query asynchronously.
/// </summary>
/// <param name="database">The database type.</param>
/// <param name="connectionString">The connection string.</param>
/// <param name="sql">The SQL.</param>
/// <returns>
/// Each row, starting with the column names.
/// </returns>
/// <exception cref="ArgumentException">There was an invalid data pump configuration.</exception>
private static async IAsyncEnumerable<object[]> ExecuteQueryAsync(
Database database,
string connectionString,
string sql)
{
// Ensure we have enough to start with
if (string.IsNullOrWhiteSpace(connectionString) || string.IsNullOrWhiteSpace(sql))
{
throw new ArgumentException(Properties.Resources.InvalidConfiguration);
}
// Query the specified database
switch (database)
{
case Database.Firebird:
{
// Open the connection and run the query
await using FbConnection connection = new FbConnection(connectionString);
await connection.OpenAsync();
await using FbCommand command = new FbCommand(sql, connection);
await using DbDataReader reader = await command.ExecuteReaderAsync();
// Execute the reader
await foreach (object[] values in ExecuteReaderAsync(reader))
{
yield return values;
}
// Close the query and connection
await reader.CloseAsync();
await connection.CloseAsync();
break;
}
case Database.MSSQL:
{
// Open the connection and run the query
await using SqlConnection connection = new SqlConnection(connectionString);
await connection.OpenAsync();
await using SqlCommand command = new SqlCommand(sql, connection);
await using SqlDataReader reader = await command.ExecuteReaderAsync();
// Execute the reader
await foreach (object[] values in ExecuteReaderAsync(reader))
{
yield return values;
}
// Close the query and connection
await reader.CloseAsync();
await connection.CloseAsync();
break;
}
case Database.MySQL:
{
// Open the connection and run the query
await using MySqlConnection connection = new MySqlConnection(connectionString);
await connection.OpenAsync();
await using MySqlCommand command = new MySqlCommand(sql, connection);
await using DbDataReader reader = await command.ExecuteReaderAsync();
// Execute the reader
await foreach (object[] values in ExecuteReaderAsync(reader))
{
yield return values;
}
// Close the query and connection
await reader.CloseAsync();
await connection.CloseAsync();
break;
}
}
}
/// <summary>
/// Executes the reader asynchronously.
/// </summary>
/// <param name="reader">The reader.</param>
/// <returns>
/// Each row from the reader, including the field names as the first row.
/// </returns>
private static async IAsyncEnumerable<object[]> ExecuteReaderAsync(DbDataReader reader)
{
// Get the field names
string[] fieldNames = new string[reader.FieldCount];
for (int i = 0; i < reader.FieldCount; i++)
{
fieldNames[i] = reader.GetName(i);
}
// Return the field names
yield return fieldNames.ToArray<object>();
// Return the values
while (await reader.ReadAsync())
{
object[] values = new object[reader.FieldCount];
reader.GetValues(values);
yield return values;
}
}
/// <summary>
/// Writes the spreadsheet.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="workbook">The workbook.</param>
/// <returns>The asynchronous task.</returns>
private static async Task WriteSpreadsheet(PumpConfiguration configuration, IWorkbook workbook)
{
// Set up the workbook
await using FileStream fs = new FileStream(
configuration.OutputFile,
FileMode.Create,
FileAccess.Write);
ISheet sheet = workbook.CreateSheet("Output");
// Set up the date format
IDataFormat dataFormatCustom = workbook.CreateDataFormat();
ICellStyle style = workbook.CreateCellStyle();
style.DataFormat = dataFormatCustom.GetFormat("d/MM/yyyy");
int i = 0;
await foreach (
object[] values in ExecuteQueryAsync(
configuration.Database,
configuration.ConnectionString,
await File.ReadAllTextAsync(configuration.SqlFile)))
{
IRow row = sheet.CreateRow(i++);
for (int j = 0; j < values.Length; j++)
{
if (values[j] is DateTime date)
{
ICell cell = row.CreateCell(j);
cell.SetCellValue(date);
cell.CellStyle = style;
}
else if (values[j] is string general)
{
row.CreateCell(j).SetCellValue(general);
}
else
{
row.CreateCell(j).SetCellValue(values[j].ToString());
}
}
}
workbook.Write(fs);
}
}