Commit 25232c9b5cc3ad3c77827aa53068cf69fed7385b

Authored by Schwirg László
1 parent f3c3c32f

v1.29.0.0

- DBUser és ServerLogin létrehozás xml-ben definiálható
Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs
@@ -267,6 +267,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS @@ -267,6 +267,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS
267 catch (Exception ex) { ColorConsole.WriteLine(ex.MessageNested(), ConsoleColor.Red); } 267 catch (Exception ex) { ColorConsole.WriteLine(ex.MessageNested(), ConsoleColor.Red); }
268 return o; 268 return o;
269 } 269 }
  270 +
  271 + #region CreateServerLogin
270 private static object CreateServerLogin(object parameter, object o) 272 private static object CreateServerLogin(object parameter, object o)
271 { 273 {
272 var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>(); 274 var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>();
@@ -280,17 +282,54 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS @@ -280,17 +282,54 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS
280 else if (sr.SelectedParameterList?.FirstOrDefault() == null) { return o; } 282 else if (sr.SelectedParameterList?.FirstOrDefault() == null) { return o; }
281 else if (sr.Result == Menu.SelectionResult.Ok) { } 283 else if (sr.Result == Menu.SelectionResult.Ok) { }
282 else { } 284 else { }
283 - string dbusername;  
284 - string password;  
285 - LoginType logintype;  
286 -  
287 var p = sr.SelectedParameterList.FirstOrDefault(); 285 var p = sr.SelectedParameterList.FirstOrDefault();
288 SQLDataBase sqld = p.Parameters as SQLDataBase; 286 SQLDataBase sqld = p.Parameters as SQLDataBase;
289 287
290 - parameterinputloop:  
291 - try 288 + getparametersloop:
  289 + List<SQLDataBase.ServerLogin> ServerLoginsToExecuteList = new List<SQLDataBase.ServerLogin>();
  290 + string selectedserverloginkey = null;
  291 + if (sqld.Xml_SQLServerLoginList.Count == 0)
292 { 292 {
293 - ColorConsole.WriteLine(prefix: $"Enter the parameters for creating server login: {sqld.DBName}. Format:", bracket: "()", text: "LOGINNAME[,PASSWORD][GRANTORUSER[,GRANTORUSERPASSWORD]]", f: ConsoleColor.Yellow); 293 + ColorConsole.WriteLine($"There are no server logins defined for database. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Red);
  294 + }
  295 + else
  296 + {
  297 + string SRVRLogin_db = sqld.DBName;
  298 + ColorConsole.WriteLine();
  299 + ColorConsole.WriteLine($"Select the server login for database. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Yellow);
  300 + var validkeylist = new List<string>();
  301 + foreach (SQLDataBase.ServerLogin srvrlogin in sqld.Xml_SQLServerLoginList)
  302 + {
  303 + ColorConsole.Write($"#{srvrlogin.Key}", ConsoleColor.Yellow, bracket: "[]", suffix: $" [{srvrlogin.LoginName}] login, default database:[{srvrlogin.DefaultDatabase}], grantor user: [{srvrlogin.GrantorUserLoginName}]", prefix: " ");
  304 + ColorConsole.WriteLine();
  305 + validkeylist.Add(srvrlogin.Key);
  306 + }
  307 + validkeylist.Add("*");
  308 + selectedserverloginkey = ColorConsole.ReadLine("Select the server login! [empty]=set manually, [*]=all, [EX]=exit.", ConsoleColor.Yellow, prefix: " ", suffix: " --> ", validitylist: validkeylist);
  309 + if (selectedserverloginkey.ToUpper() == "EX") { return o; }
  310 + else if (selectedserverloginkey == "*")
  311 + {
  312 + foreach (var srvrlogin in sqld.Xml_SQLServerLoginList)
  313 + {
  314 + srvrlogin.Db = SRVRLogin_db;
  315 + string optioncommalist = $"{srvrlogin.LoginName},{srvrlogin.LoginPassword},{srvrlogin.GrantorUserLoginName},{srvrlogin.GrantorUserLoginPassword},{srvrlogin.DefaultDatabase}";
  316 + SQLDataBase.ServerLogin SRVRLogin = CheckParametersXml(sqld.DBName, optioncommalist);
  317 + if (SRVRLogin != null) { ServerLoginsToExecuteList.Add(srvrlogin); }
  318 + }
  319 + }
  320 + else if (!string.IsNullOrWhiteSpace(selectedserverloginkey))
  321 + {
  322 + SQLDataBase.ServerLogin SRVRLogin = sqld.Xml_SQLServerLoginList.FirstOrDefault(t => t.Key == selectedserverloginkey);
  323 + if (SRVRLogin == null) { ColorConsole.WriteLine($"The selected server login key is not valid!. DB Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Red); goto getparametersloop; }
  324 + string optionlist = $"{SRVRLogin.LoginName},{SRVRLogin.LoginPassword},{SRVRLogin.GrantorUserLoginName},{SRVRLogin.GrantorUserLoginPassword},";
  325 + SRVRLogin.Db = SRVRLogin_db;
  326 + ServerLoginsToExecuteList.Add(SRVRLogin);
  327 + }
  328 + }
  329 +
  330 + if (string.IsNullOrWhiteSpace(selectedserverloginkey))
  331 + {
  332 + ColorConsole.WriteLine(prefix: $"Enter the parameters for creating server login: {sqld.DBName}. Format:", bracket: "()", text: "LOGINNAME[,PASSWORD][,GRANTORUSER[,GRANTORUSERPASSWORD]]", f: ConsoleColor.Yellow);
294 ColorConsole.WriteLine(prefix: " ", text: "LOGINNAME", bracket: "", suffix: $": server login name"); 333 ColorConsole.WriteLine(prefix: " ", text: "LOGINNAME", bracket: "", suffix: $": server login name");
295 ColorConsole.WriteLine(prefix: " ", text: "PASSWORD", bracket: "", suffix: $": password for login; empty=windows login is created, non empty=sql login is created"); 334 ColorConsole.WriteLine(prefix: " ", text: "PASSWORD", bracket: "", suffix: $": password for login; empty=windows login is created, non empty=sql login is created");
296 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"); 335 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 @@ -299,122 +338,297 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS
299 var createuseroptions = ColorConsole.ReadLine($"EX=exit.", ConsoleColor.Yellow, suffix: " --> "); 338 var createuseroptions = ColorConsole.ReadLine($"EX=exit.", ConsoleColor.Yellow, suffix: " --> ");
300 if (createuseroptions.ToUpper() == "EX") { return o; } 339 if (createuseroptions.ToUpper() == "EX") { return o; }
301 340
302 - dbusername = null;  
303 - password = null;  
304 - logintype = LoginType.WindowsUser;  
305 341
306 - var optionList = createuseroptions.Split(new char[] { ',' }, StringSplitOptions.None);  
307 - if (optionList.Length < 1) { ColorConsole.WriteLine("ERROR! LOGINNAME is required, PASSWORD is optional", ConsoleColor.Red); goto parameterinputloop; } 342 + SQLDataBase.ServerLogin SRVRLogin = CheckParametersInteractive(sqld.DBName, createuseroptions);
  343 + if (SRVRLogin==null) goto getparametersloop;
  344 + ServerLoginsToExecuteList.Add(SRVRLogin);
  345 + }
308 346
309 - dbusername = optionList[0];  
310 - if (optionList.Length == 2)  
311 - {  
312 - password = optionList[1];  
313 - if (string.IsNullOrWhiteSpace(password)) { ColorConsole.WriteLine("ERROR! PASSWORD, if set, may not be empty!", ConsoleColor.Red); goto parameterinputloop; }  
314 - logintype = LoginType.SqlLogin; 347 + bool allconfirmed = false;
  348 + foreach (var SrvrLoginToExecute in ServerLoginsToExecuteList)
  349 + {
  350 + var retcode = ExecuteOneServerLogin(sqld.SQLCS, SrvrLoginToExecute, allconfirmed);
  351 + if (retcode == "EX") return o;
  352 + else if (retcode == "ALL") { allconfirmed = true; }
  353 + else if (retcode != null) goto getparametersloop;
  354 + }
  355 + return o;
  356 + }
  357 + private static SQLDataBase.ServerLogin CheckParametersInteractive(string dbname,string optioncommalist)
  358 + {
  359 + var optionList = optioncommalist.Split(new char[] { ',' }, StringSplitOptions.None);
  360 + if (optionList.Length < 1) { ColorConsole.WriteLine("ERROR! LOGINNAME is required, PASSWORD is optional", ConsoleColor.Red); return null; }
  361 +
  362 + string SRVRLogin_loginname = optionList[0];
  363 + string SRVRLogin_loginpsw = null;
  364 + string SRVRLogin_grantoruserloginname = null;
  365 + string SRVRLogin_grantoruserpsw = null;
  366 + if (optionList.Length > 3)
  367 + {
  368 + SRVRLogin_grantoruserpsw = optionList[3];
  369 + if (string.IsNullOrWhiteSpace(SRVRLogin_grantoruserpsw)) { ColorConsole.WriteLine("ERROR! GRANTORUSERPASSWORD, if set, may not be empty!", ConsoleColor.Red); return null; }
  370 + }
  371 + if (optionList.Length > 2)
  372 + {
  373 + SRVRLogin_grantoruserloginname = optionList[2];
  374 + if (string.IsNullOrWhiteSpace(SRVRLogin_grantoruserloginname)) { ColorConsole.WriteLine("ERROR! GRANTORUSER, if set, may not be empty!", ConsoleColor.Red); return null; }
  375 + }
  376 + if (optionList.Length > 1)
  377 + {
  378 + SRVRLogin_loginpsw = optionList[1];
  379 + if (string.IsNullOrWhiteSpace(SRVRLogin_loginpsw)) { ColorConsole.WriteLine("ERROR! PASSWORD, if set, may not be empty!", ConsoleColor.Red); return null; }
  380 + }
  381 +
  382 + LoginType SRVRLogin_logintype = LoginType.WindowsUser;
  383 + if (string.IsNullOrWhiteSpace(SRVRLogin_grantoruserloginname) && string.IsNullOrWhiteSpace(SRVRLogin_grantoruserpsw)) { SRVRLogin_logintype = LoginType.SqlLogin; }
  384 +
  385 + bool SRVRLogin_removeaction = false;
  386 + string SRVRLogin_defaultdb = "master";
  387 +
  388 + return new SQLDataBase.ServerLogin()
  389 + {
  390 + Db = dbname
  391 + , LoginName = SRVRLogin_loginname
  392 + , LoginPassword = SRVRLogin_loginpsw
  393 + , LoginType = SRVRLogin_logintype
  394 + , GrantorUserLoginName = SRVRLogin_grantoruserloginname
  395 + , GrantorUserLoginPassword = SRVRLogin_grantoruserpsw
  396 + , DefaultDatabase = SRVRLogin_defaultdb
  397 + , Remove = SRVRLogin_removeaction
  398 + };
  399 + }
  400 + private static SQLDataBase.ServerLogin CheckParametersXml(string dbname, string optioncommalist)
  401 + {
  402 + var optionList = optioncommalist.Split(new char[] { ',' }, StringSplitOptions.None);
  403 + if (optionList.Length < 1) { ColorConsole.WriteLine("ERROR! LOGINNAME is required, PASSWORD is optional", ConsoleColor.Red); return null; }
  404 +
  405 + string SRVRLogin_loginname = optionList[0];
  406 + string SRVRLogin_loginpsw = optionList[1];
  407 + string SRVRLogin_grantoruserloginname = optionList[2];
  408 + string SRVRLogin_grantoruserpsw = optionList[3];
  409 + string SRVRLogin_defaultdb = optionList[4];
  410 +
  411 + if (string.IsNullOrWhiteSpace(SRVRLogin_loginname)) { ColorConsole.WriteLine("ERROR! LOGIN NAME is mandatory!", ConsoleColor.Red); return null; }
  412 + if (string.IsNullOrWhiteSpace(SRVRLogin_loginpsw)) { ColorConsole.WriteLine("ERROR! LOGIN PASSWORD is mandatory!", ConsoleColor.Red); return null; }
  413 + if (!string.IsNullOrWhiteSpace(SRVRLogin_grantoruserloginname) && string.IsNullOrWhiteSpace(SRVRLogin_grantoruserpsw) || string.IsNullOrWhiteSpace(SRVRLogin_grantoruserloginname) && !string.IsNullOrWhiteSpace(SRVRLogin_grantoruserpsw))
  414 + {
  415 + ColorConsole.WriteLine("ERROR! GRANTORUSER/GRANTORUSERPASSWORD, both must be empty or both must be non-empty!", ConsoleColor.Red); return null;
  416 + }
  417 + if (string.IsNullOrWhiteSpace(SRVRLogin_defaultdb)) { SRVRLogin_defaultdb="master"; }
  418 +
  419 + LoginType SRVRLogin_logintype = LoginType.WindowsUser;
  420 + if (string.IsNullOrWhiteSpace(SRVRLogin_grantoruserloginname) && string.IsNullOrWhiteSpace(SRVRLogin_grantoruserpsw)) { SRVRLogin_logintype = LoginType.SqlLogin; }
  421 +
  422 + bool SRVRLogin_removeaction = false;
  423 +
  424 + return new SQLDataBase.ServerLogin()
  425 + {
  426 + Db = dbname
  427 + , LoginName = SRVRLogin_loginname
  428 + , LoginPassword = SRVRLogin_loginpsw
  429 + , LoginType = SRVRLogin_logintype
  430 + , GrantorUserLoginName = SRVRLogin_grantoruserloginname
  431 + , GrantorUserLoginPassword = SRVRLogin_grantoruserpsw
  432 + , DefaultDatabase = SRVRLogin_defaultdb
  433 + , Remove = SRVRLogin_removeaction
  434 + };
  435 + }
  436 + private static string ExecuteOneServerLogin(string sqlcs, SQLDataBase.ServerLogin srvrlogintoexecute, bool allconfirmed)
  437 + {
  438 + string returntext = null;
  439 + try
  440 + {
  441 + string removeactionText = srvrlogintoexecute.Remove ? "REMOVE" : "CREATE";
  442 + string passwordtext = srvrlogintoexecute.LoginType == LoginType.WindowsUser ? "" : $", password:{srvrlogintoexecute.LoginPassword}";
  443 + string SRVRLogin_fulldesignation = $"db={srvrlogintoexecute.Db},login name={srvrlogintoexecute.LoginName},login type: {srvrlogintoexecute.LoginType}{passwordtext}";
  444 + if (!allconfirmed)
  445 + {
  446 + ColorConsole.WriteLine();
  447 + ColorConsole.WriteLine(prefix: $"Action to execute:{removeactionText}.", text: $" {SRVRLogin_fulldesignation}.", bracket: "", suffix: $"");
  448 + var confirm = ColorConsole.ReadLine($"Enter CONFIRM to confirm, ALL to confirm all, SKIP to continue.", ConsoleColor.Yellow, suffix: "");
  449 + if (confirm.ToUpper() == "EX") { return "EX"; }
  450 + else if (confirm.ToUpper() == "CONFIRM") { returntext = null; }
  451 + else if (confirm.ToUpper() == "ALL") { returntext = "ALL"; }
  452 + else { return null; }
315 } 453 }
316 - string grantoruser = null;  
317 - string grantoruserpsw = null;  
318 - if (optionList.Length > 2) 454 +
  455 + if (!srvrlogintoexecute.Remove)
319 { 456 {
320 - grantoruser = optionList[2];  
321 - if (string.IsNullOrWhiteSpace(grantoruser)) { ColorConsole.WriteLine("ERROR! GRANTORUSER, if set, may not be empty!", ConsoleColor.Red); goto parameterinputloop; } 457 + SQLDataBaseManagerCore.CreateLogin(sqlcs, srvrlogintoexecute.LoginName, srvrlogintoexecute.LoginPassword, srvrlogintoexecute.DefaultDatabase, srvrlogintoexecute.LoginType, null, srvrlogintoexecute.GrantorUserLoginName, srvrlogintoexecute.GrantorUserLoginPassword);
  458 + ColorConsole.WriteLine($"SUCCESS! {removeactionText} server login: {SRVRLogin_fulldesignation}", ConsoleColor.Green);
322 } 459 }
323 - if (optionList.Length > 3) 460 + else
324 { 461 {
325 - grantoruserpsw = optionList[3];  
326 - if (string.IsNullOrWhiteSpace(grantoruser)) { ColorConsole.WriteLine("ERROR! GRANTORUSERPASSWORD, if set, may not be empty!", ConsoleColor.Red); goto parameterinputloop; } 462 + ColorConsole.WriteLine($"Function remove DB user is not yet implemented!", ConsoleColor.Red);
327 } 463 }
328 -  
329 - SQLDataBaseManagerCore.CreateLogin(sqld.SQLCS, dbusername, password, "master", logintype, null,grantoruser,grantoruserpsw);  
330 - string passwordtext = logintype == LoginType.WindowsUser ? "" : $", password:{password}";  
331 - ColorConsole.WriteLine($"Server login created. Server login name:{dbusername}, login type: {logintype}{passwordtext}.", ConsoleColor.Green);  
332 } 464 }
333 - catch (Exception ex)  
334 - {  
335 - ColorConsole.WriteLine(ex.MessageNested(), ConsoleColor.Red);  
336 - goto parameterinputloop;  
337 - }  
338 - goto parameterinputloop;  
339 - 465 + catch (ApplicationException e) { ColorConsole.WriteLine("FATAL ERROR! in script parameter substitution!", ConsoleColor.Red); returntext = null; }
  466 + catch (Exception e) { ColorConsole.WriteLine("FATAL ERROR! " + e.MessageNested(), ConsoleColor.Red); returntext = null; }
  467 + return returntext;
340 } 468 }
  469 + #endregion CreateServerLogin
  470 +
  471 + #region CreateDBUser
341 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", }; 472 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", };
342 private static string[] fullaccessrolelist = new string[] { "db_accessadmin", "db_backupoperator", "db_datareader", "db_datawriter", "db_ddladmin", "db_owner", "db_securityadmin", }; 473 private static string[] fullaccessrolelist = new string[] { "db_accessadmin", "db_backupoperator", "db_datareader", "db_datawriter", "db_ddladmin", "db_owner", "db_securityadmin", };
343 - private static string[] datareaderrolelist = new string[] { "db_datareader", "db_denydatareader", }; 474 + private static string[] datareaderrolelist = new string[] { "db_datareader", };
344 private static string[] loginrolelist = new string[] { "dbcreator", "securityadmin", }; 475 private static string[] loginrolelist = new string[] { "dbcreator", "securityadmin", };
  476 + private const string COMMA = ",";
345 private static object CreateDBUser(object parameter, object o) 477 private static object CreateDBUser(object parameter, object o)
346 { 478 {
347 - const string COMMA = ",";  
348 var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>(); 479 var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>();
349 var args = (parameter as Menu.ExecutorParameter).Args; 480 var args = (parameter as Menu.ExecutorParameter).Args;
350 - var selectedtaskindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.ScheduledTaskManager.Function.CMD_TASKS); 481 + var selectedsqldbindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.SQLDataBaseManager.Function.CMD_DATABASES);
351 var menuofdbs = DisplaySQLDataBaseMenu(config, $"Select the SQL database(s) to manage with function '{nameof(CreateDBUser)}'!", silent: true, selectionmode: Menu.SelectionMode.Single); 482 var menuofdbs = DisplaySQLDataBaseMenu(config, $"Select the SQL database(s) to manage with function '{nameof(CreateDBUser)}'!", silent: true, selectionmode: Menu.SelectionMode.Single);
352 - Menu.Selection sr = menuofdbs.Select(selectedtaskindexes); 483 + Menu.Selection sr = menuofdbs.Select(selectedsqldbindexes);
353 if (sr.Result == Menu.SelectionResult.Exit) { return o; } 484 if (sr.Result == Menu.SelectionResult.Exit) { return o; }
354 else if (sr.Result == Menu.SelectionResult.None) { return o; } 485 else if (sr.Result == Menu.SelectionResult.None) { return o; }
355 else if (sr.Result == Menu.SelectionResult.Error) { return o; } 486 else if (sr.Result == Menu.SelectionResult.Error) { return o; }
356 else if (sr.SelectedParameterList?.FirstOrDefault() == null) { return o; } 487 else if (sr.SelectedParameterList?.FirstOrDefault() == null) { return o; }
357 else if (sr.Result == Menu.SelectionResult.Ok) { } 488 else if (sr.Result == Menu.SelectionResult.Ok) { }
358 else { } 489 else { }
359 - string dbusername = null;  
360 - string loginname = null;  
361 - string rolenamecommalist = null; 490 + string DBUser_UserName = null;
  491 + string DBUser_LoginName = null;
  492 + string DBUser_RoleNameCommaList = null;
362 493
363 var p = sr.SelectedParameterList.FirstOrDefault(); 494 var p = sr.SelectedParameterList.FirstOrDefault();
364 SQLDataBase sqld = p.Parameters as SQLDataBase; 495 SQLDataBase sqld = p.Parameters as SQLDataBase;
365 496
366 - parameterinputloop:  
367 - try 497 + getparametersloop:
  498 + List<SQLDataBase.DBUser> DbusrToExecuteList = new List<SQLDataBase.DBUser>();
  499 + string selecteddbusrkey = null;
  500 + if (sqld.Xml_SQLDbUserList.Count == 0)
  501 + {
  502 + ColorConsole.WriteLine($"There are no DBUsers defined for database. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Red);
  503 + }
  504 + else
  505 + {
  506 + string DBUser_db = sqld.DBName;
  507 + ColorConsole.WriteLine();
  508 + ColorConsole.WriteLine($"Select the DBUser for database. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Yellow);
  509 + var validkeylist = new List<string>();
  510 + foreach (SQLDataBase.DBUser dbusr in sqld.Xml_SQLDbUserList)
  511 + {
  512 + ColorConsole.Write($"#{dbusr.Key}", ConsoleColor.Yellow, bracket: "[]", suffix: $" [{dbusr.UserName}] database user for login [{dbusr.LoginName}]", prefix: " ");
  513 + ColorConsole.WriteLine();
  514 + validkeylist.Add(dbusr.Key);
  515 + }
  516 + validkeylist.Add("*");
  517 + selecteddbusrkey = ColorConsole.ReadLine("Select the database user! [empty]=set manually, [*]=all, [EX]=exit.", ConsoleColor.Yellow, prefix: " ", suffix: " --> ", validitylist: validkeylist);
  518 + if (selecteddbusrkey.ToUpper() == "EX") { return o; }
  519 + else if (selecteddbusrkey == "*")
  520 + {
  521 + foreach (var dbusr in sqld.Xml_SQLDbUserList)
  522 + {
  523 + dbusr.Db = DBUser_db;
  524 + dbusr.RoleNameCommaList = ResolvePredefinedRoles(dbusr.RoleNameCommaList);
  525 + if (!string.IsNullOrWhiteSpace(dbusr.RoleNameCommaList)) { DbusrToExecuteList.Add(dbusr); }
  526 + }
  527 + }
  528 + else if (!string.IsNullOrWhiteSpace(selecteddbusrkey))
  529 + {
  530 + SQLDataBase.DBUser DBUser = sqld.Xml_SQLDbUserList.FirstOrDefault(t => t.Key == selecteddbusrkey);
  531 + if (DBUser == null) { ColorConsole.WriteLine($"The selected dbuser key is not valid!. DB Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Red); goto getparametersloop; }
  532 + DBUser.Db = DBUser_db;
  533 + DbusrToExecuteList.Add(DBUser);
  534 + }
  535 + }
  536 +
  537 + if (string.IsNullOrWhiteSpace(selecteddbusrkey))
368 { 538 {
369 ColorConsole.WriteLine(prefix: $"Enter the parameters for creating DB user for: {sqld.DBName}. Format:", bracket: "()", text: "DBUSERNAME,SERVERLOGINNAME,ROLENAME,ROLENAME,ROLENAME...", f: ConsoleColor.Yellow); 539 ColorConsole.WriteLine(prefix: $"Enter the parameters for creating DB user for: {sqld.DBName}. Format:", bracket: "()", text: "DBUSERNAME,SERVERLOGINNAME,ROLENAME,ROLENAME,ROLENAME...", f: ConsoleColor.Yellow);
370 ColorConsole.WriteLine(prefix: " ", text: "DBUSERNAME", bracket: "", suffix: $": dbusername."); 540 ColorConsole.WriteLine(prefix: " ", text: "DBUSERNAME", bracket: "", suffix: $": dbusername.");
371 - 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)."); 541 + 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).");
372 ColorConsole.WriteLine(prefix: " ", text: "ROLENAME", bracket: "", suffix: $": One of these->" + string.Join(COMMA, selectablerolelist)); 542 ColorConsole.WriteLine(prefix: " ", text: "ROLENAME", bracket: "", suffix: $": One of these->" + string.Join(COMMA, selectablerolelist));
373 543
374 var createuseroptions = ColorConsole.ReadLine($"EX=exit.", ConsoleColor.Yellow, suffix: " --> "); 544 var createuseroptions = ColorConsole.ReadLine($"EX=exit.", ConsoleColor.Yellow, suffix: " --> ");
375 if (createuseroptions.ToUpper() == "EX") { return o; } 545 if (createuseroptions.ToUpper() == "EX") { return o; }
376 546
377 - dbusername = null;  
378 - loginname = null;  
379 - rolenamecommalist = null; 547 + DBUser_UserName = null;
  548 + DBUser_LoginName = null;
  549 + DBUser_RoleNameCommaList = null;
380 var optionList = createuseroptions.Split(new char[] { ',' }, StringSplitOptions.None); 550 var optionList = createuseroptions.Split(new char[] { ',' }, StringSplitOptions.None);
381 - if (optionList.Length < 3) { ColorConsole.WriteLine("ERROR! DBUSERNAME,SERVERLOGINNAME and at least one ROLENAME are required", ConsoleColor.Red); goto parameterinputloop; } 551 + if (optionList.Length < 3) { ColorConsole.WriteLine("ERROR! DBUSERNAME,SERVERLOGINNAME and at least one ROLENAME are required", ConsoleColor.Red); goto getparametersloop; }
382 //012345678 552 //012345678
383 //uuu,ppp,r1,r2,r3 553 //uuu,ppp,r1,r2,r3
384 - dbusername = optionList[0];  
385 - loginname = optionList[1];  
386 - var selectedrolelist = optionList.Skip(2).ToArray();  
387 - List<string> badrolenames = new List<string>();  
388 - bool enablefullaccess = false;  
389 - bool enabledatareader = false;  
390 - int selectedrolelistnum = 0;  
391 - foreach (var rolename in selectedrolelist)  
392 - {  
393 - selectedrolelistnum++;  
394 - enablefullaccess = enablefullaccess || rolename.ToUpper() == "FULLACCESS";  
395 - enabledatareader = enabledatareader || rolename.ToUpper() == "DATAREADER";  
396 - if (!selectablerolelist.Contains(rolename)) { badrolenames.Add(rolename); }  
397 - }  
398 - bool specialselectionactive = enablefullaccess || enabledatareader;  
399 - if (selectedrolelistnum > 1 && specialselectionactive) { ColorConsole.WriteLine($"ERROR! FULLACCESS or DATAREADER has to be selected alone!", ConsoleColor.Red); goto parameterinputloop; }  
400 - if (badrolenames.Count > 0) { ColorConsole.WriteLine($"ERROR! {string.Join(COMMA, badrolenames)} are not available!", ConsoleColor.Red); goto parameterinputloop; } 554 + DBUser_UserName = optionList[0];
  555 + DBUser_LoginName = optionList[1];
  556 + DBUser_RoleNameCommaList = ResolvePredefinedRoles(string.Join(COMMA, optionList.Skip(2)));
  557 + if (string.IsNullOrWhiteSpace(DBUser_RoleNameCommaList)) goto getparametersloop;
  558 + bool DBUser_Removeaction = false;
401 559
402 - var effectiverolelist =  
403 - enablefullaccess ? fullaccessrolelist  
404 - : enabledatareader ? datareaderrolelist  
405 - : selectedrolelist;  
406 - rolenamecommalist = string.Join(",", effectiverolelist); 560 + SQLDataBase.DBUser DBUser = new SQLDataBase.DBUser() { Db = sqld.DBName, UserName = DBUser_UserName, LoginName = DBUser_LoginName, RoleNameCommaList = DBUser_RoleNameCommaList, Remove = DBUser_Removeaction, };
  561 + DbusrToExecuteList.Add(DBUser);
  562 + }
407 563
408 - SQLDataBaseManagerCore.CreateUser(sqld.SQLCS, dbusername, rolenamecommalist, loginname);  
409 - ColorConsole.WriteLine($"DB user created. DB name:{sqld.DBName}, DB username:{dbusername}, rolelist={rolenamecommalist}.", ConsoleColor.Green); 564 + bool allconfirmed = false;
  565 + foreach (var DbusrToExecute in DbusrToExecuteList)
  566 + {
  567 + var retcode = ExecuteOneDBUser(sqld.SQLCS, DbusrToExecute, allconfirmed);
  568 + if (retcode == "EX") return o;
  569 + else if (retcode == "ALL") { allconfirmed = true; }
  570 + else if (retcode != null) goto getparametersloop;
410 } 571 }
411 - catch (Exception ex) 572 + return o;
  573 + }
  574 + private static string ResolvePredefinedRoles(string selectedrolecommalist)
  575 + {
  576 + var selectedrolelist = selectedrolecommalist.Split(',', ';');
  577 + List<string> badrolenames = new List<string>();
  578 + bool enablefullaccess = false;
  579 + bool enabledatareader = false;
  580 + int selectedrolelistnum = 0;
  581 + foreach (var rolename in selectedrolelist)
  582 + {
  583 + selectedrolelistnum++;
  584 + enablefullaccess = enablefullaccess || rolename.ToUpper() == "FULLACCESS";
  585 + enabledatareader = enabledatareader || rolename.ToUpper() == "DATAREADER";
  586 + if (!selectablerolelist.Contains(rolename)) { badrolenames.Add(rolename); }
  587 + }
  588 + bool specialselectionactive = enablefullaccess || enabledatareader;
  589 + if (selectedrolelistnum > 1 && specialselectionactive) { ColorConsole.WriteLine($"ERROR! FULLACCESS or DATAREADER has to be selected alone!", ConsoleColor.Red); return null; }
  590 + if (badrolenames.Count > 0) { ColorConsole.WriteLine($"ERROR! {string.Join(COMMA, badrolenames)} are not available!", ConsoleColor.Red); return null; }
  591 + var effectiverolelist =
  592 + enablefullaccess ? fullaccessrolelist
  593 + : enabledatareader ? datareaderrolelist
  594 + : selectedrolelist;
  595 + return string.Join(",", effectiverolelist);
  596 + }
  597 + private static string ExecuteOneDBUser(string sqlcs, SQLDataBase.DBUser dbusertoexecute, bool allconfirmed)
  598 + {
  599 + string returntext = null;
  600 + try
412 { 601 {
413 - ColorConsole.WriteLine(ex.MessageNested(), ConsoleColor.Red);  
414 - goto parameterinputloop; 602 + string removeactionText = dbusertoexecute.Remove ? "REMOVE" : "CREATE";
  603 + string DBUser_fulldesignation = $"db={dbusertoexecute.Db},username={dbusertoexecute.UserName},login name={dbusertoexecute.LoginName},role list={dbusertoexecute.RoleNameCommaList}";
  604 + if (!allconfirmed)
  605 + {
  606 + ColorConsole.WriteLine();
  607 + ColorConsole.WriteLine(prefix: $"Action to execute:{removeactionText}.", text: $" {DBUser_fulldesignation}.", bracket: "", suffix: $"");
  608 + var confirm = ColorConsole.ReadLine($"Enter CONFIRM to confirm, ALL to confirm all, SKIP to continue.", ConsoleColor.Yellow, suffix: "");
  609 + if (confirm.ToUpper() == "EX") { return "EX"; }
  610 + else if (confirm.ToUpper() == "CONFIRM") { returntext = null; }
  611 + else if (confirm.ToUpper() == "ALL") { returntext = "ALL"; }
  612 + else { return null; }
  613 + }
  614 +
  615 + if (!dbusertoexecute.Remove)
  616 + {
  617 + SQLDataBaseManagerCore.CreateUser(sqlcs, dbusertoexecute.UserName, dbusertoexecute.RoleNameCommaList, dbusertoexecute.LoginName);
  618 + ColorConsole.WriteLine($"SUCCESS! {removeactionText} database user: {DBUser_fulldesignation}", ConsoleColor.Green);
  619 + }
  620 + else
  621 + {
  622 + ColorConsole.WriteLine($"Function remove DB user is not yet implemented!", ConsoleColor.Red);
  623 + }
415 } 624 }
416 - goto parameterinputloop; 625 + catch (ApplicationException e) { ColorConsole.WriteLine("FATAL ERROR! in script parameter substitution!", ConsoleColor.Red); returntext = null; }
  626 + catch (Exception e) { ColorConsole.WriteLine("FATAL ERROR! " + e.MessageNested(), ConsoleColor.Red); returntext = null; }
  627 + return returntext;
417 } 628 }
  629 + #endregion CreateDBUser
  630 +
  631 + #region MoveDbToRemoteServer
418 private static object MoveDbToRemoteServer(object parameter, object o) 632 private static object MoveDbToRemoteServer(object parameter, object o)
419 { 633 {
420 var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>(); 634 var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>();
@@ -510,7 +724,9 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS @@ -510,7 +724,9 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS
510 ColorConsole.WriteLine($"Moving DB to remote server completed. Required time: {(int)(DateTime.Now.Subtract(DateTimeNow).TotalSeconds)} seconds.",ConsoleColor.Green); 724 ColorConsole.WriteLine($"Moving DB to remote server completed. Required time: {(int)(DateTime.Now.Subtract(DateTimeNow).TotalSeconds)} seconds.",ConsoleColor.Green);
511 return o; 725 return o;
512 } 726 }
  727 + #endregion MoveDbToRemoteServer
513 728
  729 + #region ManageLastUpdatedTrigger
514 private static object ManageLastUpdatedTrigger(object parameter, object o) { return _ManageLastUpdatedTrigger(parameter, o, null); } 730 private static object ManageLastUpdatedTrigger(object parameter, object o) { return _ManageLastUpdatedTrigger(parameter, o, null); }
515 #region script texts for _RemoveAndCreateLastUpdatedTrigger 731 #region script texts for _RemoveAndCreateLastUpdatedTrigger
516 const string Header_Script = 732 const string Header_Script =
@@ -569,9 +785,9 @@ GO @@ -569,9 +785,9 @@ GO
569 { 785 {
570 var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>(); 786 var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>();
571 var args = (parameter as Menu.ExecutorParameter).Args; 787 var args = (parameter as Menu.ExecutorParameter).Args;
572 - var selectedtaskindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.ScheduledTaskManager.Function.CMD_TASKS); 788 + var selectedsqldbindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.SQLDataBaseManager.Function.CMD_DATABASES);
573 var menufolders = DisplaySQLDataBaseMenu(config, $"Select the SQL database(s) to manage with function '{nameof(ManageLastUpdatedTrigger)}'!", silent: true); 789 var menufolders = DisplaySQLDataBaseMenu(config, $"Select the SQL database(s) to manage with function '{nameof(ManageLastUpdatedTrigger)}'!", silent: true);
574 - Menu.Selection sr = menufolders.Select(selectedtaskindexes); 790 + Menu.Selection sr = menufolders.Select(selectedsqldbindexes);
575 if (sr.Result == Menu.SelectionResult.Exit) { return o; } 791 if (sr.Result == Menu.SelectionResult.Exit) { return o; }
576 else if (sr.Result == Menu.SelectionResult.None) { return o; } 792 else if (sr.Result == Menu.SelectionResult.None) { return o; }
577 else if (sr.Result == Menu.SelectionResult.Error) { return o; } 793 else if (sr.Result == Menu.SelectionResult.Error) { return o; }
@@ -581,13 +797,57 @@ GO @@ -581,13 +797,57 @@ GO
581 if (p == null) { return o; } 797 if (p == null) { return o; }
582 SQLDataBase sqld = p.Parameters as SQLDataBase; 798 SQLDataBase sqld = p.Parameters as SQLDataBase;
583 799
584 - var manageaction = !forceremoveonly.HasValue;  
585 - string actiontext = manageaction ? "MANAGING": (forceremoveonly.Value ? "REMOVING" : "CREATING");  
586 - getparameters:;  
587 - try 800 + getparametersloop:;
  801 +
  802 + List<SQLDataBase.LastUpdatedTrigger> TriggerToExecuteList = new List<SQLDataBase.LastUpdatedTrigger>();
  803 + string selectedtrgkey =null;
  804 + if (sqld.Xml_SQLLastUpdatedTriggerList.Count == 0)
  805 + {
  806 + ColorConsole.WriteLine($"There are no LastUpdated triggers defined for database. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Red);
  807 + }
  808 + else
  809 + {
  810 + string LastUpdatedTrigger_db = sqld.DBName;
  811 + ColorConsole.WriteLine();
  812 + ColorConsole.WriteLine($"Select the LastUpdated triggers for database. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Yellow);
  813 + var validkeylist = new List<string>();
  814 + foreach (SQLDataBase.LastUpdatedTrigger trg in sqld.Xml_SQLLastUpdatedTriggerList)
  815 + {
  816 + ColorConsole.Write($"#{trg.Key}", ConsoleColor.Yellow, bracket: "[]", suffix: $" [{trg.Column}] column in table [{trg.Schema}].[{trg.Table}]", prefix: " ");
  817 + ColorConsole.WriteLine();
  818 + validkeylist.Add(trg.Key);
  819 + }
  820 + validkeylist.Add("*");
  821 + selectedtrgkey = ColorConsole.ReadLine("Select the trigger! [empty]=set manually, [*]=all, [EX]=exit.", ConsoleColor.Yellow, prefix: " ", suffix: " --> ", validitylist: validkeylist);
  822 + if (selectedtrgkey.ToUpper() == "EX") { return o; }
  823 + else if (selectedtrgkey == "*")
  824 + {
  825 + foreach (var trg in sqld.Xml_SQLLastUpdatedTriggerList)
  826 + {
  827 + trg.Db = LastUpdatedTrigger_db;
  828 + TriggerToExecuteList.Add(trg);
  829 + }
  830 + }
  831 + else if(!string.IsNullOrWhiteSpace(selectedtrgkey))
  832 + {
  833 + SQLDataBase.LastUpdatedTrigger LastUpdatedTrigger = sqld.Xml_SQLLastUpdatedTriggerList.FirstOrDefault(t => t.Key == selectedtrgkey);
  834 + if (LastUpdatedTrigger == null) { ColorConsole.WriteLine($"The selected trigger is not valid!. Name:{sqld.DBName}, server: {sqld.DataSource}", ConsoleColor.Red); goto getparametersloop; }
  835 + LastUpdatedTrigger.Db = LastUpdatedTrigger_db;
  836 + TriggerToExecuteList.Add(LastUpdatedTrigger);
  837 + }
  838 + }
  839 +
  840 + if (string.IsNullOrWhiteSpace(selectedtrgkey))
588 { 841 {
  842 + string LastUpdatedTrigger_db = sqld.DBName;
  843 + string LastUpdatedTrigger_schema = "dbo";
  844 + string LastUpdatedTrigger_table = null;
  845 + string LastUpdatedTrigger_column = "";
  846 + bool LastUpdatedTrigger_removeaction = false;
  847 + var manageaction = !forceremoveonly.HasValue;
  848 + string actiontext = manageaction ? "MANAGING" : (forceremoveonly.Value ? "REMOVING" : "CREATING");
589 ColorConsole.WriteLine(prefix: $" Enter the parameters for {actiontext} LastUpdated trigger.", bracket: "", text: "", f: ConsoleColor.Yellow); 849 ColorConsole.WriteLine(prefix: $" Enter the parameters for {actiontext} LastUpdated trigger.", bracket: "", text: "", f: ConsoleColor.Yellow);
590 - string formattext = (manageaction?"[-]":"")+ "[SCHEMA.]TABLE[,COLUMN]"; 850 + string formattext = (manageaction ? "[-]" : "") + "[SCHEMA.]TABLE[,COLUMN]";
591 ColorConsole.WriteLine(prefix: $" Format:", bracket: "", text: formattext, f: ConsoleColor.Yellow); 851 ColorConsole.WriteLine(prefix: $" Format:", bracket: "", text: formattext, f: ConsoleColor.Yellow);
592 852
593 ColorConsole.WriteLine(prefix: " ", text: "TABLE", bracket: "", suffix: $": MANDATORY name of the table of the trigger (example:ProductionPeriods)"); 853 ColorConsole.WriteLine(prefix: " ", text: "TABLE", bracket: "", suffix: $": MANDATORY name of the table of the trigger (example:ProductionPeriods)");
@@ -597,74 +857,90 @@ GO @@ -597,74 +857,90 @@ GO
597 857
598 var createtriggerparameters = ColorConsole.ReadLine($"", ConsoleColor.Yellow, suffix: ""); 858 var createtriggerparameters = ColorConsole.ReadLine($"", ConsoleColor.Yellow, suffix: "");
599 if (createtriggerparameters.ToUpper() == "EX") { return o; } 859 if (createtriggerparameters.ToUpper() == "EX") { return o; }
600 - if (string.IsNullOrWhiteSpace(createtriggerparameters)) { goto getparameters; } 860 + if (string.IsNullOrWhiteSpace(createtriggerparameters)) { goto getparametersloop; }
601 861
602 bool removeactionselected = createtriggerparameters.Substring(1) == "-"; 862 bool removeactionselected = createtriggerparameters.Substring(1) == "-";
603 - bool effectiveremoveaction = manageaction && removeactionselected || !manageaction && forceremoveonly.Value;  
604 - string effectiveactiontext = effectiveremoveaction ? "REMOVE" : "CREATE";  
605 - createtriggerparameters = createtriggerparameters.Substring(0,1) == "-" ?createtriggerparameters.Substring(1): createtriggerparameters; 863 + LastUpdatedTrigger_removeaction = manageaction && removeactionselected || !manageaction && forceremoveonly.Value;
  864 + createtriggerparameters = createtriggerparameters.Substring(0, 1) == "-" ? createtriggerparameters.Substring(1) : createtriggerparameters;
606 var optionList = createtriggerparameters.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); 865 var optionList = createtriggerparameters.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
607 866
608 - string db = sqld.DBName;  
609 string tablewithschema = ""; 867 string tablewithschema = "";
610 - string column = "";  
611 if (optionList.Length == 1) { tablewithschema = optionList[0]; } 868 if (optionList.Length == 1) { tablewithschema = optionList[0]; }
612 - else if (optionList.Length == 2) { tablewithschema = optionList[0]; column = optionList[1]; }  
613 - else /*if (optionList.Length > 2)*/ { ColorConsole.WriteLine("ERROR! No more parameters required beside SCHEMA, TABLE and COLUMN!", ConsoleColor.Red); goto getparameters; } 869 + else if (optionList.Length == 2) { tablewithschema = optionList[0]; LastUpdatedTrigger_column = optionList[1]; }
  870 + else /*if (optionList.Length > 2)*/ { ColorConsole.WriteLine("ERROR! No more parameters required beside SCHEMA, TABLE and COLUMN!", ConsoleColor.Red); goto getparametersloop; }
614 871
615 - if (string.IsNullOrWhiteSpace(column)) column = "LastUpdated"; 872 + if (string.IsNullOrWhiteSpace(LastUpdatedTrigger_column)) LastUpdatedTrigger_column = "LastUpdated";
616 873
617 - if (string.IsNullOrWhiteSpace(db)) { ColorConsole.WriteLine("ERROR! DATABASE can not be empty!", ConsoleColor.Red); goto getparameters; }  
618 - if (string.IsNullOrWhiteSpace(tablewithschema)) { ColorConsole.WriteLine("ERROR! [SCHEMA.]TABLE can not be empty!", ConsoleColor.Red); goto getparameters; } 874 + if (string.IsNullOrWhiteSpace(LastUpdatedTrigger_db)) { ColorConsole.WriteLine("ERROR! DATABASE can not be empty!", ConsoleColor.Red); goto getparametersloop; }
  875 + if (string.IsNullOrWhiteSpace(tablewithschema)) { ColorConsole.WriteLine("ERROR! [SCHEMA.]TABLE can not be empty!", ConsoleColor.Red); goto getparametersloop; }
619 var tablewithschemasplitted = tablewithschema.Split('.'); 876 var tablewithschemasplitted = tablewithschema.Split('.');
620 - if (tablewithschemasplitted.Length != 1 && tablewithschemasplitted.Length != 2) { ColorConsole.WriteLine("ERROR! [SCHEMA.]TABLE incorrect!", ConsoleColor.Red); goto getparameters; }  
621 - if (string.IsNullOrWhiteSpace(column)) { ColorConsole.WriteLine("ERROR! COLUMN can not be empty!", ConsoleColor.Red); goto getparameters; }  
622 -  
623 - string schema = "dbo";  
624 - string table = tablewithschema;  
625 - if (tablewithschemasplitted.Length == 2) { schema = tablewithschemasplitted[0]; table = tablewithschemasplitted[1]; }  
626 - string triggername = $"TRG_LASTUPDATETS_{db}_{schema}_{table}_{column}";  
627 - string parameters = $"DBNAME={db};SCHEMA={schema};TABLE={table};COLUMN={column};TRIGGER={triggername};";  
628 - string ssScriptText = null; 877 + if (tablewithschemasplitted.Length != 1 && tablewithschemasplitted.Length != 2) { ColorConsole.WriteLine("ERROR! [SCHEMA.]TABLE incorrect!", ConsoleColor.Red); goto getparametersloop; }
  878 + if (string.IsNullOrWhiteSpace(LastUpdatedTrigger_column)) { ColorConsole.WriteLine("ERROR! COLUMN can not be empty!", ConsoleColor.Red); goto getparametersloop; }
629 879
630 - string triggerfulldesignation = $"db={db},table={schema}.{table},column={column},trigger={triggername}";  
631 - ColorConsole.WriteLine(prefix: $"Action to execute:{effectiveactiontext}.", text: $" {triggerfulldesignation}.", bracket: "", suffix: $"");  
632 - var confirm = ColorConsole.ReadLine($"Enter CONFIRM to confirm, NO to continue.", ConsoleColor.Yellow, suffix: "");  
633 - if (createtriggerparameters.ToUpper() == "EX") { return o; }  
634 - if (confirm.ToUpper() != "CONFIRM") { goto getparameters; } 880 + LastUpdatedTrigger_table = tablewithschema;
  881 + if (tablewithschemasplitted.Length == 2) { LastUpdatedTrigger_schema = tablewithschemasplitted[0]; LastUpdatedTrigger_table = tablewithschemasplitted[1]; }
  882 +
  883 + SQLDataBase.LastUpdatedTrigger LastUpdatedTrigger = new SQLDataBase.LastUpdatedTrigger() {Db= LastUpdatedTrigger_db, Schema= LastUpdatedTrigger_schema,Table= LastUpdatedTrigger_table,Column= LastUpdatedTrigger_column , Remove= LastUpdatedTrigger_removeaction ,};
  884 + TriggerToExecuteList.Add(LastUpdatedTrigger);
  885 + }
635 886
636 - if (!Tools.ResolveArguments(parameters, RemoveLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); }  
637 - try {SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null); }  
638 - catch (Exception e) {ColorConsole.WriteLine(e.MessageNested(), ConsoleColor.Yellow);}  
639 - if (!Tools.ResolveArguments(parameters, RemoveLastUpdatedColumn_Script, out ssScriptText)) { throw new ApplicationException(); }  
640 - try {SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null);} 887 + bool allconfirmed = false;
  888 + foreach (var triggertoexecute in TriggerToExecuteList)
  889 + {
  890 + var retcode = ExecuteOneLastUpdateTrigger(sqld.SQLCS, triggertoexecute, allconfirmed);
  891 + if (retcode == "EX") return o;
  892 + else if (retcode == "ALL") { allconfirmed = true; }
  893 + else if (retcode != null) goto getparametersloop;
  894 + }
  895 + return o;
  896 + }
  897 +
  898 + private static string ExecuteOneLastUpdateTrigger(string sqlcs,SQLDataBase.LastUpdatedTrigger triggertoexecute, bool allconfirmed)
  899 + {
  900 + string returntext = null;
  901 + try
  902 + {
  903 + string removeactionText = triggertoexecute.Remove ? "REMOVE" : "CREATE";
  904 + string LastUpdatedTrigger_triggername = $"TRG_LASTUPDATETS_{triggertoexecute.Db}_{triggertoexecute.Schema}_{triggertoexecute.Table}_{triggertoexecute.Column}";
  905 + string LastUpdatedTrigger_fulldesignation = $"db={triggertoexecute.Db},table={triggertoexecute.Schema}.{triggertoexecute.Table},column={triggertoexecute.Column},trigger={LastUpdatedTrigger_triggername}";
  906 + if (!allconfirmed)
  907 + {
  908 + ColorConsole.WriteLine();
  909 + ColorConsole.WriteLine(prefix: $"Action to execute:{removeactionText}.", text: $" {LastUpdatedTrigger_fulldesignation}.", bracket: "", suffix: $"");
  910 + var confirm = ColorConsole.ReadLine($"Enter CONFIRM to confirm, ALL to confirm all, SKIP to continue.", ConsoleColor.Yellow, suffix: "");
  911 + if (confirm.ToUpper() == "EX") { return "EX"; }
  912 + else if (confirm.ToUpper() == "CONFIRM") { returntext = null; }
  913 + else if (confirm.ToUpper() == "ALL") { returntext = "ALL"; }
  914 + else { return null; }
  915 + }
  916 +
  917 + string ssScriptText = null;
  918 + string LastUpdatedTrigger_parameters = $"DBNAME={triggertoexecute.Db};SCHEMA={triggertoexecute.Schema};TABLE={triggertoexecute.Table};COLUMN={triggertoexecute.Column};TRIGGER={LastUpdatedTrigger_triggername};";
  919 + if (!Tools.ResolveArguments(LastUpdatedTrigger_parameters, RemoveLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); }
  920 + try { SQLDataBaseManagerCore.ExecuteSQLScript(sqlcs, ssScriptText, 5000, null); }
  921 + catch (Exception e) { ColorConsole.WriteLine(e.MessageNested(), ConsoleColor.Yellow); }
  922 + if (!Tools.ResolveArguments(LastUpdatedTrigger_parameters, RemoveLastUpdatedColumn_Script, out ssScriptText)) { throw new ApplicationException(); }
  923 + try { SQLDataBaseManagerCore.ExecuteSQLScript(sqlcs, ssScriptText, 5000, null); }
641 catch (Exception e) { ColorConsole.WriteLine(e.MessageNested(), ConsoleColor.Yellow); } 924 catch (Exception e) { ColorConsole.WriteLine(e.MessageNested(), ConsoleColor.Yellow); }
642 925
643 - if (!effectiveremoveaction) 926 + if (!triggertoexecute.Remove)
644 { 927 {
645 - if (!Tools.ResolveArguments(parameters, CreateLastUpdatedColumn_Script, out ssScriptText)) { throw new ApplicationException(); }  
646 - SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null); 928 + if (!Tools.ResolveArguments(LastUpdatedTrigger_parameters, CreateLastUpdatedColumn_Script, out ssScriptText)) { throw new ApplicationException(); }
  929 + SQLDataBaseManagerCore.ExecuteSQLScript(sqlcs, ssScriptText, 5000, null);
647 930
648 - if (!Tools.ResolveArguments(parameters, CreateLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); }  
649 - SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null); 931 + if (!Tools.ResolveArguments(LastUpdatedTrigger_parameters, CreateLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); }
  932 + SQLDataBaseManagerCore.ExecuteSQLScript(sqlcs, ssScriptText, 5000, null);
650 933
651 - if (!Tools.ResolveArguments(parameters, EnableLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); }  
652 - SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null); 934 + if (!Tools.ResolveArguments(LastUpdatedTrigger_parameters, EnableLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); }
  935 + SQLDataBaseManagerCore.ExecuteSQLScript(sqlcs, ssScriptText, 5000, null);
653 } 936 }
654 - ColorConsole.WriteLine($"SUCCESS! {effectiveactiontext} trigger to store LastUpdate TimeStamp: {triggerfulldesignation}", ConsoleColor.Green);  
655 - goto getparameters;  
656 - }  
657 - catch (ApplicationException e)  
658 - {  
659 - ColorConsole.WriteLine("FATAL ERROR! in script parameter substitution!", ConsoleColor.Red);  
660 - goto getparameters;  
661 - }  
662 - catch (Exception e)  
663 - {  
664 - ColorConsole.WriteLine("FATAL ERROR! "+e.MessageNested(), ConsoleColor.Red);  
665 - goto getparameters; 937 + ColorConsole.WriteLine($"SUCCESS! {removeactionText} trigger to store LastUpdate TimeStamp: {LastUpdatedTrigger_fulldesignation}", ConsoleColor.Green);
666 } 938 }
  939 + catch (ApplicationException e) { ColorConsole.WriteLine("FATAL ERROR! in script parameter substitution!", ConsoleColor.Red); returntext = null; }
  940 + catch (Exception e) { ColorConsole.WriteLine("FATAL ERROR! " + e.MessageNested(), ConsoleColor.Red); returntext = null; }
  941 + return returntext;
667 } 942 }
  943 + #endregion ManageLastUpdatedTrigger
668 944
669 private static object ExecuteScript(object parameter, object o) 945 private static object ExecuteScript(object parameter, object o)
670 { 946 {
@@ -728,8 +1004,7 @@ GO @@ -728,8 +1004,7 @@ GO
728 } 1004 }
729 return o; 1005 return o;
730 } 1006 }
731 -  
732 - private static object DropDB(object parameter, object o) 1007 + private static object DropDB(object parameter, object o)
733 { 1008 {
734 var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>(); 1009 var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>();
735 var args = (parameter as Menu.ExecutorParameter).Args; 1010 var args = (parameter as Menu.ExecutorParameter).Args;
@@ -1273,7 +1548,7 @@ GO @@ -1273,7 +1548,7 @@ GO
1273 1548
1274 string createUserQuery = "CREATE USER [" + dbusername + "]"; 1549 string createUserQuery = "CREATE USER [" + dbusername + "]";
1275 if (forlogin.HasValue && forlogin.Value) {createUserQuery += " FOR LOGIN [" + loginname + "];"; } 1550 if (forlogin.HasValue && forlogin.Value) {createUserQuery += " FOR LOGIN [" + loginname + "];"; }
1276 - else if (forlogin.HasValue && !forlogin.Value) { createUserQuery = " WITHOUT LOGIN;"; } 1551 + else if (forlogin.HasValue && !forlogin.Value) { createUserQuery += " WITHOUT LOGIN;"; }
1277 createUserQuery += ";"; 1552 createUserQuery += ";";
1278 1553
1279 using (SqlCommand createUserCommand = new SqlCommand(createUserQuery, connection)) { createUserCommand.ExecuteNonQuery(); } 1554 using (SqlCommand createUserCommand = new SqlCommand(createUserQuery, connection)) { createUserCommand.ExecuteNonQuery(); }
@@ -1968,6 +2243,9 @@ GO @@ -1968,6 +2243,9 @@ GO
1968 public string Xml_PhysicalFilesDirectoryPath; 2243 public string Xml_PhysicalFilesDirectoryPath;
1969 public int Xml_ScriptCommandTimeout; 2244 public int Xml_ScriptCommandTimeout;
1970 public List<SQLData> Xml_SQLDataList; 2245 public List<SQLData> Xml_SQLDataList;
  2246 + public List<LastUpdatedTrigger> Xml_SQLLastUpdatedTriggerList;
  2247 + public List<DBUser> Xml_SQLDbUserList;
  2248 + public List<ServerLogin> Xml_SQLServerLoginList;
1971 public List<SQLScript> Xml_SQLScriptList; 2249 public List<SQLScript> Xml_SQLScriptList;
1972 2250
1973 public string DBName; 2251 public string DBName;
@@ -2082,6 +2360,21 @@ GO @@ -2082,6 +2360,21 @@ GO
2082 var sqldataXmlList = GetAllXElements(sqldatabasexml, nameof(XmlStructure.SQLDataBase.SQLData)); 2360 var sqldataXmlList = GetAllXElements(sqldatabasexml, nameof(XmlStructure.SQLDataBase.SQLData));
2083 if (sqldataXmlList!=null) { foreach (var sqldataXml in sqldataXmlList) { Xml_SQLDataList.Add(new SQLData(sqldataXml)); } } 2361 if (sqldataXmlList!=null) { foreach (var sqldataXml in sqldataXmlList) { Xml_SQLDataList.Add(new SQLData(sqldataXml)); } }
2084 2362
  2363 + Xml_SQLLastUpdatedTriggerList = new List<LastUpdatedTrigger>();
  2364 + var trgsXml = GetXElement(sqldatabasexml, nameof(XmlStructure.SQLDataBase.Triggers));
  2365 + var trgXmlList = GetAllXElements(trgsXml, nameof(XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger));
  2366 + if (trgXmlList != null) { int ix = 0; foreach (var trgXml in trgXmlList) { Xml_SQLLastUpdatedTriggerList.Add(new LastUpdatedTrigger(trgXml,ix)); ix++; } }
  2367 +
  2368 + Xml_SQLDbUserList = new List<DBUser>();
  2369 + var dbusersXml = GetXElement(sqldatabasexml, nameof(XmlStructure.SQLDataBase.DBUsers));
  2370 + var dbuserXmlList = GetAllXElements(dbusersXml, nameof(XmlStructure.SQLDataBase.DBUsers.DBUser));
  2371 + if (dbuserXmlList != null) { int ix = 0; foreach (var dbusrXml in dbuserXmlList) { Xml_SQLDbUserList.Add(new DBUser(dbusrXml, ix)); ix++; } }
  2372 +
  2373 + Xml_SQLServerLoginList = new List<ServerLogin>();
  2374 + var srvrloginsXml = GetXElement(sqldatabasexml, nameof(XmlStructure.SQLDataBase.ServerLogins));
  2375 + var srvrloginXmlList = GetAllXElements(srvrloginsXml, nameof(XmlStructure.SQLDataBase.ServerLogins.ServerLogin));
  2376 + if (srvrloginXmlList != null) { int ix = 0; foreach (var srvrloginXml in srvrloginXmlList) { Xml_SQLServerLoginList.Add(new ServerLogin(srvrloginXml, ix)); ix++; } }
  2377 +
2085 Xml_SQLScriptList = new List<SQLScript>(); 2378 Xml_SQLScriptList = new List<SQLScript>();
2086 var sqlscriptsXml = GetXElement(sqldatabasexml, nameof(XmlStructure.SQLDataBase.Scripts)); 2379 var sqlscriptsXml = GetXElement(sqldatabasexml, nameof(XmlStructure.SQLDataBase.Scripts));
2087 var sqlscriptXmlList = GetAllXElements(sqlscriptsXml, nameof(XmlStructure.SQLDataBase.Scripts.Script)); 2380 var sqlscriptXmlList = GetAllXElements(sqlscriptsXml, nameof(XmlStructure.SQLDataBase.Scripts.Script));
@@ -2143,6 +2436,44 @@ GO @@ -2143,6 +2436,44 @@ GO
2143 public static class Group { public static class Values { public const string DEFAULT = ""; } } 2436 public static class Group { public static class Values { public const string DEFAULT = ""; } }
2144 } 2437 }
2145 } 2438 }
  2439 + public static class Triggers
  2440 + {
  2441 + public static class LastUpdatedTrigger
  2442 + {
  2443 + public static class Attributes
  2444 + {
  2445 + public static class Schema { public static class Values { public const string DEFAULT = ""; } }
  2446 + public static class Table { public static class Values { public const string DEFAULT = ""; } }
  2447 + public static class Field { public static class Values { public const string DEFAULT = ""; } }
  2448 + }
  2449 + }
  2450 + }
  2451 + public static class DBUsers
  2452 + {
  2453 + public static class DBUser
  2454 + {
  2455 + public static class Attributes
  2456 + {
  2457 + public static class UserName { public static class Values { public const string DEFAULT = ""; } }
  2458 + public static class RoleNameList { public static class Values { public const string DEFAULT = ""; } }
  2459 + public static class LoginName { public static class Values { public const string DEFAULT = ""; } }
  2460 + }
  2461 + }
  2462 + }
  2463 + public static class ServerLogins
  2464 + {
  2465 + public static class ServerLogin
  2466 + {
  2467 + public static class Attributes
  2468 + {
  2469 + public static class LoginName { public static class Values { public const string DEFAULT = ""; } }
  2470 + public static class LoginPassword { public static class Values { public const string DEFAULT = ""; } }
  2471 + public static class GrantorLoginName { public static class Values { public const string DEFAULT = ""; } }
  2472 + public static class GrantorLoginPsw { public static class Values { public const string DEFAULT = ""; } }
  2473 + public static class DefaultDatabase{ public static class Values { public const string DEFAULT = ""; } }
  2474 + }
  2475 + }
  2476 + }
2146 public static class Scripts 2477 public static class Scripts
2147 { 2478 {
2148 public static class Script 2479 public static class Script
@@ -2233,6 +2564,83 @@ GO @@ -2233,6 +2564,83 @@ GO
2233 #endregion constructors 2564 #endregion constructors
2234 } 2565 }
2235 #endregion SQLData class 2566 #endregion SQLData class
  2567 + #region LastUpdatedTrigger class
  2568 + public class LastUpdatedTrigger : XmlLinqBase
  2569 + {
  2570 + #region fields
  2571 + public string Key = "";
  2572 + public string Db = "";
  2573 + public string Schema = "";
  2574 + public string Table = "";
  2575 + public string Column= "";
  2576 + public bool Remove = false;
  2577 + #endregion fields
  2578 + #region constructors
  2579 + public LastUpdatedTrigger() { }
  2580 + public LastUpdatedTrigger(LastUpdatedTrigger dbusr) { Schema = dbusr.Schema; Column = dbusr.Column; Table = dbusr.Table; Key = dbusr.Key; Db = dbusr.Db; Remove = dbusr.Remove;}
  2581 + public LastUpdatedTrigger(XElement trgXml, int ix)
  2582 + {
  2583 + Key = $"TRG{ix}";
  2584 + Schema = GetValue(nameof(XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger.Attributes.Schema), trgXml, XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger.Attributes.Schema.Values.DEFAULT);
  2585 + Table = GetValue(nameof(XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger.Attributes.Table), trgXml, XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger.Attributes.Table.Values.DEFAULT);
  2586 + Column = GetValue(nameof(XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger.Attributes.Field), trgXml, XmlStructure.SQLDataBase.Triggers.LastUpdatedTrigger.Attributes.Field.Values.DEFAULT);
  2587 + }
  2588 + #endregion constructors
  2589 + }
  2590 + #endregion LastUpdatedTrigger class
  2591 + #region DBUser class
  2592 + public class DBUser : XmlLinqBase
  2593 + {
  2594 + #region fields
  2595 + public string Key = "";
  2596 + public string Db = "";
  2597 + public string UserName = "";
  2598 + public string LoginName = "";
  2599 + public string RoleNameCommaList = "";
  2600 + public bool Remove = false;
  2601 + #endregion fields
  2602 + #region constructors
  2603 + public DBUser() { }
  2604 + public DBUser(DBUser dbusr) { UserName = dbusr.UserName; LoginName = dbusr.LoginName; RoleNameCommaList = dbusr.RoleNameCommaList; Key = dbusr.Key; Db = dbusr.Db; Remove = dbusr.Remove; }
  2605 + public DBUser(XElement dbusrXml, int ix)
  2606 + {
  2607 + Key = $"DBU{ix}";
  2608 + UserName = GetValue(nameof(XmlStructure.SQLDataBase.DBUsers.DBUser.Attributes.UserName), dbusrXml, XmlStructure.SQLDataBase.DBUsers.DBUser.Attributes.UserName.Values.DEFAULT);
  2609 + RoleNameCommaList = GetValue(nameof(XmlStructure.SQLDataBase.DBUsers.DBUser.Attributes.RoleNameList), dbusrXml, XmlStructure.SQLDataBase.DBUsers.DBUser.Attributes.RoleNameList.Values.DEFAULT);
  2610 + LoginName = GetValue(nameof(XmlStructure.SQLDataBase.DBUsers.DBUser.Attributes.LoginName), dbusrXml, XmlStructure.SQLDataBase.DBUsers.DBUser.Attributes.LoginName.Values.DEFAULT);
  2611 + }
  2612 + #endregion constructors
  2613 + }
  2614 + #endregion DBUser class
  2615 + #region ServerLogin class
  2616 + public class ServerLogin : XmlLinqBase
  2617 + {
  2618 + #region fields
  2619 + public string Key = "";
  2620 + public string Db = "";
  2621 + public string GrantorUserLoginName = "";
  2622 + public string LoginName = "";
  2623 + public string LoginPassword = "";
  2624 + public LoginType LoginType = LoginType.WindowsUser;
  2625 + public string GrantorUserLoginPassword = "";
  2626 + public string DefaultDatabase = "";
  2627 + public bool Remove = false;
  2628 + #endregion fields
  2629 + #region constructors
  2630 + public ServerLogin() { }
  2631 + 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; }
  2632 + public ServerLogin(XElement dbusrXml, int ix)
  2633 + {
  2634 + Key = $"SL{ix}";
  2635 + LoginName = GetValue(nameof(XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.LoginName), dbusrXml, XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.LoginName.Values.DEFAULT);
  2636 + LoginPassword = GetValue(nameof(XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.LoginPassword), dbusrXml, XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.LoginPassword.Values.DEFAULT);
  2637 + GrantorUserLoginName = GetValue(nameof(XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.GrantorLoginName), dbusrXml, XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.GrantorLoginName.Values.DEFAULT);
  2638 + GrantorUserLoginPassword = GetValue(nameof(XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.GrantorLoginPsw), dbusrXml, XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.GrantorLoginPsw.Values.DEFAULT);
  2639 + DefaultDatabase = GetValue(nameof(XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.DefaultDatabase), dbusrXml, XmlStructure.SQLDataBase.ServerLogins.ServerLogin.Attributes.DefaultDatabase.Values.DEFAULT);
  2640 + }
  2641 + #endregion constructors
  2642 + }
  2643 + #endregion ServerLogin class
2236 } 2644 }
2237 #endregion SQLDataBase class 2645 #endregion SQLDataBase class
2238 2646
Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices; @@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
32 // You can specify all the values or you can default the Build and Revision Numbers 32 // You can specify all the values or you can default the Build and Revision Numbers
33 // by using the '*' as shown below: 33 // by using the '*' as shown below:
34 // [assembly: AssemblyVersion("1.0.*")] 34 // [assembly: AssemblyVersion("1.0.*")]
35 -[assembly: AssemblyVersion("1.28.1.0")]  
36 -[assembly: AssemblyFileVersion("1.28.1.0")] 35 +[assembly: AssemblyVersion("1.29.0.0")]
  36 +[assembly: AssemblyFileVersion("1.29.0.0")]