2024-01-29 09:29:16 +08:00
|
|
|
|
namespace ConsoleApp2;
|
2024-01-12 16:50:37 +08:00
|
|
|
|
|
2024-01-29 09:29:16 +08:00
|
|
|
|
public class DataRecord : ICloneable
|
2023-12-28 15:18:03 +08:00
|
|
|
|
{
|
|
|
|
|
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-29 09:29:16 +08:00
|
|
|
|
var idx = IndexOfIgnoreCase(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.");
|
2024-01-29 09:29:16 +08:00
|
|
|
|
var idx = IndexOfIgnoreCase(record.Headers, columnName);
|
2023-12-28 15:18:03 +08:00
|
|
|
|
if (idx is -1)
|
2024-01-29 09:29:16 +08:00
|
|
|
|
throw new IndexOutOfRangeException(
|
|
|
|
|
$"Column name '{columnName}' not found in this record, table name '{record.TableName}'.");
|
2023-12-28 15:18:03 +08:00
|
|
|
|
return record.Fields[idx];
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 09:29:16 +08:00
|
|
|
|
private static int IndexOfIgnoreCase(IList<string> list, string value)
|
|
|
|
|
{
|
|
|
|
|
var idx = -1;
|
|
|
|
|
for (var i = 0; i < list.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (list[i].Equals(value, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
idx = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
|
}
|
2023-12-28 15:18:03 +08:00
|
|
|
|
|
2024-01-29 09:29:16 +08:00
|
|
|
|
public string? RawField { get; set; }
|
|
|
|
|
public IList<string> Fields { get; }
|
|
|
|
|
public IList<string> Headers { get; }
|
2023-12-28 15:18:03 +08:00
|
|
|
|
public string TableName { get; }
|
2024-01-29 09:29:16 +08:00
|
|
|
|
public string? Database { get; set; }
|
2023-12-29 16:16:05 +08:00
|
|
|
|
|
2023-12-28 15:18:03 +08:00
|
|
|
|
|
2024-01-29 09:29:16 +08:00
|
|
|
|
public DataRecord(IEnumerable<string> fields, string tableName, IEnumerable<string> headers, string? database = null)
|
2023-12-28 15:18:03 +08:00
|
|
|
|
{
|
2024-01-29 09:29:16 +08:00
|
|
|
|
Fields = fields.ToList();
|
|
|
|
|
TableName = tableName;
|
|
|
|
|
Headers = headers.ToList();
|
|
|
|
|
Database = database;
|
|
|
|
|
|
|
|
|
|
if (Fields.Count != Headers.Count)
|
2023-12-28 15:18:03 +08:00
|
|
|
|
throw new ArgumentException(
|
2024-01-29 09:29:16 +08:00
|
|
|
|
$"The number of fields does not match the number of headers. Expected: {Headers.Count} Got: {Fields.Count} Fields: {string.Join(',', Fields)}",
|
2023-12-28 15:18:03 +08:00
|
|
|
|
nameof(fields));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2024-01-22 15:49:32 +08:00
|
|
|
|
public string this[string columnName]
|
|
|
|
|
{
|
|
|
|
|
get => GetField(this, columnName);
|
|
|
|
|
set => SetField(columnName, value);
|
|
|
|
|
}
|
2023-12-28 15:18:03 +08:00
|
|
|
|
|
2024-01-29 09:29:16 +08:00
|
|
|
|
public int FieldCount => Fields.Count;
|
2023-12-28 15:18:03 +08:00
|
|
|
|
|
|
|
|
|
public bool TryGetField(string columnName, out string value) => TryGetField(this, columnName, out value);
|
2024-01-16 18:00:23 +08:00
|
|
|
|
|
2024-01-29 09:29:16 +08:00
|
|
|
|
public bool SetField(string columnName, string value) => SetField(this, columnName, value);
|
2024-01-18 14:36:36 +08:00
|
|
|
|
|
2024-01-29 09:29:16 +08:00
|
|
|
|
public bool SetField(DataRecord record, string columnName, string value)
|
2024-01-16 18:00:23 +08:00
|
|
|
|
{
|
|
|
|
|
if (record.Headers is null)
|
|
|
|
|
throw new InvalidOperationException("Headers have not been set.");
|
2024-01-29 09:29:16 +08:00
|
|
|
|
var idx = IndexOfIgnoreCase(record.Headers, columnName);
|
2024-01-16 18:00:23 +08:00
|
|
|
|
if (idx is -1)
|
2024-01-29 09:29:16 +08:00
|
|
|
|
throw new IndexOutOfRangeException(
|
|
|
|
|
$"Column name '{columnName}' not found in this record, table name '{record.TableName}");
|
2024-01-16 18:00:23 +08:00
|
|
|
|
record.Fields[idx] = value;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-01-29 09:29:16 +08:00
|
|
|
|
|
|
|
|
|
public void AddField(string columnName, string value)
|
2024-01-18 14:36:36 +08:00
|
|
|
|
{
|
2024-01-29 09:29:16 +08:00
|
|
|
|
if (IndexOfIgnoreCase(Headers, columnName) != -1)
|
|
|
|
|
throw new InvalidOperationException($"{TableName}: 列名 '{columnName}' 已存在");
|
|
|
|
|
|
|
|
|
|
Fields.Add(value);
|
|
|
|
|
Headers.Add(columnName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveField(string columnName)
|
|
|
|
|
{
|
|
|
|
|
var idx = IndexOfIgnoreCase(Headers, columnName);
|
|
|
|
|
if (idx == -1)
|
|
|
|
|
throw new InvalidOperationException($"{TableName}: 列名 '{columnName}' 不存在");
|
|
|
|
|
|
|
|
|
|
Fields.RemoveAt(idx);
|
|
|
|
|
Headers.Remove(columnName);
|
|
|
|
|
}
|
2024-01-18 14:36:36 +08:00
|
|
|
|
|
2024-01-29 09:29:16 +08:00
|
|
|
|
public bool HeaderExists(string columnName) => IndexOfIgnoreCase(Headers, columnName) != -1;
|
|
|
|
|
public object Clone()
|
|
|
|
|
{
|
|
|
|
|
return new DataRecord(new List<string>(Fields), TableName, new List<string>(Headers), Database);
|
2024-01-18 14:36:36 +08:00
|
|
|
|
}
|
2023-12-28 15:18:03 +08:00
|
|
|
|
}
|