EchoTex_Payroll/Ease.Core/DataAccess/DbConfig.cs

266 lines
8.9 KiB
C#
Raw Permalink Normal View History

2024-10-14 10:01:49 +06:00
using System;
using System.Xml;
using Ease.Core.Utility;
using System.Configuration;
using Ease.Core.DataAccess;
using System.Collections.Generic;
using Ease.Core.DataAccess.SQL;
//using Ease.Core.DataAccess.Odbc;
//using Ease.Core.DataAccess.OleDb;
using Ease.Core.DataAccess.Oracle;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.IO;
namespace Ease.Core.DataAccess
{
public class DbConfig
{
private static IConfigurationRoot _config;
private string[] _names;
private Dictionary<string, ConnectionNode> _connectionHash = new Dictionary<string, ConnectionNode>();
public string[] Names
{
get { return _names; }
}
public ConnectionNode this[string name]
{
get
{
if (name == null)
throw new Exception("Connection name cannot be null");
ConnectionNode node;
if (!_connectionHash.TryGetValue(name, out node))
throw new Exception("Inavalid connection name: " + name);
return node;
}
}
public static DbConfig GetConfig()
{
DbConfig config = new DbConfig();
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
_config = builder.Build();
Dictionary<string, string> valuesBD = _config.GetSection("dbSettings").Get<Dictionary<string, string>>();
config.ConnectionParse(valuesBD);
return config;
}
//protected override void Parse(XmlElement node)
//{
// //List<ConnectionNode> connections = ReadChildren<ConnectionNode>(node, "connection");
// //_names = new string[connections.Count];
// //for (int i = 0; i < connections.Count; i++)
// //{
// // _names[i] = connections[i].Name;
// // _connectionHash[connections[i].Name] = connections[i];
// //}
//}
public void ConnectionParse(Dictionary<string, string> nodes)
{
List<ConnectionNode> connections = new List<ConnectionNode>();
_names = new string[nodes.Count];
int i = 0;
foreach (KeyValuePair<string, string> entry in nodes)
{
_names[i] = entry.Key;
ConnectionNode item = new ConnectionNode();
item.Parse(entry.Value);
_connectionHash.Add(entry.Key, item);
i = i + 1;
}
}
public class ConnectionNode
{
internal void CreateNode(Provider provider, SQLSyntax sqlSyntex, string connectionString)
{
_name = "Dynamic";
_syntax = sqlSyntex;
_provider = provider;
_connectionString = connectionString;
// createNode();
//####
if (_factory == null)
throw new Exception(string.Format("Unsupported Provider: {0}", _provider.ToString()));
}
public ConnectionNode()
{
_connectionString = "";
}
private string _name;
public string Name
{
get { return _name; }
}
private Provider _provider;
internal Provider Provider
{
get { return _provider; }
}
private ConnectionFactory _factory;
public ConnectionFactory Factory
{
get { return _factory; }
}
private string _connectionString;
internal string ConnectionString
{
get { return _connectionString; }
}
private SQLSyntax _syntax;
internal SQLSyntax Syntax
{
get { return _syntax; }
}
public void Parse(string Connvalue)
{
string[] svalue = Connvalue.Split(";");
//CheckRequiredAttributes(node, "name");
//CheckRequiredAttributes(node, "provider");
//CheckRequiredAttributes(node, "connectionString");
//CheckRequiredAttributes(node, "sqlSyntax");
const string provider = "provider";
const string sqlSyntax = "sqlSyntax";
const string passwordKey = "password";
const string EncryptKey = "Cel.Admin";
string[] tokens = Connvalue.Split(';');
Connvalue = string.Empty;
foreach (string token in tokens)
{
string[] tokenValues = token.Split('=');
if (tokenValues.Length > 1)
{
if (tokenValues[0] == provider)
{
#region Provider
switch (tokenValues[1])
{
case "sql":
_provider = Provider.Sql;
break;
case "oraclenative":
_provider = Provider.OracleNative;
break;
case "oracle":
_provider = Provider.Oracle;
break;
case "oledb":
_provider = Provider.OleDb;
break;
case "odbc":
_provider = Provider.Odbc;
break;
default:
throw new Exception("Unsupported Provider: " + tokenValues[1]);
}
#endregion Provider
}
else if (tokenValues[0] == sqlSyntax)
{
switch (tokenValues[1])
{
case "SQL":
_factory = new SqlFactory();
break;
case "Oracle":
_factory = new OracleFactory();
break;
//case Provider.OleDb:
// _factory = new OleDbFactory();
// break;
//case Provider.Odbc:
// _factory = new OdbcFactory();
// break;
default:
break;
}
}
else if (tokenValues[0] == passwordKey)
{
string password = Global.CipherFunctions.Decrypt(EncryptKey, tokenValues[1]);
_connectionString += string.Format(";{0}={1}", passwordKey, password);
}
else
{
_connectionString += _connectionString.Length > 0 ? ";" : "";
_connectionString += token;
}
}
}
}
void createNode()
{
#region Parse Password
const string passwordKey = "password";
const string EncryptKey = "Cel.Admin";
if (_connectionString.IndexOf(passwordKey) != -1)
{
string[] tokens = _connectionString.Split(';');
_connectionString = string.Empty;
foreach (string token in tokens)
{
string[] tokenValues = token.Split('=');
if (tokenValues.Length > 1)
{
if (tokenValues[0] == passwordKey)
{
string password = Global.CipherFunctions.Decrypt(EncryptKey, tokenValues[1]);
_connectionString += string.Format(";{0}={1}", passwordKey, password);
}
else
{
_connectionString += _connectionString.Length > 0 ? ";" : "";
_connectionString += token;
}
}
}
}
#endregion
}
}
}
}