7. Embedded SQL for Pascal : Advanced Processing : Sample Programs : Dynamic SQL Handler
 
Share this page                  
Dynamic SQL Handler
The following is an example of a dynamic SQL handler program:
program dynamic_hdlr(input,output):

        exec sql include sqlca;
        exec sql include sqlda;

-- Do not declare the data handlers nor the data handler argument
-- to the ESQL preprocessor

type
         String100 = packed array [1..100] of char;
         hdlr_rec = record
                argstr: String100;
                argint: Integer;
            endr;

var
            function Put_Handler(hdlr_arg: hdrlr_rec):
                     integer;external;
            function Get_Handler(hdlr_arg: hdlr_rec):
                    integer;external;
            hdlr_rec:     hdlr_arg;

-- Declare SQLDA and IISQLHDLR
         sqlda:        IIsqlda;
         data_handler:    IIsqlhdlr;
         base_type:    integer;
         col_num:    integer;

-- Declare null indicator to ESQL
        exec sql begin declare section;
            ind_var:    integer;
            stmt_buf:    String100;
        exec sql end declare section;

        .    .

begin

-- Set the IISQLHDLR structure with the appropriate datahandler and
-- datahandler argument.

    data_handler.sqlhdlr = iaddress(Get_Handler)
    data_handler.sqlarg = iaddress(hdlr)arg)

-- Describe the statment into the SQLDA.

    stmt_buf = 'select * from book'.
    exec sql prepare stmt from :stmt_buf;
    exec sql describe stmt into SQLDA;

    . . .

-- Determine the base_type of the SQLDATA variables.

    col_num := 1;
    while (col_num <= sqlda.sqld) do begin
        with sqlda.sqlvar[col_num] do begin
                if (sqltype > 0) then
                    base_type := sqltype;
                else
                    base_type := -sqltype;

-- Set the sqltype, sqldata and sqlind for each column.
-- The Long Varchar Column chapter_text will be set to use a datahandler.

        if (base_type = IISQ_LVCH_TYPE) the
            sqltype   = IISQ_HDLR_TYPE;
            sqldata   = iaddress(data_handler_;
            sqlind    = iaddress(indvar);
        else
        . . .
     end;

    end;

-- The Datahandler (Get_Handler) will be invoked for each non-null value
-- of column chapter_text retrieved.
-- For null values the indicator variable will be set to "-1" and
-- the datahandler will not be called.

    ...

    exec sql execute immediate :stmt_buf using :SQLDA
    exec sql begin
         process row...
    
exec sql end;
    ...

end.