Lint rules sql-param-mismatch and query-in-loop
Warns when ParamByName names a parameter the SQL does not have or a SQL parameter is never set, and hints at a query run once per loop iteration.
Read this to see which SQL texts the parameter check understands, which loops count as row loops, and what both rules leave alone.
How to run it
drag-lint lint <path> [--profile <name>] [--target-compiler <ver|auto>] [--rule <id>|help] [--disable id1,id2] [--rules-dir <dir>] [--json] [--labels] [--no-preprocess] [--library-db <lib.sqlite>] [--build-config <name>] [--no-budget]
Example:
drag-lint lint <file.pas> --rule sql-param-mismatch --db <index.sqlite>
Full flag list: lint in the CLI reference.
Where to click
No IDE surface: this feature runs from the CLI (see How to run it).
Details
Two checks for code that runs SQL through a dataset component, and an option of field-by-name-in-loop. They run in
lint and lint-all, in the IDE and in VS Code.
drag-lint lint <unit.pas> --db <project.sqlite> --rule sql-param-mismatch
drag-lint lint-all --db <project.sqlite> --rule query-in-loop
Turn one off with "disabled": ["query-in-loop"] in the lint config, or approve one line with a dl:ok marker.
sql-param-mismatch (warning, firedac)
Reads the one SQL text of a query and compares its parameters with the code:
Q.ParamByName('ID2')when the SQL has only:ID-- FireDAC raises "Parameter [ID2] not found" at run time.Q.Open/Q.ExecSQLwhen one of the SQL's parameters is never set -- reported at the first run.
The SQL may be a literal, literals joined with +, a constant of the unit or (with the index) of another unit,
SQL.Clear + SQL.Add lines in one statement list, Open('..'), or the SQL.Strings / CommandText of the form. The query may be a local variable, a field or component
of the class, or a component of another unit's data module (DM.qOrders, needs the index). Parameter names are compared
without case. Known classes: TFDQuery, TFDCommand, TQuery, TADOQuery, TSQLQuery, TIBQuery, TIBSQL and the UniDAC, Zeos,
MyDAC, ODAC, SDAC, PgDAC and IBDAC queries.
Silent when:
- the SQL is built by an expression, loaded from a file, or set in more than one place;
- the SQL has a macro (
&X,!X), positional (?) or numbered (:1) parameters, or a colon name in a comment; it isEXECUTE BLOCKor DDL (the colons are PSQL variables);::casts and colons in quoted text are not parameters; ParamCheckorResourceOptions.ParamCreateis switched, or code adds, clears or parsesParamsitself;- the dataset is a parameter, is passed on, aliased or put into a
withof several targets, or is not created by oneT.Createin the routine (locals); - the component is inherited, or a descendant form has its own SQL;
- for a field or a component: no index, or another unit sets its SQL or takes the dataset over;
- never-set only: a parameter is set by a name the code computes, by position or as
Open(sql, [values]); aMasterSource/DataSourcefills the parameters; the form stores a value inParamData; the statement is a stored procedure call or hasRETURNING .. INTO :x; a component whose parameters no code of the unit sets.
query-in-loop (hint, firedac, ON by default since 1.55)
It runs with no option (owner ruling 2026-11-26: a query per iteration is very slow, BDE most of all). Turn it off with "disabled": ["query-in-loop"], or approve a line with a dl:ok marker.
A dataset is opened or executed once per loop iteration:
D.Open,ExecSQL,Execute,ExecProc,Active := Truein the body offor,for in,whileorrepeat;- with the index: a call in the loop of a routine whose own SQL the index knows (
symbol_facts.sql_reads/sql_writes), directly or one call further down, over certain call edges only.
Silent when:
- the receiver is the loop variable or is taken from it (
for DS in List do DS.Open,List[I].Open), or is assigned in the loop from something else than aCreate; - the loop has at most ten literal steps (
for I := 0 to 3,for S in ['A', 'B']); - a
while/repeatthat does not step over rows or a list (its condition names neitherEof/Bofnor< Count), orfor X := 0 to MaxDepth: a chain or worklist walk, where the next step depends on what this one read; - each step asks another table, database or connection: the table name is an expression (
'.. FROM ' + T), aPRAGMA,DatabaseName/TableName/ConnectionName/StoredProcNameis set in the loop,Connectioncomes from the loop variable, or the loop variable is a connection handed to the called routine; - it is a retry loop (
try .. exceptaround the call and a way out, or a counter named retry / attempt / tries) or a polling loop (while True,Terminated,Sleep); the loop condition names the same dataset (until Q.RowsAffected = 0); - the branch leaves the loop (
Break,Exit,raiseat the end of its statement list): the query runs once; - an
ifasks a cache or the dataset first (TryGetValue,ContainsKey,not D.Active,= nil); a called routine that uses a dictionary cache is not followed; - array DML (
Execute(Count),Params.ArraySize); in-memory datasets; a whole statement taken from a list or a DDL text; - an anonymous method or nested routine declared in the loop; a call more than two levels away.
field-by-name-in-loop: ParamByName (option, OFF)
"thresholds": {"field-by-name-in-loop.param_by_name": 1} makes the rule also report ParamByName in a loop, one
finding per outermost loop (hint). Not reported: a loop that sets the SQL, creates the dataset or passes it on, and a loop
with a FieldByName (that finding and its fix come first). There is no automatic fix for this part.
What the rules add over the compiler
dcc64 37.0 with all warnings and hints compiles both cases without a message; the parameter error appears only when the line runs.
Measured
Read-only copies of four project indexes, 2026-11-23, one file and one rule at a time. Not measured yet: the largest units of drag-lint (over 900 KB), the Micronite and ORM3 projects (the Micronite copy at hand is an old index without form facts).
| corpus | sql-param-mismatch | query-in-loop |
|---|---|---|
| drag-lint (73 units) | 0 | 97, then 65 after the refinements; 33 read: about 13 walk a chain or ask one database per step |
| Loader2025 (78 units) | 0 | 12 before, of which 10 open a table in another folder per step (now silent), 2 right |
| TableTools370P (6 units) | 0 | 9, all right |
| YADF GuardTest, FDGPRuntime | 0 | 0 |
sql-param-mismatch had 325 queries with one known SQL text to judge, 146 of them with ParamByName, and found no
mistake: it ships ON, not measured in the field. query-in-loop is above the 10 % limit on drag-lint's own graph code
and shipped OFF in 1.54 (ON since 1.55 by owner ruling); on the two database applications every finding left was right. The ParamByName option stays OFF.