From eca09089d58abbe11831e57b10ca4902934d3697 Mon Sep 17 00:00:00 2001 From: Schwirg László Date: Fri, 23 Aug 2024 11:07:54 +0200 Subject: [PATCH] v1.27.1.0 --- Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs | 4 ++-- Vrh.Log4Pro.MaintenanceConsole/Tools.cs | 11 ++++++----- 3 files changed, 81 insertions(+), 21 deletions(-) diff --git a/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs b/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs index 1565842..c08618e 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs @@ -747,24 +747,54 @@ GO SQLDataBase sqld = p.Parameters as SQLDataBase; try { - ColorConsole.WriteLine(prefix: $"Enter the dbname, the userid/password (for the user in favour you want to drop the DB). Format:", bracket: "()", text: "[DBNAME][,USERNAME[,PASSWORD]]:", f: ConsoleColor.Yellow); - ColorConsole.WriteLine(prefix: " ", text: "[DBNAME]", bracket: "[]", suffix: $":name of the DB to delete, default:{sqld.DBName}", f: ConsoleColor.Yellow); - ColorConsole.WriteLine(prefix: " ", text: "[USERNAME,PASSWORD empty]", bracket: "[]", suffix: $":use windows authentication with current user", f: ConsoleColor.Yellow); - ColorConsole.WriteLine(prefix: " ", text: "USERNAME", bracket: "[]", suffix: $":use windows authentication with this user", f: ConsoleColor.Yellow); - ColorConsole.WriteLine(prefix: " ", text: "USERNAME,PASSWORD", bracket: "[]", suffix: $":use sql server authentication with this user and password.", f: ConsoleColor.Yellow); + ColorConsole.WriteLine(prefix: $"Enter the userid/password (for the user in favour you want to drop the DB). Format:", bracket: "()", text: "[USERID[,PASSWORD]]:", f: ConsoleColor.Yellow); + ColorConsole.WriteLine(prefix: " ", text: "[USERID,PASSWORD empty]", bracket: "[]", suffix: $":use windows authentication with current user", f: ConsoleColor.Yellow); + ColorConsole.WriteLine(prefix: " ", text: "USERID", bracket: "[]", suffix: $":use windows authentication with this user", f: ConsoleColor.Yellow); + ColorConsole.WriteLine(prefix: " ", text: "USERID,PASSWORD", bracket: "[]", suffix: $":use sql server authentication with this user and password.", f: ConsoleColor.Yellow); var parameters = ColorConsole.ReadLine($"EX=exit.", ConsoleColor.Yellow, suffix: " --> "); if (parameters.ToUpper() == "EX") { continue; } - string userid = null; string password = null; + if (!string.IsNullOrWhiteSpace(parameters)) + { + var parlst = parameters.Split(new char[] { ',' }); + userid = parlst[0]; + if (parlst.Count() > 1) { password = parameters.Split(new char[] { ',' })[1]; } + } + + ColorConsole.WriteLine(prefix: $"Enter the regex mask for the name of the DBs to list. Format:", bracket: "()", text: "[REGEXMASK]:", f: ConsoleColor.Yellow); + ColorConsole.WriteLine(prefix: " ", text: "[REGEXMASK]", bracket: "[]", suffix: $":regex mask for the name of the DBs to list, default:.*", f: ConsoleColor.Yellow); + parameters = ColorConsole.ReadLine($"EX=exit.", ConsoleColor.Yellow, suffix: " --> "); + if (parameters.ToUpper() == "EX") { continue; } + string dbnamemask = null; + if (!string.IsNullOrWhiteSpace(parameters)) + { + var parlst = parameters.Split(new char[] { ',' }); + dbnamemask = parlst[0]; + } + if (string.IsNullOrWhiteSpace(dbnamemask)) { dbnamemask = ""; } + + var dbdescriptordict = SQLDataBaseManagerCore.GetDatabases(sqld.SQLCS, dbnamemask, userid, password); + ColorConsole.WriteLine(text: "Available DBs", f: ConsoleColor.Yellow,suffix: $" (name masked with:'{dbnamemask}')"); + foreach (var (dbdescriptor,ix) in dbdescriptordict.Select((text,ix)=> (text, ix))) + { + ColorConsole.WriteLine(prefix: $"#{ix}: ", bracket: "[]", text: dbdescriptor.Key, f: ConsoleColor.Yellow,suffix:dbdescriptor.Value); + } + ColorConsole.WriteLine(); + + ColorConsole.WriteLine(prefix: $"Enter the name of the DB you want to drop. Format:", bracket: "()", text: "[DBNAME]:", f: ConsoleColor.Yellow); + ColorConsole.WriteLine(prefix: " ", text: "[DBNAME]", bracket: "[]", suffix: $":name of the DB to delete, default:{sqld.DBName}", f: ConsoleColor.Yellow); + parameters = ColorConsole.ReadLine($"EX=exit.", ConsoleColor.Yellow, suffix: " --> "); + if (parameters.ToUpper() == "EX") { continue; } + string dbname = null; if (!string.IsNullOrWhiteSpace(parameters)) { - dbname = parameters.Split(new char[] { ',' })[0]; - if (parameters.IndexOf(',') >= 1) { userid = parameters.Split(new char[] { ',' })[1]; } - if (parameters.IndexOf(',') >= 0) { password = parameters.Split(new char[] { ',' })[2]; } + var parlst = parameters.Split(new char[] { ',' }); + dbname = parlst[0]; } if (string.IsNullOrWhiteSpace(dbname)) { dbname = sqld.DBName; } + ColorConsole.WriteLine($"Dropping DB..."); ColorConsole.WriteLine(prefix: " connection string to server:", text: sqld.SQLCS, bracket: "[]", f: ConsoleColor.Yellow); ColorConsole.WriteLine(prefix: " DB name to drop:", text: dbname, bracket: "[]", f: ConsoleColor.Yellow); @@ -896,12 +926,12 @@ GO ColorConsole.Write($"{st.Xml_Description}", ConsoleColor.Black, ConsoleColor.White); var statuscolor = st.Status == SQLDataBaseManagerCore.SQLDBStatus.NoAccess ? ConsoleColor.Red:ConsoleColor.Green; ColorConsole.Write(st.Status.ToString(), statuscolor, bracket: "[]", prefix: " ", suffix: ". "); + ColorConsole.Write(st.DBName, statuscolor, bracket: "[]", prefix: "Database ", suffix: ". "); + ColorConsole.Write(st.DataSource, statuscolor, bracket: "[]", prefix: "from server ", suffix: ". "); if (st.Status != SQLDataBaseManagerCore.SQLDBStatus.NoAccess) { - ColorConsole.Write(st.DBName, statuscolor, bracket: "[]", prefix: "Database ", suffix: ". "); - ColorConsole.Write(st.DataSource, statuscolor, bracket: "[]", prefix: "from server ", suffix: ". "); - if (st.Xml_IsRemoteDB) { ColorConsole.Write("REMOTE", ConsoleColor.Cyan, bracket: "[]", prefix: "", suffix: ""); } } + if (st.Xml_IsRemoteDB) { ColorConsole.Write("REMOTE", ConsoleColor.Cyan, bracket: "[]", prefix: "", suffix: ""); } ColorConsole.WriteLine(); return " "; } @@ -1381,12 +1411,41 @@ GO if (sqlserver.Databases.Contains(databasename)) { GetExclusiveUse(databasename, sqlserver, sc); - sqlserver.Databases[databasename].Drop(); + var db = (sqlserver.Databases[databasename]); + db.Drop(); return true; } else { throw new ApplicationException($"Specified DB '{databasename}' does not exist!"); } } - catch (Exception ex) { ColorConsole.WriteLine(ex.MessageNested(), ConsoleColor.Red); return false; } + catch (Exception ex) { ColorConsole.WriteLine(ex.MessageNested(nameof(DropDatabase)), ConsoleColor.Red); return false; } + finally { sqlserver?.ConnectionContext.SqlConnectionObject.Dispose(); } + } + public static Dictionary GetDatabases(string cs, string databasenamemask, string userid, string password) + { + Server sqlserver = null; + Dictionary ReturnDict = new Dictionary(); + try + { + var sc = GetSqlConnection(cs, dbname: null, userid: userid, userpassword: password); + sqlserver = SQLServerConnect(sc); if (sqlserver == null) return null; + + foreach (Database db in sqlserver.Databases) + { + if (!Regex.Match(db.Name, databasenamemask).Success) continue; + string dbname=""; try { dbname = db.Name; } catch { }; + if (string.IsNullOrWhiteSpace(dbname) || dbname=="?") continue; + var dbver = ""; try { dbver = $"v{db.Version};"; } catch { }; + var dbowner = ""; try { dbowner = $"owner:{db.Owner};"; } catch { }; + var dbsize = ""; try { dbsize = $"size:{db.Size}MB;"; } catch { }; + var dbfree = ""; try { dbfree = $"free:{db.SpaceAvailable}MB;"; } catch { }; + var dbstate = ""; try { dbstate = $"state:{db.State};"; } catch { }; + var dbstatus = ""; try { dbstatus = $"status:{db.Status};"; } catch { }; + var dbstr = $"{dbver}{dbowner}{dbsize}{dbfree}{dbstate}{dbstatus}"; + ReturnDict.Add(dbname,dbstr); + } + return ReturnDict; + } + catch (Exception ex) { ColorConsole.WriteLine(ex.MessageNested(nameof(GetDatabases)), ConsoleColor.Red); return null; } finally { sqlserver?.ConnectionContext.SqlConnectionObject.Dispose(); } } public static void GetExclusiveUse(string databasename,Server sqlserver,SqlConnection sc) diff --git a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs index 3c45105..6494767 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.27.0.0")] -[assembly: AssemblyFileVersion("1.27.0.0")] +[assembly: AssemblyVersion("1.27.1.0")] +[assembly: AssemblyFileVersion("1.27.1.0")] diff --git a/Vrh.Log4Pro.MaintenanceConsole/Tools.cs b/Vrh.Log4Pro.MaintenanceConsole/Tools.cs index 0f76b1a..224ab56 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Tools.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Tools.cs @@ -502,23 +502,24 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS /// Az exception és belső exceptionjeinek összefűzése /// /// + /// /// - public static string MessageNested(this Exception ex) + public static string MessageNested(this Exception ex, string header=null) { - string rexmsg = ""; + string rexmsg = (!string.IsNullOrWhiteSpace(header)?(header+">>>: "):"")+ ""; string indent = ""; const string indentof1level = " "; Exception excl = ex; while (excl != null) { - if (!string.IsNullOrWhiteSpace(ex.Message)) - { + if (!string.IsNullOrWhiteSpace(ex.Message)) + { rexmsg += indent + excl.Message; indent += (indent == "" ? "\n" : "") + indentof1level; } excl = excl.InnerException; } - return rexmsg.Replace("\n\n","\n"); + return rexmsg.Replace("\n\n", "\n"); } } -- libgit2 0.21.2