Copyright 2009,2010, Lapetus Systems Ltd. (All rights reserved)

com.lapetus_ltd.api.db.xml.types
Class TLptsDBCreateColumnConstraintType

java.lang.Object
  extended by com.lapetus_ltd._2009.xml.types.XLptsDBCreateColumnConstraintType
      extended by com.lapetus_ltd.api.db.xml.types.TLptsDBCreateColumnConstraintType

public class TLptsDBCreateColumnConstraintType
extends XLptsDBCreateColumnConstraintType

Class Description : Initialisation class for CREATE statement constraints.

Use this class as depicted below to initiate the XLptsDBCreateColumnConstraintType, which is the actual
constraint information for a CREATE statement column.

Constraint values are NOT NULL, UNIQUE, PRIMARY KEY, REFERENCES, CHECK
The following needs to be noted for each type:
NOT NULL and UNIQUE cannot have a name. setConstraint, onDelete and onUpdate should not be set.
PRIMARY KEY can have a name but setConstraint, onDelete and onUpdate should not be set.
CHECK can have a name and setConstraint should be set. onDelete and onUpdate cannot be set.
REFERENCES can have a name. The table and column names must be set to the referenced column/table.

Below is an example of the actual SQL statement for a CREATE for MySQL with the constraints on the columns:

  CREATE TABLE `myTable`
   (
     myTableID INT PRIMARY KEY,
     myName VARCHAR(50) REFERENCES `clients` (`FirstName`),
     hasChild CHAR(1) CHECK (hasChild IN ('Y','N')),
     mySalary DECIMAL(10,2) NOT NULL,
     myInsuranceID VARCHAR(50) UNIQUE
   )

Now look at the dbJAPI code for the same CREATE statement:
  TLptsStatement createStatement = new TLptsStatement(connection,XLptsDBTypeOfStatementType.CREATE);
  TLptsDBCreateStatementRootType rootType = new TLptsDBCreateStatementRootType();
  rootType.setTitle("root");

  TLptsDBCreateStatementTableType table = new TLptsDBCreateStatementTableType();
  table.setTable("myTable");
  rootType.setTable(table);
  TLptsDBCreateStatementColumnType myTableID  = new TLptsDBCreateStatementColumnType();
  myTableID.setColumn("myTableID");

  TLptsDBCreateStatementColumnSqlType myTableIdSqlType = new TLptsDBCreateStatementColumnSqlType(myTableID);
  myTableIdSqlType.setSqlType(java.sql.Types.INTEGER);
  myTableID.setSqlType(myTableIdSqlType);

  TLptsDBCreateColumnConstraintType myTableIdColumnConstraintType = new TLptsDBCreateColumnConstraintType();
  myTableIdColumnConstraintType.setColumnId(myTableID.getId());
  myTableIdColumnConstraintType.setConstraintType(XLptsDBCreateColumnConstraintParameterType.PRIMARY_KEY);
  myTableID.addConstraint(myTableIdColumnConstraintType);

  table.addColumn(myTableID);
  TLptsDBCreateStatementColumnType myName  = new TLptsDBCreateStatementColumnType();
  myName.setColumn("myName");

  TLptsDBCreateStatementColumnSqlType myNameSqlType = new TLptsDBCreateStatementColumnSqlType(myName);
  myNameSqlType.setSqlType(java.sql.Types.VARCHAR);
  myName.setSqlType(myNameSqlType);

  TLptsDBCreateStatementColumnSizeType myNameSize = new TLptsDBCreateStatementColumnSizeType(myName);
  myNameSize.setSize1("50");
  myName.setSizes(myNameSize);

  TLptsDBCreateColumnConstraintType myNameColumnConstraintType = new TLptsDBCreateColumnConstraintType();
  myNameColumnConstraintType.setColumnId(myName.getId());
  myNameColumnConstraintType.setConstraintType(XLptsDBCreateColumnConstraintParameterType.REFERENCES);
  myNameColumnConstraintType.setTable("clients");
  myNameColumnConstraintType.setColumn("FirstName");
  myName.addConstraint(myNameColumnConstraintType);

  table.addColumn(myName);
  TLptsDBCreateStatementColumnType hasChild  = new TLptsDBCreateStatementColumnType();
  hasChild.setColumn("hasChild");

  TLptsDBCreateStatementColumnSqlType hasChildSqlType = new TLptsDBCreateStatementColumnSqlType(hasChild);
  hasChildSqlType.setSqlType(java.sql.Types.CHAR);
  hasChild.setSqlType(hasChildSqlType);

  TLptsDBCreateStatementColumnSizeType hasChildSize = new TLptsDBCreateStatementColumnSizeType(hasChild);
  hasChildSize.setSize1("1");
  hasChild.setSizes(hasChildSize);

  TLptsDBCreateColumnConstraintType hasChildColumnConstraintType = new TLptsDBCreateColumnConstraintType();
  hasChildColumnConstraintType.setColumnId(hasChild.getId());
  hasChildColumnConstraintType.setConstraintType(XLptsDBCreateColumnConstraintParameterType.CHECK);
  hasChildColumnConstraintType.setConstraint("(hasChild IN ('Y','N'))");
  hasChild.addConstraint(hasChildColumnConstraintType);

  table.addColumn(hasChild);
  TLptsDBCreateStatementColumnType mySalary  = new TLptsDBCreateStatementColumnType();
  mySalary.setColumn("mySalary");

  TLptsDBCreateStatementColumnSqlType mySalarySqlType = new TLptsDBCreateStatementColumnSqlType(mySalary);
  mySalarySqlType.setSqlType(java.sql.Types.DECIMAL);
  mySalary.setSqlType(mySalarySqlType);

  TLptsDBCreateStatementColumnSizeType mySalarySize = new TLptsDBCreateStatementColumnSizeType(mySalary);
  mySalarySize.setSize1("10");
  mySalarySize.setSize2("2");
  mySalary.setSizes(mySalarySize);

  TLptsDBCreateColumnConstraintType mySalaryColumnConstraintType = new TLptsDBCreateColumnConstraintType();
  mySalaryColumnConstraintType.setColumnId(mySalary.getId());
  mySalaryColumnConstraintType.setConstraintType(XLptsDBCreateColumnConstraintParameterType.NOT_NULL);
  mySalary.addConstraint(mySalaryColumnConstraintType);

  table.addColumn(mySalary);
  TLptsDBCreateStatementColumnType myInsuranceId  = new TLptsDBCreateStatementColumnType();
  myInsuranceId.setColumn("myInsuranceId");

  TLptsDBCreateStatementColumnSqlType myInsuranceIdSqlType = new TLptsDBCreateStatementColumnSqlType(myInsuranceId);
  myInsuranceIdSqlType.setSqlType(java.sql.Types.VARCHAR);
  myInsuranceId.setSqlType(myInsuranceIdSqlType);

  TLptsDBCreateStatementColumnSizeType myInsuranceIdSize = new TLptsDBCreateStatementColumnSizeType(myInsuranceId);
  myInsuranceIdSize.setSize1("50");
  myInsuranceId.setSizes(myInsuranceIdSize);

  TLptsDBCreateColumnConstraintType myInsuranceIdColumnConstraintType = new TLptsDBCreateColumnConstraintType();
  myInsuranceIdColumnConstraintType.setColumnId(myInsuranceId.getId());
  myInsuranceIdColumnConstraintType.setConstraintType(XLptsDBCreateColumnConstraintParameterType.UNIQUE);
  myInsuranceId.addConstraint(myInsuranceIdColumnConstraintType);

  table.addColumn(myInsuranceId);

  createStatement.setCreateRootItem(rootType);
 

Wow, that seems to be a bit long winded for that little bit of SQL above. Yes it is, but it gives you the ability
to easily incorporate it into a GUI tree mechanism. It allows for the tree renderer to use the "instanceof" for the
different class create types and organise the tree, field and rendering capabilities. The create statement dialog
of dbJAPI is build around this and it is actually a very affective method of dealing with creation.
Yes, tt is long winded, but at the same time it is simple and provides great integration capabilities.
There is also the capability of putting the above SQL string directly into TLptsStatement.setSqlStatementUser(java.lang.String)
and setting XLptsDBStatementType.setSqlStatementModified(boolean) to true.

$LastChangedRevision: 1165 $
$LastChangedDate:: 2010-11-03 10:37:03#$


Field Summary
 
Fields inherited from class com.lapetus_ltd._2009.xml.types.XLptsDBCreateColumnConstraintType
column, columnId, constraint, constraintType, id, name, onDelete, onUpdate, table
 
Constructor Summary
TLptsDBCreateColumnConstraintType()
           This constructor initiates the Column Constraint Type.
TLptsDBCreateColumnConstraintType(java.lang.String columnId)
           This constructor initiates the Column Constraint Type with a column id.
TLptsDBCreateColumnConstraintType(XLptsDBCreateColumnConstraintType ct)
           This constructor creates a new column constraint class from an existing XLptsDBCreateColumnConstraintType.
 
Method Summary
 boolean equals(java.lang.Object obj)
           Overrides the Object equals function for TLptsDBCreateColumnConstraintType objects.
 
Methods inherited from class com.lapetus_ltd._2009.xml.types.XLptsDBCreateColumnConstraintType
getColumn, getColumnId, getConstraint, getConstraintType, getId, getName, getOnDelete, getOnUpdate, getTable, setColumn, setColumnId, setConstraint, setConstraintType, setId, setName, setOnDelete, setOnUpdate, setTable
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TLptsDBCreateColumnConstraintType

public TLptsDBCreateColumnConstraintType()

This constructor initiates the Column Constraint Type.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Never.

Notes :
Default Constructor sets constraintType to NOT_NULL, onDelete and onUpdate to NO_ACTION and the other variables as empty stings.

Example :


   //Create the statement and set the Type of Statement to CREATE.
   TLptsStatement createStatement = new TLptsStatement(connection,XLptsDBTypeOfStatementType.CREATE);
   //Get the rootType
   TLptsDBCreateStatementRootType rootType = new TLptsDBCreateStatementRootType();
   ...
   //Some DB's allow column Constraints. A column can have more than one constraint
   TLptsDBCreateColumnConstraintType columnConstraintType = new TLptsDBCreateColumnConstraintType();
   //Set column Parameters
   columnConstraintType.setColumnId(idColumn.getId());
   //Set the name of the Constraint
   columnConstraintType.setName("prim_key");
 
   columnConstraintType.setConstraintType(XLptsDBCreateColumnConstraintParameterType.PRIMARY_KEY);
   //columnConstraintType.setName("ID_PRI_CONSTRAINT");
   //columnConstraintType.setConstraint("id");
   //columnConstraintType.setColumn("id_of_refTable");
   //columnConstraintType.setTable("refTable");
   //columnConstraintType.setOnDelete(XLptsDBCreateColumnConstraintOnDeleteUpdateType.CASCADE);
   //columnConstraintType.setOnUpdate(XLptsDBCreateColumnConstraintOnDeleteUpdateType.RESTRICT);
   //At last we add the columnConstraint to the constraint list of the column
   idColumn.addConstraint(columnConstraintType);

 


TLptsDBCreateColumnConstraintType

public TLptsDBCreateColumnConstraintType(java.lang.String columnId)

This constructor initiates the Column Constraint Type with a column id.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Never.

Notes : Use this constructor to set Column id in a new constraint.

Example :


   //Create the statement and set the Type of Statement to CREATE.
   TLptsStatement createStatement = new TLptsStatement(connection,XLptsDBTypeOfStatementType.CREATE);
   //Get the rootType
   TLptsDBCreateStatementRootType rootType = new TLptsDBCreateStatementRootType();
   ...
   //Some DB's allow column Constraints. A column can have more than one constraint
   TLptsDBCreateColumnConstraintType columnConstraintType = new TLptsDBCreateColumnConstraintType(idColumn.getId());
   //Set the name of the Constraint
   columnConstraintType.setName("prim_key");

   columnConstraintType.setConstraintType(XLptsDBCreateColumnConstraintParameterType.PRIMARY_KEY);
   //columnConstraintType.setName("ID_PRI_CONSTRAINT");
   //columnConstraintType.setConstraint("id");
   //columnConstraintType.setColumn("id_of_refTable");
   //columnConstraintType.setTable("refTable");
   //columnConstraintType.setOnDelete(XLptsDBCreateColumnConstraintOnDeleteUpdateType.CASCADE);
   //columnConstraintType.setOnUpdate(XLptsDBCreateColumnConstraintOnDeleteUpdateType.RESTRICT);
   //At last we add the columnConstraint to the constraint list of the column
   idColumn.addConstraint(columnConstraintType);

 

Parameters:
columnId - the column id of the constraint

TLptsDBCreateColumnConstraintType

public TLptsDBCreateColumnConstraintType(XLptsDBCreateColumnConstraintType ct)

This constructor creates a new column constraint class from an existing XLptsDBCreateColumnConstraintType.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Never.

Notes : Use this function every time you need to copy or instantiate a type XLptsDBCreateColumnConstraintType class.

Example :


 public void example(XLptsDBCreateColumnConstraintType ct)
 {
   TLptsDBCreateColumnConstraintType constraint = new TLptsDBCreateColumnConstraintType(ct);
 }

 

Parameters:
ct - the class object to copy.
Method Detail

equals

public boolean equals(java.lang.Object obj)

Overrides the Object equals function for TLptsDBCreateColumnConstraintType objects.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : n/a

Notes : compares this class to another TLptsDBCreateColumnConstraintType.

Example :


 TLptsDBCreateColumnConstraintType cct1 = new TLptsDBCreateColumnConstraintType();
 ...
 TLptsDBCreateColumnConstraintType cct2 = new TLptsDBCreateColumnConstraintType();
 ...
 return cct1.equals(cct2);

 

Overrides:
equals in class java.lang.Object
Parameters:
obj - the column constraint
Returns:
true if they are equal, else false.


Copyright 2009,2010, Lapetus Systems Ltd. (All rights reserved)