TEgStrParser is a non-visual component that wraps
ParseInput. Drop it from the Easygate
category in the Tool Palette onto a form or data module, set
SqlDialect, DefaultOptions,
DefaultRangeSep, and DefaultOperator once in
the Object Inspector, and call Parse() from event handlers.
A single component is usually sufficient for the whole form; use
multiple components only when different sections of the form need
different dialects or default operators.
The component never reads the input value from a control directly —
the caller always passes the text explicitly. This is intentional: the
relevant property differs by control type (Text,
ItemIndex, Value, etc.) and only the caller
knows which one applies.
The component is declared in unit EgStrParserComp; all
library types and constants (TParseResult,
TInputType, TSqlDialect, ERR_*,
etc.) are declared in unit EgStrParser. When the component
is dropped onto a form or data module, the IDE adds
EgStrParserComp to the uses clause
automatically, but does not add
EgStrParser. Any unit that references
TParseResult, TInputType,
TSqlOperator, TParseOptions, or any
ERR_* constant must include EgStrParser in its
own uses clause explicitly.
Concurrent Parse() calls on different
TEgStrParser instances are thread-safe. Concurrent calls on
the same instance are not. See Thread safety for the full analysis
and recommended patterns.
TEgStrParser = class(TComponent)
public
constructor Create(AOwner: TComponent); override;
// Overload 1: all parameters explicit.
function Parse(const Input: string; ValueType: TInputType;
RangeSep: Char; Options: TParseOptions;
SqlOperator: TSqlOperator;
Sender: TComponent = nil): TParseResult; overload;
// Overload 2: uses DefaultRangeSep.
function Parse(const Input: string; ValueType: TInputType;
Options: TParseOptions; SqlOperator: TSqlOperator;
Sender: TComponent = nil): TParseResult; overload;
// Overload 3: uses DefaultRangeSep and DefaultOperator.
function Parse(const Input: string; ValueType: TInputType;
Options: TParseOptions;
Sender: TComponent = nil): TParseResult; overload;
// Overload 4: uses DefaultRangeSep and DefaultOptions.
function Parse(const Input: string; ValueType: TInputType;
SqlOperator: TSqlOperator;
Sender: TComponent = nil): TParseResult; overload;
// Overload 5: all defaults from component properties.
function Parse(const Input: string; ValueType: TInputType;
Sender: TComponent = nil): TParseResult; overload;
property LastResult : TParseResult read FLastResult;
property LastControl: TComponent read FLastControl;
published
property SqlDialect : TSqlDialect read FSqlDialect write FSqlDialect default sqlElevateDB;
property DefaultOptions: TParseOptions read FDefaultOptions write FDefaultOptions default [];
property DefaultRangeSep: Char read FDefaultRangeSep write FDefaultRangeSep default '~';
property DefaultOperator: TSqlOperator read FDefaultOperator write FDefaultOperator default sopEqual;
end;property SqlDialect: TSqlDialect default sqlElevateDB;The SQL dialect passed to ParseInput on every
Parse() call. Set it once at design time to match the
target database. The value is stored per-component and passed directly
to ParseInput overload 1; the global
GDefaultDialect variable is never read or written.
Default: sqlElevateDB. See SQL dialects.
property DefaultOptions: TParseOptions default [];The set of TParseOption flags used by overloads that do
not accept an explicit Options parameter. Overloads that do
accept Options use the supplied value instead.
Default: []. See Parse options.
property DefaultRangeSep: Char default '~';The range separator used by overloads that do not accept an explicit
RangeSep parameter. Must not be ,,
;, -, ., ' (single
quote), or space; any of these causes ParseInput to return
ERR_INVALID_RANGESEP.
Default: '~'.
property DefaultOperator: TSqlOperator default sopEqual;The SQL operator used by overloads that do not accept an explicit
SqlOperator parameter.
Default: sopEqual. See Operators.
property LastResult: TParseResult;The TParseResult returned by the most recent
Parse() call on this instance. Not meaningful before the
first call.
property LastControl: TComponent;The Sender argument passed to the most recent
Parse() call, or nil when Sender
was omitted. Useful in shared event handlers to identify which control
triggered the parse. The component stores the reference but does not own
it.
All overloads return a TParseResult and store it in
LastResult. SqlDialect is always taken from
the component property and cannot be overridden per call.
Sender is optional in all overloads and defaults to
nil.
| Overload | RangeSep | Options | SqlOperator |
|---|---|---|---|
| 1 — full form | explicit | explicit | explicit |
| 2 | DefaultRangeSep |
explicit | explicit |
| 3 | DefaultRangeSep |
explicit | DefaultOperator |
| 4 | DefaultRangeSep |
DefaultOptions |
explicit |
| 5 — minimal | DefaultRangeSep |
DefaultOptions |
DefaultOperator |
// Minimal: all defaults from component properties.
R := EgParser1.Parse(Edit1.Text, itInteger);
// With Sender: identifies the control in a shared handler.
R := EgParser1.Parse(Edit1.Text, itDate, Sender as TComponent);
// Override operator only.
R := EgParser1.Parse(Edit1.Text, itString, sopLikeRight);
// Override options only.
R := EgParser1.Parse(Edit1.Text, itString, [opTrim, opUpperCase]);
// Override options and operator.
R := EgParser1.Parse(Edit1.Text, itDate, [opNotNull], sopGreaterEqual);
// Full explicit form (also overrides RangeSep).
R := EgParser1.Parse(Edit1.Text, itInteger, '|', [opRequired], sopEqual);A single component on a data module can serve multiple fields by
inspecting Sender:
procedure TdmOrders.FilterFieldExit(Sender: TObject);
var
Edit: TEdit;
R: TParseResult;
begin
Edit := Sender as TEdit;
if Edit = edOrderDate then
R := Parser1.Parse(Edit.Text, itDate, Sender as TComponent)
else if Edit = edAmount then
R := Parser1.Parse(Edit.Text, itDecimal, Sender as TComponent)
else
Exit;
if R.ResultType = otInvalid then
Edit.Color := clYellow
else
Edit.Color := clWindow;
end;