Based on the followind example, adapt if to pupulate the info from a TUniQuery and map…
- User
Based on the followind example, adapt if to pupulate the info from a TUniQuery and map it to the properties that map the Field anmes of the query:
unit Example_SetInfoNullable;
interface
uses System.SysUtils, System.Rtti, pixel_classes;
type TEmployeeProject = class private FEmployeeID: TNullable; FDepartmentID: TNullable; FEmployeeName: TNullable; FProjectID: TNullable; FProjectName: TNullable; FProjectBudget: TNullable; FProjectDate: TNullable; FProjectStartMoment: TNullable;
published property EmployeeID: TNullable read FEmployeeID write FEmployeeID; property DepartmentID: TNullable read FDepartmentID write FDepartmentID; property EmployeeName: TNullable read FEmployeeName write FEmployeeName; property ProjectID: TNullable read FProjectID write FProjectID; property ProjectName: TNullable read FProjectName write FProjectName; property ProjectBudget: TNullable read FProjectBudget write FProjectBudget; property ProjectDate: TNullable read FProjectDate write FProjectDate; property ProjectStartMoment: TNullable read FProjectStartMoment write FProjectStartMoment; end;
procedure ORM_TestNullable;
implementation
procedure Set_Info(MyObj:TObject; PropName: string; const Value: TValue); var PropKind: TTypeKind; Ctx: TRttiContext; Field: TRttiField; begin Ctx := TRttiContext.Create; try Field := Ctx.GetType(MyObj.ClassType).GetField('F' + PropName); if Assigned(Field) then Field.SetValue(MyObj, Value) else raise Exception.Create('Error: Field "' + PropName + '" not found.'); finally // TRttiContext is a record; no need to free end; end;
procedure Set_Info_NullableDateTime(MyObj:TObject; PropName: string; const Value: TNullable); begin Set_Info(MyObj, PropName, TValue.From<TNullable>(Value)); end;
procedure Set_Info_NullableExtended(MyObj:TObject; PropName: string; const Value: TNullable); begin Set_Info(MyObj, PropName, TValue.From<TNullable>(Value)); end;
procedure Set_Info_NullableInteger(MyObj:TObject; PropName: string; const Value: TNullable); begin Set_Info(MyObj, PropName, TValue.From<TNullable>(Value)); end;
procedure Set_Info_NullableInt64(MyObj:TObject; PropName: string; const Value: TNullable); begin Set_Info(MyObj, PropName, TValue.From<TNullable>(Value)); end;
procedure Set_Info_NullableString(MyObj:TObject; PropName: string; const Value: TNullable); begin Set_Info(MyObj, PropName, TValue.From<TNullable>(Value)); end;
procedure Set_Info_NullableVariant(MyObj:TObject; PropName: string; const Value: TNullable); begin Set_Info(MyObj, PropName, TValue.From<TNullable>(Value)); end;
procedure DisplayProjectInfo(Project: TEmployeeProject); begin Writeln('Employee ID: ', Project.EmployeeID.Value); Writeln('Department ID: ', Project.DepartmentID.Value); Writeln('Employee Name: ', Project.EmployeeName.Value); Writeln('Project ID: ', Project.ProjectID.Value); Writeln('Project Name: ', Project.ProjectName.Value); Writeln('Project Budget: ', FormatCurr('0.00', Project.ProjectBudget.Value)); Writeln('Project Date: ', DateTimeToStr(Project.ProjectDate.Value)); Writeln('Project Start Moment: ', DateTimeToStr(Project.ProjectStartMoment.Value)); end;
- Assistant