Code Generation: Declaring War on Deja Code

We all know the drill: Don't reinvent the wheel. Re-use code. Rely on libraries of pre-built code. And yet so few of us do it. One way to avoid "deja code", that feeling you'd written this algorithm before, is to generate code rather than write it manually. Code generation offers tremendous promise, in terms of both productivity and code quality.

I (and others) have built a number of code generation utilities over the years and I offer some of them to you on this page, all contained in a single zip file -- except for PL/Generator, which is available separately. Scan through the descriptions below to see if any of these catch your fancy. 

May 2007 Update: Even better, though, over the past several years, I built a new tool that is a very generalized Design pattern Factory, which lets you design templates for any kind of pattern, and then generate code (PL/SQL, Java, HTML, whatever you like) against that pattern.

Quest Software acquired this technology in 2006 and has released it as freeware: the Quest CodeGen Utility. I suggest that you check out CodeGen before using PL/Generator or any of these other "one off" generation utilities.

If you have a code generation program that you would like to add to these offerings, please send me a note

PL/Generator

PL/Generator is a generator engine that generates production-ready code written to your coding standards. It combines best-practice coding and sophisticated "database-aware" technology to generate table packages written specifically for your Oracle tables and to your specifications. PL/Generator employs plug-in drivers, each of which generate different source code as output. The PL/SQL Table Encapsulation Driver generates PL/SQL encapsulation packages for tables that can be compiled and deployed in production applications without any changes. 

In a special arrangement with Quest Software (which owns PL/Generator, but does not currently sell it), I am making PL/Generator available to the public. IT IS NOT SUPPORTED. Please download and use at your own risk. I will set up a discussion forum as soon as possible, so PL/Generator users can discuss and problem-solve with one another. 

Download PL/Generator by clicking here.

Subscribe to the PL/Generator announcements-only list to receive updates about PL/Generator: 

Subscribe to PL/Generator List
Code Generation Zip

The code generation zip can be downloaded by clicking here (12K). Here is a description of the contents of this zip file:

Error Message Management: msginfo.pkg (Sep 8, 2002)

Do you use -20,NNN error numbers with RAISE_APPLICATION_ERROR? If so, there is a really good chance that those -20,NNN values are hard-coded, along with error messages, in your code. What a mess! This file creates a table in which you can manage your error numbers, but also provides a package that will generate named constants and exceptions for each of those defined exceptions. 

Generate Record Comparison Logic: gen_record_comparison.pkg (Aug 22, 2002)

PL/SQL doesn't let you directly compare the contents of two records using an "=" operator; instead you must compare the values in each corresponding field. Dan Spencer offers this excellent generator to construct a function to compare record contents for the specified table or view.

Generate Collections to Cache Table Data: genaa.sql

Takes advantage of Oracle9i Release 2 associative arrays to provide rapid, cached access to table data via either the primary key or any unique index.

Generate Trigger Records to Pass NEW and OLD as Parameters: gentrigrec.sp

PL/SQL won't let you pass the NEW and OLD pseudo-records as parameters to procedurs and functions inside triggers. This utility generates declarations of local records and then populates those records with the NEW and OLD information.

Generate a Trigger WHEN Clause to Minimize Trigger Firing: genwhen.sql

An important optimization for triggers involves constructing a WHEN clause that makes sure a particular column value has actually CHANGED before going on to execute the trigger logic.

Generate Package with Lengths of Column Values: gencollenpkg.sp

Sometimes we really want to be able to look up the maximum length of a VARCHAR2 column value (such as with calls to DBMS_SQL.DEFINE_COLUMN_VALUE), but that would be very slow. Use this script to generate a package that provides a named constant for the length of each column.

Generate Basic Table Encapsulation Package: gentecode.sql

An important best practice is to avoid writing explicit SQL and instead "hide" the SQL statements behind a layer of code: table or data encapsulation. This utility generates a very basic, but functional set of code to help you do this.

Copyright 1999-2002 Steven Feuerstein. All rights reserved.