Commit 7cdc4aa4219de9dae9bd3e978e3c673f8d77b425
1 parent
cccef077
v1.25.0.0
- Adding/Removing trigger to store lastupdate timestamp
Showing
3 changed files
with
147 additions
and
4 deletions
Show diff stats
Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - CommandLineParser.cs
| @@ -308,6 +308,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.CommandLineParserNS | @@ -308,6 +308,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.CommandLineParserNS | ||
| 308 | public static class ExecuteScript{ public const string KEY = "EXE"; } | 308 | public static class ExecuteScript{ public const string KEY = "EXE"; } |
| 309 | public static class CreateLoginAndUser{ public const string KEY = "CRU"; } | 309 | public static class CreateLoginAndUser{ public const string KEY = "CRU"; } |
| 310 | public static class AddUserForLogin{ public const string KEY = "CRA"; } | 310 | public static class AddUserForLogin{ public const string KEY = "CRA"; } |
| 311 | + public static class CreateLastUpdatedTrigger{ public const string KEY = "TRA"; } | ||
| 312 | + public static class RemoveLastUpdatedTrigger{ public const string KEY = "TRD"; } | ||
| 311 | } | 313 | } |
| 312 | } | 314 | } |
| 313 | 315 |
Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs
| @@ -53,6 +53,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS | @@ -53,6 +53,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS | ||
| 53 | .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.ExecuteScript.KEY, "Execute script", ExecuteScript, ep)) | 53 | .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.ExecuteScript.KEY, "Execute script", ExecuteScript, ep)) |
| 54 | .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.CreateLoginAndUser.KEY, "Create Server login and database user", CreteLoginAndAddToDB, ep)) | 54 | .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.CreateLoginAndUser.KEY, "Create Server login and database user", CreteLoginAndAddToDB, ep)) |
| 55 | .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.AddUserForLogin.KEY, "Add database user to an existing Login", AddExistingLoginToDB, ep)) | 55 | .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.AddUserForLogin.KEY, "Add database user to an existing Login", AddExistingLoginToDB, ep)) |
| 56 | + .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.CreateLastUpdatedTrigger.KEY, "Add LastUpdated trigger to a datatable column", CreateLastUpdatedTrigger, ep)) | ||
| 57 | + .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.RemoveLastUpdatedTrigger.KEY, "Add LastUpdated trigger to a datatable column", RemoveLastUpdatedTrigger, ep)) | ||
| 56 | .SetSelectionMode(Menu.SelectionMode.Single) | 58 | .SetSelectionMode(Menu.SelectionMode.Single) |
| 57 | .SetMenuHeaderDisplayer(DataBaseListDisplayer); | 59 | .SetMenuHeaderDisplayer(DataBaseListDisplayer); |
| 58 | menufunctions.ExecuteMenu(functionkey); | 60 | menufunctions.ExecuteMenu(functionkey); |
| @@ -225,6 +227,144 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS | @@ -225,6 +227,144 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS | ||
| 225 | } | 227 | } |
| 226 | private static object CreteLoginAndAddToDB(object parameter, object o) { return _CreteLoginAndUser(parameter, o, true); } | 228 | private static object CreteLoginAndAddToDB(object parameter, object o) { return _CreteLoginAndUser(parameter, o, true); } |
| 227 | private static object AddExistingLoginToDB(object parameter, object o) { return _CreteLoginAndUser(parameter, o, false); } | 229 | private static object AddExistingLoginToDB(object parameter, object o) { return _CreteLoginAndUser(parameter, o, false); } |
| 230 | + | ||
| 231 | + private static object CreateLastUpdatedTrigger(object parameter, object o) { return _RemoveAndCreateLastUpdatedTrigger(parameter, o, false); } | ||
| 232 | + private static object RemoveLastUpdatedTrigger(object parameter, object o) { return _RemoveAndCreateLastUpdatedTrigger(parameter, o, true); } | ||
| 233 | + #region script texts for _RemoveAndCreateLastUpdatedTrigger | ||
| 234 | + const string Header_Script = | ||
| 235 | + @"USE [{DBNAME}] | ||
| 236 | + | ||
| 237 | +SET ANSI_NULLS ON | ||
| 238 | +GO | ||
| 239 | + | ||
| 240 | +SET QUOTED_IDENTIFIER ON | ||
| 241 | +GO | ||
| 242 | +"; | ||
| 243 | + const string CreateLastUpdatedColumn_Script = Header_Script + | ||
| 244 | + @"ALTER TABLE [{SCHEMA}].[{TABLE}] | ||
| 245 | + ADD [{COLUMN}] DATETIME | ||
| 246 | +GO | ||
| 247 | +"; | ||
| 248 | + const string CreateLastUpdatedTrigger_Script = Header_Script + | ||
| 249 | + @"CREATE TRIGGER [{SCHEMA}].[{TRIGGER}] | ||
| 250 | + ON [{SCHEMA}].[{TABLE}] | ||
| 251 | + AFTER INSERT, UPDATE | ||
| 252 | +AS | ||
| 253 | +BEGIN | ||
| 254 | + SET NOCOUNT ON; | ||
| 255 | + | ||
| 256 | + UPDATE [{SCHEMA}].[{TABLE}] SET [{COLUMN}] = GETDATE() | ||
| 257 | + FROM [{SCHEMA}].[{TABLE}] pper | ||
| 258 | + INNER JOIN inserted i | ||
| 259 | + ON pper.ID = i.ID; | ||
| 260 | +END | ||
| 261 | +GO | ||
| 262 | +"; | ||
| 263 | + const string EnableLastUpdatedTrigger_Script = Header_Script + | ||
| 264 | + @"ALTER TABLE [{SCHEMA}].[{TABLE}] ENABLE TRIGGER [{TRIGGER}] | ||
| 265 | +GO | ||
| 266 | +"; | ||
| 267 | + | ||
| 268 | + const string RemoveLastUpdatedColumn_Script = Header_Script + | ||
| 269 | + @"ALTER TABLE [{SCHEMA}].[{TABLE}] DROP COLUMN [{COLUMN}] | ||
| 270 | +GO | ||
| 271 | +"; | ||
| 272 | + | ||
| 273 | + const string RemoveLastUpdatedTrigger_Script = | ||
| 274 | + @"USE [{DBNAME}] | ||
| 275 | +DROP TRIGGER [{SCHEMA}].[{TRIGGER}]; | ||
| 276 | +GO | ||
| 277 | +"; | ||
| 278 | + #endregion script texts for _RemoveAndCreateLastUpdatedTrigger | ||
| 279 | + private static object _RemoveAndCreateLastUpdatedTrigger(object parameter, object o,bool removeonly) | ||
| 280 | + { | ||
| 281 | + try | ||
| 282 | + { | ||
| 283 | + var config = (parameter as Menu.ExecutorParameter).GetConfig<SQLDataBaseManagerXmlProcessor>(); | ||
| 284 | + var args = (parameter as Menu.ExecutorParameter).Args; | ||
| 285 | + var selectedtaskindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.ScheduledTaskManager.Function.CMD_TASKS); | ||
| 286 | + var menufolders = DisplaySQLDataBaseMenu(config, $"Select the SQL database(s) to manage with function '{nameof(CreateLastUpdatedTrigger)}'!", silent: true); | ||
| 287 | + Menu.Selection sr = menufolders.Select(selectedtaskindexes); | ||
| 288 | + if (sr.Result == Menu.SelectionResult.Exit) { return o; } | ||
| 289 | + else if (sr.Result == Menu.SelectionResult.None) { return o; } | ||
| 290 | + else if (sr.Result == Menu.SelectionResult.Error) { return o; } | ||
| 291 | + else if (sr.Result == Menu.SelectionResult.Ok) { } | ||
| 292 | + else { } | ||
| 293 | + var p = sr.SelectedParameterList.FirstOrDefault(); | ||
| 294 | + if (p == null) { return o; } | ||
| 295 | + SQLDataBase sqld = p.Parameters as SQLDataBase; | ||
| 296 | + | ||
| 297 | + getparameters:; | ||
| 298 | + string action = removeonly ? "REMOVING" : "CREATING"; | ||
| 299 | + ColorConsole.WriteLine(prefix: $"Enter the parameters for {action} LastUpdated trigger.", bracket: "", text: "", f: ConsoleColor.Yellow); | ||
| 300 | + ColorConsole.WriteLine(prefix: $" Format#1:", bracket: "()", text: "DBNAME,TABLEWITHSCHEMA,COLUMN", f: ConsoleColor.Yellow); | ||
| 301 | + ColorConsole.WriteLine(prefix: $" Format#2:", bracket: "()", text: "TABLEWITHSCHEMA,COLUMN", f: ConsoleColor.Yellow); | ||
| 302 | + ColorConsole.WriteLine(prefix: $" Format#3:", bracket: "()", text: "TABLEWITHSCHEMA", f: ConsoleColor.Yellow); | ||
| 303 | + ColorConsole.WriteLine(prefix: " ", text: "DBNAME", bracket: "", suffix: $": name of the database of the trigger; default is {sqld.DBName} (example:LearALM2)"); | ||
| 304 | + ColorConsole.WriteLine(prefix: " ", text: "[SCHEMA.]TABLE", bracket: "", suffix: $": name of the table (with schema) of the trigger; default dbo (example:ALM.ProductionPeriods)"); | ||
| 305 | + ColorConsole.WriteLine(prefix: " ", text: "COLUMN", bracket: "", suffix: $": name of the column (will be created) of the trigger; default is LastUpdated (example:LastUpdated)"); | ||
| 306 | + | ||
| 307 | + var createtriggerparameters = ColorConsole.ReadLine($"EX=exit.", ConsoleColor.Yellow, suffix: " --> "); | ||
| 308 | + if (createtriggerparameters.ToUpper() == "EX") { return o; } | ||
| 309 | + if (string.IsNullOrWhiteSpace(createtriggerparameters)) { goto getparameters; } | ||
| 310 | + var optionList = createtriggerparameters.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); | ||
| 311 | + | ||
| 312 | + string db = ""; | ||
| 313 | + string tablewithschema = ""; | ||
| 314 | + string column = ""; | ||
| 315 | + if (optionList.Length == 1) { tablewithschema = optionList[0]; } | ||
| 316 | + else if (optionList.Length == 2) { tablewithschema = optionList[0]; column = optionList[1]; } | ||
| 317 | + else if (optionList.Length == 3) { db = optionList[0]; tablewithschema = optionList[1]; column = optionList[2]; } | ||
| 318 | + else /*if (optionList.Length > 3)*/ { ColorConsole.WriteLine("ERROR! No more parameters required beside DBNAME,TABLEWITHSCHEMA and COLUMN!", ConsoleColor.Red); goto getparameters; } | ||
| 319 | + | ||
| 320 | + if (string.IsNullOrWhiteSpace(db)) db = sqld.DBName; | ||
| 321 | + if (string.IsNullOrWhiteSpace(column)) column = "LastUpdated"; | ||
| 322 | + | ||
| 323 | + if (string.IsNullOrWhiteSpace(db)) { ColorConsole.WriteLine("ERROR! DATABASE can not be empty!", ConsoleColor.Red); goto getparameters; } | ||
| 324 | + if (string.IsNullOrWhiteSpace(tablewithschema)) { ColorConsole.WriteLine("ERROR! TABLEWITHSCHEMA can not be empty!", ConsoleColor.Red); goto getparameters; } | ||
| 325 | + var tablewithschemasplitted = tablewithschema.Split('.'); | ||
| 326 | + if (tablewithschemasplitted.Length != 1 && tablewithschemasplitted.Length != 2) { ColorConsole.WriteLine("ERROR! TABLEWITHSCHEMA incorrect!", ConsoleColor.Red); goto getparameters; } | ||
| 327 | + if (string.IsNullOrWhiteSpace(column)) { ColorConsole.WriteLine("ERROR! COLUMN can not be empty!", ConsoleColor.Red); goto getparameters; } | ||
| 328 | + | ||
| 329 | + string schema = ""; | ||
| 330 | + string table = tablewithschema; | ||
| 331 | + if (tablewithschemasplitted.Length == 2) { schema = tablewithschemasplitted[0]; table = tablewithschemasplitted[1]; } | ||
| 332 | + string triggername = $"TRG_LASTUPDATETS_{db}_{schema}_{table}_{column}"; | ||
| 333 | + string parameters = $"DBNAME={db};SCHEMA={schema};TABLE={table};COLUMN={column};TRIGGER={triggername};"; | ||
| 334 | + string ssScriptText = null; | ||
| 335 | + | ||
| 336 | + if (!Tools.ResolveArguments(parameters, RemoveLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); } | ||
| 337 | + try {SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null); } | ||
| 338 | + catch (Exception e) {ColorConsole.WriteLine(e.Message, ConsoleColor.Yellow);} | ||
| 339 | + if (!Tools.ResolveArguments(parameters, RemoveLastUpdatedColumn_Script, out ssScriptText)) { throw new ApplicationException(); } | ||
| 340 | + try {SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null);} | ||
| 341 | + catch (Exception e) { ColorConsole.WriteLine(e.Message, ConsoleColor.Yellow); } | ||
| 342 | + | ||
| 343 | + if (!removeonly) | ||
| 344 | + { | ||
| 345 | + if (!Tools.ResolveArguments(parameters, CreateLastUpdatedColumn_Script, out ssScriptText)) { throw new ApplicationException(); } | ||
| 346 | + SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null); | ||
| 347 | + | ||
| 348 | + if (!Tools.ResolveArguments(parameters, CreateLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); } | ||
| 349 | + SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null); | ||
| 350 | + | ||
| 351 | + if (!Tools.ResolveArguments(parameters, EnableLastUpdatedTrigger_Script, out ssScriptText)) { throw new ApplicationException(); } | ||
| 352 | + SQLDataBaseManagerCore.ExecuteSQLScript(sqld.SQLCS, ssScriptText, 5000, null); | ||
| 353 | + } | ||
| 354 | + ColorConsole.WriteLine($"SUCCESS! {action} trigger to store LastUpdate TimeStamp: Database={db},Table={schema}.{table},Column={column}", ConsoleColor.Green); | ||
| 355 | + return o; | ||
| 356 | + } | ||
| 357 | + catch (ApplicationException e) | ||
| 358 | + { | ||
| 359 | + ColorConsole.WriteLine("FATAL ERROR! in script parameter substitution!", ConsoleColor.Red); | ||
| 360 | + return o; | ||
| 361 | + } | ||
| 362 | + catch (Exception e) | ||
| 363 | + { | ||
| 364 | + ColorConsole.WriteLine("FATAL ERROR! "+e.Message, ConsoleColor.Red); | ||
| 365 | + return o; | ||
| 366 | + } | ||
| 367 | + } | ||
| 228 | private static object _CreteLoginAndUser(object parameter, object o,bool createlogin) | 368 | private static object _CreteLoginAndUser(object parameter, object o,bool createlogin) |
| 229 | { | 369 | { |
| 230 | const string COMMA = ","; | 370 | const string COMMA = ","; |
| @@ -971,7 +1111,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS | @@ -971,7 +1111,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS | ||
| 971 | } | 1111 | } |
| 972 | 1112 | ||
| 973 | sqlc.Close(); | 1113 | sqlc.Close(); |
| 974 | - if (DataSet != null && DataSet.Tables != null) | 1114 | + try |
| 975 | { | 1115 | { |
| 976 | var firstreturnedtable = DataSet.Tables[0]; | 1116 | var firstreturnedtable = DataSet.Tables[0]; |
| 977 | var firstreturnedrow = firstreturnedtable.Rows[0]; | 1117 | var firstreturnedrow = firstreturnedtable.Rows[0]; |
| @@ -981,7 +1121,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS | @@ -981,7 +1121,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS | ||
| 981 | var rm = Convert.ToString(secondreturnedvalue); | 1121 | var rm = Convert.ToString(secondreturnedvalue); |
| 982 | return new ReturnInfoJSON() { ReturnValue = rv, ReturnMessage = rm, }; | 1122 | return new ReturnInfoJSON() { ReturnValue = rv, ReturnMessage = rm, }; |
| 983 | } | 1123 | } |
| 984 | - else { return new ReturnInfoJSON() { ReturnValue = 0, ReturnMessage = null, }; } | 1124 | + catch { } |
| 1125 | + return new ReturnInfoJSON() { ReturnValue = 0, ReturnMessage = null, }; | ||
| 985 | } | 1126 | } |
| 986 | 1127 | ||
| 987 | /// <summary> | 1128 | /// <summary> |
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.24.0.0")] | ||
| 36 | -[assembly: AssemblyFileVersion("1.24.0.0")] | 35 | +[assembly: AssemblyVersion("1.25.0.0")] |
| 36 | +[assembly: AssemblyFileVersion("1.25.0.0")] |