MES-ETL/ConsoleApp2/DataRecord.cs

90 lines
2.9 KiB
C#
Raw Normal View History

2024-01-12 16:50:37 +08:00
using System.ComponentModel.Design;
namespace ConsoleApp2;
2023-12-29 16:16:05 +08:00
2023-12-28 15:18:03 +08:00
public class DataRecord
{
public static bool TryGetField(DataRecord record, string columnName, out string value)
{
value = string.Empty;
if (record.Headers is null)
throw new InvalidOperationException("Cannot get field when headers of a record have not been set.");
2024-01-04 09:00:44 +08:00
var idx = Array.IndexOf(record.Headers, columnName); //可能可以优化
2023-12-28 15:18:03 +08:00
if (idx == -1)
return false;
value = record.Fields[idx];
return true;
}
public static string GetField(DataRecord record, string columnName)
{
if (record.Headers is null)
throw new InvalidOperationException("Headers have not been set.");
var idx = Array.IndexOf(record.Headers, columnName);
if (idx is -1)
throw new IndexOutOfRangeException("Column name not found in this record.");
return record.Fields[idx];
}
public string[] Fields { get; }
2023-12-29 16:16:05 +08:00
public string[] Headers { get; }
2023-12-28 15:18:03 +08:00
public string TableName { get; }
2023-12-29 16:16:05 +08:00
public string? Database { get; set; }
2024-01-12 16:50:37 +08:00
public int CompanyID { get; set; }
2023-12-29 16:16:05 +08:00
2023-12-28 15:18:03 +08:00
2024-01-12 16:50:37 +08:00
public DataRecord(string[] fields, string tableName, string[] headers, int companyID=0)
2023-12-28 15:18:03 +08:00
{
2023-12-29 16:16:05 +08:00
if (fields.Length != headers.Length)
2023-12-28 15:18:03 +08:00
throw new ArgumentException(
2023-12-29 16:16:05 +08:00
$"The number of fields does not match the number of headers. Expected: {headers.Length} Got: {fields.Length} Fields: {string.Join(',', fields)}",
2023-12-28 15:18:03 +08:00
nameof(fields));
Fields = fields;
TableName = tableName;
Headers = headers;
2024-01-12 16:50:37 +08:00
CompanyID = companyID;
2023-12-28 15:18:03 +08:00
}
2023-12-29 16:16:05 +08:00
public string this[int index]
{
get => Fields[index];
set => Fields[index] = value;
}
2023-12-28 15:18:03 +08:00
public string this[string columnName] => GetField(this, columnName);
public int Count => Fields.Length;
public bool TryGetField(string columnName, out string value) => TryGetField(this, columnName, out value);
2024-01-16 18:00:23 +08:00
public bool SetField(string columnName, string value) => SetField(this, columnName,value);
2024-01-18 14:36:36 +08:00
public string GetCacheKey(string columnName) => GetCacheKey(this, columnName);
2024-01-16 18:00:23 +08:00
public bool SetField( DataRecord record,string columnName,string value)
{
if (record.Headers is null)
throw new InvalidOperationException("Headers have not been set.");
var idx = Array.IndexOf(record.Headers, columnName);
if (idx is -1)
throw new IndexOutOfRangeException("Column name not found in this record.");
record.Fields[idx] = value;
return true;
}
2024-01-18 14:36:36 +08:00
public string GetCacheKey(DataRecord record, string columnName)
{
if (TryGetField(record, columnName, out var value))
{
2024-01-18 15:06:52 +08:00
return $"{TableName}_{value}";
2024-01-18 14:36:36 +08:00
}else
throw new IndexOutOfRangeException($"Column name:{columnName} not found in this record.");
}
2023-12-28 15:18:03 +08:00
}