Finding the Enums Hiding in Your Database
drag-lint 1.64 finds the columns, fields and properties that only ever hold a small closed set of codes, and turns them into Delphi enums with record helpers and DUnitX tests. A CHAR(1) column that holds 'A', 'R' or 'X' is an enum that nobody declared: every unit that reads it repeats the letters, and the compiler cannot tell you when one of them is misspelled.
repo enum-candidates and repo enum-gen are new in drag-lint 1.64.0-alpha, released 2026-12-03. Every output below is real output of 1.64.0-alpha on the small form in section 01 (paths shortened to ...).
Status codes stored as single letters are everywhere in Micronite, the quality and production-control software we build on Firebird, and in most Delphi database applications old enough to have outlived their first designer. The letters work, but they live as string literals scattered over forms, data modules and SQL. A Delphi enum would give the compiler something to check; finding which columns deserve one, by hand, means reading every unit. drag-lint already has the code, the forms and the database schema in its indexes, so 1.64 does the reading.
A form that treats a column as an enum
The form below has a query Q with a STATUS column and a persistent integer field QKIND. Nothing declares what values they may hold; the code just compares and assigns literals:
procedure TEnumPosForm.Approve;
begin
if Q.FieldByName('STATUS').AsString = 'A' then Exit;
Q.Edit;
Q.FieldByName('STATUS').AsString := 'A';
Q.Post;
end;
procedure TEnumPosForm.Reject;
begin
if Q['STATUS'] <> 'R' then
Q.FieldByName('Status').AsString := 'R';
end;
function TEnumPosForm.IsOpen: Boolean;
begin
Result := Q.FieldByName('STATUS').AsString[1] in ['A', 'X'];
end;
procedure TEnumPosForm.ShowKind;
begin
case QKIND.AsInteger of
1: Caption := 'one';
2, 3: Caption := 'more';
end;
if QKIND.Value = 4 then Caption := 'four';
end;
procedure TEnumPosForm.SetMode(const AOpen: Boolean);
begin
if AOpen then FMode := 'OP' else FMode := 'CL';
end;
The form file adds a TDBRadioGroup bound to STATUS, which says the same thing a second way, with captions:
Caption = 'Status'
DataField = 'STATUS'
DataSource = DS
Items.Strings = (
'Active'
'Retired'
'Expired')
Values.Strings = (
'A'
'R'
'X')
repo enum-candidates
After an ordinary drag-lint index of the folder, one command reads the index and prints a ranked list. It writes nothing:
drag-lint repo enum-candidates --db fx.sqlite
repo enum-candidates: 3 candidate(s); 2 unit(s) scanned, 11 subject(s) with a code literal
HIGH FIELD:STATUS values 'A', 'R', 'X'
code: closed set of 3 value(s) at 5 site(s); dfm: 1 fixed value list(s); name: *CODE / *TYPE* / *STATUS* / *KIND* / *STATE*
dfm ...\EnumPos.dfm:13 rgStatus: TDBRadioGroup A, R, X
...\EnumPos.pas:33:6 compare A
...\EnumPos.pas:35:3 assign A
...\EnumPos.pas:41:6 compare R
...\EnumPos.pas:42:5 assign R
...\EnumPos.pas:47:13 in A, X
MEDIUM FIELD:KIND values 1, 2, 3, 4
code: closed set of 4 value(s) at 2 site(s); name: *CODE / *TYPE* / *STATUS* / *KIND* / *STATE*
...\EnumPos.pas:52:8 case 1, 2, 3
...\EnumPos.pas:56:6 compare 4
MEDIUM SYM:EnumPos.TEnumPosForm.FMode values 'OP', 'CL'
code: closed set of 2 value(s) at 4 site(s)
...\EnumPos.pas:61:17 assign OP
...\EnumPos.pas:61:36 assign CL
...\EnumPos.pas:66:6 compare OP
...\EnumPos.pas:67:11 compare CL
Three things are worth noticing. The five STATUS sites are written four different ways (FieldByName, the default array property, a different case, an indexed character) and still count as one subject. The radio group’s value list agrees with the code, which is why STATUS is rated HIGH. And FMode is not a database column at all: a plain field that only ever holds 'OP' or 'CL' is an enum candidate too, and the comparison with #67'L' on line 67 is recognised as 'CL'.
How a subject becomes a candidate
A variable, field, property or dataset field is a candidate when the code only compares it with (=, <>), tests it with in [..], runs a case over, or assigns it a closed set of 2 to 10 short literals, at two sites or more, one of them a read. Anything else silences it: a compare with a variable, an ordering compare such as <, a range in a case label. An integer subject also needs no arithmetic and either a case or three values, because a 0 / 1 pair is usually a flag or a count.
The code is one source. The report also reads fixed value lists in the forms, the ISENUM flag of a Field Repository (--repo), and, with --connection, the distinct values actually stored in the Firebird database. A candidate whose column holds only one distinct value is dropped.
The same per-unit test is available as a lint rule, used-as-enum, off by default: drag-lint lint-all --db <project.sqlite> --rule used-as-enum.
repo enum-gen
The JSON form of the report (--json --output cand.json) is the input of the generator:
drag-lint repo enum-gen --in cand.json --output FixEnums.pas
enum TStatus <- FIELD:STATUS: 3 codes, ordinals 0..88, 86 fillers/undefined
enum TKind <- FIELD:KIND: 4 codes, ordinals 0..4, 1 fillers/undefined
skipped SYM:EnumPos.TEnumPosForm.FMode: code 'OP' is not one character: a multi-character code needs an owner map to a byte
2 enum(s) written to FixEnums.pas
It wrote two units, FixEnums.pas (291 lines) and FixEnumsTests.pas. The rule behind the generated enums is that the ordinal is the stored code: Ord('A') is 65, so Status_Active has ordinal 65, and the gaps are filled with named members that are never valid. This is the shortened declaration:
type
EFixEnumsEnumRange = class(Exception);
TStatus = (
Status_Undefined, Status_Filler1, Status_Filler2, Status_Filler3,
...
Status_Filler64, Status_Active, Status_Filler66, Status_Filler67,
...
Status_Filler80, Status_Filler81, Status_Retired, Status_Filler83,
Status_Filler84, Status_Filler85, Status_Filler86, Status_Filler87,
Status_Expired
);
TStatusHelper = record helper for TStatus
class function FromByte(const pValue: Byte): TStatus; static;
class function TryFromByte(const pValue: Byte; out pResult: TStatus): Boolean; static;
class function FromInteger(const pValue: Integer): TStatus; static;
class function FromString(const pValue: string): TStatus; static;
class function TryFromString(const pValue: string; out pResult: TStatus): Boolean; static;
function ToByte: Byte;
function ToInteger: Integer;
function ToString: string;
function ToCaption: string;
function IsValid: Boolean;
end;
Because the ordinal is the code, the conversion is a cast plus a membership check, and nothing has to be kept in step with a lookup table:
class function TStatusHelper.TryFromString(const pValue: string; out pResult: TStatus): Boolean;
begin
pResult:= Low(TStatus);
Result:= (Length(pValue) = 1) and (Ord(pValue[1]) <= 255) and TryFromByte(Byte(Ord(pValue[1])), pResult);
end;
function TStatusHelper.ToCaption: string;
begin
case Self of
Status_Active: Result:= 'Active';
Status_Retired: Result:= 'Retired';
Status_Expired: Result:= 'Expired';
else
Result:= '';
end;
end;
The member names and captions come from the radio group in the form. A value that is not a member raises EFixEnumsEnumRange, one exception class per generated unit, instead of quietly becoming a default. Approve can then read if TStatus.FromString(Q.FieldByName('STATUS').AsString) = Status_Active then Exit;, and a typo in Status_Active no longer compiles.
Every public declaration in the generated unit carries a DocInsight comment, and the test unit holds five DUnitX tests per enum: byte and integer round trip, text round trip, fillers are invalid, out of range raises, and a component stream round trip.
One production schema
Before release we ran repo enum-candidates on the client and server projects of one real ERP application with 139 tables and its Field Repository. It reported 35 candidates; reviewed by hand, 26 were real enums and 9 were not. All 20 rated MEDIUM were real; of the 15 rated LOW, 6 were. We do not show the table and column names here, because the code is a customer’s.
A deterministic Delphi algorithm, not AI
The report comes from the index drag-lint builds by parsing your source and forms, plus the repository and the database when you give them. The generator turns that report into code by fixed rules. Nothing is guessed or written by a model: the same index gives the same report, and the same report gives the same unit, byte for byte.
What to know before you rely on it
- Only one-character text codes and integer codes from 0 to 255 become enums. A multi-character code such as
'OP'is reported but skipped by the generator; it needs a mapping to a byte that you decide. - A letter code gives an enum with up to 255 members, most of them fillers (
TStatusabove has 89). That is the price of using the stored code as the ordinal. - An integer code with no caption in a form gets the digit as its caption and as its member name (
Kind_N1); rename the members by hand if you want better names, then keep them. - We compiled
FixEnums.paswith the Delphi 13 Win64 compiler: no errors, twoW1023warnings (comparing signed and unsigned types inTryFromByte). We did not run the generated tests for this article. - The report is a list to review, not a change to apply blindly: on the real schema above, about one candidate in four was not an enum.
- Changing the column itself (for example to a byte domain) is your decision; neither command writes to the database or to your source.