You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.6 KiB
87 lines
2.6 KiB
|
2 months ago
|
using System.IO;
|
||
|
|
using System.Text.RegularExpressions;
|
||
|
|
|
||
|
|
namespace Housing.Services;
|
||
|
|
|
||
|
|
public sealed class LoginSettings
|
||
|
|
{
|
||
|
|
public string Table { get; set; } = "dbo.Housing_Assembly";
|
||
|
|
public string IdColumn { get; set; } = "Operator";
|
||
|
|
public string PasswordColumn { get; set; } = "Password";
|
||
|
|
public bool OfflinePreview { get; set; }
|
||
|
|
|
||
|
|
public static LoginSettings Load(string filePath)
|
||
|
|
{
|
||
|
|
if (!File.Exists(filePath))
|
||
|
|
{
|
||
|
|
throw new FileNotFoundException("Database.ini 파일을 찾을 수 없습니다.", filePath);
|
||
|
|
}
|
||
|
|
|
||
|
|
var values = IniFile.LoadSection(filePath, "Login");
|
||
|
|
return new LoginSettings
|
||
|
|
{
|
||
|
|
Table = GetString(values, "Table", "dbo.Housing_Assembly"),
|
||
|
|
IdColumn = GetString(values, "IdColumn", "Operator"),
|
||
|
|
PasswordColumn = GetString(values, "PasswordColumn", "Password"),
|
||
|
|
OfflinePreview = GetBool(values, "OfflinePreview", false)
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
public string GetQuotedTableName()
|
||
|
|
{
|
||
|
|
var parts = Table
|
||
|
|
.Split('.', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||
|
|
.Select(QuoteIdentifier)
|
||
|
|
.ToArray();
|
||
|
|
|
||
|
|
if (parts.Length is 0 or > 3)
|
||
|
|
{
|
||
|
|
throw new InvalidOperationException("Database.ini [Login] Table 값을 확인하세요.");
|
||
|
|
}
|
||
|
|
|
||
|
|
return string.Join(".", parts);
|
||
|
|
}
|
||
|
|
|
||
|
|
public string GetQuotedIdColumn()
|
||
|
|
{
|
||
|
|
return QuoteIdentifier(IdColumn);
|
||
|
|
}
|
||
|
|
|
||
|
|
public string GetQuotedPasswordColumn()
|
||
|
|
{
|
||
|
|
return QuoteIdentifier(PasswordColumn);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static string GetString(Dictionary<string, string> values, string key, string defaultValue)
|
||
|
|
{
|
||
|
|
return values.TryGetValue(key, out var value) && !string.IsNullOrWhiteSpace(value)
|
||
|
|
? value.Trim()
|
||
|
|
: defaultValue;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static bool GetBool(Dictionary<string, string> values, string key, bool defaultValue)
|
||
|
|
{
|
||
|
|
if (!values.TryGetValue(key, out var value) || string.IsNullOrWhiteSpace(value))
|
||
|
|
{
|
||
|
|
return defaultValue;
|
||
|
|
}
|
||
|
|
|
||
|
|
return value.Trim().ToUpperInvariant() switch
|
||
|
|
{
|
||
|
|
"1" or "TRUE" or "YES" or "ON" => true,
|
||
|
|
"0" or "FALSE" or "NO" or "OFF" => false,
|
||
|
|
_ => defaultValue
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
private static string QuoteIdentifier(string identifier)
|
||
|
|
{
|
||
|
|
if (Regex.IsMatch(identifier, @"^[A-Za-z_][A-Za-z0-9_]*$"))
|
||
|
|
{
|
||
|
|
return $"[{identifier}]";
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new InvalidOperationException($"DB 식별자 값이 올바르지 않습니다: {identifier}");
|
||
|
|
}
|
||
|
|
}
|