#1 2024-04-05 12:24:54

imperyal
Member
Registered: 2018-10-11
Posts: 51

TRestStorageInMemory Add error

Hello!

The code below works on mORMot 1 but not on version 2...

procedure TForm1.Button1Click(Sender: TObject);
var
  MSC_mem:        TRestStorageInMemory;
  MSR_table:      TOrmUser;
begin
  MSC_mem   := TRestStorageInMemory.Create(TOrmUser, nil, '');
  MSR_table := TOrmUser.Create;

  MSR_table.FillPrepare(MSC_mem, '');

  // Add a Record
  MSR_table.SetFieldVariant('Name', 'Paula');
  MSR_table.SetFieldVariant('Age',  48);
  MSC_mem.Add(MSR_table, true, false, false);

  // Save JSON
  MSR_table.FillPrepare(MSC_mem, '');
  Memo1.Lines.Text := MSR_table.FillTable.GetJSONValues(true);


  MSC_mem.Free;
end;

I get an error on line: MSC_mem.Add(MSR_table, true, false, false);
(Access violation error)



What is the problem?

I will use this to convert some data to JSON, multiple records.

Thank you.

Last edited by imperyal (2024-04-05 12:27:05)

Offline

#2 2024-04-05 12:56:11

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

Re: TRestStorageInMemory Add error

TRestStorageInMemory requires to be used with a TRestOrmServer instance and its associated TRestModel.

Use TRestServerFullMemory for a stand-alone storage, e.g. as TRestServerFullMemory.CreateWithOwnModel([TOrmUser]).
Then use its Orm property to work as abstract as possible with its IRestOrm associated interface.

Offline

#3 2024-04-05 15:10:29

imperyal
Member
Registered: 2018-10-11
Posts: 51

Re: TRestStorageInMemory Add error

It is working now... Thank you ab.

Please just confirm that this is correct...

procedure TForm1.Button1Click(Sender: TObject);
var
  MSC_mem:        TRestServerFullMemory;
  MSR_table:      TOrmUser;
begin

  MSC_mem   := TRestServerFullMemory.CreateWithOwnModel([TOrmUser]);
  MSR_table := TOrmUser.Create;

  MSR_table.FillPrepare(MSC_mem.Orm, '');

  // Add a Record
  MSR_table.ClearProperties;
  MSR_table.SetFieldVariant('Name', 'Paula');
  MSR_table.SetFieldVariant('Age',  48);
  MSC_mem.Add(MSR_table, true);

  // Add another Record
  MSR_table.ClearProperties;
  MSR_table.SetFieldVariant('Name', 'Maria');
  MSR_table.SetFieldVariant('Age',  48);
  MSC_mem.Add(MSR_table, true);


  // Save JSON
  MSR_table.FillPrepare(MSC_mem.Orm, '');
  Memo1.Lines.Text := MSR_table.FillTable.GetJSONValues(true);


  MSR_table.Free;
  MSC_mem.Free;
end;

Last edited by imperyal (2024-04-05 15:11:41)

Offline

#4 2024-04-05 17:03:46

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

Re: TRestStorageInMemory Add error

Perhaps TDocVariant or IDocList may be a better approach to process JSON without any format.

But your code seems fine.
Just take care that perhaps using IRestOrm is always better than directly access the TRestServerFullMemory, as I wrote above.
Note that if you use a IRestOrm instance, you have to properly set it to nil before calling TRestServerFullMemory.Free.

Offline

#5 2024-04-05 18:02:55

imperyal
Member
Registered: 2018-10-11
Posts: 51

Re: TRestStorageInMemory Add error

I added the IRestOrm thing. This is a non critical part of the client code so I will let it stay this way (and it is working).

Thank you!

Offline

Board footer

Powered by FluxBB