#1 2021-12-28 13:19:21

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Looks like TSQLRest.AddOrUpdate needs a fix or am i wrong?

Hi everyone,

It's not about mORMot2, but i am using latest version of mORMot and find a small bug imo.

function TSQLRest.AddOrUpdate(Value: TSQLRecord; ForceID: boolean): TID;
begin
  if (self=nil) or (Value=nil) then begin
    result := 0;
    exit;
  end;
  if ForceID or (Value.fID=0) then begin // Add, raises an exception 
    result := Add(Value,true,ForceID);
    if (result<>0) or (Value.fID=0) then
      exit;
  end;
  if Update(Value) then
    result := Value.fID else
    result := 0;
end;

When i set ForceID to True, AddOrUpdate raises an exception because it's uses OR. In this scenario even Value.fID is not 0 it still try to add this record. Isn't this suppose to be AND operator?

Last edited by koraycayiroglu (2021-12-28 14:20:01)

Offline

#2 2021-12-28 16:48:32

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,686
Website

Re: Looks like TSQLRest.AddOrUpdate needs a fix or am i wrong?

ForceID is expected to be set with ID<>0.

It is documented as such.
Please read the comment/documentation of the method definition:

// - implements REST POST if Value.ID=0 or ForceID is set, or a REST PUT
// collection to update the record pointed by a Value.ID<>0

Offline

#3 2021-12-29 10:29:43

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Looks like TSQLRest.AddOrUpdate needs a fix or am i wrong?

Sorry, i thought AddOrUpdate means as it is and when i set ForceID to true, it will still add or update the same record with same ID. No problems, i changed it as:


function TSQLRest.AddOrUpdate(Value: TSQLRecord; ForceID: boolean): TID;
begin
  if (self=nil) or (Value=nil) then begin
    result := 0;
    exit;
  end;
  if (Value.fID=0) or (ForceID and (not MemberExists(Value.RecordClass, Value.fID))) then begin
    result := Add(Value,true,ForceID);
    if (result<>0) or (Value.fID=0) then
      exit;
  end;
  if Update(Value) then
    result := Value.fID else
    result := 0;
end;

it's better this way imo.

Last edited by koraycayiroglu (2021-12-29 10:50:42)

Offline

Board footer

Powered by FluxBB