HPlogo ALLBASE/SQL COBOL Application Programming Guide: HP 9000 Computer Systems > Chapter 1 Getting Started with ALLBASE/SQL Programming in COBOL

ALLBASE/SQL COBOL Programs

» 

Technical documentation

Complete book in PDF
» Feedback

 » Table of Contents

 » Index

To write a COBOL application that uses an ALLBASE/SQL database, you embed SQL commands in the COBOL source wherever you want the program to:

  • Start or terminate a DBEnvironment session, either in single-user mode or multiuser mode.

  • Start or terminate a transaction.

  • Retrieve rows from or change data in tables in a database.

  • Check the runtime execution status of an ALLBASE/SQL command.

  • Create or drop objects, such as indexes or views.

You also embed special SQL commands known as preprocessor directives. The COBOL preprocessor uses these directives to:

  • Identify COBOL variables referenced in SQL commands, known as host variables.

  • set up a special variable known as the SQL Communications Area (SQLCA) in the main program, for communicating the status of executed SQL commands to your program.

  • Generate error-handling code for SQL commands.

  • Identify cursor declarations.

Program Structure

The following skeleton program illustrates the relationship between COBOL statements and embedded SQL commands in an application program. SQL commands may appear in a program at locations highlighted.



   IDENTIFICATION DIVISION.

   PROGRAM-ID.    ProgramName.

   COBOL Statements

   .

   .

   .

   DATA DIVISION.

   FILE SECTION.

   Host Variable Declarations

   COBOL Statements

   .

   .

   .

   WORKING-STORAGE SECTION.

   SQLCA Declaration

   Host Variable Declarations

   COBOL Statements

   .

   .

   .

   LINKAGE SECTION.

   SQLCA Declaration

   Host Variable Declarations

   COBOL Statements

   .

   .

   .

   PROCEDURE DIVISION.

   .

   .

   .

   COBOL Paragraphs, some containing SQL Commands

   .

   .

   .

   STOP RUN.

To delimit SQL commands for the preprocessor, you put the prefix EXEC SQL and the suffix END-EXEC around each SQL command:



   EXEC SQL   BEGIN WORK   END-EXEC.

Most SQL commands appear within the Procedure Division where you establish DBEnvironment access and manipulate data in a database.

DBEnvironment Access

You must always specify a DBEnvironment at preprocessing time. The preprocessor needs to access the DBEnvironment you specify in order to store a module containing permanent sections used by your application program at run time. In this example, the DBEnvironment is PartsDBE which is in the sampledb directory.



   $ psqlcbl ../sampledb/PartsDBE -i mysource.sql -d

Your application program needs to access the DBEnvironment to perform its work. The CONNECT command starts a DBEnvironment session for a specific environment. The RELEASE statement terminates that session.



   PROCEDURE DIVISION.

   .

   .

   .

   EXEC SQL CONNECT TO '../sampledb/PartsDBE' END-EXEC.

   .

   .

   .

   EXEC SQL RELEASE END-EXEC.

   STOP RUN.

At run time, the program starts a DBE session in the DBEnvironment PartsDBE, where a module for the program has been stored.

A program can accept a DBEnvironment name from the program user and dynamically preprocess the SQL command that starts a DBEnvironment session. Refer to the chapter, "Using Dynamic Operations," for more information on dynamically connecting to a database and refer to the chapter, "Host Variables," for more information on using a host variable to connect to a database.

No matter how you access a DBEnvironment (dynamic or stored sections), you must always specify a DBEnvironment name when you preprocess.

In some cases an ALLBASE/SQL program is used with one or more DBEnvironments in addition to the DBEnvironment accessed at preprocessing time. In these cases, you use ISQL to install the installable module created by the preprocessor into each additional DBEnvironment accessed by your program. You can also preprocess the same application repeatedly with different DBEnvironments. See the chapter, "Using the ALLBASE/SQL COBOL Preprocessor," for information on the installable module.

An alternative method of accessing more than one DBEnvironment from the same program would be to separate the program into separate compilable files. Each source file would access a DBEnvironment. In each file you start and terminate a DBE session for the DBEnvironment accessed. You then preprocess and compile each file separately. When you invoke the preprocessor, you identify the DBEnvironment accessed by the source file being preprocessed. After a file is preprocessed, it must be compiled so that no linking is performed before the next source file is preprocessed. When all source files have been preprocessed and compiled, you link them to create the executable program.

Note that a program which accesses more than one DBEnvironment must do so in sequence. Such program design may adversely affect performance and requires special consideration.

To preprocess or to use an already preprocessed ALLBASE/SQL application program, you must satisfy the authorization requirements for each DBEnvironment accessed.

During preprocessing, the preprocessor needs to access the same DBEnvironment your source code will access at run time. Therefore, when you invoke the preprocessor, the DBEnvironment your program accesses must exist and you must identify that DBEnvironment.



   $ psqlcbl mydbe -i mysource.sql -p mysource.cbl

In addition, you define a DBEnvironment session for the same DBEnvironment in the source code:



   PROCEDURE DIVISION.

   .

   .

   .

   EXEC SQL CONNECT TO 'mydbe' END-EXEC.

   .

   .

   .

   EXEC SQL RELEASE END-EXEC.

   STOP RUN.

At run time, the program starts a DBE session in mydbe, where a module for the program is stored.

In some cases an ALLBASE/SQL program is used with a DBEnvironment named differently from the DBEnvironment accessed at preprocessing time. In these cases, you can accept a DBEnvironment name from the program user and dynamically preprocess the SQL command that starts a DBE session. Dynamic preprocessing is covered in the chapter, "Using Dynamic Operations."

Authorization

ALLBASE/SQL authorization governs who can preprocess, execute, and maintain a program that accesses an ALLBASE/SQL DBEnvironment.

To preprocess a program for the first time, you need CONNECT or DBA authority in the DBEnvironment your program accesses. When you preprocess a program, ALLBASE/SQL stores a module for that program in the DBEnvironment's system catalog and identifies your userid as the owner of that module. Subsequently, if you have OWNER or DBA authority, you can re-preprocess the program.

To run a program accessing an ALLBASE/SQL DBEnvironment, you need the authority to start the DBE session in the program:

  • If the program uses a CONNECT command to start a DBE session, you need CONNECT authority and RUN or module OWNER authority to run the program.

  • If the program uses a START DBE command to start the DBE session, you need DBA authority to run the program.

Any SQL command in the program is executed only if the OWNER of the module has the authorization to execute the command at run time, and the individual running the program has RUN authority for it. However, any dynamic command is executed only if the individual running the program has the authority to execute the command at run time. A dynamic command is an SQL command entered by the user at run time.

Maintaining an ALLBASE/SQL program includes such activities as modifying a program in production use and keeping runtime authorization current as program users change. For these activities, you need OWNER authority for the module or DBA authority. More on this topic appears later in this chapter under "Maintaining ALLBASE/SQL Programs."

File Referencing

When you create a DBEnvironment, a DataBase Environment Configuration (DBECon) file is created. The absolute pathname of this DBECon file is stored in the DBECon file itself. Once the DBECon file is created, the user, in all subsequent references to files, may use either an absolute pathname or a pathname relative to that of the DBECon file. By default, all pathnames of files and directories are relative to the pathname stored in the DBECon file. Any file reference which does not begin with a slash (/) assumes a pathname relative to that of the DBECon file.

For example, if a DBEnvironment was created with the following command:



   START DBE 'PartsDBE' NEW

and the user was currently in the directory /users/dbsupport/sql, the pathname /users/dbsupport/sql would be stored in the DBECon file. If the user were subsequently to create a DBEFile with the command:



   CREATE DBEFILE Orders WITH PAGES=50, NAME='OrdersDF'

the actual pathname of the file OrdersDF would be relative to the pathname stored in the DBECon file and would be /users/dbsupport/sql/OrdersDF. Relative pathnames are restricted to a maximum length of 128 bytes. If however, the user were to create a DBEFile with the command:



   CREATE DBEFILE Orders WITH PAGES=50, NAME='/users/shipping/OrdersDF'

the pathname stored in the DBECon file would be ignored while creating this file. The user would need to fully qualify this pathname each time this file is referenced. Absolute pathnames are restricted to a maximum length of 128 bytes.

In addition, if the DBEnvironment you want the preprocessor to access resides in a directory other than your current working directory, you will have to either absolutely or relatively qualify the pathname of that DBEnvironment.

For example, if the DBEnvironment you want the preprocessor to access resides in a subdirectory of yours, you would invoke the preprocessor as follows:



   $ psqlcbl ../sampledb/SomeDBE -i SourceFileName.sql -d

Use of Shared Memory

ALLBASE/SQL uses two types of shared memory, DBCore shared memory and inter-process communication (IPC) shared memory.

When you start a DBEnvironment with the START DBE command, a block of DBCORE shared memory is obtained for the current DBEnvironment session. Until the session ends, all users and programs accessing the DBEnvironment share this allocated memory, which includes:

  • Transaction block buffer

  • Log buffers

  • Data buffers

  • Runtime control block buffers

The use of DBCore shared memory is illustrated in Figure 1-2.

Figure 1-2 Use of DBCore Shared Memory in a DBEnvironment

[Use of DBCore Shared Memory in a DBEnvironment]

Since these are shared resources, your program cannot manipulate their size directly. However, a DBA can alter them with the SQLUtil ALTDBE command. Refer to "Estimating Shared Memory Requirements" in the "Physical Design" chapter of the ALLBASE/SQL Database Administration Guide.

An IPC shared memory segment is allocated for each local access application accessing the DBEnvironment. IPC shared memory is mapped to the user's process.

Choosing an Interprocess Communication Option

ALLBASE/SQL offers two interprocess communication options. Fast ipc is the default and is available for the E.1 release. The previously existing interprocess communication method, signal ipc, remains a viable alternative depending on your application requirements.

Any applications developed with ALLBASE/SQL release E.1 will by default use fast ipc. Any existing applications developed on a prior release will by default use signal ipc unless they are relinked. Once an existing application has been relinked, fast ipc is in effect. You have the option of setting an environment variable to indicate which form of interprocess communication you wish to use.

The following sections describe each option, how to set the environment variable, and debugging considerations:

  • Using Fast IPC

  • Using Signal IPC

  • Setting the HPSQLfast_ipc Environment Variable

  • Setting Signal Handling for Debugging

Using Fast IPC

Fast ipc offers the following features:

  • Performance is improved for applications with many, simple ALLBASE/SQL statements because overhead is reduced in communicating a request between an application and ALLBASE/SQL. It is the complexity of the statement that determines whether or not improved performance results. The size of a transaction or the size of a query result do not affect performance in regard to fast ipc.

  • Only signal 18 (SIGCLD) is used by ALLBASE/SQL when fast ipc is enabled. Therefore, if your application uses signals, you should avoid using signal 18.

NOTE: If you want to use fast ipc and trap signals, note that once a signal has been trapped and processed, your code must return from the signal handling routine to the trap point before further processing continues.

Using Signal IPC

When signal ipc is in effect, the ALLBASE/SQL software uses the following system signals:

  • signal 2 (SIGINTR)

  • signal 14 (SIGALRM)

  • signal 17 (SIGUSR2)

  • signal 18 (SIGCLD)

If your application also uses system signals, the fast ipc option might offer a solution to signal contention problems. However, if you choose to use the signal ipc option, it remains available.

Setting the HPSQLfast_ipc Environment Variable

The following example illustrates how to set the HPSQLfast_ipc environment variable.

Suppose you already have applications that must use signals and which were written prior to this release. Signal ipc is the default unless you relink your applications. In this case, you reset the signal ipc option by setting the environment variable, HPSQLfast_ipc to n or N.

For the Bourne shell or Korn shell:



   HPSQLfast_ipc=n; export HPSQLfast_ipc

For the C shell:



   setenv HPSQLfast_ipc n

Perhaps your existing applications do not use signals, and you decide to take advantage of the new option. To reset to the fast ipc option you must relink your applications.

NOTE: The appropriate HPSQLfast_ipc environment variable must be in effect at run time.

Setting Signal Handling for Debugging

Be aware that when you enter a debugging session, you must set the signal handling table for the xdb debugger, for example:

z 18 rs

NOTE: 300/400 Series 300 and 400 systems use the cdb debugger with a different format, as follows:

18 z rs

Using Semaphores

Dynamically allocated and freed semaphores coordinate access to resources. Each user session allocates a set of two semaphores. The HP-UX ipcs command displays information on semaphores. If you need more semaphores, explicitly increase the semaphore parameters in the kernel configuration file to a larger value (or twice the number of expected concurrent user sessions) and regenerate the kernel.

For more information on semaphores, refer to the HP-UX System Administrator Manual.