/// <summary> /// This method will parameterize the given SqlCommand. /// Any single literal on the RHS of a declare statement will be parameterized /// Any other literals will be ignored /// </summary> /// <param name="commandToParameterize">Command that will need to be parameterized</param> public static void Parameterize(this DbCommand commandToParameterize) { TSqlFragment rootFragment = GetAbstractSyntaxTree(commandToParameterize); TsqlMultiVisitor multiVisitor = new TsqlMultiVisitor(isCodeSenseRequest: false); // Use the vistor pattern to examine the parse tree rootFragment.AcceptChildren(multiVisitor); // Now walk the tree //reformat and validate the transformed command SqlScriptGenerator scriptGenerator = GetScriptGenerator(); scriptGenerator.GenerateScript(rootFragment, out string formattedSQL); if (!string.IsNullOrEmpty(formattedSQL)) { commandToParameterize.CommandText = formattedSQL; } commandToParameterize.Parameters.AddRange(multiVisitor.Parameters.ToArray()); multiVisitor.Reset(); }
/// <summary> /// Parses the given script to provide message, warning, error. /// </summary> /// <param name="scriptToParse">Script that will be parsed</param> /// <param name="telemetryManager">Used to emit telemetry events</param> /// <returns></returns> public static IList <ScriptFileMarker> CodeSense(string scriptToParse) { if (scriptToParse == null) { return(EmptyCodeSenseItemList); } int CurrentScriptlength = scriptToParse.Length; if (CurrentScriptlength > maxStringLength) { ScriptFileMarker maxStringLengthCodeSenseItem = new ScriptFileMarker { Level = ScriptFileMarkerLevel.Error, Message = SR.ScriptTooLarge(maxStringLength, CurrentScriptlength), ScriptRegion = new ScriptRegion { // underline first row in the text StartLineNumber = 1, StartColumnNumber = 1, EndLineNumber = 2, EndColumnNumber = 1 } }; return(new ScriptFileMarker[] { maxStringLengthCodeSenseItem }); } TSqlFragment rootFragment = GetAbstractSyntaxTree(scriptToParse); TsqlMultiVisitor multiVisitor = new TsqlMultiVisitor(isCodeSenseRequest: true); // Use the vistor pattern to examine the parse tree rootFragment.AcceptChildren(multiVisitor); // Now walk the tree if (multiVisitor.CodeSenseErrors != null && multiVisitor.CodeSenseErrors.Count != 0) { multiVisitor.CodeSenseMessages.AddRange(multiVisitor.CodeSenseErrors); } return(multiVisitor.CodeSenseMessages); }