HPlogo ALLBASE/SQL C Application Programming Guide: HP 9000 Computer Systems > Chapter 2 Using the Preprocessor

Invoking the C Preprocessor

» 

Technical documentation

Complete book in PDF
» Feedback

 » Table of Contents

 » Index

You can use the preprocessor in two modes:

  1. Full preprocessing mode: includes SQL syntax checking, creating compilable output, storing a module in a DBEnvironment, and creating a file that contains an installable copy of the stored module.

  2. Syntax checking mode: checks your SQL syntax without doing any other preprocessor tasks.

As you develop the SQL portions of your C programs, syntax checking mode is quite useful. Preprocessing is quicker in this mode than in full preprocessing mode. In addition, you can start debugging your SQL commands before the DBEnvironment itself is in place.

Command syntax for both modes is presented below.

Full Preprocessing Mode

Use the following preprocessor command to:

  • Check the embedded SQL command syntax.

  • Create compilable output files that can be processed by the C compiler

  • Store a module in the DBEnvironment named.

  • Create a file containing an installable version of the module.

psqlc DBEnvironmentName [ -i SourceFileName -p ModifiedSourceFileName -o OwnerName -m ModuleName -d [-r] ] |...|

Parameters

DBEnvironmentName

identifies the DBEnvironment in which a module is to be stored.

-i SourceFileName

identifies the name of the input file containing the source code to be preprocessed.

-p ModifiedSourceFileName

identifies the name for the modified source code file. If a name is not specified, the preprocessor generated code is written to a file with the same name as SourceFileName but with the file extension .c.

-o OwnerName

associates the stored module with a DBEUserID, a class name, or a group name. You can specify an owner name for the module if you have DBA authority in the DBEnvironment where the module is created or if you are the current owner of the module. If not specified, the owner name is your DBEUserID. Any object names in SourceFileName not qualified with an owner name are qualified with this OwnerName.

-m ModuleName

assigns a name to the stored module. Module names must follow the rules governing ALLBASE/SQL basic names as described in the ALLBASE/SQL Reference Manual. If a module name is not specified with the -p option, the preprocessor uses the ModifiedSourceFileName as the module name. If a module name is not specified without the -p option, the preprocessor uses the SourceFileName as the module name.

-d

deletes any module currently stored in the DBEnvironment by the ModuleName and OwnerName specified in the command string. If not specified, any module having these names is not dropped, and existing RUN authorities for that module are preserved.

-r

is specified when the program being preprocessed already has a stored module and you want to revoke existing RUN authorities for that module. The -r option cannot be specified unless -d is also specified. If the -r option is not specified, it is assumed that all existing RUN authorities for that module are to be PRESERVED.

Description

  1. When the program being preprocessed already has a stored module, be sure to use the -d option, or else an error will result. Also, be sure that no one is currently executing the module when you invoke the preprocessor. To avoid conflicts, do your preprocessing in single-user mode during off-hours.

  2. The preprocessor starts a DBE session in the DBEnvironment named in the preprocessor command by issuing a CONNECT TO 'DBEnvironmentName' command. If the autostart flag is OFF, the DBE session can be initiated only after a START DBE command has been processed (by means of ISQL or an embedded SQL program). Remember that if multiple, simultaneous users run a program, you should issue the START DBE command only once.

  3. If the DBEnvironment to be accessed is operating in single-user mode, preprocessing can occur only when another DBE session for the DBEnvironment does not exist.

  4. When the preprocessor's DBE session begins, ALLBASE/SQL processes a BEGIN WORK command. When preprocessing is completed, the preprocessor submits a COMMIT WORK command, and any sections created are committed to the system catalog. If the preprocessor detects an error in the source file, it processes a ROLLBACK WORK command before terminating, and no sections are stored in the DBEnvironment. Preprocessor warnings do not prevent sections from being stored.

  5. Since all preprocessor DBE sessions initiate only one transaction, any log file space used by the session is not available for re-use until after the session terminates. If rollforward logging is not in effect, you can issue the CHECKPOINT command in ISQL before preprocessing to increase the amount of available log space. Refer to the ALLBASE/SQL Database Administration Guide for additional information on log space management, such as using the START DBE NEWLOG command to increase the size of the log file and recovering log space when rollforward logging is in effect.

  6. During preprocessing, system catalog pages accessed for embedded commands are locked. In multiuser mode, other DBE sessions accessing the same objects must wait, so there is potential for a deadlock. Therefore, minimize competing transactions if possible when preprocessing an application program. Refer to the appendix, "Locks Held on the System Catalog by SQL Commands," in the ALLBASE/SQL Database Administration Guide for information on operations that lock system catalog pages.

  7. For improved runtime performance, use the UPDATE STATISTICS command in ISQL before preprocessing for each table accessed in a data manipulation command when an index on that table has been added or dropped and when data in the table is often changed.

  8. If you specify an OwnerName or ModuleName in a language other than n-computer (ASCII), be sure that the language you are using is also the language of the DBEnvironment in which the module will be stored.

Authorization

To preprocess a program, you need DBA or CONNECT authority for the DBEnvironment specified in the preprocessor command line. You also need table and view authorities for the tables and views which the program will access at run time.

DBEnvironment CONNECT authority can also be explicitly granted. If you have DBECreator or DBA authority or module OWNER authority, you have CONNECT authority by default.

Table authorities are implicitly specified at the time the table is created and depend on the table type (PUBLIC, PUBLICREAD, or PRIVATE). Once a table has been created, its implicit authorities can be changed by the table OWNER, the DBECreator, or another DBA. Table authorities are removed by using the REVOKE command and are added by using the GRANT command.

For example, for a PUBLIC table, you are implicitly granted authority for any type of table access when the table is created. For a PUBLICREAD table, you must have explicitly granted authority for any table access except READ access which is an implicit grant. For a PRIVATE table, there are no implicit grants at table creation time; only the table OWNER or a DBA can access a PRIVATE table, unless specific authorities are granted to others.

Note, in the case of the sample database, PartsDBE, the creation script REVOKEs all implicit table authorities, and desired authorities must be explicitly granted.

NOTE: When preprocessing, you cannot name another user as module owner unless you are a DBA of the DBEnvironment or you are the current module owner.

Example



                                          MON, JUL 10, 1991,  4:48 PM

$ psqlc ../sampledb/PartsDBE -o joann -m joannpgm -i prog1.sql -d -r

HP36217-02A.E1.00           C Preprocessor/9000           ALLBASE/SQL 

(C)COPYRIGHT HEWLETT-PACKARD CO.  1982,1983,1984,1985,1986,1987,1988,

1989,1990,1991. ALL RIGHTS RESERVED.



  0 ERRORS   1 WARNINGS

END OF PREPROCESSING.



$ more sqlmsg



                                          MON, JUL 10, 1991,  4:48 PM

HP36217-02A.E1.00           C Preprocessor/9000           ALLBASE/SQL 

(C)COPYRIGHT HEWLETT-PACKARD CO.  1982,1983,1984,1985,1986,1987,1988,

1989,1990,1991. ALL RIGHTS RESERVED.



DBEnvironment        = ../sampledb/PartsDBE



Module Name          = JOANNPGM



******  SELECT PartNumber, PartName, SalesPrice INTO :PartNumber, :PartName,

         :SalesPrice WHERE PartNumber = :PartNumber

******  ALLBASE/SQL warnings.  (DBERR 10602)           |

******  User joann does not have SELECT authority on PurchDB.Parts

(DBERR 2301)

  1 Sections stored in DBEnvironment.



  0 ERRORS   1 WARNINGS

END OF PROCESSING



$

Syntax Checking Mode

The following command only checks the syntax of the SQL commands embedded in the source code file.

Syntax

psqlc -s [ -i SourceFileName -p ModifiedSourceFileName ] |...|

Parameters

-s

causes the preprocessor to only check SQL syntax.

-i SourceFileName

identifies the name of the input file containing the source code to be preprocessed.

-p ModifiedSourceFileName

identifies the name for the modified source code file. If a name is not specified, the preprocessor generated code is written to a file with the same name as SourceFileName.

Description

  1. The preprocessor does not access a DBEnvironment when it is run in this mode.

  2. When performing syntax only checking, the preprocessor does not convert the SQL commands into C constructs. Therefore the modified source code file does not contain any preprocessor generated calls to ALLBASE/SQL external procedures.

  3. The include and installable module files are created, but are incomplete.

Authorization

You do not need ALLBASE/SQL authorization when you use the preprocessor to only check SQL syntax.

Example

$ psqlc -s -i mysource



                                          MON, JUL 10, 1991,  4:48 PM

HP36217-02A.E1.00           C Preprocessor/9000           ALLBASE/SQL 

(C)COPYRIGHT HEWLETT-PACKARD CO.  1982,1983,1984,1985,1986,1987,1988,

1989,1990,1991. ALL RIGHTS RESERVED.



Syntax checked.

1 ERRORS    0 WARNINGS

END OF PREPROCESSING.



$ more sqlmsg

                                          MON, JUL 10, 1991,  4:48 PM

HP36217-02A.E1.00           C Preprocessor/9000           ALLBASE/SQL 

(C)COPYRIGHT HEWLETT-PACKARD CO.  1982,1983,1984,1985,1986,1987,1988,

1989,1990,1991. ALL RIGHTS RESERVED.





       SELCT PartNumber, PartName, SalesPrice

         INTO :PartNumber, :PartName :PartNameInd,

         :SalesPrice :SalesPriceInd FROM PurchDB.Parts

         WHERE PartNumber = :PartNumber;



****** ALLBASE/SQL errors.  (DBERR 10977)

****** in SQL statement ending in line 128

***    <1001> Syntax error.  (DBERR 1001)



Syntax checked.



  1 ERRORS   0 WARNINGS

END OF PREPROCESSING



The line 128 referenced in sqlmsg is the line in

mysource where the erroneous SQL command ends.

DBEnvironment Access

When you invoke the preprocessor in full preprocessing mode, you name an ALLBASE/SQL DBEnvironment. The preprocessor starts a DBE session for that DBEnvironment when preprocessing begins and terminates that session when preprocessing is completed.

	$ psqlc DBEnvironment -m ModuleName 

	      -i SourceFileName.sql -p ModifiedSourceFileName.c

When the preprocessor terminates its DBEnvironment session, it issues a COMMIT WORK command if it encountered no errors. Created sections are stored in the DBEnvironment and associated with the module name. See Figure 2-6 later in this chapter.

ALLBASE/SQL accesses the DBEnvironment you specify during preprocessing, even if your program does not use SQL statements that store sections in this DBEnvironment. Therefore, you must specify the name of a valid DBEnvironment.

In some cases an ALLBASE/SQL progam is used with one or more DBEnvironments in addition to the DBEnvironment accessed at preprocessing time. In these cases, use ISQL to install the installable module created by the preprocessor into each additional DBEnvironment accessed by your program. See the section "Installable Module File" in this chapter.

Running the Preprocessor in the Background

You can run the C preprocessor in the background to free your terminal for performing other processes. In the following example, the C preprocessor is run in the background in the C shell to check the syntax of a program, and any messages usually sent to the terminal are redirected to an output file:

        $ psqlc -s -i myprogram >& myprogram.out &

The syntax for this statement may not be the same if the command is not entered in the C shell.

Compiling and Linking

Figure 2-1 shows the process of compiling and linking an embedded SQL C program.

Figure 2-1 Compiling and Linking

[Compiling and Linking]

As shown in the figure, you submit to the C compiler one or more modified source code files and the related include files created by the preprocessor. The compiler then generates object code. By default, any object code file name consists of the source code file name with a .o file extension.

To convert these object code files into an executable program, link them after compilation by invoking the link editor. This step creates an executable program file.

The linker is automatically invoked after compilation unless the -c compile only option is used. If you want to link multiple object code modules together to form one executable program, list the object code modules following the C compiler command, followed by the library options.

If each of the object code modules has a .o file extension, the module will not be recompiled. If you do not use the -o compiler option to specify a specific output file name for the linked output, a file named a.out is created by default.

In the following example, an executable program named someprog.r is created after a module named somemod is stored by the C preprocessor in a DBEnvironment named somedbe:

	login: pgmr1

              .

              .

              .

	$ psqlc somedbe -m somemod -i mysource.sql -p mysource.c

              .

              .

              .

	$ cc mysource.c -lsql -lcl -lportnls -o someprog.r

The options used are:

  • -lsql, to link the ALLBASE/SQL runtime library, in /usr/lib/libsql.a

  • -lcl, to link the Pascal runtime library, in /usr/lib/libcl.a

  • -lportnls, to link the native language routines, in /usr/lib/libportnls.a

  • -o, to create the executable output file someprog.r

300/400:
	login: pgmr1

              .

              .

              .

	$ psqlc somedbe -m somemod -i mysource.sql -p mysource.c

              .

              .

              .

	$ cc mysource.c -lsql -lheap2 -lportnls -lpc -o someprog.r

The options used are:

  • -lsql, to link the ALLBASE/SQL runtime library, in /usr/lib/libsql.a

  • -lheap2, to link the random heap space management routines, in /usr/lib/libheap2.a

  • -lportnls, to link the native language routines, in /usr/lib/libprotnls.a

  • -lpc, to link the Pascal runtime library, in /usr/lib/libpc.a

  • -o, to create the executable output file someprog.r

Running the Program

When an ALLBASE/SQL program is first created, it can only be executed by the module OWNER or a DBA. In addition, it can only operate on the DBEnvironment used at preprocessing time if a module was generated. If no module was generated because the SQL commands embedded in the program are only commands for which no sections are created, the program can be run against any DBEnvironment.

The program created in the previous example can be executed as follows by the module owner:

	$ someprog.r

To make the program executable by other users in other DBEnvironments:

  • Load the executable program file onto the machine where the production DBEnvironment resides.

  • Install any related module in the production DBEnvironment.

  • Ensure necessary module owner authorities exist.

  • Grant required authorities to program users.

Accessing Multiple DBEnvironments

An alternative method of accessing more than one DBEnvironment from the same program would be to divide the program into separate compilable files. Each source file would access a DBEnvironment. In each file, start and terminate a DBE session for the DBEnvironment accessed. Then preprocess and compile each file separately. When you invoke the preprocessor, 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, link them together to create an executable program. An example of this technique follows:

	$ psqlc DBEnvironment1 -i SourceFileName1.sql -p 				ModifiedSourceFileName1.c

	      .

	      .

	$ cc -c ModifiedSourceFileName1.c

	      .

	      .

	$ psqlc DBEnvironment2 -i SourceFileName2.sql -p 		ModifiedSourceFileName2.c

	      .

	      .

	$ cc -c ModifiedSourceFileName2.c

                              .

                              .

	$ cc ModifiedSourceFileName1.o ModifiedSourceFileName2.o -lsql -lcl -lportnls

  -o ExecutableFileName
300/400:

	$ cc ModifiedSourceFileName1.o ModifiedSourceFileName2.o -lsql -lheap2 	-lportnls

	-lpc -o ExecutableFileName

Note that a program which accesses more than one DBEnvironment must do so in sequence since only one DBEnvironment can be accessed at a time. Such program design may adversely affect performance and requires special consideration.

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