Firefox logo

Ant logo

Netbeans logo

Extending grammars

Grammars define the dialect of conversion for field types, because sql is not even standard on all database, for example sql dialect of oracle have some differences from mysql dialect. csv2sql support some of most common dialect, and can also be extended.

To add a new dialect support you must extend net.sf.csv2sql.grammars.AbstractGrammarFactory, this class is a factory that creates net.sf.csv2sql.grammars.AbstractField from a string.

This is the AbstractGrammarFactory class:
public abstract class AbstractGrammarFactory {

        public abstract AbstractField createField(String type, String name, Properties prop)
        throws GrammarFactoryException;


Wich Properties prop parameter extra information can be used by the field (like date format, precision, ...).
String name parameter define the name of the field in the database.
You simply need to override the createField() method, and instantiate a new net.sf.csv2sql.grammars.AbstractField based on String type parameter.

Grammars fields

A grammars field rappresent one of dialect datatype, for example VARCHAR2 in Oracle.
All fields are subclasses of net.sf.csv2sql.grammars.AbstractField.

An example of a grammar field:
public class SomeDialectFieldString extends AbstractField {

        public SomeDialectFieldString(String fieldName, java.util.Properties properties) {
                super(fieldName, properties);

        public String getDefaulNullValue() {
                return "NULL";

        public String render(Object value) throws FieldRenderException {
                if ((value == null) || ("".equals(value))) {return getDefaulNullValue();}
                try {
                        return ((String)value);
                } catch (Exception e) {
                        throw new FieldRenderException(e.getMessage());


First the constructor, this simply call parent constructor with the name of the field (name, not type) and the list of optional parameter (maxlegth for example).

getDefaulNullValue() method return only the default null value for the type of field, usually is the NULL keyword for all fields of dialects.

Finally the render() method get the value of the field and render it based on the different type of field (for example strings are rendered in another way than integers) throwing a FieldRenderException if value is incompatible with the type of field or returning result of getDefaulNullValue() if the value is null (or empty in this case).

Examples grammar can be viewed in the csv2sql source or (browsing cvs repository)

copyright 2004 Davide Consonni (