Using Descriptor Areas

PostgreSQL 9.0.4 Documentation
Prev | Fast Backward | Chapter 33. ECPG - Embedded SQL in C | Fast Forward | Next

# 33.9. Using Descriptor Areas

An SQL descriptor area is a more sophisticated method for processing the result of a `SELECT`, `FETCH` or a `DESCRIBE` statement. An SQL descriptor area groups the data of one row of data together with metadata items into one data structure. The metadata is particularly useful when executing dynamic SQL statements, where the nature of the result columns might not be known ahead of time. PostgreSQL provides two ways to use Descriptor Areas: the named SQL Descriptor Areas and the C-structure SQLDAs.

## 33.9.1. Named SQL Descriptor Areas

A named SQL descriptor area consists of a header, which contains information concerning the entire descriptor, and one or more item descriptor areas, which basically each describe one column in the result row.

Before you can use an SQL descriptor area, you need to allocate one:

    EXEC SQL ALLOCATE DESCRIPTOR identifier;

The identifier serves as the "variable name" of the descriptor area. When you don't need the descriptor anymore, you should deallocate it:

    EXEC SQL DEALLOCATE DESCRIPTOR identifier;

To use a descriptor area, specify it as the storage target in an `INTO` clause, instead of listing host variables:

    EXEC SQL FETCH NEXT FROM mycursor INTO SQL DESCRIPTOR mydesc;

If the result set is empty, the Descriptor Area will still contain the metadata from the query, i.e. the field names.

For not yet executed prepared queries, the `DESCRIBE` statement can be used to get the metadata of the result set:

    EXEC SQL BEGIN DECLARE SECTION;
    char *sql_stmt = "SELECT * FROM table1";
    EXEC SQL END DECLARE SECTION;

    EXEC SQL PREPARE stmt1 FROM :sql_stmt;
    EXEC SQL DESCRIBE stmt1 INTO SQL DESCRIPTOR mydesc;

Before PostgreSQL 9.0, the `SQL` keyword was optional, so using `DESCRIPTOR` and `SQL DESCRIPTOR` produced named SQL Descriptor Areas. Now it is mandatory, omitting the `SQL` keyword produces SQLDA Descriptor Areas, see Section 33.9.2.

In `DESCRIBE` and `FETCH` statements, the `INTO` and `USING` keywords can be used to similarly: they produce the result set and the metadata in a Descriptor Area.

Now how do you get the data out of the descriptor area? You can think of the descriptor area as a structure with named fields. To retrieve the value of a field from the header and store it into a host variable, use the following command:

    EXEC SQL GET DESCRIPTOR name :hostvar = field;

Currently, there is only one header field defined: `COUNT`, which tells how many item descriptor areas exist (that is, how many columns are contained in the result). The host variable needs to be of an integer type. To get a field from the item descriptor area, use the following command:

    EXEC SQL GET DESCRIPTOR name VALUE num :hostvar = field;

`num` can be a literal integer or a host variable containing an integer. Possible fields are:

`CARDINALITY` (integer)

  number of rows in the result set

`DATA`

  actual data item (therefore, the data type of this field depends on the query)

`DATETIME_INTERVAL_CODE` (integer)

  ?

`DATETIME_INTERVAL_PRECISION` (integer)

  not implemented

`INDICATOR` (integer)

  the indicator (indicating a null value or a value truncation)

`KEY_MEMBER` (integer)

  not implemented

`LENGTH` (integer)

  length of the datum in characters

`NAME` (string)

  name of the column

`NULLABLE` (integer)

  not implemented

`OCTET_LENGTH` (integer)

  length of the character representation of the datum in bytes

`PRECISION` (integer)

  precision (for type `numeric`)

`RETURNED_LENGTH` (integer)

  length of the datum in characters

`RETURNED_OCTET_LENGTH` (integer)

  length of the character representation of the datum in bytes

`SCALE` (integer)

  scale (for type `numeric`)

`TYPE` (integer)

  numeric code of the data type of the column

In `EXECUTE`, `DECLARE` and `OPEN` statements, the effect of the `INTO` and `USING` keywords are different. A Descriptor Area can also be manually built to provide the input parameters for a query or a cursor and `USING SQL DESCRIPTOR `name`` is the way to pass the input parameters into a parametrized query. The statement to build a named SQL Descriptor Area is below:

    EXEC SQL SET DESCRIPTOR name VALUE num field = :hostvar;

PostgreSQL supports retrieving more that one record in one `FETCH` statement and storing the data in host variables in this case assumes that the variable is an array. E.g.:

    EXEC SQL BEGIN DECLARE SECTION;
    int id[5];
    EXEC SQL END DECLARE SECTION;

    EXEC SQL FETCH 5 FROM mycursor INTO SQL DESCRIPTOR mydesc;

    EXEC SQL GET DESCRIPTOR mydesc VALUE 1 :id = DATA;

## 33.9.2. SQLDA Descriptor Areas

An SQLDA Descriptor Area is a C language structure which can be also used to get the result set and the metadata of a query. One structure stores one record from the result set.

    EXEC SQL include sqlda.h;
    sqlda_t         *mysqlda;

    EXEC SQL FETCH 3 FROM mycursor INTO DESCRIPTOR mysqlda;

Note that the `SQL` keyword is omitted. The paragraphs about the use cases of the `INTO` and `USING` keywords in Section 33.9.1 also apply here with an addition. In a `DESCRIBE` statement the `DESCRIPTOR` keyword can be completely omitted if the `INTO` keyword is used:

    EXEC SQL DESCRIBE prepared_statement INTO mysqlda;

The structure of SQLDA is:

    #define NAMEDATALEN 64

    struct sqlname
    {
            short           length;
            char            data[NAMEDATALEN];
    };

    struct sqlvar_struct
    {
            short           sqltype;
            short           sqllen;
            char       *sqldata;
            short      *sqlind;
            struct sqlname sqlname;
    };

    struct sqlda_struct
    {
            char            sqldaid[8];
            long            sqldabc;
            short           sqln;
            short           sqld;
            struct sqlda_struct *desc_next;
            struct sqlvar_struct    sqlvar[1];
    };

    typedef struct sqlvar_struct    sqlvar_t;
    typedef struct sqlda_struct     sqlda_t;

The allocated data for an SQLDA structure is variable as it depends on the number of fields in a result set and also depends on the length of the string data values in a record. The individual fields of the `SQLDA` structure are:

`sqldaid`

  It contains the "`SQLDA` " literal string.

`sqldabc`

  It contains the size of the allocated space in bytes.

`sqln`

  It contains the number of input parameters for a parametrized query case it's passed into `OPEN`, `DECLARE` or `EXECUTE` statements using the `USING` keyword. In case it's used as output of `SELECT`, `EXECUTE` or `FETCH` statements, its value is the same as `sqld` statement

`sqld`

  It contains the number of fields in a result set.

`desc_next`

  If the query returns more than one records, multiple linked SQLDA structures are returned, the first record is stored in the SQLDA returned in the

`sqlvar`

  This is the array of the fields in the result set. The fields are:

  `sqltype`

    It contains the type identifier of the field. For values, see `enum ECPGttype` in `ecpgtype.h`.

  `sqllen`

    It contains the binary length of the field. E.g. 4 bytes for `ECPGt_int`.

  `sqldata`

    `(char *)sqldata` points to the data.

  `sqlind`

    `(char *)sqlind` points to the NULL indicator for data. 0 means NOT NULL, -1 means NULL.

  `sqlname`

    `struct sqlname sqlname` contains the name of the field in a structure:

        struct sqlname
        {
                short           length;
                char            data[NAMEDATALEN];
        };

    `length`

      `sqlname.length` contains the length of the field name.

    `data`

      `sqlname.data` contains the actual field name.

Prev | Home | Next
pgtypes library | Up | Informix compatibility mode