From 25232c9b5cc3ad3c77827aa53068cf69fed7385b Mon Sep 17 00:00:00 2001 From: Schwirg László Date: Mon, 30 Sep 2024 13:59:07 +0200 Subject: [PATCH] v1.29.0.0 - DBUser és ServerLogin létrehozás xml-ben definiálható --- Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs | 684 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------ Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs | 4 ++-- 2 files changed, 548 insertions(+), 140 deletions(-) diff --git a/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs b/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs index ba98986..0857744 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs @@ -267,6 +267,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS catch (Exception ex) { ColorConsole.WriteLine(ex.MessageNested(), ConsoleColor.Red); } return o; } + + #region CreateServerLogin private static object CreateServerLogin(object parameter, object o) { var config = (parameter as Menu.ExecutorParameter).GetConfig(); @@ -280,17 +282,54 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS else if (sr.SelectedParameterList?.FirstOrDefault() == null) { return o; } else if (sr.Result == Menu.SelectionResult.Ok) { } else { } - string dbusername; - string password; - LoginType logintype; - var p = sr.SelectedParameterList.FirstOrDefault(); SQLDataBase sqld = p.Parameters as SQLDataBase; - parameterinputloop: - try + getparametersloop: + List ServerLoginsToExecuteList = new List(); + string selectedserverloginkey = null; + if (sqld.Xml_SQLServerLoginList.Count == 0) { - ColorConsole.WriteLine(prefix: $"Enter the parameters for creating server login: {sqld.DBName}. Format:", bracket: "()", text: "LOGINNAME[,PASSWORD][GRANTORUSER[,GRANTORUSERPASSWORD]]", f: ConsoleColor.Yellow); + ColorConsole.WriteLine($"There are no server logins defined for database. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Red); + } + else + { + string SRVRLogin_db = sqld.DBName; + ColorConsole.WriteLine(); + ColorConsole.WriteLine($"Select the server login for database. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Yellow); + var validkeylist = new List(); + foreach (SQLDataBase.ServerLogin srvrlogin in sqld.Xml_SQLServerLoginList) + { + ColorConsole.Write($"#{srvrlogin.Key}", ConsoleColor.Yellow, bracket: "[]", suffix: $" [{srvrlogin.LoginName}] login, default database:[{srvrlogin.DefaultDatabase}], grantor user: [{srvrlogin.GrantorUserLoginName}]", prefix: " "); + ColorConsole.WriteLine(); + validkeylist.Add(srvrlogin.Key); + } + validkeylist.Add("*"); + selectedserverloginkey = ColorConsole.ReadLine("Select the server login! [empty]=set manually, [*]=all, [EX]=exit.", ConsoleColor.Yellow, prefix: " ", suffix: " --> ", validitylist: validkeylist); + if (selectedserverloginkey.ToUpper() == "EX") { return o; } + else if (selectedserverloginkey == "*") + { + foreach (var srvrlogin in sqld.Xml_SQLServerLoginList) + { + srvrlogin.Db = SRVRLogin_db; + string optioncommalist = $"{srvrlogin.LoginName},{srvrlogin.LoginPassword},{srvrlogin.GrantorUserLoginName},{srvrlogin.GrantorUserLoginPassword},{srvrlogin.DefaultDatabase}"; + SQLDataBase.ServerLogin SRVRLogin = CheckParametersXml(sqld.DBName, optioncommalist); + if (SRVRLogin != null) { ServerLoginsToExecuteList.Add(srvrlogin); } + } + } + else if (!string.IsNullOrWhiteSpace(selectedserverloginkey)) + { + SQLDataBase.ServerLogin SRVRLogin = sqld.Xml_SQLServerLoginList.FirstOrDefault(t => t.Key == selectedserverloginkey); + if (SRVRLogin == null) { ColorConsole.WriteLine($"The selected server login key is not valid!. DB Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Red); goto getparametersloop; } + string optionlist = $"{SRVRLogin.LoginName},{SRVRLogin.LoginPassword},{SRVRLogin.GrantorUserLoginName},{SRVRLogin.GrantorUserLoginPassword},"; + SRVRLogin.Db = SRVRLogin_db; + ServerLoginsToExecuteList.Add(SRVRLogin); + } + } + + if (string.IsNullOrWhiteSpace(selectedserverloginkey)) + { + ColorConsole.WriteLine(prefix: $"Enter the parameters for creating server login: {sqld.DBName}. Format:", bracket: "()", text: "LOGINNAME[,PASSWORD][,GRANTORUSER[,GRANTORUSERPASSWORD]]", f: ConsoleColor.Yellow); ColorConsole.WriteLine(prefix: " ", text: "LOGINNAME", bracket: "", suffix: $": server login name"); ColorConsole.WriteLine(prefix: " ", text: "PASSWORD", bracket: "", suffix: $": password for login; empty=windows login is created, non empty=sql login is created"); ColorConsole.WriteLine(prefix: " ", text: "GRANTORUSER", bracket: "", suffix: $": name of the user in behalf the login is created (that logs in the sql server); default:current user with windows authentication"); @@ -299,122 +338,297 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS var createuseroptions = ColorConsole.ReadLine($"EX=exit.", ConsoleColor.Yellow, suffix: " --> "); if (createuseroptions.ToUpper() == "EX") { return o; } - dbusername = null; - password = null; - logintype = LoginType.WindowsUser; - var optionList = createuseroptions.Split(new char[] { ',' }, StringSplitOptions.None); - if (optionList.Length < 1) { ColorConsole.WriteLine("ERROR! LOGINNAME is required, PASSWORD is optional", ConsoleColor.Red); goto parameterinputloop; } + SQLDataBase.ServerLogin SRVRLogin = CheckParametersInteractive(sqld.DBName, createuseroptions); + if (SRVRLogin==null) goto getparametersloop; + ServerLoginsToExecuteList.Add(SRVRLogin); + } - dbusername = optionList[0]; - if (optionList.Length == 2) - { - password = optionList[1]; - if (string.IsNullOrWhiteSpace(password)) { ColorConsole.WriteLine("ERROR! PASSWORD, if set, may not be empty!", ConsoleColor.Red); goto parameterinputloop; } - logintype = LoginType.SqlLogin; + bool allconfirmed = false; + foreach (var SrvrLoginToExecute in ServerLoginsToExecuteList) + { + var retcode = ExecuteOneServerLogin(sqld.SQLCS, SrvrLoginToExecute, allconfirmed); + if (retcode == "EX") return o; + else if (retcode == "ALL") { allconfirmed = true; } + else if (retcode != null) goto getparametersloop; + } + return o; + } + private static SQLDataBase.ServerLogin CheckParametersInteractive(string dbname,string optioncommalist) + { + var optionList = optioncommalist.Split(new char[] { ',' }, StringSplitOptions.None); + if (optionList.Length < 1) { ColorConsole.WriteLine("ERROR! LOGINNAME is required, PASSWORD is optional", ConsoleColor.Red); return null; } + + string SRVRLogin_loginname = optionList[0]; + string SRVRLogin_loginpsw = null; + string SRVRLogin_grantoruserloginname = null; + string SRVRLogin_grantoruserpsw = null; + if (optionList.Length > 3) + { + SRVRLogin_grantoruserpsw = optionList[3]; + if (string.IsNullOrWhiteSpace(SRVRLogin_grantoruserpsw)) { ColorConsole.WriteLine("ERROR! GRANTORUSERPASSWORD, if set, may not be empty!", ConsoleColor.Red); return null; } + } + if (optionList.Length > 2) + { + SRVRLogin_grantoruserloginname = optionList[2]; + if (string.IsNullOrWhiteSpace(SRVRLogin_grantoruserloginname)) { ColorConsole.WriteLine("ERROR! GRANTORUSER, if set, may not be empty!", ConsoleColor.Red); return null; } + } + if (optionList.Length > 1) + { + SRVRLogin_loginpsw = optionList[1]; + if (string.IsNullOrWhiteSpace(SRVRLogin_loginpsw)) { ColorConsole.WriteLine("ERROR! PASSWORD, if set, may not be empty!", ConsoleColor.Red); return null; } + } + + LoginType SRVRLogin_logintype = LoginType.WindowsUser; + if (string.IsNullOrWhiteSpace(SRVRLogin_grantoruserloginname) && string.IsNullOrWhiteSpace(SRVRLogin_grantoruserpsw)) { SRVRLogin_logintype = LoginType.SqlLogin; } + + bool SRVRLogin_removeaction = false; + string SRVRLogin_defaultdb = "master"; + + return new SQLDataBase.ServerLogin() + { + Db = dbname + , LoginName = SRVRLogin_loginname + , LoginPassword = SRVRLogin_loginpsw + , LoginType = SRVRLogin_logintype + , GrantorUserLoginName = SRVRLogin_grantoruserloginname + , GrantorUserLoginPassword = SRVRLogin_grantoruserpsw + , DefaultDatabase = SRVRLogin_defaultdb + , Remove = SRVRLogin_removeaction + }; + } + private static SQLDataBase.ServerLogin CheckParametersXml(string dbname, string optioncommalist) + { + var optionList = optioncommalist.Split(new char[] { ',' }, StringSplitOptions.None); + if (optionList.Length < 1) { ColorConsole.WriteLine("ERROR! LOGINNAME is required, PASSWORD is optional", ConsoleColor.Red); return null; } + + string SRVRLogin_loginname = optionList[0]; + string SRVRLogin_loginpsw = optionList[1]; + string SRVRLogin_grantoruserloginname = optionList[2]; + string SRVRLogin_grantoruserpsw = optionList[3]; + string SRVRLogin_defaultdb = optionList[4]; + + if (string.IsNullOrWhiteSpace(SRVRLogin_loginname)) { ColorConsole.WriteLine("ERROR! LOGIN NAME is mandatory!", ConsoleColor.Red); return null; } + if (string.IsNullOrWhiteSpace(SRVRLogin_loginpsw)) { ColorConsole.WriteLine("ERROR! LOGIN PASSWORD is mandatory!", ConsoleColor.Red); return null; } + if (!string.IsNullOrWhiteSpace(SRVRLogin_grantoruserloginname) && string.IsNullOrWhiteSpace(SRVRLogin_grantoruserpsw) || string.IsNullOrWhiteSpace(SRVRLogin_grantoruserloginname) && !string.IsNullOrWhiteSpace(SRVRLogin_grantoruserpsw)) + { + ColorConsole.WriteLine("ERROR! GRANTORUSER/GRANTORUSERPASSWORD, both must be empty or both must be non-empty!", ConsoleColor.Red); return null; + } + if (string.IsNullOrWhiteSpace(SRVRLogin_defaultdb)) { SRVRLogin_defaultdb="master"; } + + LoginType SRVRLogin_logintype = LoginType.WindowsUser; + if (string.IsNullOrWhiteSpace(SRVRLogin_grantoruserloginname) && string.IsNullOrWhiteSpace(SRVRLogin_grantoruserpsw)) { SRVRLogin_logintype = LoginType.SqlLogin; } + + bool SRVRLogin_removeaction = false; + + return new SQLDataBase.ServerLogin() + { + Db = dbname + , LoginName = SRVRLogin_loginname + , LoginPassword = SRVRLogin_loginpsw + , LoginType = SRVRLogin_logintype + , GrantorUserLoginName = SRVRLogin_grantoruserloginname + , GrantorUserLoginPassword = SRVRLogin_grantoruserpsw + , DefaultDatabase = SRVRLogin_defaultdb + , Remove = SRVRLogin_removeaction + }; + } + private static string ExecuteOneServerLogin(string sqlcs, SQLDataBase.ServerLogin srvrlogintoexecute, bool allconfirmed) + { + string returntext = null; + try + { + string removeactionText = srvrlogintoexecute.Remove ? "REMOVE" : "CREATE"; + string passwordtext = srvrlogintoexecute.LoginType == LoginType.WindowsUser ? "" : $", password:{srvrlogintoexecute.LoginPassword}"; + string SRVRLogin_fulldesignation = $"db={srvrlogintoexecute.Db},login name={srvrlogintoexecute.LoginName},login type: {srvrlogintoexecute.LoginType}{passwordtext}"; + if (!allconfirmed) + { + ColorConsole.WriteLine(); + ColorConsole.WriteLine(prefix: $"Action to execute:{removeactionText}.", text: $" {SRVRLogin_fulldesignation}.", bracket: "", suffix: $""); + var confirm = ColorConsole.ReadLine($"Enter CONFIRM to confirm, ALL to confirm all, SKIP to continue.", ConsoleColor.Yellow, suffix: ""); + if (confirm.ToUpper() == "EX") { return "EX"; } + else if (confirm.ToUpper() == "CONFIRM") { returntext = null; } + else if (confirm.ToUpper() == "ALL") { returntext = "ALL"; } + else { return null; } } - string grantoruser = null; - string grantoruserpsw = null; - if (optionList.Length > 2) + + if (!srvrlogintoexecute.Remove) { - grantoruser = optionList[2]; - if (string.IsNullOrWhiteSpace(grantoruser)) { ColorConsole.WriteLine("ERROR! GRANTORUSER, if set, may not be empty!", ConsoleColor.Red); goto parameterinputloop; } + SQLDataBaseManagerCore.CreateLogin(sqlcs, srvrlogintoexecute.LoginName, srvrlogintoexecute.LoginPassword, srvrlogintoexecute.DefaultDatabase, srvrlogintoexecute.LoginType, null, srvrlogintoexecute.GrantorUserLoginName, srvrlogintoexecute.GrantorUserLoginPassword); + ColorConsole.WriteLine($"SUCCESS! {removeactionText} server login: {SRVRLogin_fulldesignation}", ConsoleColor.Green); } - if (optionList.Length > 3) + else { - grantoruserpsw = optionList[3]; - if (string.IsNullOrWhiteSpace(grantoruser)) { ColorConsole.WriteLine("ERROR! GRANTORUSERPASSWORD, if set, may not be empty!", ConsoleColor.Red); goto parameterinputloop; } + ColorConsole.WriteLine($"Function remove DB user is not yet implemented!", ConsoleColor.Red); } - - SQLDataBaseManagerCore.CreateLogin(sqld.SQLCS, dbusername, password, "master", logintype, null,grantoruser,grantoruserpsw); - string passwordtext = logintype == LoginType.WindowsUser ? "" : $", password:{password}"; - ColorConsole.WriteLine($"Server login created. Server login name:{dbusername}, login type: {logintype}{passwordtext}.", ConsoleColor.Green); } - catch (Exception ex) - { - ColorConsole.WriteLine(ex.MessageNested(), ConsoleColor.Red); - goto parameterinputloop; - } - goto parameterinputloop; - + catch (ApplicationException e) { ColorConsole.WriteLine("FATAL ERROR! in script parameter substitution!", ConsoleColor.Red); returntext = null; } + catch (Exception e) { ColorConsole.WriteLine("FATAL ERROR! " + e.MessageNested(), ConsoleColor.Red); returntext = null; } + return returntext; } + #endregion CreateServerLogin + + #region CreateDBUser private static string[] selectablerolelist = new string[] { "FULLACCESS", "DATAREADER", "db_accessadmin", "db_backupoperator", "db_datareader", "db_datawriter", "db_ddladmin", "db_denydatareader", "db_denydatawriter", "db_owner", "db_securityadmin", }; private static string[] fullaccessrolelist = new string[] { "db_accessadmin", "db_backupoperator", "db_datareader", "db_datawriter", "db_ddladmin", "db_owner", "db_securityadmin", }; - private static string[] datareaderrolelist = new string[] { "db_datareader", "db_denydatareader", }; + private static string[] datareaderrolelist = new string[] { "db_datareader", }; private static string[] loginrolelist = new string[] { "dbcreator", "securityadmin", }; + private const string COMMA = ","; private static object CreateDBUser(object parameter, object o) { - const string COMMA = ","; var config = (parameter as Menu.ExecutorParameter).GetConfig(); var args = (parameter as Menu.ExecutorParameter).Args; - var selectedtaskindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.ScheduledTaskManager.Function.CMD_TASKS); + var selectedsqldbindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.SQLDataBaseManager.Function.CMD_DATABASES); var menuofdbs = DisplaySQLDataBaseMenu(config, $"Select the SQL database(s) to manage with function '{nameof(CreateDBUser)}'!", silent: true, selectionmode: Menu.SelectionMode.Single); - Menu.Selection sr = menuofdbs.Select(selectedtaskindexes); + Menu.Selection sr = menuofdbs.Select(selectedsqldbindexes); if (sr.Result == Menu.SelectionResult.Exit) { return o; } else if (sr.Result == Menu.SelectionResult.None) { return o; } else if (sr.Result == Menu.SelectionResult.Error) { return o; } else if (sr.SelectedParameterList?.FirstOrDefault() == null) { return o; } else if (sr.Result == Menu.SelectionResult.Ok) { } else { } - string dbusername = null; - string loginname = null; - string rolenamecommalist = null; + string DBUser_UserName = null; + string DBUser_LoginName = null; + string DBUser_RoleNameCommaList = null; var p = sr.SelectedParameterList.FirstOrDefault(); SQLDataBase sqld = p.Parameters as SQLDataBase; - parameterinputloop: - try + getparametersloop: + List DbusrToExecuteList = new List(); + string selecteddbusrkey = null; + if (sqld.Xml_SQLDbUserList.Count == 0) + { + ColorConsole.WriteLine($"There are no DBUsers defined for database. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Red); + } + else + { + string DBUser_db = sqld.DBName; + ColorConsole.WriteLine(); + ColorConsole.WriteLine($"Select the DBUser for database. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Yellow); + var validkeylist = new List(); + foreach (SQLDataBase.DBUser dbusr in sqld.Xml_SQLDbUserList) + { + ColorConsole.Write($"#{dbusr.Key}", ConsoleColor.Yellow, bracket: "[]", suffix: $" [{dbusr.UserName}] database user for login [{dbusr.LoginName}]", prefix: " "); + ColorConsole.WriteLine(); + validkeylist.Add(dbusr.Key); + } + validkeylist.Add("*"); + selecteddbusrkey = ColorConsole.ReadLine("Select the database user! [empty]=set manually, [*]=all, [EX]=exit.", ConsoleColor.Yellow, prefix: " ", suffix: " --> ", validitylist: validkeylist); + if (selecteddbusrkey.ToUpper() == "EX") { return o; } + else if (selecteddbusrkey == "*") + { + foreach (var dbusr in sqld.Xml_SQLDbUserList) + { + dbusr.Db = DBUser_db; + dbusr.RoleNameCommaList = ResolvePredefinedRoles(dbusr.RoleNameCommaList); + if (!string.IsNullOrWhiteSpace(dbusr.RoleNameCommaList)) { DbusrToExecuteList.Add(dbusr); } + } + } + else if (!string.IsNullOrWhiteSpace(selecteddbusrkey)) + { + SQLDataBase.DBUser DBUser = sqld.Xml_SQLDbUserList.FirstOrDefault(t => t.Key == selecteddbusrkey); + if (DBUser == null) { ColorConsole.WriteLine($"The selected dbuser key is not valid!. DB Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Red); goto getparametersloop; } + DBUser.Db = DBUser_db; + DbusrToExecuteList.Add(DBUser); + } + } + + if (string.IsNullOrWhiteSpace(selecteddbusrkey)) { ColorConsole.WriteLine(prefix: $"Enter the parameters for creating DB user for: {sqld.DBName}. Format:", bracket: "()", text: "DBUSERNAME,SERVERLOGINNAME,ROLENAME,ROLENAME,ROLENAME...", f: ConsoleColor.Yellow); ColorConsole.WriteLine(prefix: " ", text: "DBUSERNAME", bracket: "", suffix: $": dbusername."); - ColorConsole.WriteLine(prefix: " ", text: "SERVERLOGINNAME", bracket: "", suffix: $": serverlogin name; empty=SQL user w/o login,w=WIndows user, other=SQL user for this server login name(* means server login name equals to dbusername)."); + ColorConsole.WriteLine(prefix: " ", text: "SERVERLOGINNAME", bracket: "", suffix: $": serverlogin name; empty=SQL user w/o login,w=Windows user, other=SQL user for this server login name(* means server login name equals to dbusername)."); ColorConsole.WriteLine(prefix: " ", text: "ROLENAME", bracket: "", suffix: $": One of these->" + string.Join(COMMA, selectablerolelist)); var createuseroptions = ColorConsole.ReadLine($"EX=exit.", ConsoleColor.Yellow, suffix: " --> "); if (createuseroptions.ToUpper() == "EX") { return o; } - dbusername = null; - loginname = null; - rolenamecommalist = null; + DBUser_UserName = null; + DBUser_LoginName = null; + DBUser_RoleNameCommaList = null; var optionList = createuseroptions.Split(new char[] { ',' }, StringSplitOptions.None); - if (optionList.Length < 3) { ColorConsole.WriteLine("ERROR! DBUSERNAME,SERVERLOGINNAME and at least one ROLENAME are required", ConsoleColor.Red); goto parameterinputloop; } + if (optionList.Length < 3) { ColorConsole.WriteLine("ERROR! DBUSERNAME,SERVERLOGINNAME and at least one ROLENAME are required", ConsoleColor.Red); goto getparametersloop; } //012345678 //uuu,ppp,r1,r2,r3 - dbusername = optionList[0]; - loginname = optionList[1]; - var selectedrolelist = optionList.Skip(2).ToArray(); - List badrolenames = new List(); - bool enablefullaccess = false; - bool enabledatareader = false; - int selectedrolelistnum = 0; - foreach (var rolename in selectedrolelist) - { - selectedrolelistnum++; - enablefullaccess = enablefullaccess || rolename.ToUpper() == "FULLACCESS"; - enabledatareader = enabledatareader || rolename.ToUpper() == "DATAREADER"; - if (!selectablerolelist.Contains(rolename)) { badrolenames.Add(rolename); } - } - bool specialselectionactive = enablefullaccess || enabledatareader; - if (selectedrolelistnum > 1 && specialselectionactive) { ColorConsole.WriteLine($"ERROR! FULLACCESS or DATAREADER has to be selected alone!", ConsoleColor.Red); goto parameterinputloop; } - if (badrolenames.Count > 0) { ColorConsole.WriteLine($"ERROR! {string.Join(COMMA, badrolenames)} are not available!", ConsoleColor.Red); goto parameterinputloop; } + DBUser_UserName = optionList[0]; + DBUser_LoginName = optionList[1]; + DBUser_RoleNameCommaList = ResolvePredefinedRoles(string.Join(COMMA, optionList.Skip(2))); + if (string.IsNullOrWhiteSpace(DBUser_RoleNameCommaList)) goto getparametersloop; + bool DBUser_Removeaction = false; - var effectiverolelist = - enablefullaccess ? fullaccessrolelist - : enabledatareader ? datareaderrolelist - : selectedrolelist; - rolenamecommalist = string.Join(",", effectiverolelist); + SQLDataBase.DBUser DBUser = new SQLDataBase.DBUser() { Db = sqld.DBName, UserName = DBUser_UserName, LoginName = DBUser_LoginName, RoleNameCommaList = DBUser_RoleNameCommaList, Remove = DBUser_Removeaction, }; + DbusrToExecuteList.Add(DBUser); + } - SQLDataBaseManagerCore.CreateUser(sqld.SQLCS, dbusername, rolenamecommalist, loginname); - ColorConsole.WriteLine($"DB user created. DB name:{sqld.DBName}, DB username:{dbusername}, rolelist={rolenamecommalist}.", ConsoleColor.Green); + bool allconfirmed = false; + foreach (var DbusrToExecute in DbusrToExecuteList) + { + var retcode = ExecuteOneDBUser(sqld.SQLCS, DbusrToExecute, allconfirmed); + if (retcode == "EX") return o; + else if (retcode == "ALL") { allconfirmed = true; } + else if (retcode != null) goto getparametersloop; } - catch (Exception ex) + return o; + } + private static string ResolvePredefinedRoles(string selectedrolecommalist) + { + var selectedrolelist = selectedrolecommalist.Split(',', ';'); + List badrolenames = new List(); + bool enablefullaccess = false; + bool enabledatareader = false; + int selectedrolelistnum = 0; + foreach (var rolename in selectedrolelist) + { + selectedrolelistnum++; + enablefullaccess = enablefullaccess || rolename.ToUpper() == "FULLACCESS"; + enabledatareader = enabledatareader || rolename.ToUpper() == "DATAREADER"; + if (!selectablerolelist.Contains(rolename)) { badrolenames.Add(rolename); } + } + bool specialselectionactive = enablefullaccess || enabledatareader; + if (selectedrolelistnum > 1 && specialselectionactive) { ColorConsole.WriteLine($"ERROR! FULLACCESS or DATAREADER has to be selected alone!", ConsoleColor.Red); return null; } + if (badrolenames.Count > 0) { ColorConsole.WriteLine($"ERROR! {string.Join(COMMA, badrolenames)} are not available!", ConsoleColor.Red); return null; } + var effectiverolelist = + enablefullaccess ? fullaccessrolelist + : enabledatareader ? datareaderrolelist + : selectedrolelist; + return string.Join(",", effectiverolelist); + } + private static string ExecuteOneDBUser(string sqlcs, SQLDataBase.DBUser dbusertoexecute, bool allconfirmed) + { + string returntext = null; + try { - ColorConsole.WriteLine(ex.MessageNested(), ConsoleColor.Red); - goto parameterinputloop; + string removeactionText = dbusertoexecute.Remove ? "REMOVE" : "CREATE"; + string DBUser_fulldesignation = $"db={dbusertoexecute.Db},username={dbusertoexecute.UserName},login name={dbusertoexecute.LoginName},role list={dbusertoexecute.RoleNameCommaList}"; + if (!allconfirmed) + { + ColorConsole.WriteLine(); + ColorConsole.WriteLine(prefix: $"Action to execute:{removeactionText}.", text: $" {DBUser_fulldesignation}.", bracket: "", suffix: $""); + var confirm = ColorConsole.ReadLine($"Enter CONFIRM to confirm, ALL to confirm all, SKIP to continue.", ConsoleColor.Yellow, suffix: ""); + if (confirm.ToUpper() == "EX") { return "EX"; } + else if (confirm.ToUpper() == "CONFIRM") { returntext = null; } + else if (confirm.ToUpper() == "ALL") { returntext = "ALL"; } + else { return null; } + } + + if (!dbusertoexecute.Remove) + { + SQLDataBaseManagerCore.CreateUser(sqlcs, dbusertoexecute.UserName, dbusertoexecute.RoleNameCommaList, dbusertoexecute.LoginName); + ColorConsole.WriteLine($"SUCCESS! {removeactionText} database user: {DBUser_fulldesignation}", ConsoleColor.Green); + } + else + { + ColorConsole.WriteLine($"Function remove DB user is not yet implemented!", ConsoleColor.Red); + } } - goto parameterinputloop; + catch (ApplicationException e) { ColorConsole.WriteLine("FATAL ERROR! in script parameter substitution!", ConsoleColor.Red); returntext = null; } + catch (Exception e) { ColorConsole.WriteLine("FATAL ERROR! " + e.MessageNested(), ConsoleColor.Red); returntext = null; } + return returntext; } + #endregion CreateDBUser + + #region MoveDbToRemoteServer private static object MoveDbToRemoteServer(object parameter, object o) { var config = (parameter as Menu.ExecutorParameter).GetConfig(); @@ -510,7 +724,9 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS ColorConsole.WriteLine($"Moving DB to remote server completed. Required time: {(int)(DateTime.Now.Subtract(DateTimeNow).TotalSeconds)} seconds.",ConsoleColor.Green); return o; } + #endregion MoveDbToRemoteServer + #region ManageLastUpdatedTrigger private static object ManageLastUpdatedTrigger(object parameter, object o) { return _ManageLastUpdatedTrigger(parameter, o, null); } #region script texts for _RemoveAndCreateLastUpdatedTrigger const string Header_Script = @@ -569,9 +785,9 @@ GO { var config = (parameter as Menu.ExecutorParameter).GetConfig(); var args = (parameter as Menu.ExecutorParameter).Args; - var selectedtaskindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.ScheduledTaskManager.Function.CMD_TASKS); + var selectedsqldbindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.SQLDataBaseManager.Function.CMD_DATABASES); var menufolders = DisplaySQLDataBaseMenu(config, $"Select the SQL database(s) to manage with function '{nameof(ManageLastUpdatedTrigger)}'!", silent: true); - Menu.Selection sr = menufolders.Select(selectedtaskindexes); + Menu.Selection sr = menufolders.Select(selectedsqldbindexes); if (sr.Result == Menu.SelectionResult.Exit) { return o; } else if (sr.Result == Menu.SelectionResult.None) { return o; } else if (sr.Result == Menu.SelectionResult.Error) { return o; } @@ -581,13 +797,57 @@ GO if (p == null) { return o; } SQLDataBase sqld = p.Parameters as SQLDataBase; - var manageaction = !forceremoveonly.HasValue; - string actiontext = manageaction ? "MANAGING": (forceremoveonly.Value ? "REMOVING" : "CREATING"); - getparameters:; - try + getparametersloop:; + + List TriggerToExecuteList = new List(); + string selectedtrgkey =null; + if (sqld.Xml_SQLLastUpdatedTriggerList.Count == 0) + { + ColorConsole.WriteLine($"There are no LastUpdated triggers defined for database. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Red); + } + else + { + string LastUpdatedTrigger_db = sqld.DBName; + ColorConsole.WriteLine(); + ColorConsole.WriteLine($"Select the LastUpdated triggers for database. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Yellow); + var validkeylist = new List(); + foreach (SQLDataBase.LastUpdatedTrigger trg in sqld.Xml_SQLLastUpdatedTriggerList) + { + ColorConsole.Write($"#{trg.Key}", ConsoleColor.Yellow, bracket: "[]", suffix: $" [{trg.Column}] column in table [{trg.Schema}].[{trg.Table}]", prefix: " "); + ColorConsole.WriteLine(); + validkeylist.Add(trg.Key); + } + validkeylist.Add("*"); + selectedtrgkey = ColorConsole.ReadLine("Select the trigger! [empty]=set manually, [*]=all, [EX]=exit.", ConsoleColor.Yellow, prefix: " ", suffix: " --> ", validitylist: validkeylist); + if (selectedtrgkey.ToUpper() == "EX") { return o; } + else if (selectedtrgkey == "*") + { + foreach (var trg in sqld.Xml_SQLLastUpdatedTriggerList) + { + trg.Db = LastUpdatedTrigger_db; + TriggerToExecuteList.Add(trg); + } + } + else if(!string.IsNullOrWhiteSpace(selectedtrgkey)) + { + SQLDataBase.LastUpdatedTrigger LastUpdatedTrigger = sqld.Xml_SQLLastUpdatedTriggerList.FirstOrDefault(t => t.Key == selectedtrgkey); + if (LastUpdatedTrigger == null) { ColorConsole.WriteLine($"The selected trigger is not valid!. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Red); goto getparametersloop; } + LastUpdatedTrigger.Db = LastUpdatedTrigger_db; + TriggerToExecuteList.Add(LastUpdatedTrigger); + } + } + + if (string.IsNullOrWhiteSpace(selectedtrgkey)) { + string LastUpdatedTrigger_db = sqld.DBName; + string LastUpdatedTrigger_schema = "dbo"; + string LastUpdatedTrigger_table = null; + string LastUpdatedTrigger_column = ""; + bool LastUpdatedTrigger_removeaction = false; + var manageaction = !forceremoveonly.HasValue; + string actiontext = manageaction ? "MANAGING" : (forceremoveonly.Value ? "REMOVING" : "CREATING"); ColorConsole.WriteLine(prefix: $" Enter the parameters for {actiontext} LastUpdated trigger.", bracket: "", text: "", f: ConsoleColor.Yellow); - string formattext = (manageaction?"[-]":"")+ "[SCHEMA.]TABLE[,COLUMN]"; + string formattext = (manageaction ? "[-]" : "") + "[SCHEMA.]TABLE[,COLUMN]"; ColorConsole.WriteLine(prefix: $" Format:", bracket: "", text: formattext, f: ConsoleColor.Yellow); ColorConsole.WriteLine(prefix: " ", text: "TABLE", bracket: "", suffix: $": MANDATORY name of the table of the trigger (example:ProductionPeriods)"); @@ -597,74 +857,90 @@ GO var createtriggerparameters = ColorConsole.ReadLine($"", ConsoleColor.Yellow, suffix: ""); if (createtriggerparameters.ToUpper() == "EX") { return o; } - if (string.IsNullOrWhiteSpace(createtriggerparameters)) { goto getparameters; } + if (string.IsNullOrWhiteSpace(createtriggerparameters)) { goto getparametersloop; } bool removeactionselected = createtriggerparameters.Substring(1) == "-"; - bool effectiveremoveaction = manageaction && removeactionselected || !manageaction && forceremoveonly.Value; - string effectiveactiontext = effectiveremoveaction ? "REMOVE" : "CREATE"; - createtriggerparameters = createtriggerparameters.Substring(0,1) == "-" ?createtriggerparameters.Substring(1): createtriggerparameters; + LastUpdatedTrigger_removeaction = manageaction && removeactionselected || !manageaction && forceremoveonly.Value; + createtriggerparameters = createtriggerparameters.Substring(0, 1) == "-" ? createtriggerparameters.Substring(1) : createtriggerparameters; var optionList = createtriggerparameters.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); - string db = sqld.DBName; string tablewithschema = ""; - string column = ""; if (optionList.Length == 1) { tablewithschema = optionList[0]; } - else if (optionList.Length == 2) { tablewithschema = optionList[0]; column = optionList[1]; } - else /*if (optionList.Length > 2)*/ { ColorConsole.WriteLine("ERROR! No more parameters required beside SCHEMA, TABLE and COLUMN!", ConsoleColor.Red); goto getparameters; } + else if (optionList.Length == 2) { tablewithschema = optionList[0]; LastUpdatedTrigger_column = optionList[1]; } + else /*if (optionList.Length > 2)*/ { ColorConsole.WriteLine("ERROR! No more parameters required beside SCHEMA, TABLE and COLUMN!", ConsoleColor.Red); goto getparametersloop; } - if (string.IsNullOrWhiteSpace(column)) column = "LastUpdated"; + if (string.IsNullOrWhiteSpace(LastUpdatedTrigger_column)) LastUpdatedTrigger_column = "LastUpdated"; - if (string.IsNullOrWhiteSpace(db)) { ColorConsole.WriteLine("ERROR! DATABASE can not be empty!", ConsoleColor.Red); goto getparameters; } - if (string.IsNullOrWhiteSpace(tablewithschema)) { ColorConsole.WriteLine("ERROR! [SCHEMA.]TABLE can not be empty!", ConsoleColor.Red); goto getparameters; } + if (string.IsNullOrWhiteSpace(LastUpdatedTrigger_db)) { ColorConsole.WriteLine("ERROR! DATABASE can not be empty!", ConsoleColor.Red); goto getparametersloop; } + if (string.IsNullOrWhiteSpace(tablewithschema)) { ColorConsole.WriteLine("ERROR! [SCHEMA.]TABLE can not be empty!", ConsoleColor.Red); goto getparametersloop; } var tablewithschemasplitted = tablewithschema.Split('.'); - if (tablewithschemasplitted.Length != 1 && tablewithschemasplitted.Length != 2) { ColorConsole.WriteLine("ERROR! [SCHEMA.]TABLE incorrect!", ConsoleColor.Red); goto getparameters; } - if (string.IsNullOrWhiteSpace(column)) { ColorConsole.WriteLine("ERROR! COLUMN can not be empty!", ConsoleColor.Red); goto getparameters; } - - string schema = "dbo"; - string table = tablewithschema; - if (tablewithschemasplitted.Length == 2) { schema = tablewithschemasplitted[0]; table = tablewithschemasplitted[1]; } - string triggername = $"TRG_LASTUPDATETS_{db}_{schema}_{table}_{column}"; - string parameters = $"DBNAME={db};SCHEMA={schema};TABLE={table};COLUMN={column};TRIGGER={triggername};"; - string ssScriptText = null; + if (tablewithschemasplitted.Length != 1 && tablewithschemasplitted.Length != 2) { ColorConsole.WriteLine("ERROR! [SCHEMA.]TABLE incorrect!", ConsoleColor.Red); goto getparametersloop; } + if (string.IsNullOrWhiteSpace(LastUpdatedTrigger_column)) { ColorConsole.WriteLine("ERROR! COLUMN can not be empty!", ConsoleColor.Red); goto getparametersloop; } - string triggerfulldesignation = $"db={db},table={schema}.{table},column={column},trigger={triggername}"; - ColorConsole.WriteLine(prefix: $"Action to execute:{effectiveactiontext}.", text: $" {triggerfulldesignation}.", bracket: "", suffix: $""); - var confirm = ColorConsole.ReadLine($"Enter CONFIRM to confirm, NO to continue.", ConsoleColor.Yellow, suffix: ""); - if (createtriggerparameters.ToUpper() == "EX") { return o; } - if (confirm.ToUpper() != "CONFIRM") { goto getparameters; } + LastUpdatedTrigger_table = tablewithschema; + if (tablewithschemasplitted.Length == 2) { LastUpdatedTrigger_schema = tablewithschemasplitted[0]; LastUpdatedTrigger_table = tablewithschemasplitted[1]; } + + SQLDataBase.LastUpdatedTrigger LastUpdatedTrigger = new SQLDataBase.LastUpdatedTrigger() {Db= LastUpdatedTrigger_db, Schema= LastUpdatedTrigger_schema,Table= LastUpdatedTrigger_table,Column= LastUpdatedTrigger_column , Remove= LastUpdatedTrigger_removeaction ,}; + TriggerToExecuteList.Add(LastUpdatedTrigger); + } - if (!Tools.ResolveArguments(parameters, RemoveLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); } - try {SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null); } - catch (Exception e) {ColorConsole.WriteLine(e.MessageNested(), ConsoleColor.Yellow);} - if (!Tools.ResolveArguments(parameters, RemoveLastUpdatedColumn_Script, out ssScriptText)) { throw new ApplicationException(); } - try {SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null);} + bool allconfirmed = false; + foreach (var triggertoexecute in TriggerToExecuteList) + { + var retcode = ExecuteOneLastUpdateTrigger(sqld.SQLCS, triggertoexecute, allconfirmed); + if (retcode == "EX") return o; + else if (retcode == "ALL") { allconfirmed = true; } + else if (retcode != null) goto getparametersloop; + } + return o; + } + + private static string ExecuteOneLastUpdateTrigger(string sqlcs,SQLDataBase.LastUpdatedTrigger triggertoexecute, bool allconfirmed) + { + string returntext = null; + try + { + string removeactionText = triggertoexecute.Remove ? "REMOVE" : "CREATE"; + string LastUpdatedTrigger_triggername = $"TRG_LASTUPDATETS_{triggertoexecute.Db}_{triggertoexecute.Schema}_{triggertoexecute.Table}_{triggertoexecute.Column}"; + string LastUpdatedTrigger_fulldesignation = $"db={triggertoexecute.Db},table={triggertoexecute.Schema}.{triggertoexecute.Table},column={triggertoexecute.Column},trigger={LastUpdatedTrigger_triggername}"; + if (!allconfirmed) + { + ColorConsole.WriteLine(); + ColorConsole.WriteLine(prefix: $"Action to execute:{removeactionText}.", text: $" {LastUpdatedTrigger_fulldesignation}.", bracket: "", suffix: $""); + var confirm = ColorConsole.ReadLine($"Enter CONFIRM to confirm, ALL to confirm all, SKIP to continue.", ConsoleColor.Yellow, suffix: ""); + if (confirm.ToUpper() == "EX") { return "EX"; } + else if (confirm.ToUpper() == "CONFIRM") { returntext = null; } + else if (confirm.ToUpper() == "ALL") { returntext = "ALL"; } + else { return null; } + } + + string ssScriptText = null; + string LastUpdatedTrigger_parameters = $"DBNAME={triggertoexecute.Db};SCHEMA={triggertoexecute.Schema};TABLE={triggertoexecute.Table};COLUMN={triggertoexecute.Column};TRIGGER={LastUpdatedTrigger_triggername};"; + if (!Tools.ResolveArguments(LastUpdatedTrigger_parameters, RemoveLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); } + try { SQLDataBaseManagerCore.ExecuteSQLScript(sqlcs, ssScriptText, 5000, null); } + catch (Exception e) { ColorConsole.WriteLine(e.MessageNested(), ConsoleColor.Yellow); } + if (!Tools.ResolveArguments(LastUpdatedTrigger_parameters, RemoveLastUpdatedColumn_Script, out ssScriptText)) { throw new ApplicationException(); } + try { SQLDataBaseManagerCore.ExecuteSQLScript(sqlcs, ssScriptText, 5000, null); } catch (Exception e) { ColorConsole.WriteLine(e.MessageNested(), ConsoleColor.Yellow); } - if (!effectiveremoveaction) + if (!triggertoexecute.Remove) { - if (!Tools.ResolveArguments(parameters, CreateLastUpdatedColumn_Script, out ssScriptText)) { throw new ApplicationException(); } - SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null); + if (!Tools.ResolveArguments(LastUpdatedTrigger_parameters, CreateLastUpdatedColumn_Script, out ssScriptText)) { throw new ApplicationException(); } + SQLDataBaseManagerCore.ExecuteSQLScript(sqlcs, ssScriptText, 5000, null); - if (!Tools.ResolveArguments(parameters, CreateLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); } - SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null); + if (!Tools.ResolveArguments(LastUpdatedTrigger_parameters, CreateLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); } + SQLDataBaseManagerCore.ExecuteSQLScript(sqlcs, ssScriptText, 5000, null); - if (!Tools.ResolveArguments(parameters, EnableLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); } - SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null); + if (!Tools.ResolveArguments(LastUpdatedTrigger_parameters, EnableLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); } + SQLDataBaseManagerCore.ExecuteSQLScript(sqlcs, ssScriptText, 5000, null); } - ColorConsole.WriteLine($"SUCCESS! {effectiveactiontext} trigger to store LastUpdate TimeStamp: {triggerfulldesignation}", ConsoleColor.Green); - goto getparameters; - } - catch (ApplicationException e) - { - ColorConsole.WriteLine("FATAL ERROR! in script parameter substitution!", ConsoleColor.Red); - goto getparameters; - } - catch (Exception e) - { - ColorConsole.WriteLine("FATAL ERROR! "+e.MessageNested(), ConsoleColor.Red); - goto getparameters; + ColorConsole.WriteLine($"SUCCESS! {removeactionText} trigger to store LastUpdate TimeStamp: {LastUpdatedTrigger_fulldesignation}", ConsoleColor.Green); } + catch (ApplicationException e) { ColorConsole.WriteLine("FATAL ERROR! in script parameter substitution!", ConsoleColor.Red); returntext = null; } + catch (Exception e) { ColorConsole.WriteLine("FATAL ERROR! " + e.MessageNested(), ConsoleColor.Red); returntext = null; } + return returntext; } + #endregion ManageLastUpdatedTrigger private static object ExecuteScript(object parameter, object o) { @@ -728,8 +1004,7 @@ GO } return o; } - - private static object DropDB(object parameter, object o) + private static object DropDB(object parameter, object o) { var config = (parameter as Menu.ExecutorParameter).GetConfig(); var args = (parameter as Menu.ExecutorParameter).Args; @@ -1273,7 +1548,7 @@ GO string createUserQuery = "CREATE USER [" + dbusername + "]"; if (forlogin.HasValue && forlogin.Value) {createUserQuery += " FOR LOGIN [" + loginname + "];"; } - else if (forlogin.HasValue && !forlogin.Value) { createUserQuery = " WITHOUT LOGIN;"; } + else if (forlogin.HasValue && !forlogin.Value) { createUserQuery += " WITHOUT LOGIN;"; } createUserQuery += ";"; using (SqlCommand createUserCommand = new SqlCommand(createUserQuery, connection)) { createUserCommand.ExecuteNonQuery(); } @@ -1968,6 +2243,9 @@ GO public string Xml_PhysicalFilesDirectoryPath; public int Xml_ScriptCommandTimeout; public List Xml_SQLDataList; + public List Xml_SQLLastUpdatedTriggerList; + public List Xml_SQLDbUserList; + public List Xml_SQLServerLoginList; public List Xml_SQLScriptList; public string DBName; @@ -2082,6 +2360,21 @@ GO var sqldataXmlList = GetAllXElements(sqldatabasexml, nameof(XmlStructure.SQLDataBase.SQLData)); if (sqldataXmlList!=null) { foreach (var sqldataXml in sqldataXmlList) { Xml_SQLDataList.Add(new SQLData(sqldataXml)); } } + Xml_SQLLastUpdatedTriggerList = new List(); + var trgsXml = GetXElement(sqldatabasexml, nameof(XmlStructure.SQLDataBase.Triggers)); + var trgXmlList = GetAllXElements(trgsXml, nameof(XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger)); + if (trgXmlList != null) { int ix = 0; foreach (var trgXml in trgXmlList) { Xml_SQLLastUpdatedTriggerList.Add(new LastUpdatedTrigger(trgXml,ix)); ix++; } } + + Xml_SQLDbUserList = new List(); + var dbusersXml = GetXElement(sqldatabasexml, nameof(XmlStructure.SQLDataBase.DBUsers)); + var dbuserXmlList = GetAllXElements(dbusersXml, nameof(XmlStructure.SQLDataBase.DBUsers.DBUser)); + if (dbuserXmlList != null) { int ix = 0; foreach (var dbusrXml in dbuserXmlList) { Xml_SQLDbUserList.Add(new DBUser(dbusrXml, ix)); ix++; } } + + Xml_SQLServerLoginList = new List(); + var srvrloginsXml = GetXElement(sqldatabasexml, nameof(XmlStructure.SQLDataBase.ServerLogins)); + var srvrloginXmlList = GetAllXElements(srvrloginsXml, nameof(XmlStructure.SQLDataBase.ServerLogins.ServerLogin)); + if (srvrloginXmlList != null) { int ix = 0; foreach (var srvrloginXml in srvrloginXmlList) { Xml_SQLServerLoginList.Add(new ServerLogin(srvrloginXml, ix)); ix++; } } + Xml_SQLScriptList = new List(); var sqlscriptsXml = GetXElement(sqldatabasexml, nameof(XmlStructure.SQLDataBase.Scripts)); var sqlscriptXmlList = GetAllXElements(sqlscriptsXml, nameof(XmlStructure.SQLDataBase.Scripts.Script)); @@ -2143,6 +2436,44 @@ GO public static class Group { public static class Values { public const string DEFAULT = ""; } } } } + public static class Triggers + { + public static class LastUpdatedTrigger + { + public static class Attributes + { + public static class Schema { public static class Values { public const string DEFAULT = ""; } } + public static class Table { public static class Values { public const string DEFAULT = ""; } } + public static class Field { public static class Values { public const string DEFAULT = ""; } } + } + } + } + public static class DBUsers + { + public static class DBUser + { + public static class Attributes + { + public static class UserName { public static class Values { public const string DEFAULT = ""; } } + public static class RoleNameList { public static class Values { public const string DEFAULT = ""; } } + public static class LoginName { public static class Values { public const string DEFAULT = ""; } } + } + } + } + public static class ServerLogins + { + public static class ServerLogin + { + public static class Attributes + { + public static class LoginName { public static class Values { public const string DEFAULT = ""; } } + public static class LoginPassword { public static class Values { public const string DEFAULT = ""; } } + public static class GrantorLoginName { public static class Values { public const string DEFAULT = ""; } } + public static class GrantorLoginPsw { public static class Values { public const string DEFAULT = ""; } } + public static class DefaultDatabase{ public static class Values { public const string DEFAULT = ""; } } + } + } + } public static class Scripts { public static class Script @@ -2233,6 +2564,83 @@ GO #endregion constructors } #endregion SQLData class + #region LastUpdatedTrigger class + public class LastUpdatedTrigger : XmlLinqBase + { + #region fields + public string Key = ""; + public string Db = ""; + public string Schema = ""; + public string Table = ""; + public string Column= ""; + public bool Remove = false; + #endregion fields + #region constructors + public LastUpdatedTrigger() { } + public LastUpdatedTrigger(LastUpdatedTrigger dbusr) { Schema = dbusr.Schema; Column = dbusr.Column; Table = dbusr.Table; Key = dbusr.Key; Db = dbusr.Db; Remove = dbusr.Remove;} + public LastUpdatedTrigger(XElement trgXml, int ix) + { + Key = $"TRG{ix}"; + Schema = GetValue(nameof(XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger.Attributes.Schema), trgXml, XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger.Attributes.Schema.Values.DEFAULT); + Table = GetValue(nameof(XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger.Attributes.Table), trgXml, XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger.Attributes.Table.Values.DEFAULT); + Column = GetValue(nameof(XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger.Attributes.Field), trgXml, XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger.Attributes.Field.Values.DEFAULT); + } + #endregion constructors + } + #endregion LastUpdatedTrigger class + #region DBUser class + public class DBUser : XmlLinqBase + { + #region fields + public string Key = ""; + public string Db = ""; + public string UserName = ""; + public string LoginName = ""; + public string RoleNameCommaList = ""; + public bool Remove = false; + #endregion fields + #region constructors + public DBUser() { } + public DBUser(DBUser dbusr) { UserName = dbusr.UserName; LoginName = dbusr.LoginName; RoleNameCommaList = dbusr.RoleNameCommaList; Key = dbusr.Key; Db = dbusr.Db; Remove = dbusr.Remove; } + public DBUser(XElement dbusrXml, int ix) + { + Key = $"DBU{ix}"; + UserName = GetValue(nameof(XmlStructure.SQLDataBase.DBUsers.DBUser.Attributes.UserName), dbusrXml, XmlStructure.SQLDataBase.DBUsers.DBUser.Attributes.UserName.Values.DEFAULT); + RoleNameCommaList = GetValue(nameof(XmlStructure.SQLDataBase.DBUsers.DBUser.Attributes.RoleNameList), dbusrXml, XmlStructure.SQLDataBase.DBUsers.DBUser.Attributes.RoleNameList.Values.DEFAULT); + LoginName = GetValue(nameof(XmlStructure.SQLDataBase.DBUsers.DBUser.Attributes.LoginName), dbusrXml, XmlStructure.SQLDataBase.DBUsers.DBUser.Attributes.LoginName.Values.DEFAULT); + } + #endregion constructors + } + #endregion DBUser class + #region ServerLogin class + public class ServerLogin : XmlLinqBase + { + #region fields + public string Key = ""; + public string Db = ""; + public string GrantorUserLoginName = ""; + public string LoginName = ""; + public string LoginPassword = ""; + public LoginType LoginType = LoginType.WindowsUser; + public string GrantorUserLoginPassword = ""; + public string DefaultDatabase = ""; + public bool Remove = false; + #endregion fields + #region constructors + public ServerLogin() { } + public ServerLogin(ServerLogin srvrlogin) { LoginName = srvrlogin.LoginName; GrantorUserLoginName = srvrlogin.GrantorUserLoginName; LoginPassword = srvrlogin.LoginPassword; GrantorUserLoginPassword = srvrlogin.GrantorUserLoginPassword; Key = srvrlogin.Key; Db = srvrlogin.Db; Remove = srvrlogin.Remove; } + public ServerLogin(XElement dbusrXml, int ix) + { + Key = $"SL{ix}"; + LoginName = GetValue(nameof(XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.LoginName), dbusrXml, XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.LoginName.Values.DEFAULT); + LoginPassword = GetValue(nameof(XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.LoginPassword), dbusrXml, XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.LoginPassword.Values.DEFAULT); + GrantorUserLoginName = GetValue(nameof(XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.GrantorLoginName), dbusrXml, XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.GrantorLoginName.Values.DEFAULT); + GrantorUserLoginPassword = GetValue(nameof(XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.GrantorLoginPsw), dbusrXml, XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.GrantorLoginPsw.Values.DEFAULT); + DefaultDatabase = GetValue(nameof(XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.DefaultDatabase), dbusrXml, XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.DefaultDatabase.Values.DEFAULT); + } + #endregion constructors + } + #endregion ServerLogin class } #endregion SQLDataBase class diff --git a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs index 77e403d..695667b 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.28.1.0")] -[assembly: AssemblyFileVersion("1.28.1.0")] +[assembly: AssemblyVersion("1.29.0.0")] +[assembly: AssemblyFileVersion("1.29.0.0")] -- libgit2 0.21.2