Server.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Diagnostics;
  11. using static ProcedureComparer.FormMain;
  12. using System.IO;
  13. namespace ProcedureComparer
  14. {
  15. public partial class Server : UserControl
  16. {
  17. SQLManager? _SqlManager = null;
  18. IniManager _IniManager = new IniManager();
  19. private const string INI_FOLDER_NAME = "ini_files";
  20. private const string SAVE_FOLDER_NAME = "procedure_contents";
  21. private const string BACKUP_FOLDER_NAME = "backups";
  22. private string _ConfigIniPath = string.Empty;
  23. private string _SaveDbFolderPath = string.Empty;
  24. private string _SaveDbBackupFolderPath = string.Empty;
  25. private string _ServerName = string.Empty;
  26. private string _CurrentProcedureName = string.Empty;
  27. private string _CurrentProcedureType = string.Empty;
  28. private const string INI_KEY_NAME_ADDRESS = "Address";
  29. private const string INI_KEY_NAME_NAME = "Name";
  30. private const string INI_KEY_NAME_ID = "ID";
  31. private const string INI_KEY_NAME_PW = "PW";
  32. public ExecOtherServerDelegate? _ExecOtherServer;
  33. public bool isConnect { get { return _SqlManager != null; } }
  34. public Server()
  35. {
  36. InitializeComponent();
  37. tb_ProcedureContent.MaxLength = int.MaxValue;
  38. }
  39. private void CheckDirectory(string path)
  40. {
  41. if (!Directory.Exists(path))
  42. Directory.CreateDirectory(path);
  43. }
  44. public void Initailize(string serverName, string configIniPath)
  45. {
  46. CheckDirectory(CreatePath(INI_FOLDER_NAME));
  47. CheckDirectory(CreatePath(SAVE_FOLDER_NAME));
  48. _ServerName = serverName;
  49. gb_ServerName.Text = $"{_ServerName} Info";
  50. _ConfigIniPath = configIniPath;
  51. ChangeConnectButtonText();
  52. tb_Address.Text = _IniManager.GetIniValue(_ConfigIniPath, _ServerName, INI_KEY_NAME_ADDRESS);
  53. tb_Name.Text = _IniManager.GetIniValue(_ConfigIniPath, _ServerName, INI_KEY_NAME_NAME);
  54. tb_ID.Text = _IniManager.GetIniValue(_ConfigIniPath, _ServerName, INI_KEY_NAME_ID);
  55. tb_PW.Text = _IniManager.GetIniValue(_ConfigIniPath, _ServerName, INI_KEY_NAME_PW);
  56. }
  57. private string CreatePath(params string[] paths)
  58. {
  59. string resultPath = Application.StartupPath;
  60. foreach (string path in paths)
  61. {
  62. resultPath = Path.Combine(resultPath, path);
  63. }
  64. return resultPath;
  65. }
  66. public void btn_Connect_Click(object sender, EventArgs e)
  67. {
  68. if (_SqlManager == null)
  69. Connect();
  70. else
  71. Disconnect();
  72. ChangeConnectButtonText();
  73. }
  74. private void Connect()
  75. {
  76. _SqlManager = new SQLManager();
  77. string result = _SqlManager.SetDatabaseInfo(tb_Address.Text, tb_Name.Text, tb_ID.Text, tb_PW.Text).Connect();
  78. if (result != string.Empty)
  79. {
  80. MessageBox.Show(result);
  81. _SqlManager = null;
  82. }
  83. else
  84. {
  85. _SaveDbFolderPath = CreatePath(SAVE_FOLDER_NAME, $"{tb_Address.Text}_{tb_Name.Text}");
  86. _SaveDbBackupFolderPath = Path.Combine(_SaveDbFolderPath, BACKUP_FOLDER_NAME);
  87. CheckDirectory(_SaveDbFolderPath);
  88. CheckDirectory(_SaveDbBackupFolderPath);
  89. ChangeServerInfoEnabled(false);
  90. ChangeServerFunctionEnabeld(true);
  91. SaveServerInfo();
  92. }
  93. }
  94. public void Disconnect()
  95. {
  96. if (_SqlManager != null)
  97. {
  98. _SqlManager.Disconnect();
  99. _SqlManager = null;
  100. ChangeServerInfoEnabled(true);
  101. ChangeServerFunctionEnabeld(false);
  102. DeleteFiles();
  103. }
  104. }
  105. private void SaveServerInfo()
  106. {
  107. _IniManager.SetIniValue(_ConfigIniPath, _ServerName, INI_KEY_NAME_ADDRESS, tb_Address.Text);
  108. _IniManager.SetIniValue(_ConfigIniPath, _ServerName, INI_KEY_NAME_NAME, tb_Name.Text);
  109. _IniManager.SetIniValue(_ConfigIniPath, _ServerName, INI_KEY_NAME_ID, tb_ID.Text);
  110. _IniManager.SetIniValue(_ConfigIniPath, _ServerName, INI_KEY_NAME_PW, tb_PW.Text);
  111. }
  112. private void ChangeServerInfoEnabled(bool enabled)
  113. {
  114. tb_Address.Enabled = enabled;
  115. tb_Name.Enabled = enabled;
  116. tb_ID.Enabled = enabled;
  117. tb_PW.Enabled = enabled;
  118. }
  119. private void ChangeServerFunctionEnabeld(bool enabled)
  120. {
  121. tableLayoutPanel1.Enabled = enabled;
  122. }
  123. private void ChangeConnectButtonText()
  124. {
  125. btn_Connect.Text = _SqlManager == null ? $"Connect\r\n(F{Convert.ToInt32(_ServerName.Replace("Server", "")) + 2})" : "Disconnect";
  126. }
  127. private string SearchProcedureContent(string procedureName, string type)
  128. {
  129. string result = string.Empty;
  130. if (_SqlManager != null)
  131. {
  132. string? content = _SqlManager.GetProcedureContent(procedureName);
  133. if (content != null)
  134. result = ContentSplit(content, type);
  135. else
  136. result = string.Empty;
  137. }
  138. return result;
  139. }
  140. public DataTable? SearchProcedureNames(string filterText)
  141. {
  142. if (_SqlManager == null) return null;
  143. return _SqlManager.GetProcedureNames(filterText);
  144. }
  145. public void Search(string? procedureName, string? type)
  146. {
  147. if (_SqlManager == null || procedureName == null || type == null) return;
  148. _CurrentProcedureName = procedureName;
  149. _CurrentProcedureType = type;
  150. tb_ProcedureContent.Text = SearchProcedureContent(procedureName, type);
  151. }
  152. private string ContentSplit(string text, string type)
  153. {
  154. string[] textArray = text.Replace("\\r\\n", "¶").Split('¶').ToArray();
  155. bool findCreateText = false;
  156. StringBuilder stringBuilder = new StringBuilder();
  157. foreach (string str in textArray)
  158. {
  159. if (!findCreateText)
  160. {
  161. Tuple<bool, string> tuple = CheckCreateProcedure(str, type);
  162. findCreateText = tuple.Item1;
  163. stringBuilder.AppendLine(tuple.Item2);
  164. }
  165. else
  166. stringBuilder.AppendLine(str);
  167. }
  168. return stringBuilder.ToString().Replace("\r\r", "\r");
  169. }
  170. private Tuple<bool, string> CheckCreateProcedure(string text, string type)
  171. {
  172. const string FILTER_TEXT = "CREATE";
  173. const string REPLACE_TEXT = "ALTER";
  174. string targetText = type == "P" ? "PROC" : "FUNC";
  175. int filterStringStartIndex = -1;
  176. bool matched = false;
  177. string _text = text;
  178. string upperText = _text.ToUpper();
  179. filterStringStartIndex = upperText.IndexOf(FILTER_TEXT);
  180. if (filterStringStartIndex > -1)
  181. {
  182. upperText = upperText.Substring(filterStringStartIndex + FILTER_TEXT.Length);
  183. if (upperText.Length > targetText.Length && upperText.Trim().Substring(0, targetText.Length) == targetText)
  184. {
  185. matched = true;
  186. _text = text.Substring(0, filterStringStartIndex);
  187. _text += REPLACE_TEXT;
  188. _text += text.Substring(filterStringStartIndex + FILTER_TEXT.Length);
  189. }
  190. }
  191. return new Tuple<bool, string>(matched, _text);
  192. }
  193. private void btn_Exec_Click(object sender, EventArgs e)
  194. {
  195. ExecuteProcedure(tb_ProcedureContent.Text);
  196. }
  197. public void ExecuteProcedure(string procedureContent)
  198. {
  199. if (_SqlManager == null) return;
  200. BackupProcedure();
  201. string title = $"{tb_Address.Text} > {tb_Name.Text}";
  202. string errorMessage = _SqlManager.SetProcedureContent(procedureContent);
  203. if (errorMessage != string.Empty)
  204. {
  205. MessageBox.Show(errorMessage, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
  206. }
  207. else
  208. {
  209. MessageBox.Show("실행이 완료 되었습니다.", title, MessageBoxButtons.OK, MessageBoxIcon.Information);
  210. Search(_CurrentProcedureName, _CurrentProcedureType);
  211. SendKeys.Send("{F6}");
  212. }
  213. }
  214. private void BackupProcedure()
  215. {
  216. SaveProcedureContent(_SaveDbBackupFolderPath, SearchProcedureContent(_CurrentProcedureName, _CurrentProcedureType));
  217. }
  218. public string SaveProcedure()
  219. {
  220. if (!string.IsNullOrEmpty(_CurrentProcedureName))
  221. return SaveProcedureContent(_SaveDbFolderPath, tb_ProcedureContent.Text);
  222. else
  223. return string.Empty;
  224. }
  225. private string SaveProcedureContent(string path, string procedureContent)
  226. {
  227. string fileName = $"{_CurrentProcedureName}_{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.txt";
  228. string saveFilePath = Path.Combine(path, fileName);
  229. File.WriteAllText(saveFilePath, procedureContent);
  230. return saveFilePath;
  231. }
  232. private void DeleteFiles()
  233. {
  234. foreach (FileInfo fileInfo in new DirectoryInfo(_SaveDbFolderPath).GetFiles())
  235. {
  236. File.Delete(fileInfo.FullName);
  237. }
  238. }
  239. private void btn_ExecOtherServer_Click(object sender, EventArgs e)
  240. {
  241. if (_ExecOtherServer != null)
  242. _ExecOtherServer(_ServerName, tb_ProcedureContent.Text);
  243. }
  244. private void btn_OpenBackupFolder_Click(object sender, EventArgs e)
  245. {
  246. try
  247. {
  248. Process.Start(new ProcessStartInfo()
  249. {
  250. FileName = "Explorer.exe",
  251. Arguments = $"\"{_SaveDbBackupFolderPath}\""
  252. });
  253. }
  254. catch (Exception ex)
  255. {
  256. MessageBox.Show(ex.Message, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
  257. }
  258. }
  259. }
  260. }