org.brains2b.sql.toclass
Class SqlClassWriter

java.lang.Object
  extended by org.brains2b.task.DefaultTask
      extended by org.brains2b.sql.toclass.SqlClassWriter
All Implemented Interfaces:
java.lang.Runnable, org.brains2b.task.Task
Direct Known Subclasses:
Sql2DataC, Sql2File

public abstract class SqlClassWriter
extends org.brains2b.task.DefaultTask

Abstract class which supplies some general functions to classes generating Class files from a database source.

  • Checks locations and uses or creates package directories
  • Checks if current file already exists and check if file needs to be regenerated

    Version:
    0.40.2 [19-08-2006]
    Author:
    dennis@brains2b.nl

    Field Summary
    protected  java.io.File m_location
               
    protected  java.lang.String m_packageName
               
    protected  org.brains2b.sql.meta.TableModel m_tableModel
               
    protected  org.brains2b.sql.meta.TypesInfo m_typeInfo
               
     
    Fields inherited from interface org.brains2b.task.Task
    RESULT_ERROR, RESULT_OK, RESULT_WARNING
     
    Constructor Summary
    protected SqlClassWriter(java.io.File location, java.lang.String packageName)
              Constructor
     
    Method Summary
     boolean fileExists(java.lang.String tableName)
              test if the file you want to create already exists based on the tablename supplied.
    protected abstract  java.lang.String getClassName(java.lang.String name)
              get the class name to be generated for the type of file.
    protected  java.io.File getFile(java.io.File location, java.lang.String packageName, java.lang.String className)
              get the File name on the basis of the supplied location, packageName and className
    abstract  java.io.File getResultfile()
              abstract method for returning the generated file
    protected  java.lang.String getUnreserverdWord(java.lang.String name)
              get the name to use in Java for a SQL name which was altered to prevent a reserved word being used in the name
    protected  boolean isPrimaryKey(org.brains2b.sql.meta.ColumnModel column, org.brains2b.sql.meta.TableModel table)
               
     void setTableModel(org.brains2b.sql.meta.TableModel tableModel)
              sets the model for the table used for generating the file
     void setTypeInfo(org.brains2b.sql.meta.TypesInfo typeInfo)
               
    protected  java.lang.String varName(java.lang.String str)
              check if a varName is not a java keyword and can be used as a variable name otherwise prepend x_ to it and use that as variable name
     
    Methods inherited from class org.brains2b.task.DefaultTask
    addException, addMessage, addTaskListener, fireTaskEvent, getMax, getMessages, getMin, getPosition, getResult, isFinished, progress, progress, removeTaskListener, setFinished, setMax, setMin, setResult
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     
    Methods inherited from interface java.lang.Runnable
    run
     

    Field Detail

    m_location

    protected java.io.File m_location

    m_packageName

    protected java.lang.String m_packageName

    m_tableModel

    protected org.brains2b.sql.meta.TableModel m_tableModel

    m_typeInfo

    protected org.brains2b.sql.meta.TypesInfo m_typeInfo
    Constructor Detail

    SqlClassWriter

    protected SqlClassWriter(java.io.File location,
                             java.lang.String packageName)
    Constructor

    Parameters:
    location - File, set the location to write the file to, this location should exist before run is called on this class. If this is null the current directory is used
    packageName - String, the packageName for the class, names should be separated by dots. The packagename is both used to set the package name of the class as to create or use the directory relative to location to write the file to. If this is null no package name is used when creating this file.
    Method Detail

    getClassName

    protected abstract java.lang.String getClassName(java.lang.String name)
    get the class name to be generated for the type of file.

    Implementation is left to the class extending this class

    Parameters:
    name - String, the table name
    Returns:
    String the name of the new class with proper capitalization

    fileExists

    public boolean fileExists(java.lang.String tableName)
    test if the file you want to create already exists based on the tablename supplied.

    This method will return true on any IOException except FileNotFoundException to make sure a valid file exists after the generation has finished.

    Parameters:
    tableName - String, the tableName
    Returns:
    true if fle already exists and the content appears to be the same as expected for the new file by calling isFileSame
    See Also:
    isFileSame(File)

    setTableModel

    public void setTableModel(org.brains2b.sql.meta.TableModel tableModel)
    sets the model for the table used for generating the file

    Parameters:
    tableModel - SqlTableModel, the table model used

    setTypeInfo

    public void setTypeInfo(org.brains2b.sql.meta.TypesInfo typeInfo)

    getFile

    protected java.io.File getFile(java.io.File location,
                                   java.lang.String packageName,
                                   java.lang.String className)
                            throws java.io.IOException
    get the File name on the basis of the supplied location, packageName and className

    Parameters:
    location - File, the location.
    packageName - String, the new or existing packageName. If new will be created at the location. If location is null the packageName will be ignored
    className - String, the name of the class, a result of getClassName()
    Returns:
    File, the complete file for the new file
    Throws:
    java.io.IOException - thrown if createPackageDir fails
    See Also:
    createPackageDir(File, String)

    isPrimaryKey

    protected boolean isPrimaryKey(org.brains2b.sql.meta.ColumnModel column,
                                   org.brains2b.sql.meta.TableModel table)

    getUnreserverdWord

    protected java.lang.String getUnreserverdWord(java.lang.String name)
    get the name to use in Java for a SQL name which was altered to prevent a reserved word being used in the name

    Parameters:
    name - String, the name as used in SQL, possibly with _X appended
    Returns:
    String, the name as used in Java, with _X removed if it was added to protect against a reserved name
    Since:
    Squeler 0.40.1

    varName

    protected java.lang.String varName(java.lang.String str)
    check if a varName is not a java keyword and can be used as a variable name otherwise prepend x_ to it and use that as variable name

    This method also makes sure at least the first letter is a lowercase letter

    Parameters:
    str - String, the value which needs to be used a variable Name
    Returns:
    String, the variable name with at least the first letter in lower case
    See Also:
    KeyWords

    getResultfile

    public abstract java.io.File getResultfile()
    abstract method for returning the generated file

    implementation is left to the classes extending this class

    Returns:
    File, the newly created file