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

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

java.lang.Object
  extended by com.lapetus_ltd._2009.xml.types.XLptsDBCreateTableConstraintType
      extended by com.lapetus_ltd.api.db.xml.types.TLptsDBCreateTableConstraintType

public class TLptsDBCreateTableConstraintType
extends XLptsDBCreateTableConstraintType

Class Description : This initiates the constraint type for tables of a CREATE statement.


Constraint values are UNIQUE, PRIMARY_KEY, FOREIGN_KEY and CHECK

The following needs to be noted for each type:
UNIQUE cannot have a name and setConstraint cannot be set
PRIMARY KEY can have a name but setConstraint should not be set.
FOREIGN_KEY can have a name and the setConstraint and setReferences should be set as follows:
The setConstraint() requires the name(s) of the table(s) and column(s) for the foreign key correlation.
The setReferences() requires the name(s) of the foreign table(s) and column(s) for the foreign fields above.
CHECK can have a name and setConstraint should contain the database specific checks for the fields in the constraint.
Each database has its own format for the CHECK and reference should be made to the specific SQL manual.
Below is an example of a SQL statement for MySQL showing table constraints:

 CREATE TABLE `myTable`
 (
     `myTableId` INT,
     `myName` VARCHAR(50),
     `mySalary` DECIMAL(10,2),
     `myInsuranceId` VARCHAR(50),
     CONSTRAINT id_pk PRIMARY KEY (`myTableId`),
     CONSTRAINT insuranceId UNIQUE (`myInsuranceId`),
     CONSTRAINT salary_chk CHECK (`myInsuranceId` > 0.00),
     CONSTRAINT name_fk FOREIGN KEY (`myName`) REFERENCES `clients` (`FirstName`)
 )

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);

  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);

  table.addColumn(myName);
  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);

  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);

  table.addColumn(myInsuranceId);

  //Table Constraints
  TLptsDBCreateTableConstraintType primaryCon = new TLptsDBCreateTableConstraintType();
  primaryCon.setName("id_pk");
  primaryCon.setConstraintType(XLptsDBCreateTableConstraintParameterType.PRIMARY_KEY);
  primaryCon.setConstraint("(`myTableId`)");
  table.addConstraint(primaryCon);

  TLptsDBCreateTableConstraintType insuranceCon = new TLptsDBCreateTableConstraintType();
  insuranceCon.setName("insuranceId");
  insuranceCon.setConstraintType(XLptsDBCreateTableConstraintParameterType.UNIQUE);
  insuranceCon.setConstraint("(`myInsuranceId`)");
  table.addConstraint(insuranceCon);

  TLptsDBCreateTableConstraintType salaryCon = new TLptsDBCreateTableConstraintType();
  salaryCon.setName("salary_chk");
  salaryCon.setConstraintType(XLptsDBCreateTableConstraintParameterType.CHECK);
  salaryCon.setConstraint("(`myInsuranceId` > 0.00)");
  table.addConstraint(salaryCon);

  TLptsDBCreateTableConstraintType nameCon = new TLptsDBCreateTableConstraintType();
  nameCon.setName("name_fk");
  nameCon.setConstraintType(XLptsDBCreateTableConstraintParameterType.FOREIGN_KEY);
  nameCon.setConstraint("(`myName`)");
  nameCon.setReferences("`clients` (`FirstName`)");
  table.addConstraint(nameCon);

  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.XLptsDBCreateTableConstraintType
constraint, constraintType, id, name, references
 
Constructor Summary
TLptsDBCreateTableConstraintType()
           This constructor initiates the TableConstraint.
TLptsDBCreateTableConstraintType(XLptsDBCreateTableConstraintType ct)
           This constructor copies the tableConstraints information from an XLptsDBCreateTableConstraintType.
 
Method Summary
 boolean equals(java.lang.Object obj)
           Check for equal TLptsDBCreateTableConstraintType objects.
 
Methods inherited from class com.lapetus_ltd._2009.xml.types.XLptsDBCreateTableConstraintType
getConstraint, getConstraintType, getId, getName, getReferences, setConstraint, setConstraintType, setId, setName, setReferences
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TLptsDBCreateTableConstraintType

public TLptsDBCreateTableConstraintType()

This constructor initiates the TableConstraint.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : Never.

Notes : Default Constructor set id, constraintType as PRIMARY_KEY and other variables as empty string.

Example :


  TLptsStatement createStatement = new TLptsStatement(connection,XLptsDBTypeOfStatementType.CREATE);
  TLptsDBCreateStatementRootType rootType = new TLptsDBCreateStatementRootType();
  rootType.setTitle("root");
  TLptsDBCreateStatementTableType table = new TLptsDBCreateStatementTableType();
  table.setTable("newTable");
  rootType.setTable(table);
  TLptsDBCreateStatementTableTemporaryType temporary = new TLptsDBCreateStatementTableTemporaryType();
  temporary.setTemporary(true);
  TLptsDBCreateStatementTableGlobalLocal globalLocal = new TLptsDBCreateStatementTableGlobalLocal();
  globalLocal.setGlobalLocal(XLptsDBCreateStatementTableGlobalLocalType.GLOBAL);
  temporary.setGlobalLocal(globalLocal);
  table.setTemporary(temporary);
  ...
  tableConstraint.setName("foreign_key");
  tableConstraint.setConstraintType(XLptsDBCreateTableConstraintParameterType.FOREIGN_KEY);
  tableConstraint.setConstraint("newTable.newColumn");
  tableConstraint.setReferences("foreignTable.column");
  table.addConstraint(tableConstraint);
  TLptsDBCreateStatementTableCommitType commit = new TLptsDBCreateStatementTableCommitType(table);
  commit.setCommitType(XLptsDBCreateTableCommitType.DELETE);
  table.setCommitType(commit);
  ...
 }
 


TLptsDBCreateTableConstraintType

public TLptsDBCreateTableConstraintType(XLptsDBCreateTableConstraintType ct)

This constructor copies the tableConstraints information from an XLptsDBCreateTableConstraintType.

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 XLptsDBCreateTableConstraintType class.

Example :


 public void example(XLptsDBCreateTableConstraintType ct)
 {
   TLptsDBCreateTableConstraintType tableConstraint = new XLptsDBCreateTableConstraintType(ct);
 }

 

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

equals

public boolean equals(java.lang.Object obj)

Check for equal TLptsDBCreateTableConstraintType objects.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : n/a

Notes : Use this to compare two TLptsDBCreateTableConstraintType objects by there id's.

Example :


 

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


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