Creating executable applications
So far, we have looked at some of the basic steps used to embed SQL statements in high-level programming language source code files, but we have only hinted at how source code files containing embedded SQL statements are converted into executable applications. Once a source code file has been written, the following steps must be performed, in the order shown, before an application that interacts with a DB2 database will be created:
All source code files containing embedded SQL statements must be precompiled. During the precompile process, a source code file containing embedded SQL statements is converted into a source code file that is made up entirely of high-level programming language statements -- the embedded SQL statements themselves are commented out and DB2-specific function calls are stored in their place.
At the same time, a corresponding package that contains (among other things) the access plans that are to be used to process each static SQL statement embedded in the source code file is also produced. (Access plans contain optimized information that the DB2 Database Manager uses to execute SQL statements; access plans for static SQL statements are produced at precompile time, while access plans for dynamic SQL statements are produced at application runtime.) Packages produced by the SQL precompiler can be stored in the database being used by the precompiler as they are generated, or they can be written to an external bind file and bound to any valid DB2 database later (the process of storing this package in the appropriate database is known as binding). Unless otherwise specified, the SQL precompiler is also responsible for verifying that all database objects, such as tables and columns, that have been referenced in static SQL statements actually exist, and that all application data types used are compatible with their database counterparts. (That's why you need a database connection in order to use the SQL precompiler.)
Once a source code file containing embedded SQL statements has been processed by the SQL precompiler, the source code file produced -- along with any additional source code files needed -- must be compiled by a high-level programming language compiler. This compiler is responsible for converting source code files into object modules that the linker can use to create an executable program.
After all source code files needed to build an application have been successfully compiled, the resulting object modules must be linked with high-level programming language libraries and DB2 libraries to create an executable program. In most cases, the program produced exists as an executable application. However, it may also exist as a shared library or a dynamic link library (DLL) that can be loaded and executed by other executable applications.
If the packages for the files that were processed by the SQL precompiler have not already been bound to the appropriate database, they must be bound using the bind files produced during the precompile process. This is done using a tool known as the DB2 Binder (or simply the Binder).
Figure 1 illustrates the basic process used to convert an embedded SQL source code file to an executable application when deferred binding is used.
Figure 1. Converting an embedded SQL source code file to an executable application