Enum candidates: repo enum-candidates
Reports columns, fields and properties that are really enums, scored from code, DFMs, the repository and the database; read only.
Read this when you look for the places a Delphi enum (and the D_BYTETAG domain) should replace a bare code. drag-lint repo enum-candidates --db <index> scans the indexes with the same engine as the used-as-enum rule (the per-file view of the same facts), joins the repository (--repo reads the ISENUM column) and the database (--connection), drops a candidate with one distinct value, and prints a ranked report. It writes nothing; repo enum-gen turns the report into code.
How to run it
drag-lint repo enum-candidates --db <index> [--db <index> ...] [--project <x.dproj>] [--repo <file.dlrepo>] [--connection "<firebird>"] [--fields "A,B"] [--json] [--output <file>]
Full flag list: repo in the CLI reference.
Flags
| Flag | Argument | Meaning | Default |
|---|---|---|---|
--db | <index> | ||
--project | <x.dproj> | ||
--repo | <file.dlrepo> | ||
--connection | "<firebird>" | ||
--fields | "A,B" | ||
--json | |||
--output | <file> |
Where to click
No IDE surface: this feature runs from the CLI (see How to run it).
Details
Since 1.64.0-alpha. ORM3 enum tools: find the columns, fields and properties that are really enums -- a CHAR(1) holding 'A' / 'R', a SMALLINT holding 1 / 2 / 3 -- so they can become a Delphi enum (and, for a column, the D_BYTETAG domain). Both read only; nothing is written anywhere.
The rule: used-as-enum
A per-file hint, OFF by default. It reports a variable, parameter, field, property or dataset field that
the unit ONLY compares with (=, <>), tests with in [..], case's over, or assigns a closed set of
2-10 code literals.
- A code literal is a string of 1-8 letters, digits or
_(#65is 'A'), or an integer from -128 to 255. - A dataset field is one subject however it is written:
Q.FieldByName('STATUS').AsString,Q['STATUS'],Q.FieldValues['STATUS'],Q.FieldByName('Status').AsString[1];Trim(..)/UpperCase(..)around it and the.AsString/.AsInteger/.Valueaccessor are ignored. - Anything else taints the subject and silences it: a compare with a variable or a long text, an ordering
compare (
<,>=), a set or case label holding a non-literal or a range, a compound assignment. A blank (= '',:= '') is neutral. An assignment from a non-literal is allowed. - An integer subject also needs no arithmetic and either a case statement or three values (a 0 / 1 pair is a flag or a count).
- Two sites at least, one of them a read.
drag-lint lint <file.pas> --rule used-as-enum
drag-lint lint-all --db <project.sqlite> --rule used-as-enum
Example message: `Q.FieldByName('STATUS').AsString is only used against the closed set 'A', 'R', 'X' (5 sites, 3 compare / in / case): make it an enum (column STATUS: D_BYTETAG; see repo enum-candidates)`.
The report: repo enum-candidates
drag-lint repo enum-candidates --db <client.sqlite> [--db <server.sqlite> ...] [--project <x.dproj>]
[--repo <file.dlrepo>] [--connection "<firebird>"] [--fields "A,B"]
[--json] [--output <file>]
It scans every unit of the given indexes with the same engine as the rule, then joins four kinds of evidence per candidate:
| evidence | from | |
|---|---|---|
| (a) | the closed literal set of the code, its sites, its taints | the units on disk + ds_field_accesses / refs of the index (which column, which symbol) |
| (b) | a data-aware control or grid column bound to the field with a fixed list (Values.Strings, Items.Strings, Properties.Items Value / Description) | the DFM facts of the index |
| (c) | the column type (CHAR(1..2), SMALLINT, a BYTETAG domain), ISENUM / ENUMTYPE, a name like *CODE, *TYPE*, *STATUS*, *KIND*, *STATE* | --repo (DL_FIELDS_INFO), the index's Firebird snapshot, or --connection |
| (d) | the distinct values and their row counts, NULLs, and every value OUTSIDE the code / DFM set | --connection only: one read-only snapshot transaction, rolled back |
A site maps to a column through facts the index already has: the literal field name, a dataset field
access inside the operand (a persistent TField through its DFM FieldName), else the resolved symbol.
Identities: FIELD:STATUS, SYM:Unit.TClass.FStatus, COMP:QSTATUS (a persistent field the DFM does
not name), TEXT:Unit:key (unresolved). Locals and parameters are left out of the report.
Score: closed code set +3 (taint -3, otherwise +1), DFM list +2, byte-sized type +2, name +1, at most 16 database values +2 (more than 64: -3). high >= 6, medium >= 3, low below; below 2 is not listed.
Owner rule: a database value outside the enum is a bug. It is listed (OUT OF SET 'Z'=1), never mapped.
--connection refuses OpenMode, CreateDatabase, DropDatabase and ConnectionDef keys before it opens
anything.
JSON (format enum-candidates/1) -- the input of the helper generator
{"format":"enum-candidates/1","units":N,"missingUnits":N,"subjects":N,"candidates":[
{"identity":"FIELD:STATUS","field":"STATUS","shown":"Q.FieldByName('STATUS')","literalKind":"text",
"values":["A","R","X"],"codeValues":["A","R","X"],"confidence":"high","score":10,"reasons":[..],
"nameHint":true,"taints":0,"arith":0,"opaqueWrites":0,
"sites":[{"file":..,"line":..,"col":..,"kind":"compare|in|case|assign","values":[..]}],
"dfm":[{"file":..,"line":..,"component":"rgStatus","class":"TDBRadioGroup","values":["A","R","X"],
"captions":["Active","Retired","Expired"]}],
"columns":[{"table":"ORDERS","sqlType":"CHAR(1)","domain":"..","charLength":1,"isEnum":false,
"enumType":"","source":"repo|index|database","dbChecked":true,
"dbValues":[{"value":"A","count":3},..],"dbNulls":1,"dbCapped":false,
"outOfSet":[{"value":"Z","count":1}],"dbError":""}]}]}
values is the code set followed by DFM values the code never names; captions line up with the DFM
values. Text codes keep their case; integer codes are decimal.
Source: src\sql\DRagLint.FieldRepo.EnumCand.pas (scan), ...EnumCandReport.pas, ...EnumCandDb.pas
(database), src\lint\v164\DRagLint.V164.EnumCand.pas (rule), src\cli\DRagLint.CLI.RepoEnumCand.pas.
Test: tests\autotest\run_v164_enumcand.ps1.