Commit 8d42e5c0d05ca724d43eb307f7f829993f986778
1 parent
71ae70b0
v1.23.0
Showing
3 changed files
with
99 additions
and
48 deletions
Show diff stats
Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs
... | ... | @@ -251,7 +251,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
251 | 251 | foreach (var s in sqld.Xml_SQLScriptList) |
252 | 252 | { |
253 | 253 | ColorConsole.Write(s.Key, ConsoleColor.Yellow, bracket: "[]",suffix:" ",prefix: " "); |
254 | - ColorConsole.Write($"{s.Description} ({s.Name})", ConsoleColor.Yellow, prefix: "Script:"); | |
254 | + var fromfile = string.IsNullOrWhiteSpace(s.FilePath) ? "": $",from:{s.FilePath}"; | |
255 | + ColorConsole.Write($"{s.Description} ({s.Name}{fromfile})", ConsoleColor.Yellow, prefix: "Script:"); | |
255 | 256 | ColorConsole.WriteLine(); |
256 | 257 | vlist.Add(s.Key); |
257 | 258 | } |
... | ... | @@ -260,9 +261,21 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
260 | 261 | if (scriptkey.ToUpper() == "EX") { return o; } |
261 | 262 | SQLDataBase.SQLScript ss = sqld.Xml_SQLScriptList.FirstOrDefault(s=>s.Key==scriptkey); |
262 | 263 | if (!Tools.ResolveArguments(ss.ArgumentParameters, ss.ScriptText,out string ssScriptText)) { return o; } |
263 | - ColorConsole.WriteLine(ssScriptText); | |
264 | - //SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, ss.CommandTimeout, null); | |
265 | - ColorConsole.WriteLine($"Script executed. Name:{sqld.DBName}, script name: {ss.Name}", ConsoleColor.Green); | |
264 | + if (ss.ScriptText == null) | |
265 | + { | |
266 | + ColorConsole.WriteLine($"Nothing to execute!. Check script definition entry!", ConsoleColor.Red); | |
267 | + } | |
268 | + else | |
269 | + { | |
270 | + ColorConsole.WriteLine(ssScriptText); | |
271 | + var confirm = ColorConsole.ReadLine("Enter CONFIRM to start! [EX]=exit.", ConsoleColor.Yellow, prefix:" ", suffix: " --> ", validitylist: new List<string>() {"CONFIRM"}); | |
272 | + if (confirm == "CONFIRM") | |
273 | + { | |
274 | + SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, ss.CommandTimeout, null); | |
275 | + ColorConsole.WriteLine($"Script executed. Name:{sqld.DBName}, script name: {ss.Name}", ConsoleColor.Green); | |
276 | + } | |
277 | + else {ColorConsole.WriteLine($"Script was NOT executed!", ConsoleColor.Red);} | |
278 | + } | |
266 | 279 | } |
267 | 280 | } |
268 | 281 | catch (Exception ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); } |
... | ... | @@ -361,15 +374,15 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
361 | 374 | private static Menu DisplaySQLDataBaseMenu(SQLDataBaseManagerXmlProcessor config = null, string prompt = null, bool silent = false, Menu.SelectionMode selectionmode = Menu.SelectionMode.Multi) |
362 | 375 | { |
363 | 376 | if (config == null) { config = new SQLDataBaseManagerXmlProcessor(XMLCONNECTIONSTRING, "", "hu-HU"); } |
364 | - List<SQLDataBase> schtskdefList = config.GetDefinitionList(); | |
377 | + List<SQLDataBase> defList = config.GetDefinitionList(); | |
365 | 378 | var menufct = new Menu("SQL Databases", prompt) |
366 | 379 | .SetMenuItemDisplayer(DisplayDataBaseInfo) |
367 | 380 | .SetSelectionMode(selectionmode); |
368 | 381 | menufct.ClearMenuItemList(); |
369 | - foreach (var schtskdef in schtskdefList) | |
382 | + foreach (var def in defList) | |
370 | 383 | { |
371 | - var st = SQLDataBaseManagerCore.CollectDataBaseInfo(schtskdef); | |
372 | - menufct.AddMenuItem(new Menu.Item(schtskdef.Xml_Key, null, null, new Menu.ExecutorParameter(pars: st))); | |
384 | + var st = SQLDataBaseManagerCore.CollectDataBaseInfo(def); | |
385 | + menufct.AddMenuItem(new Menu.Item(def.Xml_Key, null, null, new Menu.ExecutorParameter(pars: st))); | |
373 | 386 | } |
374 | 387 | if (!silent) { menufct.DisplayItems(1); } |
375 | 388 | return menufct; |
... | ... | @@ -406,7 +419,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
406 | 419 | } |
407 | 420 | else if (lineix == 3) |
408 | 421 | { |
409 | - var phypath = SQLDataBaseManagerCore.GetPhysicalFilesLocation(st.SQLCS); | |
422 | + var phypath = (SQLDataBaseManagerCore.GetPhysicalFilesLocation(st.SQLCS))??"ERR"; | |
410 | 423 | ColorConsole.Write($"{phypath}", ConsoleColor.Yellow, prefix: "DB files physical location:", suffix: " "); |
411 | 424 | ColorConsole.WriteLine(" "); |
412 | 425 | return " "; |
... | ... | @@ -440,11 +453,22 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
440 | 453 | #region public CollectDataBaseInfo |
441 | 454 | public static SQLDataBase CollectDataBaseInfo(SQLDataBase sqld) |
442 | 455 | { |
443 | - sqld.Status = GetStatus(sqld.SQLCS); | |
444 | - sqld.PhysicalFilesDirectoryPath = GetPhysicalFilesLocation(sqld.SQLCS); | |
445 | - sqld.DBName = GetDBName(sqld.SQLCS); | |
446 | - sqld.DataSource = GetDataSource(sqld.SQLCS); | |
447 | - sqld.SizeString = GetSize(sqld.SQLCS); | |
456 | + try | |
457 | + { | |
458 | + sqld.Status = GetStatus(sqld.SQLCS); | |
459 | + sqld.PhysicalFilesDirectoryPath = (GetPhysicalFilesLocation(sqld.SQLCS))??""; | |
460 | + sqld.DBName = GetDBName(sqld.SQLCS); | |
461 | + sqld.DataSource = GetDataSource(sqld.SQLCS); | |
462 | + sqld.SizeString = GetSize(sqld.SQLCS); | |
463 | + } | |
464 | + catch | |
465 | + { | |
466 | + sqld.Status = SQLDBStatus.NoAccess; | |
467 | + sqld.PhysicalFilesDirectoryPath = ""; | |
468 | + sqld.DBName = GetDBName(sqld.SQLCS); | |
469 | + sqld.DataSource = GetDataSource(sqld.SQLCS); | |
470 | + sqld.SizeString = ""; | |
471 | + } | |
448 | 472 | return sqld; |
449 | 473 | } |
450 | 474 | #endregion public CollectDataBaseInfo |
... | ... | @@ -661,7 +685,13 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
661 | 685 | public enum SQLDBStatus { NoAccess, OK, } |
662 | 686 | public static SQLDBStatus GetStatus(string cs) |
663 | 687 | { |
664 | - try { var s = GetPhysicalFilesLocation(cs); if (s == null) { throw new Exception(); }; return SQLDBStatus.OK; } catch { return SQLDBStatus.NoAccess; } | |
688 | + try | |
689 | + { | |
690 | + var s = GetPhysicalFilesLocation(cs); | |
691 | + if (s == null) { throw new Exception(); }; | |
692 | + return SQLDBStatus.OK; | |
693 | + } | |
694 | + catch { return SQLDBStatus.NoAccess; } | |
665 | 695 | } |
666 | 696 | public static Server SQLServerConnect(string sqlconnectionstring) |
667 | 697 | { |
... | ... | @@ -693,26 +723,30 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
693 | 723 | /// <returns></returns> |
694 | 724 | public static string GetSize(string sqlconnectionstring) |
695 | 725 | { |
696 | - var sqlc = new Microsoft.Data.SqlClient.SqlConnection(sqlconnectionstring); | |
697 | - var dbname = GetDBName(sqlconnectionstring); | |
726 | + try | |
727 | + { | |
728 | + var sqlc = new Microsoft.Data.SqlClient.SqlConnection(sqlconnectionstring); | |
729 | + var dbname = GetDBName(sqlconnectionstring); | |
698 | 730 | |
699 | - var getsizecmd = new Microsoft.Data.SqlClient.SqlCommand ("sp_spaceused", sqlc); | |
700 | - getsizecmd.CommandType = System.Data.CommandType.StoredProcedure; | |
701 | - sqlc.Open(); | |
702 | - var reader = getsizecmd.ExecuteReader(); | |
731 | + var getsizecmd = new Microsoft.Data.SqlClient.SqlCommand("sp_spaceused", sqlc); | |
732 | + getsizecmd.CommandType = System.Data.CommandType.StoredProcedure; | |
733 | + sqlc.Open(); | |
734 | + var reader = getsizecmd.ExecuteReader(); | |
703 | 735 | |
704 | - if (reader.HasRows) | |
705 | - { | |
706 | - while (reader.Read()) | |
736 | + if (reader.HasRows) | |
707 | 737 | { |
708 | - var dbn = reader["database_name"]; | |
709 | - var dbs = reader["database_size"]; | |
710 | - if (Convert.ToString(dbn) == dbname) { return Convert.ToString(dbs); }; | |
738 | + while (reader.Read()) | |
739 | + { | |
740 | + var dbn = reader["database_name"]; | |
741 | + var dbs = reader["database_size"]; | |
742 | + if (Convert.ToString(dbn) == dbname) { return Convert.ToString(dbs); }; | |
743 | + } | |
711 | 744 | } |
745 | + sqlc.Close(); | |
746 | + sqlc.Dispose(); | |
747 | + return "N/A"; | |
712 | 748 | } |
713 | - sqlc.Close(); | |
714 | - sqlc.Dispose(); | |
715 | - return "N/A"; | |
749 | + catch { return "ERR"; } | |
716 | 750 | } |
717 | 751 | #endregion GetSize |
718 | 752 | |
... | ... | @@ -724,18 +758,22 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
724 | 758 | /// <returns>the path, or null, if any error occurs</returns> |
725 | 759 | public static string GetPhysicalFilesLocation(string sqlconnectionstring) |
726 | 760 | { |
727 | - int commandtimeout = 5000; | |
728 | - string scripttext = ""; | |
729 | - scripttext += " DECLARE @physicalpath varchar(1000) = NULL;"; | |
730 | - scripttext += " SELECT @physicalpath = f.physical_name"; | |
731 | - scripttext += " FROM sys.master_files as f inner join sys.databases as d on f.database_id = d.database_id"; | |
732 | - scripttext += " where d.name = '{DATABASE}' and f.type_desc = 'ROWS';"; | |
733 | - scripttext += " SELECT 1 AS RETURNCODE, @physicalpath AS RETURNMESSAGE;"; | |
734 | - var vars = new Dictionary<string, string>(); | |
735 | - vars.Add(nameof(DBSubstitutionName.DATABASE), GetDBName(sqlconnectionstring)); | |
736 | - vars.Add(nameof(DBSubstitutionName.DATASOURCE), ""); | |
737 | - var result = ExecuteSQLScript(sqlconnectionstring, scripttext, commandtimeout, vars); | |
738 | - if (result.ReturnValue != 0) { return Path.GetDirectoryName(result.ReturnMessage); } else { return null; }; | |
761 | + try | |
762 | + { | |
763 | + int commandtimeout = 5000; | |
764 | + string scripttext = ""; | |
765 | + scripttext += " DECLARE @physicalpath varchar(1000) = NULL;"; | |
766 | + scripttext += " SELECT @physicalpath = f.physical_name"; | |
767 | + scripttext += " FROM sys.master_files as f inner join sys.databases as d on f.database_id = d.database_id"; | |
768 | + scripttext += " where d.name = '{DATABASE}' and f.type_desc = 'ROWS';"; | |
769 | + scripttext += " SELECT 1 AS RETURNCODE, @physicalpath AS RETURNMESSAGE;"; | |
770 | + var vars = new Dictionary<string, string>(); | |
771 | + vars.Add(nameof(DBSubstitutionName.DATABASE), GetDBName(sqlconnectionstring)); | |
772 | + vars.Add(nameof(DBSubstitutionName.DATASOURCE), ""); | |
773 | + var result = ExecuteSQLScript(sqlconnectionstring, scripttext, commandtimeout, vars); | |
774 | + if (result.ReturnValue != 0) { return Path.GetDirectoryName(result.ReturnMessage); } else { return null; }; | |
775 | + } | |
776 | + catch { return null; } | |
739 | 777 | } |
740 | 778 | #endregion GetPhysicalFilesLocation |
741 | 779 | #region DBSubstitution |
... | ... | @@ -1295,6 +1333,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
1295 | 1333 | public static class Attributes |
1296 | 1334 | { |
1297 | 1335 | public static class Name { public static class Values { public const string DEFAULT = ""; } } |
1336 | + public static class File { public static class Values { public const string DEFAULT = ""; } } | |
1298 | 1337 | public static class Description { public static class Values { public const string DEFAULT = ""; } } |
1299 | 1338 | public static class ScriptCommandTimeout { public static class Values { public const int DEFAULT = 10000; } } |
1300 | 1339 | public static class Parameters { public static class Values { public const string DEFAULT = ""; } } |
... | ... | @@ -1309,6 +1348,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
1309 | 1348 | { |
1310 | 1349 | public string Key = ""; |
1311 | 1350 | public string Name = ""; |
1351 | + public string FilePath = ""; | |
1312 | 1352 | public string Description = ""; |
1313 | 1353 | public string ScriptText = ""; |
1314 | 1354 | public int CommandTimeout = 10000; |
... | ... | @@ -1322,10 +1362,18 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS |
1322 | 1362 | |
1323 | 1363 | Key = $"S{index}"; |
1324 | 1364 | Name = GetValue(nameof(XmlStructure.SQLDataBase.Scripts.Script.Attributes.Name), sqlscriptXml, XmlStructure.SQLDataBase.Scripts.Script.Attributes.Name.Values.DEFAULT); |
1365 | + FilePath = GetValue(nameof(XmlStructure.SQLDataBase.Scripts.Script.Attributes.File), sqlscriptXml, XmlStructure.SQLDataBase.Scripts.Script.Attributes.File.Values.DEFAULT); | |
1325 | 1366 | Description= GetValue(nameof(XmlStructure.SQLDataBase.Scripts.Script.Attributes.Description), sqlscriptXml, XmlStructure.SQLDataBase.Scripts.Script.Attributes.Description.Values.DEFAULT); |
1326 | 1367 | Tools.ResolveArguments(ArgumentParameters, this.Name, out this.Name, interactive: false); |
1327 | 1368 | Tools.ResolveArguments(ArgumentParameters, this.Description, out this.Description, interactive: false); |
1328 | - ScriptText = GetValue(sqlscriptXml, XmlStructure.SQLDataBase.Scripts.Script.Values.DEFAULT); | |
1369 | + if (string.IsNullOrWhiteSpace(this.FilePath)) | |
1370 | + { | |
1371 | + ScriptText = GetValue(sqlscriptXml, XmlStructure.SQLDataBase.Scripts.Script.Values.DEFAULT); | |
1372 | + } | |
1373 | + else | |
1374 | + { | |
1375 | + try { ScriptText = File.ReadAllText(this.FilePath); } catch { ScriptText = null; } | |
1376 | + } | |
1329 | 1377 | CommandTimeout = GetValue(nameof(XmlStructure.SQLDataBase.Scripts.Script.Attributes.ScriptCommandTimeout), sqlscriptXml, defaultcommandtimeout); |
1330 | 1378 | } |
1331 | 1379 | } | ... | ... |
Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs
... | ... | @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; |
32 | 32 | // You can specify all the values or you can default the Build and Revision Numbers |
33 | 33 | // by using the '*' as shown below: |
34 | 34 | // [assembly: AssemblyVersion("1.0.*")] |
35 | -[assembly: AssemblyVersion("1.22.3.0")] | |
36 | -[assembly: AssemblyFileVersion("1.22.3.0")] | |
35 | +[assembly: AssemblyVersion("1.23.0.0")] | |
36 | +[assembly: AssemblyFileVersion("1.23.0.0")] | ... | ... |
Vrh.Log4Pro.MaintenanceConsole/Tools.cs
... | ... | @@ -330,14 +330,17 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS |
330 | 330 | resolvedtext = stringwithparameters; |
331 | 331 | foreach (var kvp in argumentparametersdictionary) |
332 | 332 | { |
333 | - if (kvp.Value.StartsWith("?") && interactive) | |
333 | + if (kvp.Value == null) { resolveddictionary.Add(kvp.Key, "");} | |
334 | + else if (kvp.Value.StartsWith("?") && interactive) | |
334 | 335 | { |
336 | + // "?default?prompt" | |
335 | 337 | string prompt = $"Enter value for {kvp.Key}:"; |
336 | 338 | string kvpdefaultvalue = null; |
337 | 339 | if (kvp.Value.Length > 1) |
338 | 340 | { |
339 | - prompt = kvp.Value.Substring(1).Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries)[0]; | |
340 | - if (kvp.Value.Substring(1).IndexOf('?') != -1) kvpdefaultvalue = kvp.Value.Substring(1).Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries)[1]; | |
341 | + var ppp = kvp.Value.Substring(1).Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries); | |
342 | + if (!string.IsNullOrWhiteSpace(ppp[0])) { kvpdefaultvalue = ppp[0]; }; | |
343 | + if (kvp.Value.Substring(1).IndexOf('?') != -1) { prompt = ppp[1]; } | |
341 | 344 | } |
342 | 345 | string value = ColorConsole.ReadLine(prompt, ConsoleColor.Yellow, defaultvalue: kvpdefaultvalue); |
343 | 346 | if (value.ToUpper() == "EX") { return false; } | ... | ... |