public class Sql2DataC extends SqlClassWriter
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.
Sql2ClassConsole,
TableModelm_className, m_classTpl, m_location, m_packageName, m_tableModel, m_typeInfo| Constructor and Description |
|---|
Sql2DataC(java.io.File location,
java.lang.String packageName)
Constructor
|
Sql2DataC(java.io.File location,
java.lang.String packageName,
org.brains2b.sql.meta.TableModel tmodel)
Constructor
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
createFinalClose(org.brains2b.typewriter.TryTemplate t) |
protected java.lang.String |
getClassName(java.lang.String name)
get the classname used for generating this data control class
|
protected org.brains2b.typewriter.ClassTemplate |
getClassTemp() |
org.brains2b.sql.Database |
getDatabase() |
protected java.lang.String |
getEmptyForType(int type)
get the empty value for {CLASS or PRIMITIVE} resp.
|
protected java.lang.String |
getInstance() |
protected java.lang.String |
getObjectName() |
protected org.brains2b.sql.meta.ColumnModel[] |
getPrimaryKey(org.brains2b.sql.meta.TableModel model)
get an array of SqlColumnModel which contains the columns making up the primary
key in the proper sequence.
|
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.
|
boolean |
isPrintSchema() |
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 |
setDatabase(org.brains2b.sql.Database database) |
void |
setOptimisticLock(boolean b)
sets this class to generate the required code for using optimistic locking.
|
protected void |
setParameters(org.brains2b.typewriter.ContentTemplate t,
int seq,
org.brains2b.sql.meta.ColumnModel cm) |
void |
setPrintSchema(boolean printSchema) |
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.
|
protected void |
writeInsert()
writes the normal insert statement, possibly with the optimistic locking code
|
fileExists, getFile, getUnreserverdWord, isPrimaryKey, setTableModel, setTypeInfo, varName, writeHeaderCommentpublic Sql2DataC(java.io.File location,
java.lang.String packageName)
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)
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.public Sql2DataC(java.io.File location,
java.lang.String packageName,
org.brains2b.sql.meta.TableModel tmodel)
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.tmodel - TableModel, the table model to base this dataclass onpublic void run()
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
Runnable.run(),
DefaultTask.setFinished(boolean),
Loggerpublic java.io.File getResultfile()
The result of this file is only valid after run()has finished
getResultfile in class SqlClassWriterSqlClassWriter.getResultfile(),
run()protected void writeInsert()
setOptimisticLock(boolean)protected org.brains2b.sql.meta.ColumnModel[] getPrimaryKey(org.brains2b.sql.meta.TableModel model)
model - protected java.lang.String getEmptyForType(int type)
null
or 0type - protected java.lang.String getClassName(java.lang.String name)
getClassName in class SqlClassWritername - String, the table nameSqlClassWriter.getClassName(String)protected boolean isReadOnly()
public void setReadOnly(boolean b)
This will not influence the code generated for a View
b - boolean, true if you want it to generate read only, false otherwiseprotected boolean isOptimisticLock()
setOptimisticLock(boolean)public void setOptimisticLock(boolean b)
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.
b - boolean, true if optimistic locking should be used, false otherwiseSql2Filepublic org.brains2b.sql.Database getDatabase()
public void setDatabase(org.brains2b.sql.Database database)
database - The database to set.protected org.brains2b.typewriter.ClassTemplate getClassTemp()
protected java.lang.String getInstance()
protected java.lang.String getObjectName()
protected void createFinalClose(org.brains2b.typewriter.TryTemplate t)
protected void setParameters(org.brains2b.typewriter.ContentTemplate t,
int seq,
org.brains2b.sql.meta.ColumnModel cm)
public boolean isPrintSchema()
public void setPrintSchema(boolean printSchema)