Commit 7388593
authored
FIX: False positive qmark detection for ? inside bracketed identifiers, string literals and comments (#465)
### Work Item / Issue Reference
<!--
IMPORTANT: Please follow the PR template guidelines below.
For mssql-python maintainers: Insert your ADO Work Item ID below
For external contributors: Insert Github Issue number below
Only one reference is required - either GitHub issue OR ADO Work Item.
-->
<!-- mssql-python maintainers: ADO Work Item -->
>
[AB#42937](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/42937)
<!-- External contributors: GitHub Issue -->
> GitHub Issue: #464
-------------------------------------------------------------------
### Summary
This pull request introduces a robust fix for detecting real parameter
placeholders in SQL statements, specifically addressing false positives
caused by question marks inside bracketed identifiers, string literals,
quoted identifiers, and comments. The changes add context-aware scanning
logic and comprehensive tests, ensuring that only actual parameter
placeholders are flagged and handled. This resolves a bug where SQL
containing `?` inside brackets (e.g., `[q?marks]`) would incorrectly
trigger parameter mismatch errors.
### Core logic improvements
* Added `_skip_quoted_context` helper in `parameter_helper.py` to
accurately skip over single-line comments, multi-line comments,
single-quoted string literals (with escaped quotes), double-quoted
identifiers, and bracketed identifiers when scanning SQL for
placeholders.
* Added `_has_unquoted_question_marks` function to detect real `?`
placeholders only outside quoted contexts, using the new
context-skipping logic.
* Updated `detect_and_convert_parameters` to use
`_has_unquoted_question_marks` for parameter style mismatch detection,
preventing false positives when `?` appears inside bracketed identifiers
or other quoted contexts.
### Testing improvements
* Added extensive unit tests for `_skip_quoted_context` and
`_has_unquoted_question_marks`, covering all relevant SQL quoting and
commenting scenarios, including edge cases like unclosed
quotes/brackets.
* Added integration tests verifying that SQL with `?` inside bracketed
identifiers, string literals, and comments does not trigger parameter
style mismatch errors, and that real placeholders are still detected
correctly.
### Test harness updates
* Imported the new helper functions in the test module to facilitate
direct testing.1 parent 95eef16 commit 7388593
2 files changed
Lines changed: 649 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| |||
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
21 | 153 | | |
22 | 154 | | |
23 | 155 | | |
24 | 156 | | |
25 | | - | |
26 | | - | |
27 | | - | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
28 | 160 | | |
29 | 161 | | |
30 | 162 | | |
| |||
52 | 184 | | |
53 | 185 | | |
54 | 186 | | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
55 | 193 | | |
56 | 194 | | |
57 | 195 | | |
| |||
317 | 455 | | |
318 | 456 | | |
319 | 457 | | |
320 | | - | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
321 | 461 | | |
322 | 462 | | |
323 | 463 | | |
| |||
0 commit comments