MES-ETL/ConsoleApp2/DataRecord.cs
CZY 8e5efa83f1 修复Linux环境下appsettings.json配置文件可能因控制台工作目录不同而无法读取的问题;
修复数据替换时order_module表的ShardKey无法正确赋值的问题;
旧数据库order_data_block.CompanyID数据有误,在数据替换时重新计算;
2024-01-22 15:49:32 +08:00

94 lines
3.0 KiB
C#

using System.ComponentModel.Design;
namespace ConsoleApp2;
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.");
var idx = Array.IndexOf(record.Headers, columnName);
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 {columnName} not found in this record, table name {record.TableName}.");
return record.Fields[idx];
}
public string[] Fields { get; }
public string[] Headers { get; }
public string TableName { get; }
public string Database { get; set; }
public int CompanyID { get; set; }
public DataRecord(string[] fields, string tableName, string[] headers, int companyID=0)
{
if (fields.Length != headers.Length)
throw new ArgumentException(
$"The number of fields does not match the number of headers. Expected: {headers.Length} Got: {fields.Length} Fields: {string.Join(',', fields)}",
nameof(fields));
Fields = fields;
TableName = tableName;
Headers = headers;
CompanyID = companyID;
}
public string this[int index]
{
get => Fields[index];
set => Fields[index] = value;
}
public string this[string columnName]
{
get => GetField(this, columnName);
set => SetField(columnName, value);
}
public int Count => Fields.Length;
public bool TryGetField(string columnName, out string value) => TryGetField(this, columnName, out value);
public bool SetField(string columnName, string value) => SetField(this, columnName,value);
public string GetCacheKey(string columnName) => GetCacheKey(this, columnName);
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;
}
public string GetCacheKey(DataRecord record, string columnName)
{
if (TryGetField(record, columnName, out var value))
{
return $"{TableName}_{value}";
}else
throw new IndexOutOfRangeException($"Column name:{columnName} not found in this record.");
}
}