procedure TMainForm.ButtonRegisterClick(Sender: TObject);
begin
// Unregister the EditPassword component from ORM binding
ORMManager.UnregisterComponent(EditPassword);
// Automatically register components where the Hint matches a field in FQueryRec2
RegisterComponentsByHint(FQueryRec2);
// Retrieve and display the component bound to 'EmployeeName'
var MyComp := RetrieveComponent(FQueryRec2, 'EmployeeName');
if Assigned(MyComp) then
ShowMessage('Component bound to EmployeeName: ' + MyComp.Name)
else
ShowMessage('Component EmployeeName not found.');
// Retrieve and display the component bound to 'EmployeeID'
MyComp := RetrieveComponent(FQueryRec2, 'EmployeeID');
if Assigned(MyComp) then
ShowMessage('Component bound to EmployeeID: ' + MyComp.Name)
else
ShowMessage('Component EmployeeID not found.');
end;
procedure TMainForm.ButtonUpdateORMClick(Sender: TObject);
begin
// Programmatically update the EmployeeName property in FQueryRec1
FQueryRec1.EmployeeName := 'Programmatic Name Change';
ShowMessage('ORM Property EmployeeName has been updated programmatically.');
end;
procedure TMainForm.EditExitHandler(Sender: TObject);
var
Edit: TEdit;
ORMRec: TQryRecInfo;
FieldName: string;
NewValue: TValue;
begin
// Ensure the sender is a TEdit component
if not (Sender is TEdit) then
Exit;
Edit := TEdit(Sender);
// Retrieve ORMRec and FieldName using TComponentHelper properties
ORMRec := Edit.ORMRec;
FieldName := Edit.ORMRecField;
if not Assigned(ORMRec) or (FieldName = '') then
Exit;
// Get the new value from the TEdit component
NewValue := TValue.From(Edit.Text);
// Update the ORMRec's property using RTTI
try
SetORMProperty(ORMRec, FieldName, NewValue);
ShowMessage(Format('%s ORM property updated to: %s', [FieldName, NewValue.AsString]));
except
on E: Exception do
ShowMessage('Error updating ORM property: ' + E.Message);
end;
end;
Assistant
procedure TMainForm.FormDestroy(Sender: TObject);
begin
// Clean up ORM records
FQueryRec1.Free;
FQueryRec2.Free;
end;
procedure TMainForm.FormShow(Sender: TObject);
begin
// Initialize the ORM record instances
FQueryRec1 := TEmployeeProject.Create;
FQueryRec2 := TEmployeeProject.Create;
// Setup components with ORM properties and register them
SetupComponents;
// Connect to the local database
DM.ConnectLocal;
// Fetch data into ORM records using DM.LocalServer
FQueryRec1.Fetch(DM.LocalServer, 'SELECT * FROM employees WHERE emp_id = 1');
FQueryRec2.Fetch(DM.LocalServer, 'SELECT * FROM projects WHERE proj_id = 1');
// Initialize UI components with ORM record data
UpdateAllComponents;
// Assign the shared OnExit event handler to all registered TEdit components
for var Comp in ORMManager.GetAllComponents do
if Comp is TEdit then
TEdit(Comp).OnExit := EditExitHandler;
end;
procedure TMainForm.SetupComponents;
begin
// Register EditUserName with FQueryRec1 and FieldName 'EmployeeName'
ORMManager.RegisterComponent(EditUserName, FQueryRec1, 'EmployeeName'); // Maps to TEdit.Text
// Register EditPassword with FQueryRec2 and FieldName 'ProjectName'
ORMManager.RegisterComponent(EditPassword, FQueryRec2, 'ProjectName'); // Maps to TEdit.Text
// Register LabelUserName with FQueryRec1 and FieldName 'BudgetLimit'
ORMManager.RegisterComponent(LabelUserName, FQueryRec1, 'BudgetLimit'); // Maps to TLabel.Caption
// Register DatePickerProjectStart with FQueryRec2 and FieldName 'ProjectStartMoment'
ORMManager.RegisterComponent(DatePickerProjectStart, FQueryRec2, 'ProjectStartMoment'); // Maps to TDatePicker.Date
end;
function TMainForm.RetrieveComponent(ORMRec: TQryRecInfo; FieldName: string): TComponent;
begin
// Find and return the component based on ORMRec and FieldName
Result := ORMManager.FindComponent(ORMRec, FieldName);
end;
procedure TMainForm.SetORMProperty(ORMRec: TQryRecInfo; const FieldName: string; const Value: TValue);
var
Prop: TRttiProperty;
Ctx: TRttiContext;
ConvertedValue: TValue;
begin
Ctx := TRttiContext.Create;
try
Prop := Ctx.GetType(ORMRec.ClassType).GetProperty(FieldName);
if Assigned(Prop) and Prop.IsWritable then
begin
// Handle type conversions based on the property type
case Prop.PropertyType.TypeKind of
tkString, tkLString, tkWString, tkUString:
ConvertedValue := TValue.From(Value.AsString);
tkInteger:
ConvertedValue := TValue.From(StrToIntDef(Value.AsString, 0));
tkInt64:
ConvertedValue := TValue.From(StrToInt64Def(Value.AsString, 0));
tkFloat:
if Prop.PropertyType.Handle = TypeInfo(Currency) then
ConvertedValue := TValue.From(StrToCurrDef(Value.AsString, 0))
else if Prop.PropertyType.Handle = TypeInfo(TDateTime) then
ConvertedValue := TValue.From(StrToDateDef(Value.AsString, 0))
else
ConvertedValue := Value;
tkEnumeration:
if Prop.PropertyType.Handle = TypeInfo(Boolean) then
ConvertedValue := TValue.From(UpperCase(Value.AsString) = 'S') // Assuming 'S' for True
else
ConvertedValue := Value;
// Add more type conversions as needed...
else
ConvertedValue := Value;
end;
code
// Set the property value
Prop.SetValue(ORMRec, ConvertedValue);
end
else
raise EORMUtilsException.CreateFmt('Property "%s" not found or not writable in ORMRec.', [FieldName]);
finally
// TRttiContext is a record and managed automatically
end;
end;
procedure TMainForm.UpdateAllComponents;
begin
// Iterate through all mappings and update UI components
for var Comp in ORMManager.GetAllComponents do
if Comp is TComponent then
ORMManager.UpdateComponentFromORM(Comp, TEdit(Comp).ORMRec, TEdit(Comp).ORMRecField);
end;
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;
var
Hash: THashBobJenkins;
begin
Hash.Init;
Hash.Update(Integer(NativeUInt(Key.ORMRec)));
Hash.Update(LowerCase(Key.FieldName));
Result := Hash.GetHashValue;
end;
// TORMManager
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, first copy 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;
var
Comp: TComponent;
CompList: TList;
begin
CompList := TList.Create;
try
for Comp in FComponentDict.Values do
CompList.Add(Comp);
Result := CompList.ToArray;
finally
CompList.Free;
end;
end;
// Method to update component from ORMRec using GetFormattedText
procedure TORMManager.UpdateComponentFromORM(Component: TComponent; ORMRec: TQryRecInfo; const FieldName: string);
var
FormattedText: string;
begin
FormattedText := ORMRec.GetFormattedText(FieldName);
if Component is TEdit then
TEdit(Component).Text := FormattedText
else if Component is TLabel then
TLabel(Component).Caption := FormattedText
else if Component is TDatePicker then
begin
if TryStrToDate(FormattedText, TDatePicker(Component).Date) then
; // Successfully parsed date
else
TDatePicker(Component).Date := 0; // Handle invalid date
end
else if Component is TComboBox then
TComboBox(Component).Text := FormattedText
// Add more component types as needed...
;
end;
// Global ORMManager instance
var
ORMManager: TORMManager;
initialization
ORMManager := TORMManager.Create;
finalization
ORMManager.Free;
// TComponentHelper Implementation
function TComponentHelper.GetORMRec: TQryRecInfo;
var
Key: TORMKey;
begin
Result := nil;
for Key in ORMManager.FComponentDict.Keys do
if ORMManager.FComponentDict[Key] = Self then
begin
Result := Key.ORMRec;
Exit;
end;
end;
function TComponentHelper.GetORMRecField: string;
var
Key: TORMKey;
begin
Result := '';
for Key in ORMManager.FComponentDict.Keys do
if ORMManager.FComponentDict[Key] = Self then
begin
Result := Key.FieldName;
Exit;
end;
end;
// TsPanel Implementation
procedure TsPanel.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
StrPCopy(Params.WinClassName, 'Painel');
end;
// TsEdit Implementation
procedure TsEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
StrPCopy(Params.WinClassName, 'Editor');
end;
// TsPageControl Implementation
procedure TsPageControl.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
StrPCopy(Params.WinClassName, 'PagCtrl');
end;
procedure TsPageControl.TCMAdjustRect(var Msg: TMessage);
begin
inherited;
case TabPosition of
tpTop:
begin
PRect(Msg.LParam)^.Left := 0;
PRect(Msg.LParam)^.Right := ClientWidth;
PRect(Msg.LParam)^.Bottom := ClientHeight;
end;
end;
end;
// TAdvColumnGrid Implementation
procedure TAdvColumnGrid.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
StrPCopy(Params.WinClassName, 'Grid');
end;
function TAdvColumnGrid.GetCellz(ACol, ARow: Integer): TZString;
begin
Result := Self.Cells[ACol, ARow];
end;
function TAdvColumnGrid.GetBypassAF(ACol: Integer): Boolean;
begin
Result := FByPassAF[ACol];
end;
procedure TAdvColumnGrid.SetBypassAF(ACol: Integer; const Value: Boolean);
begin
if Length(FByPassAF) < ACol + 1 then
SetLength(FByPassAF, ACol + 1);
FByPassAF[ACol] := Value;
end;
function TAdvColumnGrid.CriaLinha: Integer;
begin
Self.RowCount := Self.RowCount + 1;
Self.ClearRows(Self.RowCount - 1, 1);
Result := Self.RowCount - 1;
end;
function TAdvColumnGrid.CriaLinhaObjeto(MyObject: TObject): Integer;
begin
Self.RowCount := Self.RowCount + 1;
Self.ClearRows(Self.RowCount - 1, 1);
Self.Objects[0, Self.RowCount - 1] := MyObject;
Result := Self.RowCount - 1;
end;
function TAdvColumnGrid.GetColumnIndexByName(const nome: string): Integer;
begin
Result := 0;
for var i := 1 to Pred(Self.ColCount) do
if Self.Columns[i].Name = nome then
Exit(i);
end;
procedure TAdvColumnGrid.SetBasicColumnConfig;
var
i: Integer;
begin
with Self do
begin
SuppressColumn(0);
for i := 1 to ColCount - 1 do
if Columns[i].Tag = 0 then
begin
Columns[i].HeaderAlignment := taCenter;
Columns[i].HeaderFont.Name := 'Tahoma';
Columns[i].HeaderFont.Size := MulDiv(8, 96, Self.CurrentPPI);
Columns[i].HeaderFont.Color := clWhite;
Columns[i].HeaderFont.Style := [fsBold];
end;
for i := 1 to ColCount - 1 do
if Columns[i].Tag = 0 then
begin
Columns[i].Font.Name := 'Verdana';
Columns[i].Font.Size := MulDiv(10, 96, Self.CurrentPPI);
Columns[i].Font.Style := [];
end;
for i := 1 to ColCount - 1 do
if Columns[i].Tag = 0 then
begin
Columns[i].ShowBands := True;
Colors[i, 0] := RGB(41,3F, $42);
end;
published
property EmployeeID: TNullable index 0 read _EmployeeID write SetInfoNullableInteger;
property DepartmentID: TNullable index 1 read _DepartmentID write SetInfoNullableInteger;
property EmployeeName: TNullable index 2 read _EmployeeName write SetInfoNullableString;
property ProjectID: TNullable index 3 read _ProjectID write SetInfoNullableInteger;
property ProjectName: TNullable index 4 read _ProjectName write SetInfoNullableString;
property ProjectBudget: TNullable index 5 read _ProjectBudget write SetInfoNullableCurrency;
property BudgetLimit: Currency index 6 read _BudgetLimit write SetInfoCurrency;
property Age: Integer index 7 read _Age write SetInfoInteger;
property Older: Boolean index 8 read _Older write SetInfoBoolean;
property ProjectDate: TNullable index 9 read _ProjectDate write SetInfoNullableDateTime;
property ProjectStartMoment: TNullable index 10 read _ProjectStartMoment write SetInfoNullableDateTime;
end;
implementation
end.
unit pixel_classes;
interface
uses
System.SysUtils, Rtti, TypInfo;
{$TYPEINFO ON}
type
TZString = type string;
type
TNullValue = record
end;
TNullable<T{: record}> = record
public
Value: T;
HasValue: Boolean;
code
class operator Implicit(const AValue: T): TNullable<T>;
class operator Implicit(const AValue: TNullable<T>): T;
class operator Implicit(const AValue: TNullValue): TNullable<T>;
class operator Explicit(const AValue: T): TNullable<T>;
// add these...
class operator Equal(const A: TNullable<T>; const B: TNullValue): Boolean;
class operator NotEqual(const A: TNullable<T>; const B: TNullValue): Boolean;
end;
var NullValue: TNullValue;
implementation
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);
///
/// 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
procedure SetInfo(Index: Integer; const Value: TValue);
public
constructor Create;
destructor Destroy; override;
code
// Event property
property OnPropertyChanged: TPropertyChangedEvent read FOnPropertyChanged write FOnPropertyChanged;
// Methods to set nullable fields
procedure SetInfoNullableString(Index: Integer; const Value: TNullable<string>);
procedure SetInfoNullableInteger(Index: Integer; const Value: TNullable<Integer>);
procedure SetInfoNullableInt64(Index: Integer; const Value: TNullable<Int64>);
procedure SetInfoNullableExtended(Index: Integer; const Value: TNullable<Extended>);
procedure SetInfoNullableCurrency(Index: Integer; const Value: TNullable<Currency>);
procedure SetInfoNullableDateTime(Index: Integer; const Value: TNullable<TDateTime>);
procedure SetInfoNullableBoolean(Index: Integer; const Value: TNullable<Boolean>);
procedure SetInfoNullableVariant(Index: Integer; const Value: TNullable<Variant>);
// Methods to set non-nullable simple types
procedure SetInfoCurrency(Index: Integer; const Value: Currency);
procedure SetInfoString(Index: Integer; const Value: string);
procedure SetInfoInteger(Index: Integer; const Value: Integer);
procedure SetInfoInt64(Index: Integer; const Value: Int64);
procedure SetInfoExtended(Index: Integer; const Value: Extended);
procedure SetInfoDateTime(Index: Integer; const Value: TDateTime);
procedure SetInfoBoolean(Index: Integer; const Value: Boolean);
procedure SetInfoVariant(Index: Integer; const Value: Variant);
// 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;
/// <summary>
/// Retrieves the formatted text for a given field based on the FmtProp attribute.
/// </summary>
function GetFormattedText(const FieldName: string): string;
/// <summary>
/// Fetches data for this record from the database based on the provided SQL.
/// </summary>
procedure Fetch(DBConn: TUniConnection; const QryTxt: string);
end;
///
/// Generic class to handle arrays of TQryRecInfo descendants.
///
TQryRecArray<T: class, constructor> = class
private
FQryRecInfoList: TObjectList;
FFilterSQL: string;
FDBConn: TUniConnection;
function GetCount: Integer;
public
constructor Create;
destructor Destroy; override;
code
/// <summary>
/// Fetches all records based on the provided SQL text and populates the array.
/// </summary>
/// <param name="QryTxt">The base SQL query text.</param>
procedure FetchAllInstance(DBConn: TUniConnection; const QryTxt: string);
/// <summary>
/// Clears any SQL filters applied.
/// </summary>
procedure ClearFilter;
/// <summary>
/// Accesses a record at the specified index.
/// </summary>
/// <param name="Index">The zero-based index of the record.</param>
/// <returns>The record at the specified index.</returns>
function Rec(Index: Integer): T;
/// <summary>
/// Number of records in the array.
/// </summary>
property RecCount: Integer read GetCount;
/// <summary>
/// Database connection used for fetching records.
/// </summary>
property DBConn: TUniConnection read FDBConn write FDBConn;
/// <summary>
/// Additional SQL filter to apply to the query.
/// </summary>
property SQLFilter: string read FFilterSQL write FFilterSQL;
/// <summary>
/// Provides support for for-in loops by returning an enumerator.
/// </summary>
function GetEnumerator: TEnumerator<T>;
/// <summary>
/// Factory method to create and fetch records in a single step.
/// </summary>
/// <param name="DBConn">Database connection.</param>
/// <param name="QryTxt">SQL query text.</param>
/// <returns>Populated instance of TQryRecArray<T>.</returns>
class function FetchAll(DBConn: TUniConnection; const QryTxt: string): TQryRecArray<T>; static;
end;
// Example descendant of TQryRecInfo (You can create your own)
TDynRec = class(TQryRecInfo)
// Define additional properties as needed
end;
implementation
uses
System.Variants,
StrUtils,
pixel_utils; // Ensure this unit is in your project and properly implemented
destructor TQryRecInfo.Destroy;
begin
FDirtyFields.Free;
FPreviousValues.Free;
inherited;
end;
procedure TQryRecInfo.ClearDirtyFlags;
begin
FDirtyFields.Clear;
end;
procedure TQryRecInfo.ClearPreviousValues;
begin
FPreviousValues.Clear;
end;
function TQryRecInfo.IsFieldDirty(const FieldName: string): Boolean;
begin
Result := FDirtyFields.ContainsKey(FieldName) and FDirtyFields[FieldName];
end;
function TQryRecInfo.GetPreviousValue(const FieldName: string): TValue;
begin
if not FPreviousValues.TryGetValue(FieldName, Result) then
raise EORMUtilsException.CreateFmt('No previous value found for field "%s".', [FieldName]);
end;
function FormataInfo(info: Variant; tipo: TFormatType): string;
begin
if VarIsNull(info) or VarIsEmpty(info) then
Exit('');
case tipo of
tiString:
Result := VarToStr(info);
tiInteger:
Result := IntToStr(Integer(info));
tiMoney:
Result := FormatFloat(FMT_MONEY, Currency(info));
tiFloat:
Result := FloatToStr(info);
tiDate:
if VarIsType(info, [varDate, varDouble]) then
begin
if VarToDateTime(info) < EncodeDate(1900, 1, 1) then
Result := ''
else
Result := FormatDateTime('dd/mm/yyyy', VarToDateTime(info));
end
else
raise EORMUtilsException.CreateFmt('FormataInfo: Cannot treat information [%s] as tiDate.', [VarToStr(info)]);
tiDateTime:
if VarIsType(info, [varDate, varDouble]) then
begin
if VarToDateTime(info) = 0 then
Result := ''
else
Result := FormatDateTime('dd/mm/yyyy hh:mm:ss', VarToDateTime(info));
end
else
raise EORMUtilsException.CreateFmt('FormataInfo: Cannot treat information [%s] as tiDateTime.', [VarToStr(info)]);
tiBoolean:
Result := IfThen(Boolean(info), 'S', 'N');
tiByteArray:
Result := ByteArrayToHex(info);
tiNone:
Result := '';
else
raise EORMUtilsException.CreateFmt('FormataInfo: Format type [%s] not recognized.', [GetEnumName(TypeInfo(TFormatType), Integer(tipo))]);
end;
end;
// Find the property by name
Prop := RttiType.GetProperty(FieldName);
if not Assigned(Prop) then
raise EORMUtilsException.CreateFmt('Property "%s" not found.', [FieldName]);
// Get the value of the property
Value := Prop.GetValue(Self);
FmtType := tiNone;
// Check for FmtProp attribute on the property
for Attr in Prop.GetAttributes do
begin
if Attr is FmtPropAttribute then
begin
FmtAttr := FmtPropAttribute(Attr);
FmtType := FmtAttr.FormatType;
Break;
end;
end;
// If not found on property, check on the corresponding field
if FmtType = tiNone then
begin
Field := RttiType.GetField('_' + FieldName);
if Assigned(Field) then
begin
for Attr in Field.GetAttributes do
begin
if Attr is FmtPropAttribute then
begin
FmtAttr := FmtPropAttribute(Attr);
FmtType := FmtAttr.FormatType;
Break;
end;
end;
end;
end;
// Determine default FmtType based on property type if not set by attribute
if FmtType = tiNone then
begin
UnderlyingType := Prop.PropertyType;
// Handle TNullable<T>
if UnderlyingType.TypeKind = tkRecord then
begin
ValueField := UnderlyingType.GetField('Value');
if Assigned(ValueField) then
UnderlyingType := ValueField.FieldType;
end;
case UnderlyingType.TypeKind of
tkInteger, tkInt64:
FmtType := tiInteger;
tkFloat:
if UnderlyingType.Handle = TypeInfo(TDateTime) then
FmtType := tiDateTime
else
FmtType := tiFloat;
tkString, tkLString, tkWString, tkUString:
FmtType := tiString;
tkEnumeration:
if UnderlyingType.Handle = TypeInfo(Boolean) then
FmtType := tiBoolean
else
FmtType := tiString;
tkVariant:
FmtType := tiString;
else
FmtType := tiNone;
end;
end;
// Extract actual value from TNullable<T> if necessary
if Value.Kind = tkRecord then
begin
// Assuming TNullable<T> has 'HasValue' and 'Value' fields
UnderlyingType := Prop.PropertyType;
HasValueField := UnderlyingType.GetField('HasValue');
ValueField := UnderlyingType.GetField('Value');
if Assigned(HasValueField) and Assigned(ValueField) then
begin
HasValue := HasValueField.GetValue(Value.GetReferenceToRawData).AsBoolean;
if HasValue then
begin
ActualValue := ValueField.GetValue(Value.GetReferenceToRawData);
// Format the actual value
Result := FormataInfo(ActualValue.AsVariant, FmtType);
end
else
Result := '';
end
else
begin
// Not TNullable<T>, handle value directly
if not Value.IsEmpty then
Result := FormataInfo(Value.AsVariant, FmtType)
else
Result := '';
end;
end
else
begin
// Not a record, handle value directly
if not Value.IsEmpty then
Result := FormataInfo(Value.AsVariant, FmtType)
else
Result := '';
end;
finally
// No need to free TRttiContext as it's a record
end;
end;
procedure TQryRecInfo.Fetch(DBConn: TUniConnection; const QryTxt: string);
var
qr: TUniQuery;
Ctx: TRttiContext;
ObjType: TRttiType;
Prop: TRttiProperty;
Field: TField;
ORMFieldName: string;
begin
if not Assigned(DBConn) or Trim(QryTxt).IsEmpty then
Exit;
if qr.RecordCount > 0 then
begin
Ctx := TRttiContext.Create;
try
ObjType := Ctx.GetType(Self.ClassType);
for Prop in ObjType.GetProperties do
begin
if not Prop.IsWritable then
Continue;
// Attempt to find the corresponding field in the query
Field := qr.FindField(Prop.Name);
if not Assigned(Field) then
Continue; // Field not found, skip
ORMFieldName := '_' + Prop.Name;
// Handle TNullable<T> types
if Prop.PropertyType.TypeKind = tkRecord then
begin
// Expecting TNullable<T>
// Attempt to find the backing field for the nullable property
if ObjType.GetField(ORMFieldName) = nil then
Continue; // Backing field not found, skip
if Prop.PropertyType.Name.StartsWith('TNullable<') then
begin
// Use RTTI to determine the generic type parameter
if Field.IsNull then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<string>>(NullValue))
else
begin
if Prop.PropertyType.Name = 'TNullable<System.string>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<string>>(Field.AsString))
else if Prop.PropertyType.Name = 'TNullable<System.Integer>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Integer>>(Field.AsInteger))
else if Prop.PropertyType.Name = 'TNullable<System.Int64>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Int64>>(Field.AsLargeInt))
else if Prop.PropertyType.Name = 'TNullable<System.Extended>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Extended>>(Field.AsFloat))
else if Prop.PropertyType.Name = 'TNullable<System.Currency>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Currency>>(Field.AsCurrency))
else if Prop.PropertyType.Name = 'TNullable<System.TDateTime>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<TDateTime>>(Field.AsDateTime))
else if Prop.PropertyType.Name = 'TNullable<System.Boolean>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Boolean>>(Field.AsBoolean))
else if Prop.PropertyType.Name = 'TNullable<System.Variant>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Variant>>(Field.Value))
else
raise EORMUtilsException.CreateFmt('Unsupported TNullable type: %s', [Prop.PropertyType.Name]);
end;
end
else
raise EORMUtilsException.CreateFmt('Unsupported record type: %s', [Prop.PropertyType.Name]);
end
else
begin
// Handle non-nullable types
case Prop.PropertyType.TypeKind of
tkString, tkLString, tkWString, tkUString:
ObjType.GetField(ORMFieldName).SetValue(Self, Field.AsString);
tkInteger:
ObjType.GetField(ORMFieldName).SetValue(Self, Field.AsInteger);
tkInt64:
ObjType.GetField(ORMFieldName).SetValue(Self, Field.AsLargeInt);
tkFloat:
if Prop.PropertyType.Handle = TypeInfo(Currency) then
ObjType.GetField(ORMFieldName).SetValue(Self, Field.AsCurrency)
else
ObjType.GetField(ORMFieldName).SetValue(Self, Field.AsFloat);
tkVariant:
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.FromVariant(Field.AsVariant));
tkEnumeration:
if Prop.PropertyType.Handle = TypeInfo(Boolean) then
ObjType.GetField(ORMFieldName).SetValue(Self, Field.AsBoolean)
else
raise EORMUtilsException.CreateFmt('Unsupported enumeration type: %s', [Prop.PropertyType.Name]);
else
// Add additional handling for other types if necessary
end;
end;
end;
finally
// No need to free TRttiContext as it's a record
end;
end;
finally
qr.Free;
end;
end;
procedure TQryRecInfo.SetInfo(Index: Integer; const Value: TValue);
var
PropName: string;
PropKind: TTypeKind;
Ctx: TRttiContext;
Prop: TRttiProperty;
Field: TRttiField;
OldValue: TValue;
begin
// Retrieve property name and kind based on index
// Assuming that the index corresponds to the property order
Ctx := TRttiContext.Create;
try
Prop := Ctx.GetType(Self.ClassType).GetProperties()[Index];
if not Assigned(Prop) then
raise EORMUtilsException.CreateFmt('Error: The index "%d" does not correspond to any property in class "%s".', [Index, ClassName]);
code
PropName := Prop.Name;
PropKind := Prop.PropertyType.TypeKind;
Field := Ctx.GetType(Self.ClassType).GetField('_' + PropName);
if not Assigned(Field) then
raise EORMUtilsException.CreateFmt('Error: Field "_%s" not found in class "%s".', [PropName, ClassName]);
// If the field is not yet dirty, store the previous value
if not FDirtyFields.ContainsKey(PropName) then
begin
OldValue := Field.GetValue(Self);
FPreviousValues.AddOrSetValue(PropName, OldValue);
end;
// Set the new value
Field.SetValue(Self, Value);
// Mark the property as dirty
FDirtyFields.AddOrSetValue(PropName, True);
// Trigger the OnPropertyChanged event
if Assigned(FOnPropertyChanged) then
FOnPropertyChanged(Self, PropName);
finally
// No need to free TRttiContext as it's a record
end;
end;
procedure TQryRecInfo.SetInfoCurrency(Index: Integer; const Value: Currency);
begin
SetInfo(Index, TValue.From(Value));
end;
procedure TQryRecInfo.SetInfoString(Index: Integer; const Value: string);
begin
SetInfo(Index, TValue.From(Value));
end;
procedure TQryRecInfo.SetInfoInteger(Index: Integer; const Value: Integer);
begin
SetInfo(Index, TValue.From(Value));
end;
procedure TQryRecInfo.SetInfoInt64(Index: Integer; const Value: Int64);
begin
SetInfo(Index, TValue.From(Value));
end;
procedure TQryRecInfo.SetInfoExtended(Index: Integer; const Value: Extended);
begin
SetInfo(Index, TValue.From(Value));
end;
procedure TQryRecInfo.SetInfoDateTime(Index: Integer; const Value: TDateTime);
begin
SetInfo(Index, TValue.From(Value));
end;
procedure TQryRecInfo.SetInfoBoolean(Index: Integer; const Value: Boolean);
begin
SetInfo(Index, TValue.From(Value));
end;
procedure TQryRecInfo.SetInfoVariant(Index: Integer; const Value: Variant);
begin
SetInfo(Index, TValue.From(Value));
end;
procedure TQryRecInfo.SetInfoNullableBoolean(Index: Integer; const Value: TNullable);
begin
SetInfo(Index, TValue.From<TNullable>(Value));
end;
procedure TQryRecInfo.SetInfoNullableCurrency(Index: Integer; const Value: TNullable);
begin
SetInfo(Index, TValue.From<TNullable>(Value));
end;
procedure TQryRecInfo.SetInfoNullableDateTime(Index: Integer; const Value: TNullable);
begin
SetInfo(Index, TValue.From<TNullable>(Value));
end;
procedure TQryRecInfo.SetInfoNullableExtended(Index: Integer; const Value: TNullable);
begin
SetInfo(Index, TValue.From<TNullable>(Value));
end;
procedure TQryRecInfo.SetInfoNullableInteger(Index: Integer; const Value: TNullable);
begin
SetInfo(Index, TValue.From<TNullable>(Value));
end;
procedure TQryRecInfo.SetInfoNullableInt64(Index: Integer; const Value: TNullable);
begin
SetInfo(Index, TValue.From<TNullable>(Value));
end;
procedure TQryRecInfo.SetInfoNullableString(Index: Integer; const Value: TNullable);
begin
SetInfo(Index, TValue.From<TNullable>(Value));
end;
procedure TQryRecInfo.SetInfoNullableVariant(Index: Integer; const Value: TNullable);
begin
SetInfo(Index, TValue.From<TNullable>(Value));
end;
destructor TQryRecArray.Destroy;
begin
FQryRecInfoList.Free;
inherited;
end;
procedure TQryRecArray.ClearFilter;
begin
FFilterSQL := '';
end;
function TQryRecArray.GetCount: Integer;
begin
Result := FQryRecInfoList.Count;
end;
function TQryRecArray.Rec(Index: Integer): T;
begin
if (Index < 0) or (Index >= FQryRecInfoList.Count) then
raise EORMUtilsException.CreateFmt('Index %d out of bounds.', [Index]);
Result := FQryRecInfoList[Index];
end;
procedure TQryRecArray.FetchAllInstance(DBConn: TUniConnection; const QryTxt: string);
var
qr: TUniQuery;
RecInstance: T;
Ctx: TRttiContext;
ObjType: TRttiType;
Prop: TRttiProperty;
Field: TField;
ORMFieldName: string;
begin
if not Assigned(DBConn) then
raise EORMUtilsException.Create('Database connection (DBConn) is not assigned.');
if Trim(QryTxt).IsEmpty then
raise EORMUtilsException.Create('Query text (QryTxt) cannot be empty.');
if qr.IsEmpty then
Exit; // No records to fetch
Ctx := TRttiContext.Create;
ObjType := Ctx.GetType(T);
try
while not qr.Eof do
begin
RecInstance := T.Create;
try
// Iterate over each property of T and assign values from the query
for Prop in ObjType.GetProperties do
begin
if not Prop.IsWritable then
Continue;
// Attempt to find the corresponding field in the query
Field := qr.FindField(Prop.Name);
if not Assigned(Field) then
Continue; // Field not found, skip
ORMFieldName := '_' + Prop.Name;
// Handle TNullable<T> types
if Prop.PropertyType.TypeKind = tkRecord then
begin
// Expecting TNullable<T>
// Attempt to find the backing field for the nullable property
if ObjType.GetField(ORMFieldName) = nil then
Continue; // Backing field not found, skip
if Prop.PropertyType.Name.StartsWith('TNullable<') then
begin
// Use RTTI to determine the generic type parameter
if Field.IsNull then
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), TValue.From<TNullable<string>>(NullValue))
else
begin
// Determine the generic type and set the value accordingly
if Prop.PropertyType.Name = 'TNullable<System.string>' then
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), TValue.From<TNullable<string>>(Field.AsString))
else if Prop.PropertyType.Name = 'TNullable<System.Integer>' then
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), TValue.From<TNullable<Integer>>(Field.AsInteger))
else if Prop.PropertyType.Name = 'TNullable<System.Int64>' then
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), TValue.From<TNullable<Int64>>(Field.AsLargeInt))
else if Prop.PropertyType.Name = 'TNullable<System.Extended>' then
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), TValue.From<TNullable<Extended>>(Field.AsFloat))
else if Prop.PropertyType.Name = 'TNullable<System.Currency>' then
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), TValue.From<TNullable<Currency>>(Field.AsCurrency))
else if Prop.PropertyType.Name = 'TNullable<System.TDateTime>' then
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), TValue.From<TNullable<TDateTime>>(Field.AsDateTime))
else if Prop.PropertyType.Name = 'TNullable<System.Boolean>' then
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), TValue.From<TNullable<Boolean>>(Field.AsBoolean))
else if Prop.PropertyType.Name = 'TNullable<System.Variant>' then
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), TValue.From<TNullable<Variant>>(Field.Value))
else
raise EORMUtilsException.CreateFmt('Unsupported TNullable type: %s', [Prop.PropertyType.Name]);
end;
end
else
raise EORMUtilsException.CreateFmt('Unsupported record type: %s', [Prop.PropertyType.Name]);
end
else
begin
// Handle non-nullable types
case Prop.PropertyType.TypeKind of
tkString, tkLString, tkWString, tkUString:
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), Field.AsString);
tkInteger:
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), Field.AsInteger);
tkInt64:
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), Field.AsLargeInt);
tkFloat:
if Prop.PropertyType.Handle = TypeInfo(Currency) then
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), Field.AsCurrency)
else if Prop.PropertyType.Handle = TypeInfo(TDateTime) then
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), Field.AsDateTime)
else
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), Field.AsFloat);
tkVariant:
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), TValue.FromVariant(Field.AsVariant));
tkEnumeration:
if Prop.PropertyType.Handle = TypeInfo(Boolean) then
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), Field.AsBoolean)
else
ObjType.GetField(ORMFieldName).SetValue(Pointer(RecInstance), Field.AsString);
else
// Unsupported type; you can extend this as needed
Continue;
end;
end;
end;
// Add the populated record to the list
FQryRecInfoList.Add(RecInstance);
except
RecInstance.Free;
raise; // Re-raise the exception after cleanup
end;
qr.Next;
end;
finally
// No need to free TRttiContext as it's a record
end;
finally
qr.Free;
end;
end;
function TQryRecArray.GetEnumerator: TEnumerator;
begin
Result := FQryRecInfoList.GetEnumerator;
end;
class function TQryRecArray.FetchAll(DBConn: TUniConnection; const QryTxt: string): TQryRecArray;
var
Instance: TQryRecArray;
begin
Instance := TQryRecArray.Create;
try
Instance.FetchAllInstance(DBConn, QryTxt);
Result := Instance;
except
Instance.Free;
raise;
end;
end;
end.
Carrefully review the Delphi code above, find out what is wrong with units interposer_classs and Main
After a thorough review of the provided Delphi code in the interposer_class and Main units, several critical issues were identified that could lead to compilation errors, runtime exceptions, and unexpected behaviors. Below is a detailed analysis of these problems along with recommendations for their resolution.
1. Accessing Private Members in TComponentHelper
Issue:
In the interposer_class unit, the TComponentHelper class helper is attempting to directly access the private member FComponentDict of the TORMManager class:
delphi
function TComponentHelper.GetORMRec: TQryRecInfo;
var
Key: TORMKey;
begin
Result := nil;
for Key in ORMManager.FComponentDict.Keys do // ❌ Accessing private member
if ORMManager.FComponentDict[Key] = Self then
begin
Result := Key.ORMRec;
Exit;
end;
end;
function TComponentHelper.GetORMRecField: string;
var
Key: TORMKey;
begin
Result := '';
for Key in ORMManager.FComponentDict.Keys do // ❌ Accessing private member
if ORMManager.FComponentDict[Key] = Self then
begin
Result := Key.FieldName;
Exit;
end;
end;
Why It's Problematic:
In Delphi, class helpers cannot access private or protected members of the classes they are extending. Attempting to do so will result in compilation errors because FComponentDict is declared as a private member of TORMManager.
Solution:
To resolve this, you should expose necessary functionality through public or protected methods within the TORMManager class. This way, the TComponentHelper can interact with TORMManager without directly accessing its private members.
Revised Code Example:
Add public methods to TORMManager to retrieve the ORMRec and FieldName based on a component:
delphi
type
TORMManager = class
private
FComponentDict: TDictionary<TORMKey, TComponent>;
public
// Existing methods...
function GetORMRecForComponent(Component: TComponent): TQryRecInfo;
function GetFieldNameForComponent(Component: TComponent): string;
end;
implementation
function TORMManager.GetORMRecForComponent(Component: TComponent): TQryRecInfo;
var
Key: TORMKey;
begin
Result := nil;
for Key in FComponentDict.Keys do
if FComponentDict[Key] = Component then
begin
Result := Key.ORMRec;
Exit;
end;
end;
function TORMManager.GetFieldNameForComponent(Component: TComponent): string;
var
Key: TORMKey;
begin
Result := '';
for Key in FComponentDict.Keys do
if FComponentDict[Key] = Component then
begin
Result := Key.FieldName;
Exit;
end;
end;
Update the TComponentHelper to use these public methods:
delphi
function TComponentHelper.GetORMRec: TQryRecInfo;
begin
Result := ORMManager.GetORMRecForComponent(Self);
end;
function TComponentHelper.GetORMRecField: string;
begin
Result := ORMManager.GetFieldNameForComponent(Self);
end;
2. Incorrect Type Casting in Main Unit's UpdateAllComponents Method
Issue:
In the Main unit, the UpdateAllComponents method indiscriminately casts every component retrieved from ORMManager.GetAllComponents to TEdit, regardless of the component's actual type:
delphi
procedure TMainForm.UpdateAllComponents;
begin
// Iterate through all mappings and update UI components
for var Comp in ORMManager.GetAllComponents do
if Comp is TComponent then
ORMManager.UpdateComponentFromORM(Comp, TEdit(Comp).ORMRec, TEdit(Comp).ORMRecField); // ❌ Incorrect casting
end;
Why It's Problematic:
Redundant Type Check: The check if Comp is TComponent is unnecessary since Comp is already of type TComponent.
Unsafe Casting: Casting all components to TEdit without verifying their actual type can lead to runtime exceptions. For instance, if Comp is a TLabel or TDatePicker, the cast to TEdit will fail.
Solution:
Implement type-specific handling by checking the type of each component before casting. This ensures that each component is safely cast to its appropriate type, preventing runtime errors.
Revised Code Example:
delphi
procedure TMainForm.UpdateAllComponents;
begin
// Iterate through all mappings and update UI components
for var Comp in ORMManager.GetAllComponents do
begin
if Comp is TEdit then
ORMManager.UpdateComponentFromORM(Comp, TEdit(Comp).ORMRec, TEdit(Comp).ORMRecField)
else if Comp is TLabel then
ORMManager.UpdateComponentFromORM(Comp, TLabel(Comp).ORMRec, TLabel(Comp).ORMRecField)
else if Comp is TDatePicker then
ORMManager.UpdateComponentFromORM(Comp, TDatePicker(Comp).ORMRec, TDatePicker(Comp).ORMRecField)
else if Comp is TComboBox then
ORMManager.UpdateComponentFromORM(Comp, TComboBox(Comp).ORMRec, TComboBox(Comp).ORMRecField)
// Add more component types as needed...
else
ShowMessage('Unsupported component type: ' + Comp.ClassName);
end;
end;
3. Potential Hash Code Overflow in TORMKeyComparer
Issue:
In the interposer_class unit, the TORMKeyComparer.GetHashCode method casts a NativeUInt to an Integer, which can lead to overflow issues on 64-bit systems:
delphi
function TORMKeyComparer.GetHashCode(const Key: TORMKey): Integer;
var
Hash: THashBobJenkins;
begin
Hash.Init;
Hash.Update(Integer(NativeUInt(Key.ORMRec))); // ❌ Potential overflow
Hash.Update(LowerCase(Key.FieldName));
Result := Hash.GetHashValue;
end;
Why It's Problematic:
32-bit vs. 64-bit Systems: On 64-bit systems, NativeUInt is 64 bits, whereas Integer is typically 32 bits. This casting can truncate the value, leading to poor hash distribution and potential collisions.
Solution:
Use a data type that accommodates the size of NativeUInt, such as Cardinal or a 64-bit integer type like Int64 or UInt64.
Revised Code Example:
delphi
function TORMKeyComparer.GetHashCode(const Key: TORMKey): Integer;
var
Hash: THashBobJenkins;
RecPtr: Cardinal; // Use Cardinal instead of Integer
begin
Hash.Init;
RecPtr := Cardinal(NativeUInt(Key.ORMRec)); // Safely cast NativeUInt to Cardinal
Hash.Update(RecPtr);
Hash.Update(LowerCase(Key.FieldName));
Result := Hash.GetHashValue;
end;
Alternatively, if you need to support 64-bit pointer sizes more reliably, consider using Int64 and ensuring that the hash function accommodates the larger size.
4. Improper Handling of Date Parsing in TORMManager.UpdateComponentFromORM
Issue:
In the interposer_class unit, the UpdateComponentFromORM method sets the DatePicker.Date to 0 when date parsing fails:
delphi
else if Component is TDatePicker then
begin
if TryStrToDate(FormattedText, TDatePicker(Component).Date) then
; // Successfully parsed date
else
TDatePicker(Component).Date := 0; // Handle invalid date ❌
end;
Why It's Problematic:
Setting the date to 0 corresponds to 30-Dec-1899, which is likely not a valid or meaningful date in most applications. This can lead to confusion for users and potential data integrity issues.
Solution:
Instead of setting the date to 0, consider implementing a more graceful error handling mechanism, such as displaying an error message, leaving the date unchanged, or setting it to a default valid date.
Revised Code Example:
delphi
else if Component is TDatePicker then
begin
if TryStrToDate(FormattedText, TDatePicker(Component).Date) then
; // Successfully parsed date
else
begin
ShowMessage('Invalid date format for component: ' + Component.Name);
// Optionally, set to a default date or leave unchanged
// TDatePicker(Component).Date := Now; // Example of setting to current date
end;
end;
5. Additional Recommendations
Consistent Naming Conventions: Ensure that unit names are consistent across usage. For example, if the unit is named interposer_class, avoid referencing it as interposer_classs.
Error Handling Enhancements: Throughout both units, enhance error handling to provide more descriptive messages and possibly recover from certain errors without terminating the application.
Code Documentation: Adding comments and documentation can help in maintaining the code and understanding the flow, especially in complex interaction scenarios like ORM bindings.
Conclusion
The primary issues in the interposer_class and Main units revolve around improper access to private class members and unsafe type casting, which can lead to significant runtime problems. Addressing these issues by adhering to Delphi's encapsulation principles and implementing safe type-checking mechanisms will enhance the stability and reliability of the application.
Final Note: Always thoroughly test these changes in a controlled environment to ensure that the modifications work as intended and do not introduce new issues.