From 42a5919ca7a4d1bda267794619eb7a9757e5d7d6 Mon Sep 17 00:00:00 2001 From: SHM Date: Wed, 26 Jun 2024 12:44:29 +0900 Subject: [PATCH] =?UTF-8?q?[=EC=84=B1=ED=98=84=EB=AA=A8]=20Middleware.Log?= =?UTF-8?q?=20=EB=88=84=EB=9D=BD=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- .../SystemX.Net.DB.Log/ConnInfo.cs | 308 +++++++++ .../SystemX.Net.DB.Log/ConnManager.cs | 644 ++++++++++++++++++ .../DBType/DBTMSSQL - Query.cs | 412 +++++++++++ .../SystemX.Net.DB.Log/DBType/DBTMSSQL.cs | 450 ++++++++++++ .../SystemX.Net.DB.Log/DBType/IDBControl.cs | 51 ++ .../SystemX.Net.DB.Log/DBType/MariaDB.cs | 112 +++ 7 files changed, 1979 insertions(+), 1 deletion(-) create mode 100644 SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/ConnInfo.cs create mode 100644 SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/ConnManager.cs create mode 100644 SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/DBTMSSQL - Query.cs create mode 100644 SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/DBTMSSQL.cs create mode 100644 SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/IDBControl.cs create mode 100644 SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/MariaDB.cs diff --git a/.gitignore b/.gitignore index a4c5101..34582cf 100644 --- a/.gitignore +++ b/.gitignore @@ -400,4 +400,5 @@ FodyWeavers.xsd #======================Custom .svn -!SystemX.Net.CP.Middleware.Log \ No newline at end of file +!SystemX.Net.CP.Middleware.Log +!SystemX.Net.DB.Log \ No newline at end of file diff --git a/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/ConnInfo.cs b/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/ConnInfo.cs new file mode 100644 index 0000000..158d9fd --- /dev/null +++ b/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/ConnInfo.cs @@ -0,0 +1,308 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +using SystemX.Common; +using SystemX.Common.Serialization; +using static SystemX.Net.DB.LogProcess.XLogDBConnManager; + +namespace SystemX.Net.DB.LogProcess +{ + public class CMainConnInfo + { + public string Type { get; set; } + public string IP { get; set; } + public string Port { get; set; } + public string ID { get; set; } + public string PW { get; set; } + public string SCHEMA { get; set; } + public string SUMMARY_TABLE { get; set; } + public string VERSION_TABLE { get; set; } + public string VRFY_TABLE { get; set; } + public string MAC_ADDR { get; set; } + public string SSPI { get; set; } + + public CMainConnInfo() + { + Type = string.Empty; + IP = string.Empty; + Port = string.Empty; + ID = string.Empty; + PW = string.Empty; + SCHEMA = string.Empty; + MAC_ADDR = string.Empty; + SSPI = string.Empty; + } + } + + public class CShortTermConnInfo + { + public string Type { get; set; } + public string IP { get; set; } + public string Port { get; set; } + public string ID { get; set; } + public string PW { get; set; } + public string SCHEMA { get; set; } + //public string KEY_TABLE { get; set; } + public string SUMMARY_TABLE { get; set; } + public string LOG_TABLE { get; set; } + public string MAC_ADDR { get; set; } + public string SSPI { get; set; } + + public CShortTermConnInfo() + { + Type = string.Empty; + IP = string.Empty; + Port = string.Empty; + ID = string.Empty; + PW = string.Empty; + SCHEMA = string.Empty; + MAC_ADDR = string.Empty; + SSPI = string.Empty; + } + } + + + public class CLongTermConnInfo + { + public string Type { get; set; } + public string IP { get; set; } + public string Port { get; set; } + public string ID { get; set; } + public string PW { get; set; } + public string SCHEMA { get; set; } + //public string KEY_TABLE { get; set; } + public string SUMMARY_TABLE { get; set; } + public string LOG_TABLE { get; set; } + public string MAC_ADDR { get; set; } + public string SSPI { get; set; } + + public CLongTermConnInfo() + { + Type = string.Empty; + IP = string.Empty; + Port = string.Empty; + ID = string.Empty; + PW = string.Empty; + SCHEMA = string.Empty; + MAC_ADDR = string.Empty; + SSPI = string.Empty; + } + } + + public class XDBConnInfo + { + public enum INFO_STRUCTURE + { + DBConnInfo, + IP, + Port, + ID, + PW, + DB_CONN, + MAC_ADDR, + SSPI + } + + public enum eVersion + { + CPX, + CPXV2 + } + + public eVersion ConnVersion; + + public CMainConnInfo ConnMain; + + public CShortTermConnInfo ConnShortTerm; + + public CLongTermConnInfo ConnLongTerm; + + + public XDBConnInfo() + { + ConnVersion = eVersion.CPX; + + ConnMain = new CMainConnInfo(); + ConnShortTerm = new CShortTermConnInfo(); + ConnLongTerm = new CLongTermConnInfo(); + } + + public bool ReadConfig(string strPath) + { + try + { + FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read); + + XmlDocument xmldoc = new XmlDocument(); + XmlNodeList nodeList; + + Type InfoType = typeof(XDBConnInfo); + + xmldoc.Load(fs); + + nodeList = xmldoc.GetElementsByTagName(INFO_STRUCTURE.DBConnInfo.ToString()); + + XmlNode topNode = nodeList.Item(0); + + foreach (XmlNode node in topNode.ChildNodes) + { + string strNodeName = node.Name; + + if (strNodeName.CompareTo("UseVersion") == 0) + { + if (Enum.TryParse(node.InnerText, out ConnVersion) == false) + ConnVersion = eVersion.CPX; + } + + if (strNodeName.CompareTo("MainConnInfo") == 0) + { + if (node.HasChildNodes == false) + throw new Exception("[MainConnInfo] Has no entries."); + if (node.ChildNodes.Count != 11) + throw new Exception("[MainConnInfo] Entries do not match."); + + ConnMain.Type = node.ChildNodes[0].InnerText; + ConnMain.IP = node.ChildNodes[1].InnerText; + ConnMain.Port = node.ChildNodes[2].InnerText; + ConnMain.ID = node.ChildNodes[3].InnerText; + ConnMain.PW = node.ChildNodes[4].InnerText; + ConnMain.SCHEMA = node.ChildNodes[5].InnerText; + ConnMain.SUMMARY_TABLE = node.ChildNodes[6].InnerText; + ConnMain.VERSION_TABLE = node.ChildNodes[7].InnerText; + ConnMain.VRFY_TABLE = node.ChildNodes[8].InnerText; + ConnMain.MAC_ADDR = node.ChildNodes[9].InnerText; + ConnMain.SSPI = node.ChildNodes[10].InnerText; + } + else if (strNodeName.CompareTo("ShortTermInfo") == 0) + { + if (node.HasChildNodes == false) + throw new Exception("[ShortTermInfo] Has no entries."); + if (node.ChildNodes.Count != 10) + throw new Exception("[ShortTermInfo] Entries do not match."); + + ConnShortTerm.Type = node.ChildNodes[0].InnerText; + ConnShortTerm.IP = node.ChildNodes[1].InnerText; + ConnShortTerm.Port = node.ChildNodes[2].InnerText; + ConnShortTerm.ID = node.ChildNodes[3].InnerText; + ConnShortTerm.PW = node.ChildNodes[4].InnerText; + ConnShortTerm.SCHEMA = node.ChildNodes[5].InnerText; + //ConnShortTerm.KEY_TABLE = node.ChildNodes[6].InnerText; + ConnShortTerm.SUMMARY_TABLE = node.ChildNodes[6].InnerText; + ConnShortTerm.LOG_TABLE = node.ChildNodes[7].InnerText; + ConnShortTerm.MAC_ADDR = node.ChildNodes[8].InnerText; + ConnShortTerm.SSPI = node.ChildNodes[9].InnerText; + } + else if(strNodeName.CompareTo("LongTermInfo") == 0) + { + if (node.HasChildNodes == false) + throw new Exception("[LongTermInfo] Has no entries."); + if (node.ChildNodes.Count != 10) + throw new Exception("[LongTermInfo] Entries do not match."); + + ConnLongTerm.Type = node.ChildNodes[0].InnerText; + ConnLongTerm.IP = node.ChildNodes[1].InnerText; + ConnLongTerm.Port = node.ChildNodes[2].InnerText; + ConnLongTerm.ID = node.ChildNodes[3].InnerText; + ConnLongTerm.PW = node.ChildNodes[4].InnerText; + ConnLongTerm.SCHEMA = node.ChildNodes[5].InnerText; + //ConnLongTerm.KEY_TABLE = node.ChildNodes[6].InnerText; + ConnLongTerm.SUMMARY_TABLE = node.ChildNodes[6].InnerText; + ConnLongTerm.LOG_TABLE = node.ChildNodes[7].InnerText; + ConnLongTerm.MAC_ADDR = node.ChildNodes[8].InnerText; + ConnLongTerm.SSPI = node.ChildNodes[9].InnerText; + } + + /* + else + { + PropertyInfo propFound = InfoType.GetProperty(strNodeName); + + if (propFound != null) + propFound.SetValue(this, node.InnerText); + } + */ + } + + fs.Close(); + } + catch (Exception ex) + { + Console.WriteLine($"DB Connection Info. Reading Error : {ex.Message}"); + + return false; + } + + return true; + } + + public string GetConnectionStatement(eDbType eType, eConnCategory eConn) + { + string strStatement = string.Empty; + + bool bSSPI = true; + + string IP_ADDRESS = string.Empty; + string PORT_NUMBER = string.Empty; + string CONN_SCHEMA = string.Empty; + string IDENTIFIER = string.Empty; + string PASSWORD = string.Empty; + + switch (eConn) + { + case eConnCategory.Main: + { + if (ConnMain.SSPI.IndexOf("False") >= 0) bSSPI = false; + + IP_ADDRESS = ConnMain.IP; + PORT_NUMBER = ConnMain.Port; + CONN_SCHEMA = ConnMain.SCHEMA; + IDENTIFIER = ConnMain.ID; + PASSWORD = ConnMain.PW; + } + break; + case eConnCategory.ShortTerm: + { + if (ConnShortTerm.SSPI.IndexOf("False") >= 0) bSSPI = false; + + IP_ADDRESS = ConnShortTerm.IP; + PORT_NUMBER = ConnShortTerm.Port; + CONN_SCHEMA = ConnShortTerm.SCHEMA; + IDENTIFIER = ConnShortTerm.ID; + PASSWORD = ConnShortTerm.PW; + } + break; + case eConnCategory.LongTerm: + { + if (ConnLongTerm.SSPI.IndexOf("False") >= 0) bSSPI = false; + + IP_ADDRESS = ConnLongTerm.IP; + PORT_NUMBER = ConnLongTerm.Port; + CONN_SCHEMA = ConnLongTerm.SCHEMA; + IDENTIFIER = ConnLongTerm.ID; + PASSWORD = ConnLongTerm.PW; + } + break; + } + + switch (eType) + { + case eDbType.MS_SQL: + if (bSSPI == false) + strStatement = $"Data Source = {IP_ADDRESS},{PORT_NUMBER}; Initial Catalog = {CONN_SCHEMA}; User ID = {IDENTIFIER}; Password = {PASSWORD}; MultipleActiveResultSets=True;"; + else + strStatement = $"Data Source = localhost; Initial Catalog = {CONN_SCHEMA}; Integrated Security = SSPI; MultipleActiveResultSets=True;"; + break; + default: + break; + } + + return strStatement; + } + } +} diff --git a/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/ConnManager.cs b/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/ConnManager.cs new file mode 100644 index 0000000..ef3a445 --- /dev/null +++ b/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/ConnManager.cs @@ -0,0 +1,644 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Data; +using System.Data.SqlClient; +using System.Diagnostics; + +using SystemX.Net.DB.LogProcess.DBType; +using SystemX.Common; +using SystemX.Common.Serialization; + +using static SystemX.Net.Platform.Common.Util.LogMessage; + +using log4net.Config; + +namespace SystemX.Net.DB.LogProcess +{ + public class XLogDBConnManager + { + public enum eDbType + { + Unknown, + MS_SQL + } + + public enum eConnCategory + { + Main, + ShortTerm, + LongTerm + } + + private Stopwatch stMainDBAccessTime; + + private Stopwatch stShortTermDBAccessTime; + + private Stopwatch stLongTermDBAccessTime; + + private bool MAIN_CONNECT_STATE { set; get; } + + private bool SHORTTERM_CONNECT_STATE { set; get; } + + private bool LONGTERM_CONNECT_STATE { set; get; } + + private bool SET_CONN_STATE { set; get; } + + private string strCurrentLongTermSchemaName { set; get; } + private string strCurrentShortTermSummaryTableName { set; get; } + private string strCurrentShortTermLogTableName { set; get; } + + public bool CONNECT_STATE + { + get { return MAIN_CONNECT_STATE && SHORTTERM_CONNECT_STATE && LONGTERM_CONNECT_STATE && SET_CONN_STATE; } + } + + protected void SET_CONNECT_STATE(eConnCategory eConn, bool bValue) + { + switch (eConn) + { + case eConnCategory.Main: + MAIN_CONNECT_STATE = bValue; + break; + case eConnCategory.ShortTerm: + SHORTTERM_CONNECT_STATE = bValue; + break; + case eConnCategory.LongTerm: + LONGTERM_CONNECT_STATE = bValue; + break; + } + } + + protected bool GET_CONNECT_STATE(eConnCategory eConn) + { + switch (eConn) + { + case eConnCategory.Main: + return MAIN_CONNECT_STATE; + case eConnCategory.ShortTerm: + return SHORTTERM_CONNECT_STATE; + case eConnCategory.LongTerm: + return LONGTERM_CONNECT_STATE; + } + + return false; + } + + private string ConfigPath { get; set; } + + public bool SetConfigFilePath(string strConfigFilePath, bool bReadConfig = false) + { + ConfigPath = strConfigFilePath; + + if (bReadConfig) + return InfoConnection.ReadConfig(ConfigPath); + + return true; + } + + public string GetConfigFilePath() + { + return ConfigPath; + } + + private XDBConnInfo InfoConnection; + + public XDBConnInfo GetDBConnectInfo() + { + return InfoConnection; + } + + private IDBLogControl MgrMainConnection { set; get; } + + private IDBLogControl MgrShortTermConnection { set; get; } + + private IDBLogControl MgrLongTermConnection { set; get; } + + public XLogDBConnManager() + { + stMainDBAccessTime = new Stopwatch(); + stMainDBAccessTime.Start(); + + stShortTermDBAccessTime = new Stopwatch(); + stShortTermDBAccessTime.Start(); + + stLongTermDBAccessTime = new Stopwatch(); + stLongTermDBAccessTime.Start(); + + MAIN_CONNECT_STATE = false; + SHORTTERM_CONNECT_STATE = false; + LONGTERM_CONNECT_STATE = false; + + InfoConnection = new XDBConnInfo(); + + strCurrentLongTermSchemaName = string.Empty; + strCurrentShortTermSummaryTableName = string.Empty; + strCurrentShortTermLogTableName = string.Empty; + + SET_CONN_STATE = true; + } + + public long GetMainDBAccessTime() + { + if (stMainDBAccessTime != null) + return stMainDBAccessTime.ElapsedMilliseconds; + else + return long.MinValue; + } + + public void SetMainDBAccessTime() + { + if (stMainDBAccessTime != null) + stMainDBAccessTime.Restart(); + } + + public long GetShortTermDBAccessTime() + { + if (stShortTermDBAccessTime != null) + return stShortTermDBAccessTime.ElapsedMilliseconds; + else + return long.MinValue; + } + + public void SetShortTermDBAccessTime() + { + if (stShortTermDBAccessTime != null) + stShortTermDBAccessTime.Restart(); + } + + public long GetLongTermDBAccessTime() + { + if (stLongTermDBAccessTime != null) + return stLongTermDBAccessTime.ElapsedMilliseconds; + else + return long.MinValue; + } + + public void SetLongTermDBAccessTime() + { + if (stLongTermDBAccessTime != null) + stLongTermDBAccessTime.Restart(); + } + + public IDBLogControl CurrentConnection(eConnCategory eConn) + { + if (GET_CONNECT_STATE(eConn)) + { + switch (eConn) + { + case eConnCategory.Main: + { + if (stMainDBAccessTime != null) + stMainDBAccessTime.Restart(); + } + return MgrMainConnection; + case eConnCategory.ShortTerm: + { + if (stShortTermDBAccessTime != null) + stShortTermDBAccessTime.Restart(); + } + return MgrShortTermConnection; + case eConnCategory.LongTerm: + { + if (stLongTermDBAccessTime != null) + stLongTermDBAccessTime.Restart(); + } + return MgrLongTermConnection; + } + + return null; + } + else + return null; + } + + private bool ReadyForConnection(eConnCategory eConn) + { + bool bResult = true; + + try + { + string strSetType = string.Empty; + + eDbType eType = eDbType.Unknown; + + switch (eConn) + { + case eConnCategory.Main: + strSetType = InfoConnection.ConnMain.Type; + break; + case eConnCategory.ShortTerm: + strSetType = InfoConnection.ConnShortTerm.Type; + break; + case eConnCategory.LongTerm: + strSetType = InfoConnection.ConnLongTerm.Type; + break; + } + + if (!Enum.TryParse(strSetType, out eType)) + throw new Exception("Failed Database type check."); + + switch (eConn) + { + case eConnCategory.Main: + MgrMainConnection = CreateDBConnMgr(eType); + MgrMainConnection.ConnectionText = InfoConnection.GetConnectionStatement(eType, eConn); + break; + case eConnCategory.ShortTerm: + MgrShortTermConnection = CreateDBConnMgr(eType); + MgrShortTermConnection.ConnectionText = InfoConnection.GetConnectionStatement(eType, eConn); + break; + case eConnCategory.LongTerm: + MgrLongTermConnection = CreateDBConnMgr(eType); + MgrLongTermConnection.ConnectionText = InfoConnection.GetConnectionStatement(eType, eConn); + break; + } + } + catch (Exception e) + { + bResult = false; + + MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"Failed make connection.[SystemX.Net.DB : XDBConnManager.MakeConnection]\r\n" + e.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG); + } + + return bResult; + } + + private bool MakeConnection(eConnCategory eConn) + { + bool bResult = true; + + try + { + bResult = ReadyForConnection(eConn); + + if(bResult) + { + switch (eConn) + { + case eConnCategory.Main: + if (!MgrMainConnection.OpenConnection()) + throw new Exception("Failed [Main] IDBControl.OpenConnection()"); + break; + case eConnCategory.ShortTerm: + if (!MgrShortTermConnection.OpenConnection()) + throw new Exception("Failed [ShortTerm] IDBControl.OpenConnection()"); + break; + case eConnCategory.LongTerm: + if (!MgrLongTermConnection.OpenConnection()) + throw new Exception("Failed [LongTerm] IDBControl.OpenConnection()"); + break; + } + } + } + catch (Exception e) + { + bResult = false; + + MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"Failed make connection.[SystemX.Net.DB : XDBConnManager.MakeConnection]\r\n" + e.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG); + } + + return bResult; + } + + + private bool MakeConnection(eConnCategory eConn, bool bUseHostInfo, string strHostTableName, string strUserTableName) + { + bool bResult = true; + + try + { + bResult = ReadyForConnection(eConn); + + if (bResult) + { + switch (eConn) + { + case eConnCategory.Main: + if (!MgrMainConnection.OpenConnection(bUseHostInfo, strHostTableName, strUserTableName)) + throw new Exception("Failed [Main] IDBControl.OpenConnection()"); + break; + case eConnCategory.ShortTerm: + if (!MgrShortTermConnection.OpenConnection(bUseHostInfo, strHostTableName, strUserTableName)) + throw new Exception("Failed [ShortTerm] IDBControl.OpenConnection()"); + break; + case eConnCategory.LongTerm: + if (!MgrLongTermConnection.OpenConnection(bUseHostInfo, strHostTableName, strUserTableName)) + throw new Exception("Failed [LongTerm] IDBControl.OpenConnection()"); + break; + } + } + } + catch (Exception e) + { + bResult = false; + + MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"Failed make connection.[SystemX.Net.DB : XDBConnManager.MakeConnection]\r\n" + e.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG); + } + + return bResult; + } + + public string GetDataDatabaseSchemaName(eConnCategory SetCategory) + { + if (SetCategory == eConnCategory.LongTerm) return InfoConnection.ConnLongTerm.SCHEMA; + else throw new ArgumentException("Get of connection schema is supported only in LongTerm."); + } + public bool SetDataDatabaseSchemaName(eConnCategory SetCategory, string strSetName) + { + if (SetCategory == eConnCategory.LongTerm) + { + InfoConnection.ConnLongTerm.SCHEMA = strSetName; + + return true; + } + else + throw new ArgumentException("Change of connection schema is supported only in LongTerm."); + } + public string GetDataDatabaseSummaryTableName(eConnCategory SetCategory) + { + if (SetCategory == eConnCategory.ShortTerm) return InfoConnection.ConnShortTerm.SUMMARY_TABLE; + else throw new ArgumentException("Get of summary table is supported only in ShortTerm."); + } + public bool SetDataDatabaseSummaryTableName(eConnCategory SetCategory, string strSetName) + { + if (SetCategory == eConnCategory.ShortTerm) + { + InfoConnection.ConnShortTerm.SUMMARY_TABLE = strSetName; + + return true; + } + else + throw new ArgumentException("Change of summary table is supported only in ShortTerm."); + } + public string GetDataDatabaseLogTableName(eConnCategory SetCategory) + { + if (SetCategory == eConnCategory.ShortTerm) return InfoConnection.ConnShortTerm.LOG_TABLE; + else throw new ArgumentException("Get of log table is supported only in ShortTerm."); + } + public bool SetDataDatabaseLogTableName(eConnCategory SetCategory, string strSetName) + { + if (SetCategory == eConnCategory.ShortTerm) + { + InfoConnection.ConnShortTerm.LOG_TABLE = strSetName; + + return true; + } + else + throw new ArgumentException("Change of log table is supported only in ShortTerm."); + } + + public bool CheckDatabaseConnection(bool bReadCfgFile = true, bool bAutoAttachNameDataDb = true) + { + SET_CONN_STATE = true; + + return OpenConnection(bReadCfgFile, bAutoAttachNameDataDb); + } + + public bool OpenConnection(bool bReadCfgFile = true, bool bAutoAttachNameDataDb = true) + { + SET_CONNECT_STATE(eConnCategory.Main, true); + SET_CONNECT_STATE(eConnCategory.ShortTerm, true); + SET_CONNECT_STATE(eConnCategory.LongTerm, true); + + try + { + if (bReadCfgFile) + { + if (InfoConnection.ReadConfig(ConfigPath) == false) + { + MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"Fail read DB info file.[SystemX.Net.DB : XDBConnManager.OpenConnection]", ConsoleColor.Yellow, LogMessageLevel.DEBUG); + + throw new Exception("Failed ReadConfig()"); + } + } + + bool bNeedTryLongTermConn = false; + bool bNeedTryShortTermConn = false; + + if (bAutoAttachNameDataDb) + { + string strYYYY = DateTime.Now.ToString("yyyy"); + string strMM = DateTime.Now.ToString("MM"); + string strdd = DateTime.Now.ToString("dd"); + + string strGetSchemaName = GetDataDatabaseSchemaName(eConnCategory.LongTerm); + string strGetSummaryTableName = GetDataDatabaseSummaryTableName(eConnCategory.ShortTerm); + string strGetLogTableName = GetDataDatabaseLogTableName(eConnCategory.ShortTerm); + + //LongTerm Schema Name + string[] GetNames = strGetSchemaName.Split('_'); + + if(GetNames.Count() >= 1) + { + strGetSchemaName = GetNames[0] + ("_" + strYYYY); + + SetDataDatabaseSchemaName(eConnCategory.LongTerm, strGetSchemaName); + + if (strCurrentLongTermSchemaName.Length <= 0) + { + strCurrentLongTermSchemaName = GetDataDatabaseSchemaName(eConnCategory.LongTerm); + + bNeedTryLongTermConn = true; + } + else + { + if(strCurrentLongTermSchemaName.CompareTo(GetDataDatabaseSchemaName(eConnCategory.LongTerm)) != 0) + { + strCurrentLongTermSchemaName = GetDataDatabaseSchemaName(eConnCategory.LongTerm); + + bNeedTryLongTermConn = true; + } + } + } + + //ShortTerm Summary Table Name + GetNames = strGetSummaryTableName.Split('_'); + + if (GetNames.Count() >= 2) + { + strGetSummaryTableName = GetNames[0] + "_" + GetNames[1] + ("_" + strYYYY); + + SetDataDatabaseSummaryTableName(eConnCategory.ShortTerm, strGetSummaryTableName); + + if (strCurrentShortTermSummaryTableName.Length <= 0) + { + strCurrentShortTermSummaryTableName = GetDataDatabaseSummaryTableName(eConnCategory.ShortTerm); + + bNeedTryShortTermConn = true; + } + else + { + if (strCurrentShortTermSummaryTableName.CompareTo(GetDataDatabaseSummaryTableName(eConnCategory.ShortTerm)) != 0) + { + strCurrentShortTermSummaryTableName = GetDataDatabaseSummaryTableName(eConnCategory.ShortTerm); + + bNeedTryShortTermConn = true; + } + } + } + + //ShortTerm Log Table Name + GetNames = strGetLogTableName.Split('_'); + + if (GetNames.Count() >= 2) + { + strGetLogTableName = GetNames[0] + "_" + GetNames[1] + ("_" + strYYYY); + + SetDataDatabaseLogTableName(eConnCategory.ShortTerm, strGetLogTableName); + + if (strCurrentShortTermLogTableName.Length <= 0) + { + strCurrentShortTermLogTableName = GetDataDatabaseLogTableName(eConnCategory.ShortTerm); + + bNeedTryShortTermConn = true; + } + else + { + if (strCurrentShortTermLogTableName.CompareTo(GetDataDatabaseLogTableName(eConnCategory.ShortTerm)) != 0) + { + strCurrentShortTermLogTableName = GetDataDatabaseLogTableName(eConnCategory.ShortTerm); + + bNeedTryShortTermConn = true; + } + } + } + } + + if(bNeedTryShortTermConn) + SET_CONNECT_STATE(eConnCategory.ShortTerm, MakeConnection(eConnCategory.ShortTerm)); + if(bNeedTryLongTermConn) + SET_CONNECT_STATE(eConnCategory.LongTerm, MakeConnection(eConnCategory.LongTerm)); + + if (bNeedTryShortTermConn || bNeedTryLongTermConn) + SET_CONNECT_STATE(eConnCategory.Main, MakeConnection(eConnCategory.Main)); + + if (GET_CONNECT_STATE(eConnCategory.Main) == false || + GET_CONNECT_STATE(eConnCategory.ShortTerm) == false || + GET_CONNECT_STATE(eConnCategory.LongTerm) == false) + throw new Exception(); + } + catch (Exception e) + { + MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"Fail db connect.[SystemX.Net.DB : XDBConnManager.OpenConnection - Parameter Type]\r\n" + e.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG); + + SET_CONN_STATE = false; + } + + return CONNECT_STATE; + } + + public bool OpenConnection(bool bUseHostInfo, string strHostTableName, string strUserTableName, bool bReadCfgFile = true) + { + SET_CONNECT_STATE(eConnCategory.Main, true); + SET_CONNECT_STATE(eConnCategory.ShortTerm, true); + SET_CONNECT_STATE(eConnCategory.LongTerm, true); + + try + { + if (bReadCfgFile) + { + if (InfoConnection.ReadConfig(ConfigPath) == false) + { + MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"Fail read DB info file.[SystemX.Net.DB : XDBConnManager.OpenConnection]", ConsoleColor.Yellow, LogMessageLevel.DEBUG); + + throw new Exception("Failed ReadConfig()"); + } + } + + SET_CONNECT_STATE(eConnCategory.Main, MakeConnection(eConnCategory.Main, bUseHostInfo, strHostTableName, strUserTableName)); + SET_CONNECT_STATE(eConnCategory.ShortTerm, MakeConnection(eConnCategory.ShortTerm)); + SET_CONNECT_STATE(eConnCategory.LongTerm, MakeConnection(eConnCategory.LongTerm)); + + if (GET_CONNECT_STATE(eConnCategory.Main) == false || + GET_CONNECT_STATE(eConnCategory.ShortTerm) == false || + GET_CONNECT_STATE(eConnCategory.LongTerm) == false) + throw new Exception(); + } + catch (Exception e) + { + MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"Fail db connect.[SystemX.Net.DB : XDBConnManager.OpenConnection - Parameter Type]\r\n" + e.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG); + + SET_CONN_STATE = false; + } + + return CONNECT_STATE; + } + + IDBLogControl CreateDBConnMgr(eDbType eType) + { + switch (eType) + { + case eDbType.MS_SQL: + return new XDBLogTMSSQL(); + default: + return null; + } + } + public SqlDataReader QueryDatabase(eConnCategory eConn, string strStmt) + { + IDBLogControl ctrlDB = null; + + switch (eConn) + { + case eConnCategory.Main: + ctrlDB = MgrMainConnection; + break; + case eConnCategory.ShortTerm: + ctrlDB = MgrShortTermConnection; + break; + case eConnCategory.LongTerm: + ctrlDB = MgrLongTermConnection; + break; + } + + SqlDataReader drResult = ctrlDB.QueryDatabase(strStmt); + + return drResult; + } + public DataTable QueryDatabaseInTable(eConnCategory eConn, string strStmt) + { + IDBLogControl ctrlDB = null; + + switch (eConn) + { + case eConnCategory.Main: + ctrlDB = MgrMainConnection; + break; + case eConnCategory.ShortTerm: + ctrlDB = MgrShortTermConnection; + break; + case eConnCategory.LongTerm: + ctrlDB = MgrLongTermConnection; + break; + } + + DataTable dtResult = ctrlDB.QueryDataTable(strStmt); + + return dtResult; + } + public DataSet QueryDatabaseInSet(eConnCategory eConn, string strStmt) + { + IDBLogControl ctrlDB = null; + + switch (eConn) + { + case eConnCategory.Main: + ctrlDB = MgrMainConnection; + break; + case eConnCategory.ShortTerm: + ctrlDB = MgrShortTermConnection; + break; + case eConnCategory.LongTerm: + ctrlDB = MgrLongTermConnection; + break; + } + + DataSet dsResult = ctrlDB.QueryDataSet(strStmt); + + return dsResult; + } + } +} diff --git a/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/DBTMSSQL - Query.cs b/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/DBTMSSQL - Query.cs new file mode 100644 index 0000000..e3a5776 --- /dev/null +++ b/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/DBTMSSQL - Query.cs @@ -0,0 +1,412 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using SystemX.Common; +using SystemX.Common.Serialization; +using static SystemX.Net.Platform.Common.Util.LogMessage; + +namespace SystemX.Net.DB.LogProcess.DBType +{ + public partial class XDBLogTMSSQL + { + public bool ExecuteNonQuery(SqlCommand cmd) + { + bool bStatus = true; + int iRecordsAffected = 0; + + if (cmd == null) + return false; + + try + { + if (cmd.Connection == null) + cmd.Connection = dbConnection; + + iRecordsAffected = cmd.ExecuteNonQuery(); + } + catch (Exception e) + { + + iRecordsAffected = 0; + + bStatus = false; + + string strErrorMsg = e.Message; + string strSetData = DateTime.Now.ToString("yyyy -MM-dd HH:mm:ss>>"); + MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] ExecuteNonQuery command failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + MessageOutput.ConsoleWrite(strSetData + @"! Command [" + cmd.CommandText + "] ExecuteNonQuery command failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + } + + if (iRecordsAffected > 0) + bStatus = true; + else + bStatus = false; + + return bStatus; + } + + public bool ExecuteNonCommandQuery(SqlCommand cmd) + { + bool bStatus = true; + int iRecordsAffected = 0; + + if (cmd == null) + return false; + + try + { + if (cmd.Connection == null) + cmd.Connection = dbConnection; + + iRecordsAffected = cmd.ExecuteNonQuery(); + } + catch (Exception e) + { + string strErrorMsg = e.Message; + + iRecordsAffected = 0; + + bStatus = false; + + string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>"); + MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] ExecuteNonQuery command failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + MessageOutput.ConsoleWrite(strSetData + @"! Command [" + cmd.CommandText + "] ExecuteNonQuery command failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + } + + if (iRecordsAffected > 0) + bStatus = true; + else + bStatus = false; + + return bStatus; + } + + public bool ExecuteNonStreamQuery(SqlCommand cmd) + { + bool bStatus = true; + int iRecordsAffected = 0; + + if (cmd == null) + return false; + + try + { + if (cmd.Connection == null) + cmd.Connection = dbConnection; + + iRecordsAffected = cmd.ExecuteNonQuery(); + } + catch (Exception e) + { + string strErrorMsg = e.Message; + + iRecordsAffected = 0; + + bStatus = false; + + string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>"); + MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] ExecuteNonQuery command failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + MessageOutput.ConsoleWrite(strSetData + @"! Command [" + cmd.CommandText + "] ExecuteNonQuery command failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + } + + if (iRecordsAffected > 0) + bStatus = true; + else + bStatus = false; + + return bStatus; + } + + public SqlDataReader QueryDatabase(string strSQL) + { + bool bHasRow = false; + int nCnt = 0; + + SqlDataReader QueryReader = null; + + try + { + using (var dbExcuteCommand = new SqlCommand(strSQL, dbConnection)) + { + QueryReader = dbExcuteCommand.ExecuteReader(); + } + } + catch (Exception e) + { + if (QueryReader != null) + QueryReader.Close(); + + QueryReader = null; + + string strErrorMsg = e.Message; + string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>"); + MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] QueryDatabase failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + MessageOutput.ConsoleWrite(strSetData + @"! Command [" + strSQL + "] QueryDatabase failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + } + + if (QueryReader != null) + { + bHasRow = QueryReader.HasRows; + nCnt = QueryReader.RecordsAffected; + } + + return QueryReader; + } + + public SqlDataReader QueryDatabaseSub(string strSQL) + { + bool bHasRow = false; + int nCnt = 0; + + SqlDataReader QueryReader = null; + + try + { + using (var dbExcuteCommand = new SqlCommand(strSQL, dbConnection)) + { + QueryReader = dbExcuteCommand.ExecuteReader(); + } + } + catch (Exception e) + { + if (QueryReader != null) + QueryReader.Close(); + + QueryReader = null; + + string strErrorMsg = e.Message; + string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>"); + MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] QueryDatabaseSub failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + MessageOutput.ConsoleWrite(strSetData + @"! Command [" + strSQL + "] QueryDatabaseSub failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + } + + if (QueryReader != null) + { + bHasRow = QueryReader.HasRows; + nCnt = QueryReader.RecordsAffected; + } + + return QueryReader; + } + + public SqlDataReader QueryStreamDatabase(string strSQL) + { + bool bHasRow = false; + int nCnt = 0; + + SqlDataReader QueryReader = null; + + try + { + using (var dbExcuteCommand = new SqlCommand(strSQL, dbConnection)) + { + QueryReader = dbExcuteCommand.ExecuteReader(); + } + } + catch (Exception e) + { + if (QueryReader != null) + QueryReader.Close(); + + QueryReader = null; + + string strErrorMsg = e.Message; + string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>"); + MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] QueryStreamDatabase failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + MessageOutput.ConsoleWrite(strSetData + @"! Command [" + strSQL + "] QueryStreamDatabase failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + } + + if (QueryReader != null) + { + bHasRow = QueryReader.HasRows; + nCnt = QueryReader.RecordsAffected; + } + + return QueryReader; + } + + public SqlDataReader QueryCommandDatabase(string strSQL, bool bUseTransaction = false) + { + bool bHasRow = false; + int nCnt = 0; + + SqlDataReader QueryReader = null; + + try + { + if (bUseTransaction) + { + // Start a local transaction. + SqlTransaction sqlTran = dbConnection.BeginTransaction(); + + // Enlist a command in the current transaction. + SqlCommand command = dbConnection.CreateCommand(); + command.Transaction = sqlTran; + + try + { + // Execute two separate commands. + command.CommandText = strSQL; + QueryReader = command.ExecuteReader(); + + // Commit the transaction. + sqlTran.Commit(); + } + catch (Exception ex) + { + // Handle the exception if the transaction fails to commit. + string strErrorMsg = ex.Message; + string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>"); + MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] QueryCommandDatabase Transaction failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + MessageOutput.ConsoleWrite(strSetData + @"! Command [" + strSQL + "] QueryCommandDatabase Transaction failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + + try + { + // Attempt to roll back the transaction. + sqlTran.Rollback(); + } + catch (Exception exRollback) + { + // Throws an InvalidOperationException if the connection + // is closed or the transaction has already been rolled + // back on the server. + strErrorMsg = exRollback.Message; + strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>"); + MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] QueryCommandDatabase Transaction-Rollback failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + } + } + } + else + { + using (var dbExcuteCommand = new SqlCommand(strSQL, dbConnection)) + { + QueryReader = dbExcuteCommand.ExecuteReader(); + } + } + } + catch (Exception e) + { + if (QueryReader != null) + QueryReader.Close(); + + QueryReader = null; + + string strErrorMsg = e.Message; + string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>"); + MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] QueryCommandDatabase failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + MessageOutput.ConsoleWrite(strSetData + @"! Command [" + strSQL + "] QueryCommandDatabase failed. [SystemX.Net.DB.DBType : XDBTMSSQL.ExecuteNonQuery]", ConsoleColor.Red, LogMessageLevel.DEBUG); + } + + if (QueryReader != null) + { + bHasRow = QueryReader.HasRows; + nCnt = QueryReader.RecordsAffected; + } + + return QueryReader; + } + + public DataTable QueryDataTable(string strSQL) + { + SqlDataReader Sqldr = QueryDatabase(strSQL); + + queryDataTable = new DataTable(); + queryDataTable.Load(Sqldr); + + return queryDataTable; + + //return null; + } + + public DataTable QueryDataTable(string strSQL, out bool bHasRows) + { + bHasRows = false; + + return null; + } + + public DataTable QueryDataTable(string strSQL, out int iRecordsAffected) + { + iRecordsAffected = 0; + + return null; + } + + public DataTable QueryDataTable(string strSQL, out bool bHasRows, out int iRecordsAffected) + { + bHasRows = false; + iRecordsAffected = 0; + + return null; + } + + public DataSet QueryDataSet(string strSQL) + { + SqlDataReader Sqldr = QueryDatabase(strSQL); + + queryDataTable = new DataTable(); + queryDataTable.Load(Sqldr); + + queryDataSet = new DataSet(); + queryDataSet.Tables.Add(queryDataTable); + + return queryDataSet; + } + + public DataSet QueryDataSet(string strSQL, out bool bHasRows) + { + bHasRows = false; + + return null; + } + + public DataSet QueryDataSet(string strSQL, out int iRecordsAffected) + { + iRecordsAffected = 0; + + return null; + } + + public DataSet QueryDataSet(string strSQL, out bool bHasRows, out int iRecordsAffected) + { + bHasRows = false; + iRecordsAffected = 0; + + return null; + } + + /* + public DataSet Query(string strSetCommand, out int iRecordsAffect) + { + iRecordsAffect = 0; + + queryDataSet = null; + + try + { + dbCommand.CommandText = strSetCommand; + queryDataReader = dbCommand.ExecuteReader(); + + queryDataTable = new DataTable(); + queryDataTable.Load(queryDataReader); + + queryDataSet = new DataSet(); + queryDataSet.Tables.Add(queryDataTable); + } + catch (Exception e) + { + //throw new CommonQueryException("Execution failed for the statement.[SystemX.Net.DB : MSSQL|COMMON.Query]"); + MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"General packet marshalling failed.[SystemX.Common : COMMON.GetHeaderProtocol]", ConsoleColor.Yellow, LogMessageLevel.DEBUG); + } + + return queryDataSet; + } + */ + } +} + diff --git a/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/DBTMSSQL.cs b/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/DBTMSSQL.cs new file mode 100644 index 0000000..da4f5e7 --- /dev/null +++ b/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/DBTMSSQL.cs @@ -0,0 +1,450 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using SystemX.Common; +using SystemX.Common.Serialization; +using static SystemX.Net.Platform.Common.Util.LogMessage; + +namespace SystemX.Net.DB.LogProcess.DBType +{ + public partial class XDBLogTMSSQL : IDBLogControl, IDisposable + { + private SqlConnection dbConnection; + private SqlCommand dbCommand; + private SqlDataReader queryDataReader; + + private DataSet queryDataSet; + private DataTable queryDataTable; + + private byte[] ucCollectSerializationSchema; + private XTableInfo XSchemaTables; + + private DataTable HostList; + private DataTable UserList; + + public string ConnectionText { get; set; } + + public bool IsConnected { get; set; } + + private string strSetHostTableName; + private string strSetUserTableName; + + public byte[] GetCollectSerializationSchemaInfo() + { + return ucCollectSerializationSchema; + } + + public XTableInfo GetCollectSchemaInfo() + { + return XSchemaTables; + } + + public DataTable GetHostList(bool bDBQueryCheck = false) + { + if(bDBQueryCheck == false) + return HostList; + else + { + if (QueryHostList(strSetHostTableName)) + return HostList; + else + return null; + } + } + + public DataTable GetUserList(bool bDBQueryCheck = false) + { + if (bDBQueryCheck == false) + return UserList; + else + { + if (QueryUserList(strSetUserTableName)) + return UserList; + else + return null; + } + } + + public XDBLogTMSSQL() + { + HostList = null; + } + ~XDBLogTMSSQL() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + } + + protected virtual void Dispose(bool bDisposing) + { + if (bDisposing) + ChkCloseConnect(); + + // do releasing unmanaged resource (종결자가 없는 객체의 자원 해제) + // i.e. close file handle of operating systems + + // suppress calling of Finalizer + GC.SuppressFinalize(this); + } + + public bool InitConnection() + { + try + { + ChkCloseConnect(); + + dbConnection = null; + dbCommand = null; + queryDataReader = null; + queryDataSet = null; + queryDataTable = null; + + IsConnected = false; + } + catch (Exception ex) + { + Console.WriteLine($"SystemX Database Initialization is Failed. Error: {ex.Message}"); + + return false; + } + + return true; + } + + public void ChkCloseConnect() + { + if (IsConnected) + { + if (dbConnection != null) + { + dbConnection.Close(); + dbConnection.Dispose(); + dbConnection = null; + } + + IsConnected = false; + } + } + + public bool OpenConnection() + { + ChkCloseConnect(); + + try + { + dbCommand = new SqlCommand(); + dbConnection = new SqlConnection(ConnectionText); + dbConnection.Open(); + + dbCommand.Connection = dbConnection; + + if (CollectSchemaInfomation()) + IsConnected = true; + else + Console.WriteLine($"SystemX Database CollectSchemaInfomation() is Failed."); + } + catch (SqlException exsql) + { + Console.WriteLine($"SystemX Database Connection Open is Failed. Error: {exsql.Message}"); + + IsConnected = false; + } + catch (Exception ex) + { + Console.WriteLine($"SystemX Database Connection Open is Failed. Error: {ex.Message}"); + + IsConnected = false; + } + + return IsConnected; + } + public bool OpenConnection(bool bUseHostInfo, string strHostTableName, string strUserTableName) + { + ChkCloseConnect(); + + strSetHostTableName = string.Empty; + strSetUserTableName = string.Empty; + + try + { + dbCommand = new SqlCommand(); + dbConnection = new SqlConnection(ConnectionText); + dbConnection.Open(); + + dbCommand.Connection = dbConnection; + + if (CollectSchemaInfomation()) + { + if (bUseHostInfo) + { + strSetHostTableName = strHostTableName; + strSetUserTableName = strUserTableName; + + if (QueryHostList(strSetHostTableName) && + QueryUserList(strSetUserTableName)) + IsConnected = true; + else + Console.WriteLine($"SystemX Database QueryHostList() is Failed."); + } + else + IsConnected = true; + } + else + Console.WriteLine($"SystemX Database CollectSchemaInfomation() is Failed."); + } + catch (SqlException exsql) + { + Console.WriteLine($"SystemX Database Connection Open is Failed. Error: {exsql.Message}"); + + IsConnected = false; + } + catch (Exception ex) + { + Console.WriteLine($"SystemX Database Connection Open is Failed. Error: {ex.Message}"); + + IsConnected = false; + } + + return IsConnected; + } + + public bool CloseConnection() + { + try + { + ChkCloseConnect(); + } + catch (SqlException exsql) + { + Console.WriteLine($"SystemX Database Connection Open is Failed. Error: {exsql.Message}"); + + throw; + } + catch (Exception ex) + { + Console.WriteLine($"SystemX Database Connection Open is Failed. Error: {ex.Message}"); + + throw; + } + + return true; + } + + + public SqlConnection getConnection() + { + return dbConnection; + } + + public string MakeTableInfoQuery(string strTableName) + { + //--컬럼 정보 가져오기 + string strTableColInfoCommand = "SELECT A.TABLE_CATALOG" + + ",A.TABLE_NAME" + + ",A.ORDINAL_POSITION" + + ",A.COLUMN_NAME" + + ",A.DATA_TYPE" + + ",ISNULL(A.CHARACTER_MAXIMUM_LENGTH, '') CHARACTER_LENGTH" + + ",ISNULL(A.NUMERIC_PRECISION, '') NUMBERIC_LENGTH" + + ",A.IS_NULLABLE" + + ",ISNULL(A.COLUMN_DEFAULT, '')" + + ",ISNULL(B.CONSTRAINT_NAME, '')" + + ",ISNULL(A.CHARACTER_SET_NAME, '')" + + ",ISNULL(A.COLLATION_NAME, '')" + + ",CASE WHEN ISNULL(C.NAME, '') = '' THEN '' ELSE 'Identity' END AUTO" + + " FROM INFORMATION_SCHEMA.COLUMNS A LEFT OUTER JOIN" + + " INFORMATION_SCHEMA.KEY_COLUMN_USAGE B" + + " ON A.TABLE_NAME = B.TABLE_NAME" + + " AND A.COLUMN_NAME = B.COLUMN_NAME" + + " LEFT OUTER JOIN" + + " syscolumns C" + + " ON C.ID = object_id(A.TABLE_NAME) AND A.COLUMN_NAME = C.NAME AND C.COLSTAT & 1 = 1 WHERE A.TABLE_NAME = " + + "'" + strTableName + "'" + + " ORDER BY A.ORDINAL_POSITION"; + + return strTableColInfoCommand; + } + + public bool CollectSchemaInfomation() + { + bool bInfoCollectResult = true; + + try + { + dbCommand.CommandText = "SELECT * FROM INFORMATION_SCHEMA.TABLES A ORDER BY A.TABLE_NAME;"; + + queryDataReader = dbCommand.ExecuteReader(); + DataTable dtQueryTable = new DataTable(); + dtQueryTable.Load(queryDataReader); + + List listTableInfo = new List(); + + if (dtQueryTable.Columns.Contains("TABLE_NAME")) + { + int iColPos = dtQueryTable.Columns.IndexOf("TABLE_NAME"); + + for (int i = 0; i < dtQueryTable.Columns[0].Table.Rows.Count; i++) + { + object[] objRows = dtQueryTable.Columns[0].Table.Rows[i].ItemArray; + + string strGetTableName = objRows[iColPos] as string; + + if (strGetTableName.IndexOf("sysdiagrams") < 0) + listTableInfo.Add(objRows[iColPos] as string); + } + } + + int iNamePos = 0, iTypePos = 0, iCharLengPos = 0, iNumLengPos = 0, iIsNullAblePos = 0, iAutoPos = 0; + + int iPosition = 0; + XTable[] listTable = new XTable[listTableInfo.Count]; + + foreach (string strTbName in listTableInfo) + { + dbCommand.CommandText = MakeTableInfoQuery(strTbName); + queryDataReader = dbCommand.ExecuteReader(); + dtQueryTable = new DataTable(); + dtQueryTable.Load(queryDataReader); + + if (dtQueryTable.Columns.Contains("COLUMN_NAME")) + iNamePos = dtQueryTable.Columns.IndexOf("COLUMN_NAME"); + if (dtQueryTable.Columns.Contains("DATA_TYPE")) + iTypePos = dtQueryTable.Columns.IndexOf("DATA_TYPE"); + if (dtQueryTable.Columns.Contains("CHARACTER_LENGTH")) + iCharLengPos = dtQueryTable.Columns.IndexOf("CHARACTER_LENGTH"); + if (dtQueryTable.Columns.Contains("NUMBERIC_LENGTH")) + iNumLengPos = dtQueryTable.Columns.IndexOf("NUMBERIC_LENGTH"); + if (dtQueryTable.Columns.Contains("IS_NULLABLE")) + iIsNullAblePos = dtQueryTable.Columns.IndexOf("IS_NULLABLE"); + if (dtQueryTable.Columns.Contains("AUTO")) + iAutoPos = dtQueryTable.Columns.IndexOf("AUTO"); + + listTable[iPosition] = new XTable(strTbName); + for (int i = 0; i < dtQueryTable.Rows[0].Table.Rows.Count; i++) + { + object[] objRows = dtQueryTable.Columns[0].Table.Rows[i].ItemArray; + + string strGetName = objRows[iNamePos] as string; + string strGetTypeName = objRows[iTypePos] as string; + int iGetTypeCharLeng = Convert.ToInt32(objRows[iCharLengPos]); + int iGetTypeNumLeng = Convert.ToInt32(objRows[iNumLengPos]); + string strGetNullState = objRows[iIsNullAblePos] as string; + string strGetAuto = objRows[iAutoPos] as string; + + bool bGetIsNullAble = false; + + if (strGetNullState.IndexOf("YES") >= 0) + bGetIsNullAble = true; + else if (strGetNullState.IndexOf("NO") >= 0) + bGetIsNullAble = false; + + Type SetThisType = null; + switch (strGetTypeName) + { + case "bit": + //SET_CODE = TypeCode.Boolean; + SetThisType = typeof(Boolean); + break; + case "tinyint": + //SET_CODE = TypeCode.Int64; + SetThisType = typeof(Byte); + break; + case "int": + //SET_CODE = TypeCode.Int32; + SetThisType = typeof(Int32); + break; + case "bigint": + //SET_CODE = TypeCode.Int64; + SetThisType = typeof(Int64); + break; + case "float": + //SET_CODE = TypeCode.Double; + SetThisType = typeof(Double); + break; + case "nvarchar": + //SET_CODE = TypeCode.String; + SetThisType = typeof(String); + break; + case "nchar": + //SET_CODE = TypeCode.String; + SetThisType = typeof(String); + break; + case "datetime": + //SET_CODE = TypeCode.DateTime; + SetThisType = typeof(DateTime); + break; + } + + listTable[iPosition].XTableAdd(i, strGetName, SetThisType, iGetTypeCharLeng, bGetIsNullAble); + } + + iPosition++; + } + + XSchemaTables = new XTableInfo(listTable); + + ucCollectSerializationSchema = SystemXNetSerialization.ObjectToByteArray(XSchemaTables); + XTableInfo XTablesReturn = (XTableInfo)SystemXNetSerialization.ByteArrayToObject(ucCollectSerializationSchema); + } + catch (Exception e) + { + bInfoCollectResult = false; + + //throw new Exception("Collect Schema infomation fail!"); + + MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"Collect Schema infomation fail![SystemX.Net.DB.DBType : XDBTMSSQL.CollectSchemaInfomation]\r\n" + e.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG); + } + + return bInfoCollectResult; + } + public bool QueryHostList(string strTableName) + { + try + { + dbCommand.CommandText = "SELECT * FROM " + $"{strTableName} WITH(NOLOCK) ORDER BY NO ASC;"; + + queryDataReader = dbCommand.ExecuteReader(); + HostList = new DataTable(); + HostList.Load(queryDataReader); + } + catch + { + HostList = null; + + return false; + } + + return true; + } + + public bool QueryUserList(string strTableName) + { + try + { + dbCommand.CommandText = "SELECT * FROM " + $"{strTableName} WITH(NOLOCK) ORDER BY NO ASC;"; + + queryDataReader = dbCommand.ExecuteReader(); + UserList = new DataTable(); + UserList.Load(queryDataReader); + } + catch + { + UserList = null; + + return false; + } + + return true; + } + } +} + diff --git a/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/IDBControl.cs b/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/IDBControl.cs new file mode 100644 index 0000000..c6899e4 --- /dev/null +++ b/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/IDBControl.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SystemX.Net.DB.LogProcess.DBType +{ + public interface IDBLogControl + { + string ConnectionText { get; set; } + bool IsConnected { get; set; } + + bool InitConnection(); + bool OpenConnection(); + bool OpenConnection(bool bUseHostInfo, string strHostTableName, string strUserTableName); + + bool CloseConnection(); + + SqlConnection getConnection(); + + string MakeTableInfoQuery(string strTableName); + bool CollectSchemaInfomation(); + + byte[] GetCollectSerializationSchemaInfo(); + + XTableInfo GetCollectSchemaInfo(); + DataTable GetHostList(bool bDBQueryCheck = false); + DataTable GetUserList(bool bDBQueryCheck = false); + + bool ExecuteNonQuery(SqlCommand cmd); + bool ExecuteNonCommandQuery(SqlCommand cmd); + bool ExecuteNonStreamQuery(SqlCommand cmd); + + SqlDataReader QueryDatabase(string strSQL); + SqlDataReader QueryDatabaseSub(string strSQL); + SqlDataReader QueryCommandDatabase(string strSQL, bool bUseTransaction = false); + SqlDataReader QueryStreamDatabase(string strSQL); + + DataTable QueryDataTable(string strSQL); + DataTable QueryDataTable(string strSQL, out bool bHasRows); + DataTable QueryDataTable(string strSQL, out int iRecordsAffected); + DataTable QueryDataTable(string strSQL, out bool bHasRows, out int iRecordsAffected); + DataSet QueryDataSet(string strSQL); + DataSet QueryDataSet(string strSQL, out bool bHasRows); + DataSet QueryDataSet(string strSQL, out int iRecordsAffected); + DataSet QueryDataSet(string strSQL, out bool bHasRows, out int iRecordsAffected); + } +} diff --git a/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/MariaDB.cs b/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/MariaDB.cs new file mode 100644 index 0000000..2a9b62e --- /dev/null +++ b/SystemX.Net.CP.Middleware.Log/SystemX.Net.Middleware.UI/SystemX.Net.DB.Log/DBType/MariaDB.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static SystemX.Net.Platform.Common.Util.LogMessage; +//using MySql.Data.MySqlClient; + +namespace SystemX.Net.DB.LogProcess.DBType +{ + public class MariaXDB + { + //private MySqlConnection sqlConnection; + //private MySqlCommand sqlCommand; + //private MySqlDataReader sqlDataReader; + private DataSet sqlDataSet; + private DataTable sqlDataTable; + + private string m_strConnectionText; + + private bool m_bConnect; + + public bool CONNECT { get { return m_bConnect; } } + + public MariaXDB(string strSetConnectionInfo) + { + //sqlConnection = null; + //sqlCommand = null; + //sqlDataReader = null; + sqlDataSet = null; + sqlDataTable = null; + + m_strConnectionText = strSetConnectionInfo; + + m_bConnect = false; + } + ~MariaXDB() + { + ChkCloseConnect(); + } + public void ChkCloseConnect() + { + if (m_bConnect) + { + m_bConnect = false; + + /* + if (sqlConnection != null) + { + sqlConnection.Close(); + sqlConnection.Dispose(); + sqlConnection = null; + } + */ + } + } + public bool ConnectDatabase() + { + ChkCloseConnect(); + + try + { + //sqlConnection = new MySqlConnection(m_strConnectionText); + //sqlCommand.Connection = sqlConnection; + + m_bConnect = true; + } + catch (Exception e) + { + m_bConnect = false; + + string strErrorMsg = e.Message; + string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>"); + MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] ConnectDatabase failed. [SystemX.Net.DB.DBType : MariaXDB.ConnectDatabase]", ConsoleColor.Red, LogMessageLevel.DEBUG); + } + + return m_bConnect; + } + public DataSet Query(string strSetCommand, out int iRecordsAffect) + { + iRecordsAffect = 0; + + sqlDataSet = null; + + try + { + //sqlCommand.CommandText = strSetCommand; + //sqlDataReader = sqlCommand.ExecuteReader(); + + //iRecordsAffect = sqlDataReader.RecordsAffected; + + sqlDataTable = new DataTable(); + //sqlDataTable.Load(sqlDataReader); + + sqlDataSet = new DataSet(); + sqlDataSet.Tables.Add(sqlDataTable); + } + catch (Exception e) + { + ;// throw new XDBCommonQueryException("Execution failed for the statement.[SystemX.Net.DB : MariaDB|COMMON.Query]"); + + string strErrorMsg = e.Message; + string strSetData = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>"); + MessageOutput.ConsoleWrite(strSetData + @"! Message [" + strErrorMsg + "] Query failed. [SystemX.Net.DB.DBType : MariaXDB.Query]", ConsoleColor.Red, LogMessageLevel.DEBUG); + MessageOutput.ConsoleWrite(strSetData + @"! Command [" + strSetCommand + "] Query failed. [SystemX.Net.DB.DBType : MariaXDB.Query]", ConsoleColor.Red, LogMessageLevel.DEBUG); + } + + return sqlDataSet; + } + } +}