How to write a custom plugin for Salto-db Generator

It is possible to write your own plugins and to use them from the Ant task. It is not yet possible to use custom plugins within the Eclipse plugin.

To write a plugin you need to create class that implements the IGeneratorPlugin interface.

I advise you to extend the abstract class DefaultAbstractGeneratorPlugin .

We're going to create a plugin called "myPlugin" which create a text file for each table. In each of those file we will write the name of the table, the name of the columns and their types.

Here is the code of our custom plugin :

package com.test;

import java.io.FileOutputStream;
import java.io.PrintWriter;

import salto.tool.jdo.data.JdoInfo;
import salto.tool.sql.data.TableColInfo;

import com.salto.db.generator.plugin.DefaultAbstractGeneratorPlugin;

public class MyPlugin extends DefaultAbstractGeneratorPlugin {

        /**
         * Create a file for each table that contains column names and types
         * 
         * @see com.salto.db.generator.plugin.IGeneratorPlugin#execute(java.lang.String,
         *      salto.tool.jdo.data.JdoInfo)
         */
        public void execute(String fileName, JdoInfo jdoInfo) throws Exception {

                FileOutputStream fos = null;
                PrintWriter printWriter = null;

                try {
                        fos = new FileOutputStream(fileName + ".txt");
                        printWriter = new PrintWriter(fos);
                        printWriter.println("----------------------------------------");
                        printWriter.println("Table " + jdoInfo.getTableName());
                        printWriter.println("----------------------------------------");
                        for (int i = 0; i < jdoInfo.getColInfos().length; i++) {
                                TableColInfo info = jdoInfo.getColInfos()[i];
                                printWriter.println(info.getColName() + " "
                                                + info.getColTypName());
                        }
                }

                catch (Exception e) {
                        e.printStackTrace();

                } finally {
                        try {
                                printWriter.close();
                        } catch (Exception e) {

                        }

                        try {
                                fos.close();
                        } catch (Exception e) {

                        }
                }
        }

        /**
         * This prefix will be added to the name that we are passed as a parameter
         * in the execute() method.
         * 
         * @see com.salto.db.generator.plugin.IGeneratorPlugin#getDefaultPrefix()
         */
        public String getDefaultPrefix() {
                return "MyCustomPrefix";
        }

        /**
         * Long description show in the eclipse plugin.
         * 
         * @see com.salto.db.generator.plugin.IGeneratorPlugin#getLongDescription()
         */
        public String getLongDescription() {
                return "This plugin has been written to show how to develop a custom plugin and how to register it."
                                + " It will generate a simple text for each table containing column names and types.";
        }

        /**
         * Plugin identifier
         * 
         * @see com.salto.db.generator.plugin.IGeneratorPlugin#getName()
         */
        public String getName() {
                return "myplugin";
        }

        /**
         * short description show in the eclipse plugin.
         * 
         * @see com.salto.db.generator.plugin.IGeneratorPlugin#getShortDescription()
         */
        public String getShortDescription() {
                return "tutorial custom plugin";
        }

}

Next we need to compile our plugin. Once this is done we can use the salto-db-register task to use it from the salto-db-generate task :

<salto-db-register className="com.test.MyPlugin"/>
                
<salto-db-generate nameOffset="0" generateView="${generateView}" plugin="myPlugin" schema="${schema}"
         tableName="${tableName}" login="${login}" password="${password}" jdbcUrl="${jdbcUrl}"
         driverClassName="${driverClassName}" outputDir="${outputDir}" packageName="${packageName}"/>   

After running the Ant build, we end up with as many MyCustomPrefix*.txt files as there are tables in the database.

Each generated file looks like that :

----------------------------------------
Table person
----------------------------------------
id_person INTEGER
name VARCHAR
first_name VARCHAR
age INTEGER
id_city INTEGER

For people familiar with the Velocity template engine, it might be easier to extend the VelocityAbstractPlugin when you write a plugin.

If you write a custom plugin and if you think it could be useful to other people, don't hesitate to send it to us, we'll include it on next releases.