#1 2016-05-18 12:01:25

igors233
Member
Registered: 2012-09-10
Posts: 241

TSQLTableJSON.UpdateFrom

UpdateFrom method will not update RowCount if passed json content is empty.
Here is an example:

  TempTable := TSQLTableJSON.Create('', '[{Field1:"Test"}, {Field1:"Test2"}]');
  TempTable.UpdateFrom('[]', Refreshed, nil);

Offline

#2 2016-05-19 05:55:24

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

Re: TSQLTableJSON.UpdateFrom

It returns an error in this case, so RowCount is not set.

Offline

#3 2016-05-19 11:24:55

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: TSQLTableJSON.UpdateFrom

> It returns an error in this case, so RowCount is not set.

I understand that, but isn't that an error? I think it should replace previous content, but it exits and previous content is kept.

Consider this:

1) TempTable := TSQLTableJSON.Create('', '[]');
2) TempTable.UpdateFrom('[{Field1:"Test"}, {Field1:"Test2"}]'', Refreshed, nil);
3) TempTable.UpdateFrom('[]', Refreshed, nil);

1) TempTable is empty
2) TempTable has two rows
3) TempTable has two rows (same content from 2)

Last edited by igors233 (2016-05-19 11:25:21)

Offline

#4 2016-05-19 11:40:23

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

Re: TSQLTableJSON.UpdateFrom

The content is expected to come from a mORMot server, which would never return '[]' but the field names, in non-expanded JSON format.

It is easy in your code to check for '[]' as input, if you need another behavior.
If you need to parse some JSON input, use TDocVariantData or the other JSON serialization of class or record/arrays of the framework.

Offline

#5 2016-05-21 01:53:42

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: TSQLTableJSON.UpdateFrom

> The content is expected to come from a mORMot server, which would never return '[]' but the field names, in non-expanded JSON format.

That does sounds like the way I'm using it, I have TSQLRecord that I:
1) create with CreateAndFillPrepare (that returns some records)
2) that FillTable is transferred to created TSQLTableJSON and it's content is filled with SQlRecord.FillTable.GetJSONValues(True).
3) TSQLTableJSON is associated with TSQLTableToGrid
4) User changes some (where) conditions which in effect calls SQlRecord.FillPrepare
5) Results are feed back to same TSQLTableJSON and retrieved in same way (SQlRecord.FillTable.GetJSONValues(True))

if FillPrepare returns no results, then GetJsonValues(True), returns [], if I use GetJsonValues(False) then {"FieldCount":0} is returned but it has same effect on UpdateFrom, RowCount is not changed.

> It is easy in your code to check for '[]' as input, if you need another behavior.

I know, but I think this is inconsistent/confusing behaviour, if you pass to UpdateFrom an emptycontent, no matter how you got it (ORM, interface service, local code) it should reflect that there are no records.

I do know I can create every time TSQLTableJSON and TSQLTableToGrid but
1) I don't like to destroy/allocate objects if it's not necessary
2) it looses column, sort and fields setting (that user has changed) so I need to add additional code to restore it every time

Offline

#6 2016-05-21 13:49:44

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

Re: TSQLTableJSON.UpdateFrom

In such cases, you may just download the JSON value, and handle '[]' as you need, before injecting it to the TSQLTableJSON.

Offline

Board footer

Powered by FluxBB