#1 2021-12-23 11:03:46

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Fields order from TRestOrm.MultiFieldValue in a TRestStorageInMemory

I am using with Mormot2 the following function in a table with TRestStorageInMemory type storage

function TRestOrm.MultiFieldValue(Table: TOrmClass;
  const FieldName: array of RawUtf8; var FieldValue: array of RawUtf8;
  const WhereClause: RawUtf8): boolean;

I noticed that the order of FieldValue results do not follow the FieldName fields order but the fields' order in the table definition.

Offline

#2 2021-12-23 12:13:36

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

Re: Fields order from TRestOrm.MultiFieldValue in a TRestStorageInMemory

Yes, this is by design.

It made the code simpler, enhanced the TRestStorageInMemory speed, and also the ORM parsing speed.

Offline

#3 2021-12-23 12:18:17

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Fields order from TRestOrm.MultiFieldValue in a TRestStorageInMemory

If we do not know the table definition, Is it possible somehow to get the fieldname of each result returned?

I used the ExecuteJson and parse it with _json(json,variant,JSON_FAST) as alternative

Do you recommend an other function?

Last edited by dcoun (2021-12-23 13:40:08)

Offline

#4 2021-12-23 13:56:05

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

Re: Fields order from TRestOrm.MultiFieldValue in a TRestStorageInMemory

Oh you are right: for MultiFieldValue it is confusing.
I will fix it.

Edit: Please check https://github.com/synopse/mORMot2/commit/35583c64

Offline

#5 2021-12-23 17:24:57

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Fields order from TRestOrm.MultiFieldValue in a TRestStorageInMemory

ab wrote:

Oh you are right: for MultiFieldValue it is confusing.
I will fix it.

Edit: Please check https://github.com/synopse/mORMot2/commit/35583c64

I tested requesting 7 fields from a table with 72 fields.
                                         
As they exist in table definition: RowID, field1, field2, field3, field4, field5, field6
As requested: RowID, field1, field4, field6, field2, field3, field5
as received: RowID, field1, field4, field5, field2, field6, field3

Offline

#6 2021-12-23 20:35:32

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

Re: Fields order from TRestOrm.MultiFieldValue in a TRestStorageInMemory

I am not able to reproduce the issue.

Could you please try to debug it a little and find out what is wrong?

Offline

#7 2021-12-23 22:00:34

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Fields order from TRestOrm.MultiFieldValue in a TRestStorageInMemory

I think the code in mormot.orm.rest line 908 should be like the following:

        // get field values from the first (and unique) row
        for f := 0 to T.FieldCount - 1 do
        begin
          i := f; // regular SQL SELECT order by default
          if (T.FieldCount > 1) and
             (StrIComp(T.Results[i], pointer(FieldName[i])) <> 0) then
          begin
            // not the regular order (e.g. TRestStorageInMemory = TOrm order)
            i := T.FieldIndex(FieldName[f]);
            if i < 0 then
              if not IsFieldName(pointer(FieldName[f])) then
                exit    // something went wrong
              else
                i := f; // expects the function results to be in-order
          end;
          o := i + T.FieldCount;
          FastSetString(FieldValue[f], T.Results[o], T.ResultsLen[o]);
        end;

The above code works

Offline

#8 2021-12-24 09:54:57

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

Re: Fields order from TRestOrm.MultiFieldValue in a TRestStorageInMemory

You are right.

Please try https://github.com/synopse/mORMot2/commit/6f29067f

Sorry for the issue.

Offline

#9 2021-12-24 10:08:26

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Fields order from TRestOrm.MultiFieldValue in a TRestStorageInMemory

ab wrote:

You are right.

Please try https://github.com/synopse/mORMot2/commit/6f29067f

Sorry for the issue.

@ab,
Thank you again for your work and the people that contribute
Merry Christmas, Happy Year, seasonal wishes to all

Offline

Board footer

Powered by FluxBB