Nonstop Database, HiRDB Version 9 UAP Development Guide

[Contents][Index][Back][Next]

7.2.1 Coding rules

When a UAP is created, the labelling rules, SQL coding rules, and SQL syntax rules must be followed.

Organization of this subsection
(1) Labeling rules
(2) SQL coding rules

(1) Labeling rules

Labels must be assigned according to the C language rules. These types of labels cannot be used:

For naming embedded variables, indicator variables, and branching destination labels, the labeling and the C language rules must be followed.

(2) SQL coding rules

  1. Each SQL statement must be preceded by the SQL prefix (EXEC SQL) and followed by the SQL terminator (;).
    Valid example:
    EXEC SQL SQL-statement;
  2. The C language macro function cannot be used for an embedded SQL statement or any part of it.
    Invalid example:
    #define X USER.MEMBER
    EXEC SQL
      SELECT NAME INTO  MANNAME FROM X;
  3. The underline indicates the invalid portion.
    SQL reserved words can be in uppercase letters, lowercase letters, or a mixture of both.
    Example 1:
    EXEC SQL
      SELECT MEM INTO  :NAME FROM TABLE;
    Example 2:
    exec sql
    select MEM into  :NAME from TABLE;
    Example 3:
    exec SQL
      SELECT MEM Into  :NAME From TABLE;
  4. One line each must be used for the SQL prefix, the embedded SQL start declaration, and the embedded SQL termination declaration. A space is used to separate the words making up each item.
    A line consists of a character string that begins with the character following the linefeed character and ends with the next linefeed character. The maximum length of a row in a UAP source program that can be preprocessed is 32,000 characters.
    Valid specification:
    EXEC SQL
      BEGIN DECLARE SECTION;
    ...
    EXEC SQL
      END DECLARE SECTION;
     
    EXEC SQL
      SELECT...  ;
    Invalid specification:
    EXEC SQL
      BEGIN
      DECLARE SECTION;
        :
     
    EXEC SQL
      END
      DECLARE SECTION;
     
    EXEC \
      SQL
      SELECT ...    ;
  5. The embedded SQL declaration section must precede the SQL statements that use the embedded variables and indicator variables.
    Example:
    EXEC SQL
      BEGIN DECLARE SECTION;
    short SALES;
    EXEC SQL
      END DECLARE SECTION;
    ...
    EXEC SQL
      SELECT PRICE INTO :SALES
        FROM TABLE;
  6. The following rules apply to specifying embedded variables and indicator variables.
    • A declaration statement can span multiple lines. Multiple definition statements can also be described in a single declaration statement.
    Specification example:
    short SALES,
          QUANTITY;
    short SALES;     short QUANTITY;
    • The following table shows the items that can be described in an embedded SQL declaration section.

      Table 7-1 Items that can be described in an embedded SQL declaration section

      Described Item Description within embedded declaration
      Note D
      C language instruction statement --
      C language control statement --
      SQL statement --
      Embedded variable declaration D
      Indicator variable declaration D

      D: Can be described.

      --: Cannot be described.

    • The same embedded variable or indicator variable cannot be repeated within the same source file.
    • Multiple embedded variables or indicator variables can be declared in a single declaration statement.
    Specification example:
    short SALES, QUANTITY;                   1
     
    short XSALES, XQUANTITY;                 2
    1: Declaration of embedded variables
    2: Declaration of indicator variables
  7. Embedded variables declared within a function become local variables; embedded variables declared outside a function become global variables.
  8. Although embedded SQL statements can also be described in locations within a function block where C language instruction statements can be described, they cannot be described on the same lines as another SQL statement or statements written in C language.
    Note
    A label can be placed before an SQL prefix.
    The following table shows the locations where SQL statements can be specified.

    Table 7-2 Locations where SQL statements can be described

    Description location within a line SQL statement description
    C language and instruction statement Front --
    Middle --
    Back --
    C language control statement Front --
    Middle --
    Back --
    Label Front --
    Back D
    Comments Front D
    Middle --
    Back D
    SQL statement# Front --
    Middle --
    Back --

    D: Can be described.

    --: Cannot be described.

    #: Must begin with an SQL prefix and end with an SQL terminator.

  9. To include a Microsoft Foundation Class (MFC) header file (AFXxxxxx.H) in a UAP source program that uses HiRDB with the Visual C++ compiler, include the HiRDB header file after the MFC header file by using the following SQL statement:
    EXEC SQL INCLUDE HIRDB_HEADERS;
    • INCLUDE HIRDB_HEADERS includes the HiRDB header file that was automatically included at the beginning of the post source file at the specified location.
    • INCLUDE HIRDB_HEADERS can be used only with C and C++. It cannot be used with other languages.
    • INCLUDE HIRDB_HEADERS can only be used once in a UAP.
    • If INCLUDE HIRDB_HEADERS is not used, the HiRDB header file is included at the beginning of the post source file.
    The MFC header files provided by Visual C++ are sequentially related according to the order in which they are included. If the WINDOWS.H header file (header file used by HiRDB) is included first, an error may occur. In this case, use INCLUDE HIRDB_HEADERS.
    HiRDB uses the following Visual C++ header files:
    • WINDOWS.H
    • STRING.H
    An example of using INCLUDE HIRDB_HEADERS is shown as follows.
    #include <afx.h>
    EXEC SQL INCLUDE HIRDB_HEADERS ;
  10. Comments (/*...*/) specified between the SQL prefix and the SQL terminator are deleted. However, SQL optimization specifications (/*>>...<<*/) are not deleted but instead treated as SQL statements. For details about comments and SQL optimization specifications in SQL statements, see the HiRDB Version 9 SQL Reference manual.
  11. The backslash (\) symbol cannot be used to indicate row continuation.
  12. When you use the -E option, the preprocessor declaration statement for the C compiler becomes effective. Consequently, you can use #ifdef to specify SQL statement switching and use macro literals to specify literals in the embedded SQL declare section. However, the following restrictions apply:
    • Preprocessor declaration statements cannot be specified between the SQL prefix and SQL terminator.
    • Macros that change the column positions of the SQL prefix and SQL terminator cannot be specified.
    • Macro definitions of the SQL prefix and SQL terminator cannot be specified.
  13. When you use the -E option, you can use an embedded variable, as long as you declare the variable according to the C syntax rules, and the embedded variable corresponds to an SQL data type. This is allowed even if you do not declare the embedded variable in the embedded SQL declare section. If there is another variable with the same name, the effective scope of each variable is determined according to the C syntax rules. You can also use variables declared in an included header. However, the following restrictions apply:
    • Only the first 63 characters of the variable name are distinguished. The subsequent characters are not distinguished. Note that a universal character name (\uxxxx or \Uxxxxxxxx) is counted as one character.
    • Nested structures cannot be used.
    • Declare statements cannot contain embedded variables that use an expression in a subscript.
    • const-type embedded variables can be used only as input variables.
    • varchar cannot be used for C language identifiers such as variable names and function names, regardless of whether upper or lower case is used.
  14. When you use the -E option, you can declare a structure that has multiple embedded variables as members as an embedded variable. All members must have a format that corresponds to an SQL data type. A structure cannot contain another structure or a union. However, you can use a structure that corresponds to the variable-length character string type or the BINARY type.
  15. When you use the -E option, you can declare a pointer as an embedded variable. The declaration format conforms to the C syntax rules. When using such an embedded variable in an SQL statement, specify the variable with the same format used for normal embedded variables. Do not add an asterisk in front of the embedded variable name.
  16. When you use the -E option and specify a structure member explicitly as an embedded variable, include the structure name as a modifier. The format of the structure member specification becomes :structure.member-name. If you are using a pointer to the structure, include the pointer as a modifier. The format of the structure member specification becomes :pointer->member-name.
  17. When you specify the -E option in Windows, the following restrictions apply:
    • Defining the same typedef name twice with typedef does not trigger a syntax error. However, there is also no check for determining whether the defined contents are the same.
    • Members of anonymous structures cannot be used as embedded variables.
    • Declarators specified without a storage class or data type cannot be used as embedded variables.
  18. When you specify the -E option, you cannot use the COPY statement.
  19. A variable-length array cannot be used as an embedded variable.
  20. If you specify the -E option, neither digraphs, such as <: and <%, nor trigraphs, such as ??( and ??=, can be specified. Even if you do not specify the -E option, you cannot specify trigraphs in SQL statements or the embedded SQL declare section. If you use trigraphs, they are treated as normal characters.
  21. When you specify the -E option, you can use the reserved words listed below that are not stipulated in the C standard (C99):
    Usage Processing Environment#1 Reserved words
    Type qualifier or type specifier#2 Treated as a type qualifier or type specifier during syntax analysis Windows __int8,__int16,__int32,
    __int64,_int8,_int16,
    _int32,_int64
    UNIX __volatile__,
    __builtin_va_list,
    __complex__,
    __signed__
    Other Ignored because the word is treated during syntax analysis as meaningless Windows __based,__cdecl,
    __export,__far,
    __fastcall,
    __forceinline,
    __inline,__near,
    __pascal,__ptr32,
    __ptr64,__stdcall,
    __unaligned,__w64,
    _based,_cdecl,
    _export,_far,_fastcall,
    _forceinline,_inline,
    _near,_pascal,
    _stdcall__declspec,
    __pragma,
    _declspec__try,
    __except,
    __finally__asm,_asm
    UNIX __const,__const__,
    __extension__,
    __inline,__inline__,
    __restrict,
    __attribute__,
    __asm__
    #1
    The Windows environment's reserved words are used in Visual C++. These reserved words are defined in Visual C++ 6.0 or later and begin with two consecutive underscores (__). You can also use the reserved words that begin with a single underscore (_) in source codes created with an earlier version of Visual C++.
    The UNIX environment's reserved words are used in GCC.
    #2
    Reserved words treated as type qualifiers or type specifiers cannot be used in the declaration of an embedded variable.
  22. When you specify the -E option, a bit field can be declared as any integer type. Note that a bit field cannot be used in the declaration of an embedded variable.