Boolean fields

Discussion of open issues, suggestions and bugs regarding PgDAC (PostgreSQL Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
piopio1
Posts: 32
Joined: Thu 10 Jan 2013 23:13

Boolean fields

Post by piopio1 » Wed 30 Jan 2013 18:46

Hello,

I have a problem with the boolean fields.

I connected a PgDataSource to a PGtable. The table linked to the PGTable contains one boolean field. I also connected the PgDataSource with the standard DBGrid shipped with Delphi.

When I open the table I have the boolean column showing True/False correctly. I can change the value by typing True/False or T/F in the DBGrid but if I type Yes/No or y/n or 0/1 or on/off I have an error message "Yes is not a valid boolean value for field xxxx".

I double checked and ran an update query from pgAdmin shipped with PosgtreSQL and all the combinations above are accepted and the value of the fields are amended accordingly.

How can I fix this in PGDac ?


Many thanks


Pio Pio

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Boolean fields

Post by AlexP » Thu 31 Jan 2013 11:34

Hello,

If, when setting a boolean field value from Grid, the standard TBooleanField.SetAsString method described in the DB.pas module is used, in which the comparison occurs as follows:

Code: Select all

    if AnsiCompareText(Value, Copy(FTextValues[False], 1, L)) = 0 then
      SetAsBoolean(False)
    else
      if AnsiCompareText(Value, Copy(FTextValues[True], 1, L)) = 0 then
        SetAsBoolean(True)
      else
        DatabaseErrorFmt(SInvalidBoolValue, [Value, DisplayName]);
then, as shown in the example, the following values are allowed for False: F, Fa, Fal, Fals and False; for True - T, Tr, Tru and True, respectively.

If you want to use any other values for input, you should create an OnSetText event handler for your Boolean field, for example:

Code: Select all

procedure TForm1.PgTable1f_boolSetText(Sender: TField; const Text: String);
begin
  if AnsiIndexText(Text,['On', 'Yes', 'Y','1']) > -1 then
    TBooleanField(Sender).AsBoolean := True
  else if AnsiIndexText(Text,['Off', 'No', 'N', '0']) > -1 then
    TBooleanField(Sender).AsBoolean := False
  else
    DatabaseErrorFmt(SInvalidBoolValue, [Text, Sender.DisplayName]);
end;

piopio1
Posts: 32
Joined: Thu 10 Jan 2013 23:13

Re: Boolean fields

Post by piopio1 » Sat 09 Feb 2013 00:04

Hello again,

I have added the code you suggest but I get an error message on the line "DatabaseErrorFmt(SInvalidBoolValue, [Text, Sender.DisplayName]);" because the identifier SInvalidBoolValue in not declared. Do I have to include any unit ?

As Yes/No or y/n or 0/1 or on/off are accepted by default by PostgreSQL http://www.postgresql.org/docs/9.1/stat ... olean.html I suggest to include them in the values accepted by default.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Boolean fields

Post by AlexP » Mon 11 Feb 2013 12:19

Hello,

The SInvalidBoolValue constant is described in the DBConsts.pas standard module. As I wrote before, validation of the values assigned to TBooleanField is performed in the DB.pas standard module, and we cannot modify this behaviour, therefore you should offer your suggestions to modify this functionality to Embarcadero developers.

Post Reply