DECLARE Statement

DECLARE block statements specify the variables, parameters, typedefs and functions used in a CCL project.

Syntax

DECLARE
     [declaration;]
     ...
END;

Usage

CCL declare blocks consist of a DECLARE statement and an END statement with zero or more declarations between them.

A DECLARE block statement can be used to define variables, typedefs, parameters, and functions. The syntax for each of these declarations is:
  • Variables use the SPLASH syntax, and you can specify a default value:
    datatypeName variableName [:=any_expression] [,...]
  • Typedefs declare new names for datatypes:
    existingdatatypeName newdatatypeName
  • Parameters use the qualifier parameter, and you can specify a default value:
     parameter datatypeName parameterName [:=constant_expression]
  • Thetypeof() operator provides a convenient way to declare variables. An example of the typeof usage would be: if rec1 is an expression with type [ int32 key1; string key2; | string data; ] then the declaration typeof(rec1) rec2; is the same as the declaration [ int32 key1; string key2; | string data; ] rec2;

Declare blocks can be local or global. When declare blocks are used inside a CREATE stream or window statement they become local declare blocks. A local declare block is visible only inside the stream or window with which it is used. When a DECLARE block statement is used inside a module or project, it becomes a global declare block. Global declare blocks are visible anywhere within that project or module.

Terminate each declaration in the DECLARE block statement with a semicolon.

Example

This example demonstrates the DECLARE block in the global context, meaning it is outside of any CREATE command.
declare
    integer toggle(integer x) { if ( x%2 = 0) { return 1; } else { return 2; } }
end;

CREATE SCHEMA sc1 (k1 integer,k2 string);
CREATE SCHEMA sc2a (k1 integer,k2 string,k3 string, k4 integer);
create schema s1_104(c2 integer, c3 date, c4 float, c5 string, c6 money );

CREATE INPUT WINDOW iwin1 SCHEMA sc1 primary key(k1);
CREATE INPUT WINDOW iwin2 SCHEMA sc1 primary key(k1);

create input window w1_104 schema s1_104 primary key(c2);
create delta stream ds2_104 primary key deduced as select * from w1_104;

create output window ww_innerjoin1 schema sc2a primary key (k1,k2)
This example shows the DECLARE block local to a stream, meaning it is inside a CREATE command (not flex)
declare
    integer i1 := 1;
    string s1 := 'ok';
end
    as
    select A.k1,(A.k2 + s1) k2,B.k2 k3, toggle(A.k1) k4
    from iwin1 A join iwin2 B
    on A.k1 = B.k1
;


This example shows a DECLARE block local to a flex stream.
create flex flex104
        in ds2_104
        out output stream flexos104 schema s1_104
        begin 
            declare
                integer counter := 0;
            end;

        on ds2_104 {
            counter++;
            output ds2_104_stream[ [c2=ds2_104.c2;|] ];
        };

        on end transaction {
            if( counter = 4 ) {
                typeof( flexos104 ) rec;
                rec := flexos104_stream[ [c2=0;|] ];
                rec.c2 := rec.c2 + counter;
                output rec;
                rec := flexos104_stream[ [c2=1;|] ];
                rec.c2 := rec.c2 + counter;
                output rec;
                rec := flexos104_stream[ [c2=2;|] ];
                rec.c2 := rec.c2 + counter;
                output rec;
                rec := flexos104_stream[ [c2=3;|] ];
                rec.c2 := rec.c2 + counter;
                output rec;
            }
        };

    end;