javatools.db
Class DbConstraint

java.lang.Object
  |
  +--javatools.db.DbConstraint
Direct Known Subclasses:
DbDynamicConstraint, FKAuthor, FKAuthorTelephoneNo, FKContainer, FKContains, FKData, FKDataSet, FKEditor, FKEditorTelephoneNo, FKFatherOf, FKFileType, FKGenre, FKIncludes, FKInstalledProgram, FKMarks, FKNeedsExecutionOf, FKPublished, FKRealized, FKSession, FKVolume

public abstract class DbConstraint
extends java.lang.Object

This abstract class represents a generic set of constraints for a table. It contains a lot of code already written for generic purposes, i.e. it can be derived with few code. If you want to add additional checks, you can easily add your own code, rewriting check and update methods.


Field Summary
protected  boolean automaticChecking
          true: check is done at all times; false: check is performed only with an explicit call to check.
protected  boolean canDoUpdate
          true: update can be done.
static int CASCADE
          It means that, in case of an UPDATE/DELETE operation, the constraint MUST cascade the operation, that is updating/deleting in cascade all involved rows.
protected  boolean cloned
          true: the object is cloned.
protected  boolean constraintEmulation
          true: emulation made as needed; false: emulation is not made.
protected  DbConstraint[] constraints
          Contains an array of constraint to perform cascade-check and cascade-updates.
protected  int currentOperation
          Represents the current operation.
protected  java.lang.Object[][] defaultValues
          Contains default values for each referenced field of each father table.
static int DELETE_OPERATION
          It represents a DELETE operation.
protected  java.lang.Integer[] deleteCascadeChildren
          Currently unused.
protected  java.lang.Integer[] deleteOperations
          It contains the delete operations for each father table.
static int DO_NOTHING
          It means that, in case of an UPDATE/DELETE operation, the table MUST NOT do anything to repair itself.
protected  DbColumn[][] fatherColsChildren
          For each son, it contains the referenced column of THIS table.
protected  DbTable fatherRecords
          Contains the father records used to find eventually involved rows.
protected  DbAbstractTable[] fatherTables
          It contains all father tables of the one referenced by this constraint.
protected  DbColumn[][] fatherTablesColumns
          It contains the father keys for each father table.
protected  java.util.List fromList
          A list of values/expression to put data into (used in INSERT/UPDATE operations).
static int INSERT_OPERATION
          It represents an INSERT operation.
protected  java.util.List intoList
          A list of columns to put data into (used in INSERT/UPDATE operations).
protected  DbColumn[][] refColsChildren
          For each son it contains the referenced columns of son tables.
protected  DbColumn[][] refColumns
          It contains for each father table, the referenced column in contained table.
protected  java.lang.Integer[][] refs
          It's an array built at runtime to represent, for each father, the referenced fields if they are specified in intoList for INSERT or UPDATE operations.
protected  java.util.TreeSet[] searchSets
          Contains the search sets used to perform checks for each son table.
protected  DbSelector selector
          A selector used in INSERT operations.
static int SET_DEFAULT
          It means that, in case of an UPDATE/DELETE operation, the constraint MUST set an array of fields in involved rows to DEFAULT values, to repair referenced table.
static int SET_NULL
          It means that, in case of an UPDATE/DELETE operation, the constraint MUST set an array of fields in involved rows to NULL values, to repair referenced table.
protected  java.lang.Integer[] sonIndexes
          For each son, it contains the position in the fatherTables of the son table itself.
protected  DbExpr[] sonsWhere
          Contains the "where" clauses for each son.
protected  DbAbstractTable[] sonTables
          It contains all son tables of the one referenced by this constraint.
protected  DbAbstractTable table
          The referenced table to check.
protected static int UPDATE_CASCADE
          It means that an UPDATE-CASCADE operation is outgoing.
static int UPDATE_OPERATION
          It represents a normal UPDATE operation.
protected  java.lang.Integer[] updateCascadeChildren
          Currently unused.
protected  java.util.List[] updateFromLists
          Contains an array of fromList-like lists, to perform update operations in son tables.
protected  java.util.List[] updateIntoLists
          Contains an array of intoList-like lists, to perform update operations in son tables.
protected  java.lang.Integer[] updateOperations
          It contains the update operations for each father table.
protected  DbExpr where
          A DbExpr clause used int DELETE/UPDATE operations.
 
Constructor Summary
DbConstraint()
          Creates new DbConstraint
DbConstraint(DbAbstractTable tbl)
          Creates a new DbConstraint for a specified table.
 
Method Summary
 void build()
          After all settings, it finally builds all static references.
abstract  void check(int operation)
          After setting all dynamic (i.e. operations, lists etc.) information, it checks explicitly if all operations can be done.
protected  void checkChildren()
          Checks recursively each son.
protected  void checkFathers()
          Performs checking for father tables.
protected  void checkThis(int operation)
          Performs base checks.
 void clear()
          Clears everything.
protected  java.lang.Object clone()
          Clones this object.
 boolean getAutomaticChecking()
          Returns the current automatic checking setting.
 boolean getConstraintEmulation()
          Returns if constraint emulation is enabled.
 java.lang.Object[] getDefaultValues(int index)
          Returns an array containing the default values for fields of a father table whose index is specified.
 int getDeleteOperation(int index)
          Returns what kind of operation will be done in case of a deletion of the father table specified by index.
 DbColumn[] getFatherColumns(int index)
          Returns the referenced father columns of a father table whose index is specified.
 DbAbstractTable getFatherTable(int index)
          Returns a father table whose index is specified.
 int getFatherTablesCount()
          Returns the number of referenced father tables.
 DbColumn[] getRefColumns(int index)
          Returns the referencing columns of THIS table for a father table whose index is specified.
 int getSonFatherIndex(int index)
          Returns in which position a son table references THIS table in its constraint.
 DbAbstractTable getSonTable(int index)
          Returns a son table whose index is specified.
 int getSonTablesCount()
          Returns the number of son tables.
 DbAbstractTable getTable()
          Returns the referenced table.
 int getUpdateOperation(int index)
          Returns what kind of operation will be done in case of an update of the father table specified by index.
protected  void initLists()
          Builds all dynamic lists (where clauses, constraints etc.).
protected  void initStaticLists()
          Inits static lists, like indexes of son tables, default values of father tables etc.
 void setAutomaticChecking(boolean value)
          Sets a flag to represent automatic checking at all times.
 void setConstraintEmulation(boolean value)
          Sets the constraint emulation.
 void setSelector(DbSelector pSelector)
          Sets the selector for an INSERT operation.
 void setValueLists(java.util.List pIntoList, java.util.List pFromList)
          Sets the lists to perform INSERT/UPDATE operations.
 void setWhere(DbExpr pWhere)
          Sets the "where" clause for an DELETE/UPDATE operation.
abstract  int update(int operation)
          Actually updates the table and all of its son tables.
protected  int updateThis(int operation)
          Performs base update.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INSERT_OPERATION

public static final int INSERT_OPERATION
It represents an INSERT operation.

See Also:
Constant Field Values

DELETE_OPERATION

public static final int DELETE_OPERATION
It represents a DELETE operation.

See Also:
Constant Field Values

UPDATE_OPERATION

public static final int UPDATE_OPERATION
It represents a normal UPDATE operation.

See Also:
Constant Field Values

UPDATE_CASCADE

protected static final int UPDATE_CASCADE
It means that an UPDATE-CASCADE operation is outgoing. It is protected because it is useful only for internal checks-updates.

See Also:
Constant Field Values

DO_NOTHING

public static final int DO_NOTHING
It means that, in case of an UPDATE/DELETE operation, the table MUST NOT do anything to repair itself.

See Also:
Constant Field Values

SET_NULL

public static final int SET_NULL
It means that, in case of an UPDATE/DELETE operation, the constraint MUST set an array of fields in involved rows to NULL values, to repair referenced table.

See Also:
Constant Field Values

SET_DEFAULT

public static final int SET_DEFAULT
It means that, in case of an UPDATE/DELETE operation, the constraint MUST set an array of fields in involved rows to DEFAULT values, to repair referenced table.

See Also:
Constant Field Values

CASCADE

public static final int CASCADE
It means that, in case of an UPDATE/DELETE operation, the constraint MUST cascade the operation, that is updating/deleting in cascade all involved rows.

See Also:
Constant Field Values

table

protected DbAbstractTable table
The referenced table to check.


where

protected DbExpr where
A DbExpr clause used int DELETE/UPDATE operations.


selector

protected DbSelector selector
A selector used in INSERT operations.


intoList

protected java.util.List intoList
A list of columns to put data into (used in INSERT/UPDATE operations).


fromList

protected java.util.List fromList
A list of values/expression to put data into (used in INSERT/UPDATE operations).


automaticChecking

protected boolean automaticChecking
true: check is done at all times; false: check is performed only with an explicit call to check.


constraintEmulation

protected boolean constraintEmulation
true: emulation made as needed; false: emulation is not made.


currentOperation

protected int currentOperation
Represents the current operation. (INSERT, DELETE, UPDATE, UPDATE-CASCADE).


canDoUpdate

protected boolean canDoUpdate
true: update can be done. false: update CANNOT be done. It is put to false whenever the DBMS can perform referencial integrity check by itself; e.g. MySQL cannot, so it must be done in a "software" way.


fatherTables

protected DbAbstractTable[] fatherTables
It contains all father tables of the one referenced by this constraint.


sonTables

protected DbAbstractTable[] sonTables
It contains all son tables of the one referenced by this constraint.


updateOperations

protected java.lang.Integer[] updateOperations
It contains the update operations for each father table.


deleteOperations

protected java.lang.Integer[] deleteOperations
It contains the delete operations for each father table.


fatherTablesColumns

protected DbColumn[][] fatherTablesColumns
It contains the father keys for each father table.


refColumns

protected DbColumn[][] refColumns
It contains for each father table, the referenced column in contained table. That is each element in fatherTablesColumns, columns of father tables, must be equal to each element in refColumns, columns of THIS table.


fatherColsChildren

protected DbColumn[][] fatherColsChildren
For each son, it contains the referenced column of THIS table. It is built automatically, to ease checking.


refColsChildren

protected DbColumn[][] refColsChildren
For each son it contains the referenced columns of son tables. It is built automatically, to ease checking.


sonIndexes

protected java.lang.Integer[] sonIndexes
For each son, it contains the position in the fatherTables of the son table itself.


refs

protected java.lang.Integer[][] refs
It's an array built at runtime to represent, for each father, the referenced fields if they are specified in intoList for INSERT or UPDATE operations.


searchSets

protected java.util.TreeSet[] searchSets
Contains the search sets used to perform checks for each son table.


sonsWhere

protected DbExpr[] sonsWhere
Contains the "where" clauses for each son.


fatherRecords

protected DbTable fatherRecords
Contains the father records used to find eventually involved rows.


deleteCascadeChildren

protected java.lang.Integer[] deleteCascadeChildren
Currently unused.


updateCascadeChildren

protected java.lang.Integer[] updateCascadeChildren
Currently unused.


constraints

protected DbConstraint[] constraints
Contains an array of constraint to perform cascade-check and cascade-updates.


defaultValues

protected java.lang.Object[][] defaultValues
Contains default values for each referenced field of each father table.


updateIntoLists

protected java.util.List[] updateIntoLists
Contains an array of intoList-like lists, to perform update operations in son tables.


updateFromLists

protected java.util.List[] updateFromLists
Contains an array of fromList-like lists, to perform update operations in son tables.


cloned

protected boolean cloned
true: the object is cloned. false: the object is NOT cloned.

Constructor Detail

DbConstraint

public DbConstraint()
Creates new DbConstraint


DbConstraint

public DbConstraint(DbAbstractTable tbl)
Creates a new DbConstraint for a specified table.

Parameters:
tbl - The table to be checked.
Method Detail

getTable

public DbAbstractTable getTable()
Returns the referenced table.

Returns:
The referenced table.

setWhere

public void setWhere(DbExpr pWhere)
Sets the "where" clause for an DELETE/UPDATE operation.

Parameters:
pWhere - The requested clause.

setSelector

public void setSelector(DbSelector pSelector)
Sets the selector for an INSERT operation.

Parameters:
pSelector - The requested selector.

setValueLists

public void setValueLists(java.util.List pIntoList,
                          java.util.List pFromList)
Sets the lists to perform INSERT/UPDATE operations.

Parameters:
pIntoList - It will be put into intoList.
pFromList - It will be put into fromList.

setAutomaticChecking

public void setAutomaticChecking(boolean value)
Sets a flag to represent automatic checking at all times.

Parameters:
value - true: check is done at all times; false: check is performed only with an explicit call to check.

getAutomaticChecking

public boolean getAutomaticChecking()
Returns the current automatic checking setting.

Returns:
true: check is done at all times; false: check is performed only with an explicit call to check.

setConstraintEmulation

public void setConstraintEmulation(boolean value)
Sets the constraint emulation.

Parameters:
value - true: constraint emulation is done as needed; false: constraint emulation is NEVER done.

getConstraintEmulation

public boolean getConstraintEmulation()
Returns if constraint emulation is enabled.

Returns:
true: constraint emulation is done as needed; false: constraint emulation is NEVER done.

getFatherTablesCount

public int getFatherTablesCount()
Returns the number of referenced father tables.

Returns:
The count of referenced father tables.

getSonTablesCount

public int getSonTablesCount()
Returns the number of son tables.

Returns:
The son tables count.

getFatherTable

public DbAbstractTable getFatherTable(int index)
                               throws DbException
Returns a father table whose index is specified.

Parameters:
index - The index of a father table.
Returns:
The requested father table.
Throws:
DbException - If something goes wrong.

getSonTable

public DbAbstractTable getSonTable(int index)
                            throws DbException
Returns a son table whose index is specified.

Parameters:
index - The index of a son table.
Returns:
The requested son table.
Throws:
DbException - If something goes wrong.

getSonFatherIndex

public int getSonFatherIndex(int index)
                      throws DbException
Returns in which position a son table references THIS table in its constraint.

Parameters:
index - The son table index.
Returns:
The needed index.
Throws:
DbException - If something goes wrong.

getUpdateOperation

public int getUpdateOperation(int index)
                       throws DbException
Returns what kind of operation will be done in case of an update of the father table specified by index.

Parameters:
index - The index of the father table.
Returns:
The requested operation.
Throws:
DbException - If something goes wrong.

getDeleteOperation

public int getDeleteOperation(int index)
                       throws DbException
Returns what kind of operation will be done in case of a deletion of the father table specified by index.

Parameters:
index - The index of the father table.
Returns:
The requested operation.
Throws:
DbException - If something goes wrong.

getFatherColumns

public DbColumn[] getFatherColumns(int index)
                            throws DbException
Returns the referenced father columns of a father table whose index is specified.

Parameters:
index - The index of the father table.
Returns:
An array containing the referenced father columns.
Throws:
DbException - If something goes wrong.

getRefColumns

public DbColumn[] getRefColumns(int index)
                         throws DbException
Returns the referencing columns of THIS table for a father table whose index is specified.

Parameters:
index - The index of the father column.
Returns:
The referencing columns.
Throws:
DbException - If something goes wrong.

getDefaultValues

public java.lang.Object[] getDefaultValues(int index)
                                    throws DbException
Returns an array containing the default values for fields of a father table whose index is specified.

Parameters:
index - The index of the father table.
Returns:
An array containing the default values for referencing columns related to requested father table.
Throws:
DbException - If something goes wrong.

clear

public void clear()
Clears everything.


build

public void build()
           throws DbException
After all settings, it finally builds all static references.

Throws:
DbException - If something goes wrong.

check

public abstract void check(int operation)
                    throws DbException
After setting all dynamic (i.e. operations, lists etc.) information, it checks explicitly if all operations can be done. I suggest to call checkThis method to perform base checking.

Parameters:
operation - The kind of operation (INSERT, UPDATE etc.) that should be done.
Throws:
DbException - If something goes wrong.

update

public abstract int update(int operation)
                    throws DbException
Actually updates the table and all of its son tables. I suggest to call updateThis method to perform base update.

Parameters:
operation - The kind of operation (INSERT, UPDATE etc.) that should be done.
Returns:
It should contain the final result of execute calls to inserters, deleters of updaters.
Throws:
DbException - If something goes wrong.

updateThis

protected int updateThis(int operation)
                  throws DbException
Performs base update. It also checks if automatic checking is set.

Parameters:
operation - The operation to be done.
Returns:
The real result from an inserter, updater, deleter.
Throws:
DbException - If something goes wrong.

checkThis

protected void checkThis(int operation)
                  throws DbException
Performs base checks.

Parameters:
operation - The operations that should be done.
Throws:
DbException - If something goes wrong.

checkFathers

protected void checkFathers()
                     throws DbException
Performs checking for father tables. Used in INSERT and UPDATE operations. N.B. Not used in UPDATE-CASCADE operation.

Throws:
DbException - If checking fails.

checkChildren

protected void checkChildren()
                      throws DbException
Checks recursively each son.

Throws:
DbException - If checking fails.

clone

protected java.lang.Object clone()
                          throws java.lang.CloneNotSupportedException
Clones this object. It is used to build a temporary DbConstraint in self-referencing tables.

Overrides:
clone in class java.lang.Object
Returns:
The cloned object.
Throws:
java.lang.CloneNotSupportedException - Never thrown... usually...

initStaticLists

protected void initStaticLists()
                        throws DbException
Inits static lists, like indexes of son tables, default values of father tables etc.

Throws:
DbException - If something goes wrong.

initLists

protected void initLists()
                  throws DbException
Builds all dynamic lists (where clauses, constraints etc.).

Throws:
DbException - If something goes wrong.