Examples Delphi

Title: Boolean fields in applications for FireBird and Interbase
Question: How to get real boolean support in client applications for Interbase and FireBird
Answer:
Boolean fields in applications for FireBird and Interbase
Boolean fields in applications for FireBird and Interbase.
The absence of evident support of boolean fields in Interbase always surprised
and interested the developers who used this server. And in any case these developers
had to emulate the support of boolean fields.
Here it is necessary to mention that this emulation is possible only in client
applications. It is so because the server itself is a determinate thing and
any programmer cannot change it separately. That is why the question arises:
what is the best way of emulation of boolean fields in applications?
In practice there are two usual solutions. You create either the first or second
following domains:
CREATE DOMAIN T_BOOLEAN_INT
AS SMALLINT
DEFAULT 0
NOT NULL
CHECK (VALUE IN (0,1))
Or
CREATE DOMAIN T_BOOLEAN_CHAR
AS CHAR(1)
DEFAULT 'F'
NOT NULL
CHECK (VALUE IN ('T', 'F'))
Fields created with these domains are considered by the developer to be Boolean
and with the help of visual components are shown to users. In particular if
using TDBCheckBox you can show such properties as ValueChecked := 'T', ValueUnchecked
:= 'F'. In this case the component will behave as if it deals with a real boolean
field.
It is more difficult to allow users to edit some boolean fields in components
of the TDBGrid type simply by ticking off instead of evident setting the 'F'
property. Such way of programming will require non-trivial steps.
Of course you probably have thought a lot about the easiest way of solving
this problem. If while executing the query Delphi automatically creates TBooleanField
instances for necessary fields, the programmer's life will become more comfortable!
This possibility has already been realized in FIBPlus. For this in the database
it is necessary to describe some domain like the above-mentioned 'T_BOOLEAN_INT'.
That is the domain should necessarily be either of the smallint or integer type,
which allows two possible values "0" and "not 0" and contains the word "boolean"
in its name. Then you enable the corresponding option in TpFIBDataSet, for example:
pFIBDataSet1.PrepareOptions := pFIBDataSet1.PrepareOptions + [psUseBooleanField]
(this can be done both in design-time and in run-time)
After this all the fields created with our special domain will be considered
to be boolean and for them there will be automatically created TFIBBooleanField
instances (it is a direct descendant of the TBooleanField class). That means
that any visual component will consider the fields to be real boolean fields
and this is very important for normal work!
Serg Vostrikov,
Devrace Company