#1 2024-11-16 03:43:58

newsxy
Member
Registered: 2024-11-16
Posts: 1

TOrmTableDataSet.Locate multi-field value positioning

Hello!

I am using the TOrmTableDataSet.Locate method to locate records. When I specify a single field, it can correctly locate the record row. However, when I specify two fields for locating, it cannot correctly locate the record row.

Section    Ident
alipay      alipay_mchid
alipay      alipay_paymentrate

The following two lines of code both return True, and RecNo is always 1:

ds.Locate('Section;Ident', VararrayOf([Alipay, Alipay_MchId]), [loCaseInsensitive]);
ds.Locate('Section;Ident', VararrayOf([Alipay, Alipay_PaymentRate]), [loCaseInsensitive]);


I looked at the following code, and it seems that if the value of the first field matches successfully, it considers the location successful and does not proceed to compare the value of the second field. Shouldn't it be that both field values must match successfully for the location to be considered successful?

function TVirtualDataSet.Locate(const KeyFields: string;
  const KeyValues: variant; Options: TLocateOptions): boolean;
...
      found := SearchForField(StringToUtf8(KeyFields), KeyValues, Options);
      if found > 0 then
      begin
        RecNo := found;
        exit;
      end;
...

If there is no issue with the above code, how should I correctly locate records using multiple field values? Thank you!

Last edited by newsxy (2024-11-16 03:45:05)

Offline

#2 2024-11-16 08:03:33

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

Re: TOrmTableDataSet.Locate multi-field value positioning

My guess is that here fields.Count should = 2, so this is not this loop, but the 2nd one which is involved.

But anyway, the 2nd loop seems indeed buggy.
And there may be also some other smaller issues with the KeyValues[] support (range check, value retrieval)...

So a new TVirtualDataSet.SeachForFields() virtual method is needed, with proper support, for both TDocVariantArrayDataSet and TOrmTableDataSet.

I have created https://github.com/synopse/mORMot2/issues/325 to track this.

Offline

#3 Yesterday 10:18:27

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

Re: TOrmTableDataSet.Locate multi-field value positioning

Please try with
https://github.com/synopse/mORMot2/comm … e5742e7789

It should now work for all kind of TVirtualDataSet, i.e. ORM result, TDocVariant array, binary resultset...
Performance may not be optimal (there are temporary memory allocations of text strings during the search), but it should be fast enough on client side. If really needed, the new methods are virtual, so could be optimized for a given class.

Offline

Board footer

Powered by FluxBB