After carefully reviewing your Delphi unit orm_utils, I've identified and addressed several issues to ensure the code functions correctly and adheres to best practices. Below are the key fixes and improvements made:
code
Incorrect Use of TRttiContext.Free:
TRttiContext is a record, not a class. Therefore, calling Free on it is incorrect and has been removed from all relevant procedures.
Improper Handling of RecInstance:
In the SetValue method, Pointer(RecInstance) was incorrectly used. It has been replaced with RecInstance to correctly reference the object instance.
Type Name Comparisons:
Comparing type names as strings (e.g., 'TNullable<System.string>') can be fragile. Consider using RTTI type comparisons or generics for more robust type handling.
Event Handling to Prevent Recursion:
Added a flag to prevent recursive updates when binding components to properties, ensuring that updates don't trigger each other indefinitely.
Additional Enhancements:
Improved error handling and added comments for better readability and maintainability.
Ensured that all RTTI operations correctly reference object instances without typecasting to pointers.
Below is the revised and fixed version of your orm_utils unit:
///
/// 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>;
FBindings: TBindingDictionary;
FUpdating: Boolean; // Flag to prevent recursive updates
procedure ComponentChangeHandler(Sender: TObject);
procedure SetInfo(Index: Integer; const Value: TValue);
public
constructor Create;
destructor Destroy; override;
code
// 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);
/// <summary>
/// Binds a property to a UI component.
/// </summary>
procedure Bind(const PropertyName: string; Component: TComponent; Direction: TBindDirection = bdTwoWay);
/// <summary>
/// Binds properties based on component hints.
/// </summary>
procedure BindByHint;
/// <summary>
/// Unbinds a property or a component.
/// </summary>
procedure Unbind(const PropertyName: string); overload;
procedure Unbind(Component: TComponent); overload;
/// <summary>
/// Unbinds all bindings.
/// </summary>
procedure UnbindAll;
property Bindings: TBindingDictionary read FBindings;
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;
implementation
uses
System.Variants,
StrUtils,
pixel_utils, // Ensure this unit is in your project and properly implemented
Vcl.Forms; // Added for TControl to retrieve Owner in BindByHint
destructor TQryRecInfo.Destroy;
begin
UnbindAll;
FBindings.Free;
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;
NullableTypeName: string;
FieldValue: TValue;
begin
if not Assigned(DBConn) or Trim(QryTxt).IsEmpty then
Exit;
qr := TUniQuery.Create(nil);
try
qr.Connection := DBConn;
// SpecificOptions might vary based on your database components
qr.SQL.Text := QryTxt;
qr.Open;
code
if not qr.IsEmpty 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
NullableTypeName := Prop.PropertyType.Name;
if NullableTypeName.StartsWith('TNullable<') then
begin
// Use RTTI to determine the generic type parameter
if Field.IsNull then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Variant>>(NullValue))
else
begin
if NullableTypeName = 'TNullable<string>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<string>>(Field.AsString))
else if NullableTypeName = 'TNullable<Integer>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Integer>>(Field.AsInteger))
else if NullableTypeName = 'TNullable<Int64>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Int64>>(Field.AsLargeInt))
else if NullableTypeName = 'TNullable<Extended>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Extended>>(Field.AsFloat))
else if NullableTypeName = 'TNullable<Currency>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Currency>>(Field.AsCurrency))
else if NullableTypeName = 'TNullable<TDateTime>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<TDateTime>>(Field.AsDateTime))
else if NullableTypeName = 'TNullable<Boolean>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Boolean>>(Field.AsBoolean))
else if NullableTypeName = 'TNullable<Variant>' then
ObjType.GetField(ORMFieldName).SetValue(Self, TValue.From<TNullable<Variant>>(Field.Value))
else
raise EORMUtilsException.CreateFmt('Unsupported TNullable type: %s', [NullableTypeName]);
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
ObjType.GetField(ORMFieldName).SetValue(Self, Field.AsString);
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
// Prevent recursive updates
if FUpdating then
Exit;
FUpdating := True;
try
// Retrieve property name and kind based on index
// Assuming that the index corresponds to the property order
Ctx := TRttiContext.Create;
try
if (Index < 0) or (Index >= Ctx.GetType(Self.ClassType).GetProperties.Length) then
raise EORMUtilsException.CreateFmt('Error: The index "%d" does not correspond to any property in class "%s".', [Index, ClassName]);
code
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]);
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);
finally
// No need to free TRttiContext as it's a record
end;
finally
FUpdating := False;
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;
NullableTypeName: string;
FieldValue: TValue;
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.');
qr := TUniQuery.Create(nil);
try
qr.Connection := DBConn;
// SpecificOptions might vary based on your database components
qr.SQL.Text := QryTxt;
qr.Open;
code
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
NullableTypeName := Prop.PropertyType.Name;
if NullableTypeName.StartsWith('TNullable<') then
begin
// Use RTTI to determine the generic type parameter
if Field.IsNull then
ObjType.GetField(ORMFieldName).SetValue(RecInstance, TValue.From<TNullable<Variant>>(NullValue))
else
begin
// Determine the generic type and set the value accordingly
if NullableTypeName = 'TNullable<string>' then
ObjType.GetField(ORMFieldName).SetValue(RecInstance, TValue.From<TNullable<string>>(Field.AsString))
else if NullableTypeName = 'TNullable<Integer>' then
ObjType.GetField(ORMFieldName).SetValue(RecInstance, TValue.From<TNullable<Integer>>(Field.AsInteger))
else if NullableTypeName = 'TNullable<Int64>' then
ObjType.GetField(ORMFieldName).SetValue(RecInstance, TValue.From<TNullable<Int64>>(Field.AsLargeInt))
else if NullableTypeName = 'TNullable<Extended>' then
ObjType.GetField(ORMFieldName).SetValue(RecInstance, TValue.From<TNullable<Extended>>(Field.AsFloat))
else if NullableTypeName = 'TNullable<Currency>' then
ObjType.GetField(ORMFieldName).SetValue(RecInstance, TValue.From<TNullable<Currency>>(Field.AsCurrency))
else if NullableTypeName = 'TNullable<TDateTime>' then
ObjType.GetField(ORMFieldName).SetValue(RecInstance, TValue.From<TNullable<TDateTime>>(Field.AsDateTime))
else if NullableTypeName = 'TNullable<Boolean>' then
ObjType.GetField(ORMFieldName).SetValue(RecInstance, TValue.From<TNullable<Boolean>>(Field.AsBoolean))
else if NullableTypeName = 'TNullable<Variant>' then
ObjType.GetField(ORMFieldName).SetValue(RecInstance, TValue.From<TNullable<Variant>>(Field.Value))
else
raise EORMUtilsException.CreateFmt('Unsupported TNullable type: %s', [NullableTypeName]);
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(RecInstance, Field.AsString);
tkInteger:
ObjType.GetField(ORMFieldName).SetValue(RecInstance, Field.AsInteger);
tkInt64:
ObjType.GetField(ORMFieldName).SetValue(RecInstance, Field.AsLargeInt);
tkFloat:
if Prop.PropertyType.Handle = TypeInfo(Currency) then
ObjType.GetField(ORMFieldName).SetValue(RecInstance, Field.AsCurrency)
else if Prop.PropertyType.Handle = TypeInfo(TDateTime) then
ObjType.GetField(ORMFieldName).SetValue(RecInstance, Field.AsDateTime)
else
ObjType.GetField(ORMFieldName).SetValue(RecInstance, Field.AsFloat);
tkVariant:
ObjType.GetField(ORMFieldName).SetValue(RecInstance, TValue.FromVariant(Field.AsVariant));
tkEnumeration:
if Prop.PropertyType.Handle = TypeInfo(Boolean) then
ObjType.GetField(ORMFieldName).SetValue(RecInstance, Field.AsBoolean)
else
ObjType.GetField(ORMFieldName).SetValue(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;
{ TBindingDictionary }
constructor TBindingDictionary.Create(AOwner: TQryRecInfo);
begin
inherited Create;
FOwner := AOwner;
end;
procedure TBindingDictionary.UpdateComponents;
var
Pair: TPair<string, TComponentBindingInfo>;
PropValue: string;
Component: TComponent;
PropInfo: PPropInfo;
begin
for Pair in Self do
begin
if not (Pair.Value.Direction in [bdTwoWay, bdToComponent]) then
Continue;
code
Component := Pair.Value.Component;
if not Assigned(Component) then
Continue;
// Get formatted text value from the property
PropValue := FOwner.GetFormattedText(Pair.Key);
// Set component property based on its type
PropInfo := GetPropInfo(Component.ClassInfo, Pair.Value.PropertyName);
if not Assigned(PropInfo) then
Continue;
try
case PropInfo^.PropType^.Kind of
tkString, tkLString, tkWString, tkUString:
SetStrProp(Component, PropInfo, PropValue);
tkFloat:
if PropInfo^.PropType^ = TypeInfo(TDateTime) then
SetFloatProp(Component, PropInfo, StrToDateTimeDef(PropValue, 0))
else
SetFloatProp(Component, PropInfo, StrToFloatDef(PropValue, 0));
tkInteger:
SetOrdProp(Component, PropInfo, StrToIntDef(PropValue, 0));
tkInt64:
SetInt64Prop(Component, PropInfo, StrToInt64Def(PropValue, 0));
tkVariant:
SetVariantProp(Component, PropInfo, PropValue);
end;
except
on E: Exception do
raise EORMUtilsException.CreateFmt('Error updating component property "%s": %s', [Pair.Key, E.Message]);
end;
for Pair in Self do
begin
if not (Pair.Value.Direction in [bdTwoWay, bdFromComponent]) then
Continue;
Component := Pair.Value.Component;
if not Assigned(Component) then
Continue;
// Get component property value
PropInfo := GetPropInfo(Component.ClassInfo, Pair.Value.PropertyName);
if not Assigned(PropInfo) then
Continue;
try
case PropInfo^.PropType^.Kind of
tkString, tkLString, tkWString, tkUString:
PropValue := GetStrProp(Component, PropInfo);
tkFloat:
if PropInfo^.PropType^ = TypeInfo(TDateTime) then
PropValue := DateTimeToStr(GetFloatProp(Component, PropInfo))
else
PropValue := FloatToStr(GetFloatProp(Component, PropInfo));
tkInteger:
PropValue := IntToStr(GetOrdProp(Component, PropInfo));
tkInt64:
PropValue := IntToStr(GetInt64Prop(Component, PropInfo));
tkVariant:
PropValue := VarToStr(GetVariantProp(Component, PropInfo));
else
Continue;
end;
except
on E: Exception do
raise EORMUtilsException.CreateFmt('Error reading component property "%s": %s', [Pair.Key, E.Message]);
end;
// Find and update the property using RTTI
RttiProp := RttiType.GetProperty(Pair.Key);
if Assigned(RttiProp) and RttiProp.IsWritable then
begin
try
case RttiProp.PropertyType.TypeKind of
tkString, tkLString, tkWString, tkUString:
RttiProp.SetValue(FOwner, PropValue);
tkInteger:
RttiProp.SetValue(FOwner, StrToIntDef(PropValue, 0));
tkInt64:
RttiProp.SetValue(FOwner, StrToInt64Def(PropValue, 0));
tkFloat:
if RttiProp.PropertyType.Handle = TypeInfo(TDateTime) then
RttiProp.SetValue(FOwner, StrToDateTimeDef(PropValue, 0))
else if RttiProp.PropertyType.Handle = TypeInfo(Currency) then
RttiProp.SetValue(FOwner, StrToCurrDef(PropValue, 0))
else
RttiProp.SetValue(FOwner, StrToFloatDef(PropValue, 0));
tkVariant:
RttiProp.SetValue(FOwner, PropValue);
end;
except
on E: Exception do
raise EORMUtilsException.CreateFmt('Error updating property "%s": %s', [Pair.Key, E.Message]);
end;
end;
end;
finally
// No need to free TRttiContext as it's a record
end;
end;
{ Prevent Recursive Updates }
procedure TQryRecInfo.ComponentChangeHandler(Sender: TObject);
begin
if FUpdating then
Exit;
if Sender is TComponent then
begin
FUpdating := True;
try
FBindings.UpdateProperties;
finally
FUpdating := False;
end;
end;
end;
procedure TQryRecInfo.Bind(const PropertyName: string; Component: TComponent; Direction: TBindDirection = bdTwoWay);
var
BindingInfo: TComponentBindingInfo;
PropName: string;
begin
if not Assigned(Component) then
Exit;
// Determine the appropriate component property to bind to
PropName := '';
if Component is TEdit then
PropName := 'Text'
else if Component is TLabel then
PropName := 'Caption'
else if Component is TDateTimePicker then
PropName := 'Date'
else if Component is TMemo then
PropName := 'Text'
else if Component is TComboBox then
PropName := 'Text'
else if Component is TCheckBox then
PropName := 'Checked'
else if Component is TRadioButton then
PropName := 'Checked'
else if Component is TListBox then
PropName := 'Items.Text'
else if Component is TComboBoxEx then
PropName := 'Text'
// Add more component types as needed
;
// Remove any existing binding for this property
Unbind(PropertyName);
// Add the new binding
FBindings.Add(PropertyName, BindingInfo);
// Set up change notification
if Component is TEdit then
TEdit(Component).OnChange := ComponentChangeHandler
else if Component is TMemo then
TMemo(Component).OnChange := ComponentChangeHandler
else if Component is TComboBox then
TComboBox(Component).OnChange := ComponentChangeHandler
else if Component is TDateTimePicker then
TDateTimePicker(Component).OnChange := ComponentChangeHandler
else if Component is TCheckBox then
TCheckBox(Component).OnClick := ComponentChangeHandler
else if Component is TRadioButton then
TRadioButton(Component).OnClick := ComponentChangeHandler
else if Component is TListBox then
TListBox(Component).OnClick := ComponentChangeHandler
// Add more component event handlers as needed
;
// Get the owner component (form or frame)
// Assumes that the owner is the owner of all bound components
Owner := nil;
if FBindings.Count > 0 then
Owner := FBindings.Items[FBindings.Count - 1].Component.Owner
else
begin
// Attempt to find the owner from the component hints
// This requires that at least one binding exists or handle differently
for I := 0 to Screen.FormCount - 1 do
begin
if Screen.Forms[I] is TComponent then
begin
Owner := Screen.Forms[I];
Break;
end;
end;
end;
if not Assigned(Owner) then
Exit;
// Look through all components
for I := 0 to Owner.ComponentCount - 1 do
begin
Component := Owner.Components[I];
if (Component is TControl) and (Component.Hint <> '') and Assigned(RttiType.GetProperty(Component.Hint)) then
Bind(Component.Hint, Component);
end;
finally
// No need to free TRttiContext as it's a record
end;
end;
procedure TQryRecInfo.Unbind(const PropertyName: string);
var
BindingInfo: TComponentBindingInfo;
begin
if FBindings.TryGetValue(PropertyName, BindingInfo) then
begin
// Remove change handler
if BindingInfo.Component is TEdit then
TEdit(BindingInfo.Component).OnChange := nil
else if BindingInfo.Component is TMemo then
TMemo(BindingInfo.Component).OnChange := nil
else if BindingInfo.Component is TComboBox then
TComboBox(BindingInfo.Component).OnChange := nil
else if BindingInfo.Component is TDateTimePicker then
TDateTimePicker(BindingInfo.Component).OnChange := nil
else if BindingInfo.Component is TCheckBox then
TCheckBox(BindingInfo.Component).OnClick := nil
else if BindingInfo.Component is TRadioButton then
TRadioButton(BindingInfo.Component).OnClick := nil
else if BindingInfo.Component is TListBox then
TListBox(BindingInfo.Component).OnClick := nil
// Remove more component event handlers as needed
;
code
FBindings.Remove(PropertyName);
end;
end;
procedure TQryRecInfo.Unbind(Component: TComponent);
var
Pair: TPair<string, TComponentBindingInfo>;
ToRemove: TList;
PropName: string;
begin
ToRemove := TList.Create;
try
for Pair in FBindings do
begin
if Pair.Value.Component = Component then
begin
ToRemove.Add(Pair.Key);
end;
end;
code
for PropName in ToRemove do
Unbind(PropName);
finally
ToRemove.Free;
end;
end;
procedure TQryRecInfo.UnbindAll;
var
PropNames: TArray;
PropName: string;
begin
PropNames := FBindings.Keys.ToArray;
for PropName in PropNames do
Unbind(PropName);
end;
{ TQryRecArray }
function TQryRecArray.GetCount: Integer;
begin
Result := FQryRecInfoList.Count;
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.
Thank you for sharing the revised orm_utils Delphi unit. I appreciate the detailed context and the comprehensive changes you've implemented. Below, I’ll provide a thorough review of your revisions, highlighting both strengths and areas where further improvements or clarifications might be beneficial.
1. Addressed Issues
a. Incorrect Use of TRttiContext.Free
Your Fix:
You correctly identified that TRttiContext is a record, not a class, and therefore should not be freed. Removing the .Free calls is the right approach.
Recommendation:
Ensure that all usages of TRttiContext across the unit adhere to this change, as you’ve done.
b. Improper Handling of RecInstance
Your Fix:
Replacing Pointer(RecInstance) with RecInstance ensures correct referencing of the object instance in the SetValue method.
Recommendation:
Double-check all instances where object references are handled to prevent similar issues elsewhere in the code.
c. Type Name Comparisons
Your Observation:
Comparing type names as strings (e.g., 'TNullable<System.string>') can be fragile.
Recommendation:
Beyond your suggestion, consider leveraging Delphi’s generic type handling more robustly. Utilizing RTTI to extract and compare generic type parameters can enhance type safety and reduce string-based errors. For example:
delphi
if (UnderlyingType.IsGeneric) and
(UnderlyingType.GetGenericTypeDefinition.Name = 'TNullable`1') then
begin
// Handle TNullable<T>
end;
This approach avoids hardcoding type names and leverages Delphi's type system more effectively.
d. Event Handling to Prevent Recursion
Your Fix:
Introducing an FUpdating flag to prevent recursive updates when binding components is a solid strategy to mitigate infinite loops.
Recommendation:
Ensure that all entry and exit points of property and component updates correctly set and unset the FUpdating flag. Consider using a stack or counter if multiple nested updates may occur, providing more flexibility and safety.
e. Additional Enhancements
Your Enhancements:
Improved error handling, added comments, and ensured accurate RTTI operations contribute significantly to the code's robustness and maintainability.
Recommendation:
Maintain consistent commenting standards and consider documenting complex logic blocks with more detailed explanations or references to where necessary.
2. Code Review and Recommendations
While your revisions address key issues, there are additional areas where further enhancements can be made to improve functionality, performance, and code clarity.
a. Error Handling
Current Implementation:
You have implemented error handling using EORMUtilsException in various parts of the code, which is excellent for tracing issues.
Recommendations:
Granularity: Where possible, differentiate error messages based on the error context to aid in debugging. For example, specify whether an error occurred during fetching, binding, or property updating.
Logging: Integrate a logging mechanism to record exceptions and operational messages. This can be invaluable for diagnosing issues in production environments.
b. RTTI Usage
Current Implementation:
Extensive use of RTTI (TRttiContext, TRttiType, etc.) is evident, which provides flexibility in handling properties and fields dynamically.
Recommendations:
Performance Considerations: RTTI operations can be performance-intensive, especially within loops or frequently called methods like FetchAllInstance. To mitigate this:
Caching: Cache RTTI results (e.g., property and field metadata) if they are reused multiple times.
Limit Scope: Minimize the scope and frequency of RTTI contexts where possible.
Error Handling: Ensure that all RTTI operations are enclosed in appropriate try-except blocks to gracefully handle unexpected scenarios, such as missing properties or type mismatches.
c. Type Safety and Generics
Current Implementation:
The use of generics in TQryRecArray<T> enhances type safety and reusability.
Recommendations:
Generic Constraints: You’ve applied constraints (T: class, constructor), which is good. If T is expected to descend from TQryRecInfo, consider specifying that constraint explicitly:
delphi
TQryRecArray<T: TQryRecInfo, constructor>
This allows the compiler to enforce that T inherits properties and methods from TQryRecInfo, preventing misuse.
Type Checks: Instead of relying on type names, utilize Delphi’s type system to handle generic parameters more robustly. For example, use T.IsInstance or type inheritance hierarchies to manage different subclasses.
d. Bindings and UI Integration
Current Implementation:
The binding mechanism maps properties to UI components and handles two-way data synchronization, which is crucial for responsive applications.
Recommendations:
Support for More Components: While you’ve covered several common VCL components, consider abstracting the binding registration to allow easier extension for additional components. For instance, implement a registration mechanism where component types and their bindable properties can be registered dynamically.
Validation and Conversion: Incorporate data validation and conversion logic within bindings to handle scenarios where component inputs may not match the expected property types. This can prevent runtime errors and ensure data integrity.
Memory Management: Ensure that all event handlers are properly detached during Unbind and UnbindAll to prevent memory leaks or dangling references, especially in dynamic UI scenarios.
e. Nullable Types Handling
Current Implementation:
Handling TNullable<T> types with RTTI ensures that nullable database fields are correctly managed.
Recommendations:
Cross-Type Handling: If additional nullable types are introduced in the future, consider abstracting the nullable handling into separate utility functions or classes. This modularity can simplify extensions and maintenance.
Default Values: Define and manage default values for nullable properties to ensure consistent behavior when fields are null.
f. Code Duplication
Current Implementation:
There is noticeable duplication, especially in handling different property types in methods like FetchAllInstance and GetFormattedText.
Recommendations:
Refactoring: Abstract repetitive code into helper methods. For example, creating a method that maps TypeKind to appropriate getter and setter functions can reduce redundancy.
Enumerations and Mappings: Utilize enumerations or dictionaries to map TypeKind values to specific handling logic, making the code more scalable and easier to update.
g. Documentation and Comments
Current Implementation:
You’ve added comments for better readability and maintainability, which is highly beneficial.
Recommendations:
Detailed Descriptions: For complex methods like Fetch and FetchAllInstance, include step-by-step explanations or clarify assumptions (e.g., naming conventions for fields).
Usage Examples: Provide usage examples or references in the documentation to guide other developers on integrating orm_utils within their projects.
h. Naming Conventions and Code Style
Current Implementation:
The code follows standard Delphi naming conventions, enhancing readability.
Recommendations:
Consistency: Ensure consistent naming across all methods and variables. For instance, method names can follow a verb-noun pattern (UpdateComponents, ClearDirtyFlags).
Field Prefixes: You’re using underscores (_) for backing fields, which is standard. Ensure this is consistently applied and documented.
i. Handling Unsupported Types
Current Implementation:
For unsupported types in methods like FormataInfo and FetchAllInstance, you raise exceptions.
Recommendations:
Extensibility: Provide mechanisms to extend the supported types without modifying core logic. For example, implement an interface or use a factory pattern to allow registration of custom type handlers.
Graceful Degradation: Instead of stopping execution with an exception, consider logging a warning and continuing, especially in scenarios where non-critical fields might be skipped.
j. Thread Safety
Current Implementation:
The current implementation doesn’t explicitly address thread safety.
Recommendations:
Concurrency Considerations: If orm_utils is intended to be used in multi-threaded environments, ensure that shared resources (like dictionaries) are protected with appropriate synchronization mechanisms.
Immutable Contexts: Where possible, use immutable objects or make copies of shared data to prevent race conditions.
3. Potential Code Enhancements
Here are some specific code enhancements and optimizations based on your current implementation:
a. Caching RTTI Contexts and Types
Instead of creating a new TRttiContext in every method, consider caching essential RTTI information at the class level:
delphi
type
TQryRecInfo = class
private
class var FContext: TRttiContext;
class var FType: TRttiType;
// ...
public
class constructor Create;
class destructor Destroy;
// ...
end;
class constructor TQryRecInfo.Create;
begin
FContext := TRttiContext.Create;
FType := FContext.GetType(Self);
end;
class destructor TQryRecInfo.Destroy;
begin
FContext.Free;
end;
This avoids repeatedly creating and destroying TRttiContext instances, improving performance.
b. Improving BindByHint Logic
The current implementation of BindByHint assumes that component hints correspond directly to property names. To enhance flexibility:
Attribute-Based Binding: Use custom attributes to mark components for binding, allowing more descriptive and flexible bindings.
delphi
type
BindPropAttribute = class(TCustomAttribute)
private
FPropertyName: string;
public
constructor Create(const APropertyName: string);
property PropertyName: string read FPropertyName;
end;
Components can then be tagged with [BindProp('PropertyName')], and BindByHint can search for this attribute instead of relying on hints.
c. Utilizing Enumerations and Mappings
Define mappings between component types and their bindable properties to streamline the Bind method:
This approach simplifies binding logic and makes it easier to add support for new components.
4. General Best Practices
Unit Segregation: If the unit grows larger, consider splitting it into multiple units based on functionality (e.g., bindings, RTTI utilities, data fetching) to enhance maintainability.
Memory Management: Ensure that all dynamically created objects are appropriately freed, especially in exception scenarios. Utilizing try...finally blocks, as you have, is essential.
Performance Profiling: As RTTI and reflection can introduce performance overhead, profile the application to identify and optimize bottlenecks.
Testing: Implement comprehensive unit tests to verify the functionality of critical components, especially dynamic property handling and bindings.
5. Final Thoughts
Your revisions to the orm_utils unit demonstrate a strong understanding of Delphi’s RTTI, generics, and component-binding mechanisms. By addressing the initial issues and implementing best practices, you've significantly enhanced the unit's robustness and maintainability.
The additional recommendations and enhancements provided aim to further refine the utility, ensuring it remains scalable, efficient, and easy to maintain as the project evolves. Keep up the excellent work, and consider implementing these suggestions to elevate the orm_utils unit even further.
If you have any specific questions or need further clarification on any of the points discussed, feel free to ask!