-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Importer.cs
147 lines (123 loc) · 4.87 KB
/
Importer.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
namespace Orc.DbToCsv
{
using System;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Catel.Logging;
using CsvHelper;
using DataAccess.Database;
public static class Importer
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();
public static async Task ProcessProjectAsync(string projectFilePath, string outputFolderPath)
{
var project = await Project.LoadAsync(projectFilePath);
if (project is not null)
{
await ProcessProjectAsync(project);
}
}
public static async Task ProcessProjectAsync(Project project)
{
Log.Info("Project processing started ...");
try
{
var exportDescriptions = project.GetDbToCsvExportDescriptions();
Log.Info("{0} tables to process", exportDescriptions.Count.ToString());
foreach (var exportDescription in exportDescriptions)
{
await ProcessTableAsync(exportDescription, project);
}
}
catch (SqlException ex)
{
Log.Error(ex.Message);
}
catch (Exception ex)
{
Log.Error(ex);
}
}
private static async Task ProcessTableAsync(DbToCsvExportDescription exportDescription, Project project)
{
var fullFileName = exportDescription.CsvFilePath;
if (string.IsNullOrWhiteSpace(fullFileName))
{
throw Log.ErrorAndCreateException<InvalidOperationException>("Cannot process empty csv file path");
}
var outputFolderPath = Path.GetDirectoryName(fullFileName);
if (string.IsNullOrWhiteSpace(outputFolderPath))
{
throw Log.ErrorAndCreateException<InvalidOperationException>("Cannot process empty output folder path");
}
if (!Directory.Exists(outputFolderPath))
{
Directory.CreateDirectory(outputFolderPath);
}
var records = 0;
var source = exportDescription.Source;
if (source is null)
{
throw Log.ErrorAndCreateException<InvalidOperationException>("Cannot process null source");
}
try
{
if (File.Exists(fullFileName))
{
File.Delete(fullFileName);
}
await using var streamWriter = new StreamWriter(new FileStream(fullFileName, FileMode.OpenOrCreate));
await using var csvWriter = new CsvWriter(streamWriter, CultureInfo.CurrentCulture);
using var dataReader = new SqlTableReader(source.ToString(), 0, project.MaximumRowsInTable.Value, exportDescription.Parameters);
while (true)
{
var headers = dataReader.FieldHeaders;
if (dataReader.ValidationContext.HasErrors)
{
Log.Error(dataReader.ValidationContext.ToString());
return;
}
if (headers.Any())
{
foreach (var field in headers)
{
csvWriter.WriteField(field);
}
await csvWriter.NextRecordAsync();
}
while (await dataReader.ReadAsync())
{
for (var i = 0; i < headers.Length; i++)
{
var value = dataReader.GetValue(i);
if (value is string strValue)
{
value = strValue.Trim(); // Remove all white spaces
}
csvWriter.WriteField(value);
}
records++;
await csvWriter.NextRecordAsync();
}
if (dataReader.ValidationContext.HasErrors)
{
Log.Error(dataReader.ValidationContext.ToString());
return;
}
if (!await dataReader.NextResultAsync())
{
break;
}
}
Log.Info($"{records} records of '{source.Schema}' '{source.Table}' table successfully exported to {fullFileName}.");
}
catch (Exception ex)
{
Log.Error($"{source.Table} export failed because of exception: {ex.Message}");
}
}
}
}