B4J=true Group=Default Group ModulesStructureVersion=1 Type=StaticCode Version=10.3 @EndOfDesignText@ ' File: ParameterValidationUtils.bas ' Utility module: ParameterValidationUtils ' Centralizes SQL parameter validation and adjustment logic. ' Now supports trimming of excessive parameters. Sub Process_Globals ' The ParameterValidationResult Type is declared in Main.bas, not here. End Sub ' Validates and adjusts the parameter list for SQL execution, applying tolerance logic. ' Returns a ParameterValidationResult indicating success/error and the parameters to use. Public Sub ValidateAndAdjustParameters (CommandName As String, DBKey As String, sqlCommand As String, receivedParams As List, IsToleranceEnabled As Boolean) As ParameterValidationResult Dim res As ParameterValidationResult res.Initialize res.Success = True ' Assume success initially ' Log(">>>> IsToleranceEnabled: " & IsToleranceEnabled) ' Ensure receivedParams is initialized, even if it's empty or Null If receivedParams = Null Or receivedParams.IsInitialized = False Then receivedParams.Initialize ' Initialize an empty list if Null or uninitialized. End If ' Count how many '?' are in the SQL statement to know how many parameters are expected. Dim expectedParams As Int = sqlCommand.Length - sqlCommand.Replace("?", "").Length Dim receivedParamsSize As Int = receivedParams.Size If receivedParamsSize < expectedParams Then ' Fewer parameters were received than expected. This is an error. res.Success = False res.ErrorMessage = $"ERROR: Insufficient number of parameters for "${CommandName}" (DB: ${DBKey}). Expected ${expectedParams} and received ${receivedParamsSize}."$ Log(res.ErrorMessage) Main.LogServerError("ERROR", "ParameterValidationUtils.ValidateAndAdjustParameters", res.ErrorMessage, DBKey, CommandName, Null) Return res Else If receivedParamsSize > expectedParams Then ' More parameters were received than expected. If IsToleranceEnabled Then ' We only trim if tolerance is enabled Dim adjustedParams As List adjustedParams.Initialize For i = 0 To expectedParams - 1 adjustedParams.Add(receivedParams.Get(i)) Next res.ParamsToExecute = adjustedParams res.Success = True Dim WarningMsg As String = $"WARNING: More parameters received than expected for "${CommandName}" (DB: ${DBKey}). Expected ${expectedParams} and received ${receivedParamsSize}. Adjusted parameter list to ${expectedParams} items."$ ' Log(WarningMsg) ' Log("Cache: " & Main.LOG_CACHE_THRESHOLD & "|" & Main.ErrorLogCache.Size) Main.LogServerError("WARNING", "ParameterValidationUtils.ValidateAndAdjustParameters", WarningMsg, DBKey, CommandName, Null) Else ' If tolerance is NOT enabled, this is a critical error. res.Success = False res.ErrorMessage = $"ERROR: Excessive number of parameters for "${CommandName}" (DB: ${DBKey}). Expected ${expectedParams} and received ${receivedParamsSize}. Extra parameter tolerance is DISABLED."$ Log(res.ErrorMessage) Main.LogServerError("ERROR", "ParameterValidationUtils.ValidateAndAdjustParameters", res.ErrorMessage, DBKey, CommandName, Null) Return res End If Else ' The EXACT number of parameters was received. All good. res.ParamsToExecute = receivedParams ' Use the original list as-is. res.Success = True ' Confirm success. End If Return res End Sub