Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Update Fields Enum #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions SimpleRTTI.pas
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,21 @@ function TSimpleRTTI<T>.FieldsInsert(var aFields: String): iSimpleRTTI<T>;
if prpRtti.IsIgnore then
Continue;

if (prpRtti.IsEnum) then
begin
case prpRtti.PropertyType.TypeKind of
tkInteger, tkInt64, tkFloat:
if prpRtti.GetValue(Pointer(FInstance)).AsInteger = 0 then
Continue;
tkWChar,
tkLString,
tkWString,
tkUString,
tkString:
if prpRtti.GetValue(Pointer(FInstance)).AsString = EmptyStr then
Continue;
end;
end;
aFields := aFields + prpRtti.FieldName + ', ';
end;
finally
Expand Down Expand Up @@ -713,10 +728,14 @@ function TSimpleRTTI<T>.Param (var aParam : String) : iSimpleRTTI<T>;
typRtti : TRttiType;
prpRtti : TRttiProperty;
Info : PTypeInfo;
DictionaryFields: TDictionary<String, Variant>;
FieldValue: Variant;
begin
Result := Self;
Info := System.TypeInfo(T);
ctxRtti := TRttiContext.Create;
DictionaryFields := TDictionary<String, Variant>.Create;
Self.DictionaryFields(DictionaryFields);
try
typRtti := ctxRtti.GetType(Info);
for prpRtti in typRtti.GetProperties do
Expand All @@ -729,6 +748,10 @@ function TSimpleRTTI<T>.Param (var aParam : String) : iSimpleRTTI<T>;

if prpRtti.IsEnum then
begin
DictionaryFields.TryGetValue(prpRtti.FieldName, FieldValue);
if FieldValue = EmptyStr then
Continue;

aParam := aParam + ':' + prpRtti.FieldName + '::' + prpRtti.EnumName + ', ';
Continue;
end;
Expand All @@ -738,6 +761,7 @@ function TSimpleRTTI<T>.Param (var aParam : String) : iSimpleRTTI<T>;
finally
aParam := Copy(aParam, 0, Length(aParam) - 2) + ' ';
ctxRtti.Free;
DictionaryFields.Free;
end;
end;

Expand Down Expand Up @@ -804,6 +828,19 @@ function TSimpleRTTI<T>.Update(var aUpdate : String) : iSimpleRTTI<T>;

if prpRtti.IsEnum then
begin
case prpRtti.PropertyType.TypeKind of
tkInteger, tkInt64, tkFloat:
if prpRtti.GetValue(Pointer(FInstance)).AsInteger = 0 then
Continue;
tkWChar,
tkLString,
tkWString,
tkUString,
tkString:
if prpRtti.GetValue(Pointer(FInstance)).AsString = EmptyStr then
Continue;
end;

aUpdate := aUpdate + prpRtti.FieldName + ' = :' + prpRtti.FieldName + '::' + prpRtti.EnumName + ', ';
Continue;
end;
Expand Down