This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFakeDbExporterCSVConfiguration.cs
60 lines (52 loc) · 1.89 KB
/
FakeDbExporterCSVConfiguration.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
using System.Collections.Generic;
using System.IO;
namespace KD.FakeDb.Export.Files.CSV
{
/// <summary>
/// Configuration for <see cref="FakeDbExporter{TStream}"/> to export to CSV <see cref="File"/>.
/// </summary>
public class FakeDbExporterCSVConfiguration : FakeDbExporterAbstractFileConfiguration
{
/// <summary>
/// It will export given <see cref="IFakeDatabase"/> using given <see cref="FileStream"/>.
/// It will flush and close the given <see cref="FileStream"/>.
/// </summary>
protected override void ExportToFile(FileStream stream, IFakeDatabase database)
{
List<string> lines = new List<string>();
// Add Database Name
lines.Add(database.Name);
// Add Each Table to String Builder
foreach (var table in database.Tables)
{
lines.Add("");
// Add Table Name
lines.Add(table.Name);
// Add Column Names from Table
string columnNames = "";
foreach (var column in table.Columns)
{
columnNames += column.Name + ",";
}
lines.Add(columnNames);
// Add each Row from current Table
foreach (var row in table.Rows)
{
string rowValues = "";
foreach (var record in row)
{
rowValues += record.Value + ",";
}
lines.Add(rowValues);
}
}
// Store path to File.
string path = stream.Name;
// Flush and Close stream
stream.Flush();
stream.Close();
// Add Everything from StringBuilder to File
File.AppendAllLines(path, lines);
}
}
}