Skip to content

Commit 23cb3f8

Browse files
committed
wip updates for 2.1
- add autoregister for module instances - update to use single parameter log block - align goto label names - align start and end processing for these sp's - fix linter warnings
1 parent a72c50d commit 23cb3f8

11 files changed

Lines changed: 480 additions & 334 deletions

Direct_Framework/Stored Procedures/omd.BatchEvaluation.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ BEGIN
120120
@BatchInstanceId = @BatchInstanceId,
121121
@EventDetail = @EventDetail;
122122

123-
GOTO FailureEndOfProcedure;
123+
GOTO EndOfProcedureFailure;
124124
END
125125

126126
SET @LogMessage = 'For Batch Instance Id ' + CONVERT(NVARCHAR(20), @BatchInstanceId)+ ' the Batch Id ' + CONVERT(NVARCHAR(10), @BatchId) + ' was found in [omd].[BATCH].'
@@ -419,7 +419,7 @@ BEGIN
419419

420420
RAISERROR('Incorrect Batch Evaluation path encountered (post-rollback).',16,1)
421421

422-
FailureEndOfProcedure:
422+
EndOfProcedureFailure:
423423

424424
SET @SuccessIndicator = 'N'
425425
SET @InternalProcessingStatusCode = 'Failure';

Direct_Framework/Stored Procedures/omd.CreateBatchInstance.sql

Lines changed: 110 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ EXEC [omd].[PrintMessageLog] @MessageLog = @MessageLog;
5858

5959
CREATE PROCEDURE [omd].[CreateBatchInstance]
6060
(
61-
-- Required parameters
61+
/* Required parameters */
6262
@BatchCode NVARCHAR(500) = NULL
63-
-- Optional parameters
63+
/* Optional parameters */
6464
,@ParentBatchInstanceId BIGINT = 0
65-
,@Debug CHAR(1) = 'N'
6665
,@ExecutionContext NVARCHAR(4000) = N''
67-
-- Output parameters
66+
,@Debug CHAR(1) = 'N'
67+
/* Output parameters */
6868
,@BatchInstanceId BIGINT = NULL OUTPUT
6969
,@BatchInstanceStartTimestamp DATETIME2 = NULL OUTPUT
7070
,@SuccessIndicator CHAR(1) = 'N' OUTPUT
@@ -81,6 +81,7 @@ BEGIN
8181
SET @SuccessIndicator = 'N';
8282
SET @MessageLog = N'[]';
8383
DECLARE @LogMessage NVARCHAR(2048);
84+
DECLARE @ProcessDescription NVARCHAR(4000) = N'Create Batch Instance process';
8485

8586
/* Set output variables to default */
8687
SET @BatchInstanceId = NULL;
@@ -105,7 +106,7 @@ BEGIN
105106
DECLARE @DefaultTimeZone NVARCHAR(4000) =
106107
[omd_metadata].[GetSetting]('DEFAULT_TIMEZONE');
107108

108-
/* ----- Validate input parameters ------------------------------------------ */
109+
/* ----- Validate input parameters -------------------------------------- */
109110

110111
IF @BatchCode IS NULL OR TRIM(@BatchCode) = ''
111112
BEGIN
@@ -117,7 +118,7 @@ BEGIN
117118
GOTO EndOfProcedureFailure;
118119
END;
119120

120-
/* ----- Default logging setup ---------------------------------------------- */
121+
/* ----- Default logging setup ------------------------------------------ */
121122

122123
DECLARE @StartTimestamp DATETIME2 = SYSUTCDATETIME();
123124
DECLARE @StartTimestampString NVARCHAR(4000) = [omd_metadata].[GetTimestampString](@StartTimestamp);
@@ -129,108 +130,116 @@ BEGIN
129130
BEGIN
130131
/* Log standard metadata */
131132
SET @MessageLog = [omd].[AddLogMessage]('DEBUG', DEFAULT, N'Procedure',
132-
@SpName, @MessageLog);
133+
@SpName, @MessageLog);
133134
SET @MessageLog = [omd].[AddLogMessage]('DEBUG', DEFAULT, N'Version',
134-
[omd_metadata].[GetFrameworkVersion](), @MessageLog);
135+
[omd_metadata].[GetFrameworkVersion](), @MessageLog);
135136
SET @MessageLog = [omd].[AddLogMessage]('DEBUG', DEFAULT, N'Start Timestamp',
136-
@StartTimestampString, @MessageLog);
137-
/* Log parameters */
138-
SET @MessageLog = [omd].[AddLogMessage]('INFO', DEFAULT, N'Parameter @BatchCode',
139-
@BatchCode, @MessageLog);
140-
SET @MessageLog = [omd].[AddLogMessage]('INFO', DEFAULT, N'Parameter @ParentBatchInstanceId',
141-
@ParentBatchInstanceId, @MessageLog);
137+
@StartTimestampString, @MessageLog);
138+
139+
/* Log parameters in a single JSON block */
140+
DECLARE @paramsJson NVARCHAR(MAX) = (
141+
SELECT
142+
@BatchCode AS BatchCode,
143+
@ParentBatchInstanceId AS ParentBatchInstanceId,
144+
@ExecutionContext AS ExecutionContext,
145+
@Debug AS Debug
146+
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER);
147+
148+
SET @MessageLog = [omd].[AddLogMessage]('INFO', DEFAULT, N'Parameters',
149+
@paramsJson, @MessageLog);
142150
END
143151

144-
/* ----- Start of main process ---------------------------------------------- */
152+
DECLARE @RowsAffected INT = 0;
145153

154+
/* ----- Start of main process ------------------------------------------ */
146155

147-
/* Local procedure variables */
148-
DECLARE @BatchId INT;
149-
DECLARE @ParentBatchId INT;
156+
BEGIN TRY
157+
BEGIN TRANSACTION;
150158

151-
SELECT @BatchId = [omd].[GetBatchIdByCode](@BatchCode);
159+
/* Local procedure variables */
160+
DECLARE @BatchId INT;
161+
DECLARE @ParentBatchId INT;
152162

153-
/* If auto-register Batch is enabled, register the Batch if needed */
154-
IF @BatchId IS NULL AND @AutoRegisterBatches = 'Y'
155-
BEGIN
156-
SET @LogMessage = CONCAT(N'Batch Code ''', @BatchCode,
157-
''' not found, attempting to auto-register the Batch.');
158-
IF @ProcessMessageLog = 'Y' SET @MessageLog =
159-
[omd].[AddLogMessage](DEFAULT, DEFAULT, DEFAULT, @LogMessage, @MessageLog);
163+
SELECT @BatchId = [omd].[GetBatchIdByCode](@BatchCode);
160164

161-
DECLARE @RegisterSuccessIndicator CHAR(1);
162-
EXEC [omd].[RegisterBatch]
163-
@BatchCode = @BatchCode,
164-
@Debug = @Debug,
165-
@BatchId = @BatchId OUTPUT,
166-
@SuccessIndicator = @RegisterSuccessIndicator OUTPUT,
167-
@MessageLog = @MessageLog OUTPUT;
165+
/* If auto-register Batch is enabled, register the Batch if needed */
166+
IF @BatchId IS NULL AND @AutoRegisterBatches = 'Y'
167+
BEGIN
168+
SET @LogMessage = CONCAT(N'Batch Code ''', @BatchCode,
169+
''' not found, attempting to auto-register the Batch.');
170+
IF @ProcessMessageLog = 'Y' SET @MessageLog =
171+
[omd].[AddLogMessage](DEFAULT, DEFAULT, DEFAULT, @LogMessage, @MessageLog);
172+
173+
DECLARE @RegisterSuccessIndicator CHAR(1);
174+
EXEC [omd].[RegisterBatch]
175+
@BatchCode = @BatchCode,
176+
@Debug = @Debug,
177+
@BatchId = @BatchId OUTPUT,
178+
@SuccessIndicator = @RegisterSuccessIndicator OUTPUT,
179+
@MessageLog = @MessageLog OUTPUT;
180+
181+
IF @RegisterSuccessIndicator = 'N' OR @BatchId IS NULL
182+
BEGIN
183+
SET @SuccessIndicator = 'N';
184+
SET @LogMessage = CONCAT(N'Auto-registration of Batch Code ''', @BatchCode,
185+
''' failed.');
186+
IF @ProcessMessageLog = 'Y' SET @MessageLog =
187+
[omd].[AddLogMessage]('ERROR', DEFAULT, DEFAULT, @LogMessage, @MessageLog);
188+
IF @ThrowOnFailure = 'Y' THROW 50000, @LogMessage, 1;
189+
GOTO EndOfProcedureFailure;
190+
END
191+
END
168192

169-
IF @RegisterSuccessIndicator = 'N' OR @BatchId IS NULL
193+
/* Exception handling: The Batch Id cannot be NULL, 0, or negative. */
194+
IF @BatchId IS NULL OR @BatchId <= 0
170195
BEGIN
171196
SET @SuccessIndicator = 'N';
172-
SET @LogMessage = CONCAT(N'Auto-registration of Batch Code ''', @BatchCode,
173-
''' failed.');
197+
SET @LogMessage = CONCAT(N'A Valid Batch Id was not found for Batch Code ''', @BatchCode, '''');
174198
IF @ProcessMessageLog = 'Y' SET @MessageLog =
175-
[omd].[AddLogMessage]('ERROR', DEFAULT, DEFAULT, @LogMessage, @MessageLog);
199+
[omd].[AddLogMessage]('ERROR', DEFAULT, 'Parameter', @LogMessage, @MessageLog);
176200
IF @ThrowOnFailure = 'Y' THROW 50000, @LogMessage, 1;
177201
GOTO EndOfProcedureFailure;
178202
END
179-
END
180203

181-
/* Exception handling: The Batch Id cannot be NULL, 0, or negative. */
182-
IF @BatchId IS NULL OR @BatchId <= 0
183-
BEGIN
184-
SET @SuccessIndicator = 'N';
185-
SET @LogMessage = CONCAT(N'A Valid Batch Id was not found for Batch Code ''', @BatchCode, '''');
204+
SET @LogMessage = CONCAT(N'For Batch Code ''', @BatchCode,
205+
''' the following Batch Id was found in omd.BATCH: ', @BatchId);
186206
IF @ProcessMessageLog = 'Y' SET @MessageLog =
187-
[omd].[AddLogMessage]('ERROR', DEFAULT, 'Parameter', @LogMessage, @MessageLog);
188-
IF @ThrowOnFailure = 'Y' THROW 50000, @LogMessage, 1;
189-
GOTO EndOfProcedureFailure;
190-
END
191-
192-
SET @LogMessage = CONCAT(N'For Batch Code ''', @BatchCode,
193-
''' the following Batch Id was found in omd.BATCH: ', @BatchId);
194-
IF @ProcessMessageLog = 'Y' SET @MessageLog =
195-
[omd].[AddLogMessage](DEFAULT, DEFAULT, DEFAULT, @LogMessage, @MessageLog);
207+
[omd].[AddLogMessage](DEFAULT, DEFAULT, DEFAULT, @LogMessage, @MessageLog);
196208

197-
/* If a ParentBatchInstanceId was provided and auto-register relationships
198-
is enabled, register any missing relationship */
209+
/* If a ParentBatchInstanceId was provided and auto-register relationships
210+
is enabled, register any missing relationship */
199211

200-
IF @ParentBatchInstanceId IS NOT NULL AND @ParentBatchInstanceId > 0
201-
AND @AutoRegisterRelationships = 'Y'
202-
BEGIN
203-
/* Get the Parents BATCH_ID and BATCH_CODE from the instance */
204-
SET @ParentBatchId = [omd].[GetBatchIdByBatchInstanceId](@ParentBatchInstanceId);
205-
DECLARE @ParentBatchCode NVARCHAR(500) =
206-
[omd].[GetBatchCodeById](@ParentBatchId);
207-
SET @LogMessage = CONCAT(N'Auto-registering any missing Batch relationship ' +
208-
N'between Parent Batch Code ''', @ParentBatchCode,
209-
''' and Child Batch Code ''', @BatchCode, '''.');
210-
IF @ProcessMessageLog = 'Y' SET @MessageLog =
211-
[omd].[AddLogMessage](DEFAULT, DEFAULT, DEFAULT, @LogMessage, @MessageLog);
212-
DECLARE @RegisterRelSuccessIndicator CHAR(1);
213-
EXEC [omd].[AddBatchToParentBatch]
214-
@BatchCode = @BatchCode,
215-
@ParentBatchCode = @ParentBatchCode,
216-
@Debug = @Debug,
217-
@SuccessIndicator = @RegisterRelSuccessIndicator OUTPUT,
218-
@MessageLog = @MessageLog OUTPUT;
219-
IF @RegisterRelSuccessIndicator = 'N'
212+
IF @ParentBatchInstanceId IS NOT NULL AND @ParentBatchInstanceId > 0
213+
AND @AutoRegisterRelationships = 'Y'
220214
BEGIN
221-
SET @SuccessIndicator = 'N';
222-
SET @LogMessage = CONCAT(N'Auto-registration of Batch relationship ' +
223-
N'between Parent Batch Instance Id ''', @ParentBatchInstanceId,
224-
''' and Child Batch Code ''', @BatchCode, ''' failed.');
215+
/* Get the Parents BATCH_ID and BATCH_CODE from the instance */
216+
SET @ParentBatchId = [omd].[GetBatchIdByBatchInstanceId](@ParentBatchInstanceId);
217+
DECLARE @ParentBatchCode NVARCHAR(500) =
218+
[omd].[GetBatchCodeById](@ParentBatchId);
219+
SET @LogMessage = CONCAT(N'Auto-registering any missing Batch relationship ' +
220+
N'between Parent Batch Code ''', @ParentBatchCode,
221+
''' and Child Batch Code ''', @BatchCode, '''.');
225222
IF @ProcessMessageLog = 'Y' SET @MessageLog =
226-
[omd].[AddLogMessage]('ERROR', DEFAULT, DEFAULT, @LogMessage, @MessageLog);
227-
IF @ThrowOnFailure = 'Y' THROW 50000, @LogMessage, 1;
228-
GOTO EndOfProcedureFailure;
223+
[omd].[AddLogMessage](DEFAULT, DEFAULT, DEFAULT, @LogMessage, @MessageLog);
224+
DECLARE @RegisterRelSuccessIndicator CHAR(1);
225+
EXEC [omd].[AddBatchToParentBatch]
226+
@BatchCode = @BatchCode,
227+
@ParentBatchCode = @ParentBatchCode,
228+
@Debug = @Debug,
229+
@SuccessIndicator = @RegisterRelSuccessIndicator OUTPUT,
230+
@MessageLog = @MessageLog OUTPUT;
231+
IF @RegisterRelSuccessIndicator = 'N'
232+
BEGIN
233+
SET @SuccessIndicator = 'N';
234+
SET @LogMessage = CONCAT(N'Auto-registration of Batch relationship ' +
235+
N'between Parent Batch Code ''', @ParentBatchCode,
236+
''' and Child Batch Code ''', @BatchCode, ''' failed.');
237+
IF @ProcessMessageLog = 'Y' SET @MessageLog =
238+
[omd].[AddLogMessage]('ERROR', DEFAULT, DEFAULT, @LogMessage, @MessageLog);
239+
IF @ThrowOnFailure = 'Y' THROW 50000, @LogMessage, 1;
240+
GOTO EndOfProcedureFailure;
241+
END
229242
END
230-
END
231-
232-
BEGIN TRY
233-
BEGIN TRANSACTION;
234243

235244
INSERT INTO omd.BATCH_INSTANCE
236245
(
@@ -265,23 +274,31 @@ BEGIN
265274
GOTO EndOfProcedureSuccess;
266275
END TRY
267276
BEGIN CATCH
277+
DECLARE
278+
@TxnErrorMessage NVARCHAR(4000) = ERROR_MESSAGE(),
279+
@TxnErrorNumber INT = ERROR_NUMBER(),
280+
@TxnErrorSeverity INT = ERROR_SEVERITY(),
281+
@TxnErrorState INT = ERROR_STATE(),
282+
@TxnErrorLine INT = ERROR_LINE();
283+
268284
IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;
269285
SET @SuccessIndicator = 'N';
270286
IF COALESCE(TRIM(@MessageLog), '') = '' SET @MessageLog = N'[]';
271287

272-
SET @LogMessage = 'Unknown Transaction Processing Error';
288+
SET @LogMessage = CONCAT('Transaction error (', @TxnErrorNumber, '/', @TxnErrorState,
289+
') at line ', @TxnErrorLine, ': ', COALESCE(@TxnErrorMessage, 'No additional details.'));
273290
IF @ProcessMessageLog = 'Y' SET @MessageLog =
274-
[omd].[AddLogMessage]('ERROR', DEFAULT, 'Process Output', @LogMessage, @MessageLog);
275-
IF @ThrowOnFailure = 'Y' THROW 50000, @LogMessage, 1;
291+
[omd].[AddLogMessage]('ERROR', DEFAULT, 'Transaction Error', @LogMessage, @MessageLog);
292+
IF @ThrowOnFailure = 'Y' THROW;
276293
GOTO EndOfProcedureFailure;
277294
END CATCH;
278295

279-
/* ----- Start of end state management -------------------------------------- */
296+
/* ----- Start of end state management ---------------------------------- */
280297

281298
EndOfProcedureFailure:
282299

283300
SET @SuccessIndicator = 'N';
284-
SET @LogMessage = N'Create Batch Instance Id process encountered errors.';
301+
SET @LogMessage = CONCAT(@ProcessDescription, N' encountered errors.');
285302
IF @ProcessMessageLog = 'Y' SET @MessageLog =
286303
[omd].[AddLogMessage]('ERROR', DEFAULT, 'Processing Error', @LogMessage, @MessageLog);
287304
SET @ReturnCode = -1;
@@ -291,7 +308,7 @@ BEGIN
291308
EndOfProcedureSuccess:
292309

293310
SET @SuccessIndicator = 'Y';
294-
SET @LogMessage = N'Create Batch Instance Id process completed successfully.';
311+
SET @LogMessage = CONCAT(@ProcessDescription, N' completed successfully.');
295312
IF @ProcessMessageLog = 'Y' SET @MessageLog =
296313
[omd].[AddLogMessage]('SUCCESS', DEFAULT, 'Processing Completion', @LogMessage, @MessageLog);
297314
SET @ReturnCode = 0;
@@ -319,7 +336,7 @@ BEGIN
319336
RETURN @ReturnCode;
320337
END TRY
321338

322-
/* ----- Common, standardized, Procedure-wrapping error handling ------------ */
339+
/* ----- Common, standardized, Procedure-wrapping error handling ---------- */
323340

324341
BEGIN CATCH
325342
/* reset all return/output values except the message log */
@@ -328,7 +345,6 @@ BEGIN
328345
SET @BatchInstanceId = NULL;
329346
SET @BatchInstanceStartTimestamp = NULL;
330347

331-
332348
IF @ProcessMessageLog <> 'Y' SET @MessageLog = N'[]'
333349
ELSE SET @MessageLog =
334350
[omd].[AddLogMessage]('DEBUG', DEFAULT, N'Parameter @SuccessIndicator',
@@ -393,5 +409,6 @@ BEGIN
393409

394410
IF @ThrowOnFailure = 'Y' THROW;
395411
RETURN @ReturnCode;
412+
396413
END CATCH;
397414
END;

Direct_Framework/Stored Procedures/omd.CreateLoadWindow.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ BEGIN
126126
EXEC [omd].[InsertIntoEventLog] @EventDetail = @EventDetail;
127127
SET @MessageLog = [omd].[AddLogMessage]('ERROR', DEFAULT, 'Parameter Error', @LogMessage, @MessageLog);
128128

129-
GOTO FailureEndOfProcedure
129+
GOTO EndOfProcedureFailure
130130
END
131131

132132
-- Figure out what the source is.
@@ -254,7 +254,7 @@ BEGIN
254254

255255
GOTO EndOfProcedure;
256256

257-
FailureEndOfProcedure:
257+
EndOfProcedureFailure:
258258

259259
SET @SuccessIndicator = 'N'
260260
SET @StartValue = NULL;

0 commit comments

Comments
 (0)