Files
jRDC-MultiDB-Hikari/ParameterValidationUtils.bas
jaguerrau 9c9e2975e9 - VERSION 5.10.27
- feat(arquitectura): Consolidación de estabilidad y diagnóstico.
- refactor: Arquitectura de base de datos local y políticas de logs.
- arch(sqlite): Aislamiento total de las conexiones SQLite en SQL_Auth y SQL_Logs. Esto protege las operaciones de autenticación críticas de la alta carga de I/O generada por el subsistema de logs.
- feat(logs): Implementación de modo de almacenamiento flexible para logs (disco o en memoria), mejorando la capacidad de testing.
- refactor(logs): Se estandariza el límite de retención de registros a 10,000 para todas las tablas de logs, y se renombra la subrutina de limpieza a borraArribaDe10000Logs.
2025-10-29 05:25:56 -06:00

71 lines
3.3 KiB
QBasic

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