119 lines
3.9 KiB
C#
119 lines
3.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using DataCompare.Core;
|
|
|
|
namespace DataCompare.App
|
|
{
|
|
sealed class RuleFile
|
|
{
|
|
public int Version { get; set; }
|
|
public List<RuleDto> Rules { get; set; }
|
|
}
|
|
|
|
sealed class RuleDto
|
|
{
|
|
public string Id { get; set; }
|
|
public string Name { get; set; }
|
|
public bool IgnoreCase { get; set; }
|
|
public bool StripSchemaPrefix { get; set; }
|
|
public string Separator { get; set; }
|
|
public bool NameOnly { get; set; }
|
|
}
|
|
|
|
static class MatchRuleStore
|
|
{
|
|
static readonly JsonSerializerOptions StoreJson = new JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
WriteIndented = true
|
|
};
|
|
|
|
public static string FilePath
|
|
{
|
|
get
|
|
{
|
|
return Path.Combine(
|
|
Path.GetDirectoryName(ConnectionStore.FilePath),
|
|
"table-rules.json");
|
|
}
|
|
}
|
|
|
|
public static List<TableMatchRule> Load()
|
|
{
|
|
var list = new List<TableMatchRule>();
|
|
if (!File.Exists(FilePath))
|
|
return list;
|
|
RuleFile file;
|
|
try
|
|
{
|
|
var json = File.ReadAllText(FilePath, Encoding.UTF8);
|
|
file = JsonSerializer.Deserialize<RuleFile>(json, StoreJson);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return list;
|
|
}
|
|
if (file == null || file.Rules == null)
|
|
return list;
|
|
for (int i = 0; i < file.Rules.Count; i++)
|
|
{
|
|
var dto = file.Rules[i];
|
|
if (dto == null || string.IsNullOrWhiteSpace(dto.Name))
|
|
continue;
|
|
var rule = new TableMatchRule
|
|
{
|
|
Id = string.IsNullOrWhiteSpace(dto.Id) ? Guid.NewGuid().ToString("D") : dto.Id.Trim(),
|
|
Name = dto.Name.Trim(),
|
|
IgnoreCase = dto.IgnoreCase,
|
|
StripSchemaPrefix = dto.StripSchemaPrefix,
|
|
Separator = string.IsNullOrEmpty(dto.Separator) ? "_" : dto.Separator,
|
|
NameOnly = dto.NameOnly
|
|
};
|
|
if (rule.IsBuiltIn)
|
|
continue;
|
|
list.Add(rule);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public static void Save(IList<TableMatchRule> rules)
|
|
{
|
|
var dtos = new List<RuleDto>();
|
|
if (rules != null)
|
|
{
|
|
for (int i = 0; i < rules.Count; i++)
|
|
{
|
|
var rule = rules[i];
|
|
if (rule == null || rule.IsBuiltIn || string.IsNullOrWhiteSpace(rule.Name))
|
|
continue;
|
|
dtos.Add(new RuleDto
|
|
{
|
|
Id = string.IsNullOrWhiteSpace(rule.Id) ? Guid.NewGuid().ToString("D") : rule.Id,
|
|
Name = rule.Name.Trim(),
|
|
IgnoreCase = rule.IgnoreCase,
|
|
StripSchemaPrefix = rule.StripSchemaPrefix,
|
|
Separator = string.IsNullOrEmpty(rule.Separator) ? "_" : rule.Separator,
|
|
NameOnly = rule.NameOnly
|
|
});
|
|
}
|
|
}
|
|
var json = JsonSerializer.Serialize(new RuleFile
|
|
{
|
|
Version = 1,
|
|
Rules = dtos
|
|
}, StoreJson);
|
|
var directory = Path.GetDirectoryName(FilePath);
|
|
Directory.CreateDirectory(directory);
|
|
var temp = FilePath + ".tmp";
|
|
File.WriteAllText(temp, json, new UTF8Encoding(false));
|
|
if (File.Exists(FilePath))
|
|
File.Replace(temp, FilePath, null);
|
|
else
|
|
File.Move(temp, FilePath);
|
|
}
|
|
}
|
|
}
|