org.brains2b.sql.toclass
Class Sql2DataC

java.lang.Object
  |
  +--org.brains2b.task.DefaultTask
        |
        +--org.brains2b.sql.toclass.SqlClassWriter
              |
              +--org.brains2b.sql.toclass.Sql2DataC
All Implemented Interfaces:
java.lang.Runnable, org.brains2b.task.Task

public class Sql2DataC
extends SqlClassWriter

This class generates a DataControl class in line with the DataC specifications to control insert,delete,update and select statements for a given data class.

This class is called from Sql2ClassConsole and some other implementations which supply the necessary information. If you use this class just to generate files look at Sql2ClassConsole first.

The naming of the new class file will be equal to the table name with initial caps and followed by DC. example: EMPLOYEES will become EmployeesDC. Underscores will be treated as an instance to initialize caps again. example: EMP_SALARIES will become EmpSalariesDC

The class is written to the given location and packageName. The location should not include the package directory itself. If the package directory does not exist it will be created. If the location does not exist you will get a FileNotFoundException

The option setReadOnly generates a data class which will throw an Exception and performs no other function if you try to update,insert or delete a record through this data control, making this data control basically a read only data control.

If you generate a data control as a view the insert,update and delete will be generated throwing only an Exception while updating views is not supported.

Version:
0.40 [23-03-2004]
Author:
dennis@brains2b.nl
See Also:
Sql2ClassConsole, SqlTableModel

Field Summary
 
Fields inherited from class org.brains2b.sql.toclass.SqlClassWriter
m_location, m_packageName, m_tableModel
 
Fields inherited from interface org.brains2b.task.Task
RESULT_ERROR, RESULT_OK, RESULT_WARNING
 
Constructor Summary
Sql2DataC(java.io.File location, java.lang.String packageName)
          Constructor
Sql2DataC(java.io.File location, java.lang.String packageName, org.brains2b.sql.meta.SqlTableModel tmodel)
          Constructor
 
Method Summary
protected  java.lang.String getClassName(java.lang.String name)
          get the classname used for generating this data control class
 java.io.File getResultfile()
          get the file which has been created.
protected  boolean isOptimisticLock()
          check if this Data Control should contain the code for optimistic locking.
protected  boolean isReadOnly()
          check if this Data Control class should be generated as read only
 void run()
          run does the actual work in this class.
 void setOptimisticLock(boolean b)
          sets this class to generate the required code for using optimistic locking.
 void setReadOnly(boolean b)
          set to true if this Data Control class is to be generated read only, false is the default for tables, true for Views.
 
Methods inherited from class org.brains2b.sql.toclass.SqlClassWriter
fileExists, getFile, setTableModel, varName
 
Methods inherited from class org.brains2b.task.DefaultTask
addException, addMessage, addTaskListener, fireTaskEvent, getMax, getMessages, getMin, getPosition, getResult, isFinished, progress, removeTaskListener, setFinished, setMax, setMin, setResult
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Sql2DataC

public Sql2DataC(java.io.File location,
                 java.lang.String packageName)
Constructor

This constructor does not set the TableModel, this is however required for this class to be able to generate the datacontrol. You should set the tablemodel by calling setTableModel(org.brains2b.sql.meta.SqlTableModel)

Parameters:
location - File, set the location to write the file to, this location should exist before run is called on this class.
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.

Sql2DataC

public Sql2DataC(java.io.File location,
                 java.lang.String packageName,
                 org.brains2b.sql.meta.SqlTableModel tmodel)
Constructor

Parameters:
location - File, set the location to write the file to, this location should exist before run is called on this class.
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.
Method Detail

run

public void run()
run does the actual work in this class.

Output is written to the org.brains2b.log.Logger. Dependant on the setting there the code will show up on System.out. This is the case if you do nothing

Specified by:
run in interface java.lang.Runnable
Specified by:
run in class org.brains2b.task.DefaultTask
See Also:
Runnable.run(), DefaultTask.setFinished(boolean), Logger

getResultfile

public java.io.File getResultfile()
get the file which has been created.

The result of this file is only valid after run()has finished

Specified by:
getResultfile in class SqlClassWriter
Returns:
File, the newly created file
See Also:
SqlClassWriter.getResultfile(), run()

getClassName

protected java.lang.String getClassName(java.lang.String name)
get the classname used for generating this data control class

Specified by:
getClassName in class SqlClassWriter
Parameters:
name - String, the table name
Returns:
String the name of the new class with proper capitalization
See Also:
SqlClassWriter.getClassName(String)

isReadOnly

protected boolean isReadOnly()
check if this Data Control class should be generated as read only

Returns:
boolean, true if class should be generated read only, false otherwise

setReadOnly

public void setReadOnly(boolean b)
set to true if this Data Control class is to be generated read only, false is the default for tables, true for Views.

This will not influence the code generated for a View

Parameters:
b - boolean, true if you want it to generate read only, false otherwise

isOptimisticLock

protected boolean isOptimisticLock()
check if this Data Control should contain the code for optimistic locking.

Returns:
boolean, true if class should generate optimistic locking code, false otherwise
See Also:
setOptimisticLock(boolean)

setOptimisticLock

public void setOptimisticLock(boolean b)
sets this class to generate the required code for using optimistic locking.

Optimistic locking is done by setting a checksum value on a data class whenever a record is selected or updated. This value is the same as the calculated hashCode.
Before a record is updated the record is refetched and the checksum is compared to the hashCode of the newly fetched record. Whenever there is a change made to the database values will no longer match and a 'Record has been changed by another user' Exception is thrown.

For this to work correctly the Data class used for this control class should contain a setChecksum(int) and getChecksum() method, something which is automatically generated if setOptimisticLock(true) is used on Sql2File.

Parameters:
b - boolean, true if optimistic locking should be used, false otherwise
See Also:
Sql2File