Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi XE2 Dataset field type TStringField does not support Unicode?

I've been looking through the TDataset class and its string fields, in Delphi XE2 and noticed that AsWideString returns a type of UnicodeString. However it gets the value from the function TField.AsString: String which in turn calls TFIeld.AsAnsiString:AnsiString. Therefore any unicode characters would be lost? Also the buffer which is passed to TDataset.GetFieldData is declared as an array of AnsiChar.

Am I understanding this correctly?

like image 867
There is no spoon Avatar asked Feb 27 '12 01:02

There is no spoon


2 Answers

No, you should be examining the TWideStringField class which is for Unicode fields and the TStringField class which is for non-Unicode strings. TField is just a base class and TField.GetAsWideString is a virtual method with a fall back implementation that is overridden by descendants that are Unicode aware.

like image 77
Henrick Hellström Avatar answered Oct 06 '22 01:10

Henrick Hellström


YES, you did understand it correctly. This is the VCL and its documentation which are broken. Your confusion does perfectly make sense!

In the Delphi 2009+ implementation, you have to use AsString property for AnsiString and AsWideString for string=UnicodeString.

In fact, the As*String properties are defined as such:

property AsString: string read GetAsString write SetAsString;
property AsWideString: UnicodeString read GetAsWideString write SetAsWideString;
property AsAnsiString: AnsiString read GetAsAnsiString write SetAsAnsiString;

How on earth may we be able to find out that AsString returns an AnsiString? It just does not make sense at all, when compared to the rest of the VCL/RTL.

The implementation, which uses TStringField class for AnsiString and TWideStringField for string=UnicodeString is broken.

Furthermore, the documentation is also broken:

Data.DB.TField.AsString

Represents the field's value as a string (Delphi) or an AnsiString (C++).

This does not represent a string in Delphi, but an AnsiString! The fact that the property uses a plain string=UnicodeString type is perfectly missleading.

On the database point of view, it is up to the DB driver to handle Unicode or work with a specific charset. But on the VCL point of view, in Delphi 2009+ you should only know about string type, and be confident that using AsString: String will be Unicode-ready.

like image 27
Arnaud Bouchez Avatar answered Oct 05 '22 23:10

Arnaud Bouchez