[fpc-devel] TFieldDef.Size vs TField.Size

michael.vancanneyt at wisa.be michael.vancanneyt at wisa.be
Thu Feb 24 11:11:47 CET 2011

On Thu, 24 Feb 2011, LacaK wrote:

> See please 
> http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/packages/fcl-db/src/sqldb/odbc/odbcconn.pas?r1=16094&r2=16988
> And look at ftWideString, ftFixedWideChar cases
> What happens if we have NVARCHAR(20) column on MS SQL Server (uses UCS-2 
> 1char=2bytes):
> 1. in AddFieldDefs method is created TFieldDef object with Size=20*2 (because 
> FieldSize:=ColumnSize*sizeof(Widechar))
> 2. later when TField objects are created in TFieldDef.CreateField method, 
> TWideString object is created with Size=40 (because Size is copied from 
> TFieldDef object) ... and this is wrong, because:
> 2.1 TWideString.Size should IMO return max.number of characters ... Size is 
> used for example in data-aware controls to limit MaxLength
> 2.2 TWideStringField.GetDataSize will return 82, which is also wrong 
> (because (40+1)*2)
> 2.3 in record buffer will be allocated also 82 bytes (because of 
> TCustomBufDataset.GetFieldSize)
> So IMO correct will be in step 1 FieldSize:=ColumnSize not 
> FieldSize:=ColumnSize*sizeof(WideChar)
> (like in my original fix in bug report)

This seems correct, yes.

> Note also, that Size is used (also in Delphi) to limit the number of 
> characters in data-aware controls (for ftString, ftWideString), so it must be 
> number of characters in both cases.
>> Do you see an implementation problem or a documentation problem ?
> Implementation problem at first
> but also Documentation is confusing, because do not clearly says about 
> menaing of TWideStringField.Size (only indirect guess can be done, that it is 
> inherited from TStringField.Size, so it is number of characters)
> See, that also other users are confusing by it 
> https://forums.embarcadero.com/thread.jspa?messageID=188792

Yes, but that is an embarcadero bug :-)

>> Note that nowhere in the documentation pages you mention, widestrings are
>> mentioned (which is convenient, since that's where 1 char <> 1 byte).
> Yes, but in Delphi6 help is for TWideStringField.DataSize:
> "Inspect DataSize to determine the number of bytes required to store the 
> fields value. Use DataSize to determine the size needed for a buffer when 
> working with the GetData and SetData methods.
> For wide string fields, DataSize is the value of the Size property plus one 
> (for the trailing NULL character), multiplied by two."
> So we can estimate, that Size is number of characters, while 
> DataSize=(Size+1)*2



More information about the fpc-devel mailing list