Cato: A template based, database driven source code generator

Welcome to the main page for my Cato CRUD generator application. Getting right into it, here’s what the marketing department says about Cato:

“Cato is a free, open source, template-based, database-driven, GUI app that lets you generate text -- source code, configuration files, etc. -- for any programming language or tool, based on the tables in your database.”

If you know what the code generator does in Ruby on Rails, Cato works just like that, but it’s not specific to any programming language or framework.

How Cato works

As a simple example of how Cato works, imagine that you want to generate a JavaBean class for a database table named users that’s defined like this:

create table users (
    id int unsigned auto_increment not null,
    email_address varchar(64) not null,
    password varchar(16) not null,
    first_name varchar(32),
    last_name varchar(32),
    date_created timestamp default now(),
    primary key (id),
    constraint unique index idx_users_unique (email_address)
) ENGINE = InnoDB;

All you have to do is start Cato, connect to your database, and select the Java templates:

Cato's first panel - database properties and templates directory

then switch to Cato’s second tab, choose the users table, all of the fields in the table, and then the JavaBean template:

Cato's second panel

Then when you press the “Generate Code” button, you’ll see the source code for the JavaBean appear in a dialog:

Cato's generated source code

That’s what Cato does; it generates all sorts of text files based on database table definitions. In this case, Cato generated a JavaBean by merging the fields in the users database table with the template file named JavaBean.tpl, which is located in the resources/templates/Java folder of the Cato project.

The files Cato generates can be any sort of text file, including:

  • JavaBean classes
  • Scala classes and case classes
  • All the source code and files needed for Play Framework forms
  • Everything needed for Drupal forms (PHP)
  • Everything needed for Sencha Touch and Ext (JavaScript)
  • Any sort of text configuration file, including XML, JSON, and YAML
  • etc., etc., etc.

If you can define a template for what you want, you can generate all sorts of source code and configuration files.

Video: Create Play Framework “CRUD” forms in less than seven minutes

No convinced yet? Here’s a video where I show how to create a complete set of Play Framework “CRUD” forms (Create, Read, Update, and Delete) for the users table shown above. The process of creating the forms and testing them takes exactly six minutes and thirty seconds:

Note that I could probably generate all five files at once and do this in about half the time, but right now the generated code isn’t perfect, so that can be a little tricky for inexperienced developers.

In that video I generate Play Framework 2.2.x code; I haven’t updated to 2.3.x yet (and I don’t know all of the differences yet).

Getting started with Cato

If you’re using Mac OS X (or Linux) with a MySQL database, all you have to do to get started with Cato is (a) check the project out of Github, (b) cd to the directory named latest, and (c) run the script named run.sh. That should start the Cato GUI, at which point you just need to enter your database information, connect to your database, and select a templates directory.

I recommend starting with the Java templates directory, as the JavaBean template is very simple. That directory is located at resources/templates/Java in the Github project. Once you get comfortable with how Cato works, you can choose from the other templates, based on your needs.

If you’re a Windows user, I’m sorry, but I don’t have a Windows system to test on, so I haven’t created a batch file for you to use. That being said, it should be easy to convert the Mac/Unix shell scripts to work on Windows.

Here’s a link to the project on Github:

Databases besides MySQL

So far I’ve only tested Cato against MySQL, but I do know that the code that reads the database metadata used to work with Postgres, so there’s a chance that will work right out of the box; all you need is a Postgres JDBC driver. I think it also used to work with Microsoft SQL Server, but that was many, many years ago.

A command-line version of Cato

The command line version of Cato also works. I just confirmed this by creating a shell script and creating those same Play Framework forms in less than a minute from the command line. Here’s a much shorter video demo:

This is still in an “alpha” state (that can wreak some havoc if you use it wrong), but as you can see, that was pretty easy.

Creating your own templates

Cato currently ships with the templates I need, which are:

  • JavaBean
  • Two Scala templates
  • Play Framework templates
  • I started working on some Sencha Touch templates, but they’re not complete yet

I haven’t used Drupal in a while, but when I get back to using it, I have some slightly old Drupal 7 templates that I can convert to work with Cato in just a few hours. They need to be converted to Cato’s new template syntax (which is based on the Java FreeMarker project), but once they’re converted they should work well.

Source code

Cato is primarily written in Scala, though there is still some very old, crusty Java code in there that I need to continue converting. I don’t have a formal schedule set up, but I will continue to convert the application to 100% Scala as time goes by.

Please read the docs

I’m trying to include plenty of documentation in the Cato project on Github, and in an effort not to duplicate all of that information, I’ll just say, “Please read the ‘README’ files included in the project.” There are README files for users, programmers, and template developers, so I hope that’s enough for everyone.

Future announcements

As I add new features to Cato, I’ll make announcements on this website and on Twitter, so if you’re interested in following Cato’s progress, please follow me here on Twitter.

All the best,
Al