class operator TNullable.Implicit(const AValue: T): TNullable;
begin
Result.Value := AValue;
Result.HasValue := True;
end;
class operator TNullable.Implicit(const AValue: TNullable): T;
begin
if AValue.HasValue then
Result := AValue.Value
else
Result := Default(T); // or raise an exception
end;
class operator TNullable.Implicit(const AValue: TNullValue): TNullable;
begin
Result.Value := Default(T);
Result.HasValue := False;
end;
class operator TNullable.Explicit(const AValue: T): TNullable;
begin
Result.Value := AValue;
Result.HasValue := True;
end;
class operator TNullable.Equal(const A: TNullable; const B: TNullValue): Boolean;
begin
Result := not A.HasValue;
end;
class operator TNullable.NotEqual(const A: TNullable; const B: TNullValue): Boolean;
begin
Result := A.HasValue;
end;
// Define the event type
TPropertyChangedEvent = procedure(Sender: TQryRecInfo; const PropertyName: string) of object;
EORMUtilsException = class(Exception);
///
/// Represents the key used in the component-ORM mapping dictionary.
/// Combines a reference to an ORM record with a specific field name.
///
TORMKey = record
ORMRec: TQryRecInfo;
FieldName: string;
end;
///
/// Custom comparer for TORMKey to handle equality checks and hashing.
///
TORMKeyComparer = class(TEqualityComparer)
function Equals(const Left, Right: TORMKey): Boolean; override;
function GetHashCode(const Key: TORMKey): Integer; override;
end;
///
/// Manages the binding between UI components and ORM records.
///
TORMManager = class
private
FComponentDict: TDictionary<TORMKey, TComponent>;
public
constructor Create;
destructor Destroy; override;
Assistant
In this conversation so far, there are 11378 tokens. This includes the entire content of the pixel_classes and orm_utils units that you've provided, as well as your interaction with me.
If you have any specific questions about the code or need further assistance, feel free to ask!
///
/// Registers a component with its corresponding ORM record and field name.
///
procedure RegisterComponent(Component: TComponent; ORMRec: TQryRecInfo; const FieldName: string);
///
/// Unregisters a component from ORM binding.
///
procedure UnregisterComponent(Component: TComponent);
///
/// Finds the component associated with a specific ORM record and field name.
///
function FindComponent(ORMRec: TQryRecInfo; const FieldName: string): TComponent;
///
/// Retrieves all registered components.
///
function GetAllComponents: TArray;
///
/// Updates a component's UI based on the latest ORM record data.
///
procedure UpdateComponentFromORM(Component: TComponent; ORMRec: TQryRecInfo; const FieldName: string);
///
/// Retrieves the ORM record associated with a given component.
///
function GetORMRecForComponent(Component: TComponent): TQryRecInfo;
///
/// Retrieves the field name associated with a given component.
///
function GetFieldNameForComponent(Component: TComponent): string;
///
/// Registers components based on their Hint property.
///
procedure RegisterComponentsByHint(MyForm: TForm; ORMRec: TQryRecInfo);
end;
///
/// Helper methods extending TComponent to interface with ORMManager.
///
TComponentHelper = class helper for TComponent
public
///
/// Retrieves the ORM record associated with the component.
///
function GetORMRec: TQryRecInfo;
///
/// Retrieves the field name associated with the component.
///
function GetORMRecField: string;
end;
///
/// Base class for query record information
/// Use descendants of this class to represent specific database records.
///
TQryRecInfo = class
private
FDirtyFields: TDictionary<string, Boolean>;
FPreviousValues: TDictionary<string, TValue>;
FOnPropertyChanged: TPropertyChangedEvent; // New event
FIsNewRec: Boolean; // New field
procedure SetInfo(Index: Integer; const Value: TValue);
// Other SetInfo overloads...
public
constructor Create;
destructor Destroy; override;
// Methods to handle dirty fields
function IsFieldDirty(const FieldName: string): Boolean;
procedure ClearDirtyFlags;
property DirtyFields: TDictionary<string, Boolean> read FDirtyFields;
// Methods to handle previous values
function GetPreviousValue(const FieldName: string): TValue;
procedure ClearPreviousValues;
property PreviousValues: TDictionary<string, TValue> read FPreviousValues;
///
/// Retrieves the formatted text for a given field based on the FmtProp attribute.
///
function GetFormattedText(const FieldName: string): string;
///
/// Fetches data for this record from the database based on the provided SQL.
///
procedure Fetch(DBConn: TUniConnection; const QryTxt: string);
///
/// Retrieves the value of a property by name.
///
/// The name of the property to retrieve.
/// The value of the specified property as TValue.
function GetPropertyValue(const PropertyName: string): TValue; // New method
end;
///
/// Generic class to handle arrays of TQryRecInfo descendants.
///
TQryRecArray<T: TQryRecInfo, constructor> = class
private
FQryRecInfoList: TObjectList;
FFilterSQL: string;
FDBConn: TUniConnection;
function GetCount: Integer;
public
constructor Create;
destructor Destroy; override;
///
/// Fetches all records based on the provided SQL text and populates the array.
///
/// The base SQL query text.
procedure FetchAllInstance(DBConn: TUniConnection; const QryTxt: string);
///
/// Clears any SQL filters applied.
///
procedure ClearFilter;
///
/// Accesses a record at the specified index.
///
/// The zero-based index of the record.
/// The record at the specified index.
function Rec(Index: Integer): T;
///
/// Number of records in the array.
///
property RecCount: Integer read GetCount;
///
/// Database connection used for fetching records.
///
property DBConn: TUniConnection read FDBConn write FDBConn;
///
/// Additional SQL filter to apply to the query.
///
property SQLFilter: string read FFilterSQL write FFilterSQL;
///
/// Provides support for for-in loops by returning an enumerator.
///
function GetEnumerator: TEnumerator;
///
/// Factory method to create and fetch records in a single step.
///
/// Database connection.
/// SQL query text.
/// Populated instance of TQryRecArray.
class function FetchAll(DBConn: TUniConnection; const QryTxt: string): TQryRecArray; static;
end;
// Declare ORMManager as a global variable in the interface for accessibility
var
ORMManager: TORMManager;
function TORMKeyComparer.Equals(const Left, Right: TORMKey): Boolean;
begin
Result := (Left.ORMRec = Right.ORMRec) and SameText(Left.FieldName, Right.FieldName);
end;
function TORMKeyComparer.GetHashCode(const Key: TORMKey): Integer;
begin
Result := Integer(NativeUInt(Key.ORMRec)) xor THashBobJenkins.GetHashValue(LowerCase(Key.FieldName));
end;
constructor TORMManager.Create;
begin
inherited Create;
FComponentDict := TDictionary<TORMKey, TComponent>.Create(TORMKeyComparer.Create);
end;
destructor TORMManager.Destroy;
begin
FComponentDict.Free;
inherited;
end;
procedure TORMManager.RegisterComponent(Component: TComponent; ORMRec: TQryRecInfo; const FieldName: string);
var
Key: TORMKey;
begin
Key.ORMRec := ORMRec;
Key.FieldName := FieldName;
if not FComponentDict.ContainsKey(Key) then
begin
FComponentDict.Add(Key, Component);
end;
end;
procedure TORMManager.UnregisterComponent(Component: TComponent);
var
Key: TORMKey;
CopyKeys: TArray;
i: Integer;
begin
// To safely remove components, first copy the keys
CopyKeys := FComponentDict.Keys.ToArray;
for i := 0 to Length(CopyKeys) - 1 do
begin
Key := CopyKeys[i];
if FComponentDict[Key] = Component then
FComponentDict.Remove(Key);
end;
end;
function TORMManager.FindComponent(ORMRec: TQryRecInfo; const FieldName: string): TComponent;
var
Key: TORMKey;
begin
Key.ORMRec := ORMRec;
Key.FieldName := FieldName;
if FComponentDict.TryGetValue(Key, Result) then
Exit
else
Result := nil;
end;
function TORMManager.GetAllComponents: TArray<
How many tokens are in this conversation up to now?