Generating PostgreSQL Functions with CodeJohnny

CodeJohnny is billed as a Java Code Generator for Eclipse, but because of its data-driven, template design we can generate any type of patterns we need. To demonstrate, here's a new template I created today which generates a PostgreSQL Function.

The Template

The core of the CodeJohnny Template contains the {tags} and {{methods}} which fill out the PLPGSQL code from the datatable defined in the template's <datatable /> field.

CodeJohnny Methods

In the above template we see simple properties used like {datatable} and {primarykey}, but the real action is in using Java Methods to generate multiple lines of code. The method we'll look at below displays fields in the form of

_nixmashup_id INT4,
_link_title VARCHAR,
_link_url VARCHAR...

The CodeJohnnyTemplate object contains everything needed from the template and the data source object, along with helper functions for the objects like codeJohnnyColumn.isPrimary and codeJohnnyColumn.isLastColumn demonstrated here.

The PostgreSQL Function

With one click the following PLPGSQL is generated. It's not this pretty when first generated, but copy-n-paste into Navicat and a click of the “Sql Beautify” function and we're done. That's a lot of typing we don't have to do. And it's pretty, too!

About CodeJohnny

CodeJohnny is available at http://codejohnny.com. The template demonstrated in this post will be included in the next release.

Posted April 15, 2014 10:34 PM EDT

More Like This Post