Previous Topic

Next Topic

Guidelines

Some general coding guidelines for an application developer using ESQL are listed below.

  • The character strings used in an ESQL program must be null terminated.
  • The WHENEVER statement can be used in an ESQL program to check for error conditions. This statement provides more flexibility and reduces the code size of the application.
  • There will be problems if any of the c-treeSQL reserved words happen to have been used as symbol names in #define directives in any included header file, and if the +P option is used while executing esql. When the +P option is used in esqlc, the source files are passed through the C preprocessor before the esql statements are translated. One frequent problem is the reserved word NULL that is used as a symbol name in the standard header file stdio.h. To work around the name conflict, use one of the following suggestions:
    • Avoid using the +P option.
    • Avoid the inclusion of the header file that contains the definition of the reserved word.
    • Reverse the case of the reserved word when used in the ESQL statement, as shown below. This would not affect esqlc since reserved words in ESQL statements are not case sensitive.
EXEC SQL
SELECT ename
FROM  employee
WHERE commission is null ;
  • It is recommended that the owner name of the table be used while referencing tables in an ESQL program, instead of using a non-qualified table name. For example, to access the table “customer” the SELECT statement could be:
EXEC SQL
     SELECT CUST_NO, name
     FROM john.customer ;

Here, john is the owner name of the table customer.

  • While declaring host variable arrays, expressions are not allowed within the array subscript. For example, the following declaration in the declare section is not allowed:
#define  MAXNAMELEN  18

EXEC SQL BEGIN DECLARE SECTION ;
  char  name [MAXNAMELEN + 1] ;   /* not allowed */
EXEC SQL END DECLARE SECTION ;

Here, the symbol MAXNAMELEN is defined. To handle such cases, define a new symbol for the required value and use that symbol, as shown below:

...
#define  MAXNAMELEN_P1  19

EXEC SQL BEGIN DECLARE SECTION ;
     char  name [MAXNAMELEN_P1] ;   /* allowed */
EXEC SQL END DECLARE SECTION ;
  • #define symbols can be used in c-treeSQL statements wherever constants can be used. The following example shows the usage of the #define symbol, MAX_SALARY:
#define MAX_SALARY 10000

SELECT ename, salary
FROM employee
WHERE salary = MAX_SALARY ;
  • It is a good practice to specify all the columns in the SELECT list of a SELECT statement instead of specifying a ‘*’. This improves the readability of the SELECT statement. In addition, if the database table schema is changed later on, the ESQL statement need not be changed.