#1 2014-07-04 05:38:58

Alek
Member
From: Russia
Registered: 2014-07-04
Posts: 43

Problems with TDocVariant

Good day. I use the following code.

  TSQLRecordOtchet = class(TSQLRecord)
  private
    fOtchety_nastrk     : Variant;
  public
    constructor Create; override;
  published 
    property Otchety_nastrk     : Variant read fOtchety_nastrk  write fOtchety_nastrk;

constructor TSQLRecordOtchet.Create;
begin
  inherited;
  fOtchety_nastrk:=TDocVariant.New;
end;

and when I created a record with
rec:=TSQLRecordOtchet.Create, then I get rec.Otchety_nastrk = 'null', but

when I created a record already with
rec:=TSQLRecordOtchet.Create(DataBase,ID), then I get rec.Otchety_nastrk = null, i.e. without quotes.

And if I want assign rec.Otchety_nastrk.Href:='http://...' I get exception "Invalid variant type 1 invoke"

In then first case, after create rec.Otchety_nastrk.Href:='http://...' it's ok. {Href:'http://...'}

Offline

#2 2014-07-04 05:55:00

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

Re: Problems with TDocVariant

TSQLRecordOtchet.Create(DataBase,ID) does read the Otchety_nastrk value from the DB, so it is set back to null, which is the value from the DB.

There is no way to force the variant field to be a TDocVariant.
As stated by the documentation, a variant field can be any kind of variant: null, a number, a string, or a TDocVariant.
There is no direct way to force the field to be a TDocVariant at all time.

In all cases, you should better not use TDocVariant.New in the constructor, but assume it is null by default, then explicitly check for VarIsNull() and set TDocVariant.New/_JSON to initialize it.

It may be possible to add a custom type, so that the published field may be a TDocVariant object or array...
But it would request a lot of code modification, and a new class inheriting from TSQLPropInfoRTTIVariant.
You may create a feature request ticket in http://synopse.info/fossil/tktnew

Offline

#3 2014-07-04 06:25:07

Alek
Member
From: Russia
Registered: 2014-07-04
Posts: 43

Re: Problems with TDocVariant

thank you for quick answer

Offline

Board footer

Powered by FluxBB