#1 2014-08-26 11:39:51

uian2000
Member
Registered: 2014-05-06
Posts: 69

How dose SetFieldType work to enumerate type

Guys

I've implemented a  interface based service, and retrieve data with TDocVariant type param.
When display this data, field type missed. I've tried Table.SetFieldType, but colum value disappeared. T_T

Here is the sample code. Enviroment: Win7/X64/XE2

unit uMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    DrawGrid1: TDrawGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  SynCommons,
  mORMot,
  mORMotUI;

type
  TEnumSample = (esOne, esTwo, esThree);
  TSQLMyRecord = class(TSQLRecord)
  private
    fSample: TEnumSample;
  published
    property Sample: TEnumSample read fSample write fSample;
  end;

procedure TForm1.Button1Click(Sender: TObject);
const
  MaxCount = 10;
var
  oTable, oRow: Variant;
  iIdx, iHigh: Integer;
begin
  oTable := _Arr([]);
  oRow := _Obj([]);
  for iIdx := 0 to MaxCount do
  begin
    iHigh := Ord(esThree) + 1;
    oRow.Sample := TEnumSample(iIdx mod iHigh);
    oTable._ := oRow;
  end;

  with TSQLTableToGrid.Create(DrawGrid1,
    TSQLTableJSON.Create('', VariantSaveJSON(oTable)), nil) do
  begin
    Table.SetFieldType('Sample', sftEnumerate, TypeInfo(TEnumSample));
  end;
end;

end.

Last edited by uian2000 (2014-08-26 12:11:24)

Offline

#2 2014-08-26 15:37:59

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

Re: How dose SetFieldType work to enumerate type

In fact, current implementation expected the following expression:

Table.SetFieldType('Sample', sftEnumerate, PTypeInfo(TypeInfo(TEnumSample))^.EnumBaseType);

which was indeed a bit confusing, I understand!
sad

I've modified SetFieldType() to accept the raw TypeInfo() pointer, as you did expect.
See http://synopse.info/fossil/info/d8ba1796c8
It will work for both enumerates and sets.
smile

Thanks for the feedback.
With a little sample project to reproduce the issue, it is just perfect!

Online

#3 2014-08-26 16:23:54

uian2000
Member
Registered: 2014-05-06
Posts: 69

Re: How dose SetFieldType work to enumerate type

Thanks ab!

It's very kind of you to improve expression so quickly!
Thanks for your work. It's awesome! smile

Last edited by uian2000 (2014-08-26 16:43:16)

Offline

Board footer

Powered by FluxBB