SQL Parser

Backpedal makes use of a pluggable SQL parser so we can take advantage of the systematic nature of SQL generated from OR mapping tools. A comprehensive SQL parser could be written, but good luck doing it. This section dscribes how to write and install your SQL parser so you can use Backpedal with whatever form of SQL you're using in your project.

Writing a SQL Parser

You must implement this interface:



package net.sourceforge.backpedal.api.core;

public interface StatementParser {
    public ParsedStatement parseInsertStatement(String sql);
    public ParsedStatement parseDeleteStatement(String sql);
    public ParsedStatement parseUpdateStatement(String sql);
}

Whats probably more instructive is to look at the ParsedStatement interface:



package net.sourceforge.backpedal.api.core;

public interface ParsedStatement {
    /**
     * Return one of INSERT, UPDATE or DELETE, depending on what type
     * of SQL statement this represents.
     */
    public String getSqlKeyword();

    /**
     * Return the name of the table being affected by the SQL statement
     * this represents.
     */
    public String getTableName();

    /**
     * Return the names of the columns involved in the where clause
     * of the SQL statement this represents.  In the case of
     * UPDATE TABLE SET A=B, C=D WHERE E=F and G=H, the result would be
     * E and G.
     */
    public String [] getWhereColumnNames();

    /**
     * Return the names of the columns affected by the SQL statement this
     * represents.  In the case of
     * UPDATE TABLE SET A=B, C=D WHERE E=F and G=H, the result would be
     * A and C.
     */
    public String []getChangedColumnNames();

    /**
     * Return the original, unparsed SQL that this represents.
     */
    public String unparsedSql();
}

So this is pretty straight forward. Given the SQL "UPDATE TABLE SET A=B, C=D WHERE E=F and G=H", the resulting ParsedStatement would behave as follows:



parsedStatement.getSqlKeyword() == UPDATE
parsedStatement.getTableName() == TABLE
parsedStatement.getWhereColumnNames() == {E,G}
parsedStatement.getChangedColumnNames() == {A,C}
parsedStatement.unparsedSql() == UPDATE TABLE SET A=B, C=D WHERE E=F and G=H

All fine and well. Check out KodoStatementParser and KodoStatementParserJUnitTest for an example. If after implementing the SQL parser you're still not getting backpedalling to happen, contact me using the link at the top of the page.

Installing a SQL parser

The parser implementation to use is configured in backpedal.nconf. The relevant section is:

    <component
        type="net.sourceforge.backpedal.api.core.StatementParser"
        class="net.sourceforge.backpedal.kodo.KodoStatementParser">
    </component>

Simply change the class attribute to the name of the SQL parser implementation you want to use.