Bitfields allow for naming and accessing sequences of bits in a column.

Defining bitfields with CREATE TABLE

When creating a table with a bitfield column using the CREATE TABLE statement, the type of the column needs to be already defined with CREATE TYPE, for example:

CREATE TYPE bf AS (f1 bit1, f2 bit2); 
CREATE TABLE foo AS 
  ( x INTEGER, y DOUBLE, v STRING, status bf) 
ON 'new_api_example.odb';

In the above example we have declared a bitfield type called bf, consisting of two members: f1 and f2. f1 occupies 1 bit ("bit1"), f2 2 bits ("bit2").

Later the type bf was used to declare column status in the CREATE TABLE statement.

Referring to bitfield members

Syntax for accessing a member of a bitfield is:

<column-name>.<bitfield-member-name>

In case it is necessary to specify table name when referring to a column, correct way of referring to a member is:

<column-name>.<bitfield-member-name>@<table-name>

for example:

report_status.active@hdr

 

Expanding list of members with star (*) operator

The list of bitfield members can be expanded with the star (*) operator, for example:

report_status.*@hdr

will be expanded to

report_status.active@hdr, report_status.passive@hdr, report_status.rejected@hdr, report_status.blacklisted@hdr, report_status.use_emiskf_only@hdr

 

Finding details of bitfield definition

The odb header tool can be used to find out details of bitfield definition: its members and number of bits they occupy:

$ odb header conv.odb | grep datum_status | head -n 1
40. name: datum_status@body, type: BITFIELD [active:1;passive:1;rejected:1;blacklisted:1;use_emiskf_only:1] , codec: int8, range=<1.000000,5.000000>

Option -dll produces output in the Data Definition Language (part of SQL) format:

$ odb header -ddl conv.odb | grep datum_status | head -n 1
CREATE TYPE datum_status@body_at_foo_t AS (active bit1, passive bit1, rejected bit1, blacklisted bit1, use_emiskf_only bit1);