Scalable Database Server, HiRDB Version 8 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 blank 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;
    • Table 7-1 shows the items that can be described within an embedded SQL declare section.

      Table 7-1 Items that can be described within an embedded SQL declare section

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

      D: Can be described.

      [Figure]: 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.
    Table 7-2 shows the locations where SQL statements can be described.

    Table 7-2 Locations where SQL statements can be described

    Description location within a line SQL statement description
    C language and instruction statement Front [Figure]
    Middle [Figure]
    Back [Figure]
    C language control statement Front [Figure]
    Middle [Figure]
    Back [Figure]
    Label Front [Figure]
    Back D
    Comments Front D
    Middle [Figure]
    Back D
    SQL statement* Front [Figure]
    Middle [Figure]
    Back [Figure]

    D: Can be described.

    [Figure]: 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 8 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 31 characters of the variable name are distinguished. The subsequent characters are not distinguished.
    • 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:
    • The following keywords, each of which starts with two underscore (_) characters, undergo syntax analysis but are treated as meaningless phrases. These keywords are those defined by Visual C++ 6.0. For sources that are created with a older version of Visual C++, this restriction also applies to keywords that begin with one underscore character.
      __asm, __except, __forceinline, __int32, __stdcall, __based, __far, __inline, __int64, __try, __cdecl, __fastcall, __int8, __leave, __uuidof, __declspec, __finally, __int16, and __near
    • 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.
    • Variable-length arrays 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. When you specify the -E option, you cannot specify trigraphs, such as ?? or ??=. 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.