122 lines
4.3 KiB
C#
122 lines
4.3 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Collections.ObjectModel;
|
|
using System.Text.Json;
|
|
|
|
namespace File_Folder_Helper.ADO2025.PI5;
|
|
|
|
internal static partial class Helper20250228
|
|
{
|
|
|
|
private record Record(string TableName, ReadOnlyCollection<string> Columns, ReadOnlyCollection<string[]> Rows);
|
|
|
|
private static ReadOnlyCollection<Record> GetRecords(string headerA, string headerB, string file)
|
|
{
|
|
List<Record> results = [];
|
|
string line;
|
|
string[] segmentsA;
|
|
string[] segmentsB;
|
|
string[] segmentsC;
|
|
string[] segmentsD;
|
|
string[] segmentsE;
|
|
string[] segmentsF;
|
|
List<string[]> rows;
|
|
string? tableName = null;
|
|
string[] lines = File.ReadAllLines(file);
|
|
ReadOnlyCollection<string>? columns = null;
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
line = lines[i];
|
|
if (tableName is null)
|
|
{
|
|
segmentsA = line.Split(headerA);
|
|
if (segmentsA.Length != 2)
|
|
continue;
|
|
segmentsB = segmentsA[1].Split(headerB);
|
|
if (segmentsB.Length != 2)
|
|
continue;
|
|
segmentsC = segmentsB[0].Split('(');
|
|
if (segmentsC.Length != 2)
|
|
continue;
|
|
segmentsD = segmentsC[1].Split(')');
|
|
if (segmentsD.Length != 2)
|
|
continue;
|
|
columns = segmentsD[0].Split(',').Select(l => l.Trim(' ').Trim('"')).ToArray().AsReadOnly();
|
|
if (columns.Count == 0)
|
|
continue;
|
|
segmentsE = segmentsB[0].Split(' ');
|
|
tableName = segmentsE[0];
|
|
}
|
|
else if (columns is null)
|
|
break;
|
|
else
|
|
{
|
|
rows = [];
|
|
for (int j = i + 1; j < lines.Length; j++)
|
|
{
|
|
i = j;
|
|
segmentsF = lines[j].Split('\t');
|
|
if (segmentsF.Length != columns.Count)
|
|
{
|
|
if (rows.Count > 0)
|
|
results.Add(new(TableName: tableName, Columns: columns, Rows: rows.AsReadOnly()));
|
|
break;
|
|
}
|
|
rows.Add(segmentsF);
|
|
}
|
|
columns = null;
|
|
tableName = null;
|
|
}
|
|
}
|
|
return results.AsReadOnly();
|
|
}
|
|
|
|
private static void WriteFile(string file, ReadOnlyCollection<Record> records)
|
|
{
|
|
List<string> results = [];
|
|
string json;
|
|
string text;
|
|
Dictionary<string, string?> keyValuePairs = [];
|
|
foreach (Record record in records)
|
|
{
|
|
results.Clear();
|
|
foreach (string[] row in record.Rows)
|
|
{
|
|
keyValuePairs.Clear();
|
|
for (int i = 0; i < row.Length; i++)
|
|
{
|
|
if (row[i] == "\\N")
|
|
keyValuePairs.Add(record.Columns[i], null);
|
|
else
|
|
keyValuePairs.Add(record.Columns[i], row[i]);
|
|
}
|
|
json = JsonSerializer.Serialize(keyValuePairs);
|
|
results.Add(json);
|
|
}
|
|
text = string.Join($",{Environment.NewLine}", results);
|
|
File.WriteAllText($"{file[..^4]}-{record.TableName}.json", $"[{Environment.NewLine}{text}{Environment.NewLine}]");
|
|
}
|
|
}
|
|
|
|
private static void PostgresDumpToJson(ILogger<Worker> logger, string headerA, string headerB, string file)
|
|
{
|
|
ReadOnlyCollection<Record> records = GetRecords(headerA, headerB, file);
|
|
if (records.Count > 0)
|
|
WriteFile(file, records);
|
|
else
|
|
logger.LogWarning("<{records}>(s)", records.Count);
|
|
}
|
|
|
|
internal static void PostgresDumpToJson(ILogger<Worker> logger, List<string> args)
|
|
{
|
|
string searchPattern = args[2];
|
|
string headerA = args[3].Replace('_', ' ');
|
|
string headerB = args[4].Replace('_', ' ');
|
|
string sourceDirectory = Path.GetFullPath(args[0]);
|
|
string[] files = Directory.GetFiles(sourceDirectory, searchPattern, SearchOption.AllDirectories);
|
|
if (files.Length != 1)
|
|
logger.LogWarning("<{files}>(s)", files.Length);
|
|
else
|
|
PostgresDumpToJson(logger, headerA, headerB, files[0]);
|
|
}
|
|
|
|
} |