use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ConstraintComparator method getFKsDDL.
/**
* get the pk DDL
*
* @param schemaInfo SchemaInfo
* @return pk DDL
*/
public String getFKsDDL(SchemaInfo schemaInfo) {
String tableName = schemaInfo.getClassname();
StringBuffer ddlBuffer = new StringBuffer();
//Get the FK
List<Constraint> fkList = schemaInfo.getFKConstraints();
if (fkList != null) {
for (Constraint fk : fkList) {
ddlBuffer.append(getAddFKDDL(tableName, fk));
ddlBuffer.append(endLineChar).append(StringUtil.NEWLINE);
}
}
return ddlBuffer.toString();
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ConstraintComparator method getSchemaDDL.
/**
* Return DDL of a schema
*
* @param schemaInfo SchemaInfo the given reference of a SChemaInfo object
* @param isContainIndex boolean whether include the index DDL
* @param isVirtual boolean whether be a virtual table
* @return String a string indicates a instance of SchemaInfo
*/
public String getSchemaDDL(SchemaInfo schemaInfo, boolean isContainIndex, boolean isVirtual) {
StringBuffer ddlBuffer = new StringBuffer();
ddlBuffer.append("CREATE TABLE ");
final String tableName = schemaInfo.getClassname().toLowerCase();
if (null == tableName || tableName.equals("")) {
ddlBuffer.append("<class_name>");
} else {
ddlBuffer.append(QuerySyntax.escapeKeyword(tableName));
}
List<String> slist = schemaInfo.getSuperClasses();
if (!slist.isEmpty()) {
ddlBuffer.append(StringUtil.NEWLINE).append("\t\t UNDER ");
for (int i = 0; i < slist.size(); i++) {
if (i != 0) {
ddlBuffer.append(",");
}
ddlBuffer.append(QuerySyntax.escapeKeyword(slist.get(i).toLowerCase()));
}
}
boolean attrBegin = false;
int count = 0;
// class attribute
List<DBAttribute> clist = schemaInfo.getClassAttributes();
if (!clist.isEmpty()) {
for (int i = 0; i < clist.size(); i++) {
DBAttribute classAttr = clist.get(i);
String inherit = classAttr.getInherit();
if (inherit.equalsIgnoreCase(schemaInfo.getClassname())) {
if (count == 0) {
ddlBuffer.append(StringUtil.NEWLINE);
attrBegin = true;
ddlBuffer.append("CLASS ATTRIBUTE(").append(StringUtil.NEWLINE);
} else {
ddlBuffer.append(",").append(StringUtil.NEWLINE);
}
ddlBuffer.append(getClassAttributeDDL(classAttr, isVirtual));
count++;
}
}
if (attrBegin) {
ddlBuffer.append(StringUtil.NEWLINE).append(")").append(StringUtil.NEWLINE);
}
}
// instance attribute
List<SchemaInfo> newSupers = SuperClassUtil.getSuperClasses(databaseInfo, schemaInfo);
Constraint pk = schemaInfo.getPK(newSupers);
List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
count = 0;
attrBegin = false;
List<DBAttribute> nlist = schemaInfo.getAttributes();
if (!nlist.isEmpty()) {
for (int i = 0; i < nlist.size(); i++) {
DBAttribute instanceAttr = nlist.get(i);
String inherit = instanceAttr.getInherit();
String className = schemaInfo.getClassname();
if (StringUtil.isEqualIgnoreCase(inherit, className)) {
if (count == 0) {
if (!attrBegin) {
ddlBuffer.append("(").append(StringUtil.NEWLINE);
attrBegin = true;
}
} else {
ddlBuffer.append(",").append(StringUtil.NEWLINE);
}
ddlBuffer.append(getInstanceAttributeDDL(instanceAttr, pkAttributes, schemaInfo, isVirtual));
count++;
}
}
}
// constraint
List<Constraint> constraintList = new ArrayList<Constraint>();
if (isContainIndex) {
constraintList = schemaInfo.getConstraints();
}
/*Sort the constaints first*/
Collections.sort(constraintList, new ConstraintComparator(tableName));
if (!constraintList.isEmpty()) {
for (int i = 0; i < constraintList.size(); i++) {
Constraint constraint = constraintList.get(i);
List<SchemaInfo> superList = SuperClassUtil.getSuperClasses(databaseInfo, schemaInfo);
if (!schemaInfo.isInSuperClasses(superList, constraint.getName())) {
String contraintDDL = getContraintDDL(tableName, constraint);
if (StringUtil.isNotEmpty(contraintDDL)) {
ddlBuffer.append(",").append(StringUtil.NEWLINE).append(contraintDDL);
}
}
}
}
if (count > 0) {
ddlBuffer.append(StringUtil.NEWLINE).append(")");
}
boolean supportCharset = CompatibleUtil.isSupportCreateDBByCharset(databaseInfo);
if ((isVirtual || supportCharset) && !StringUtil.isEmpty(schemaInfo.getCollation())) {
ddlBuffer.append(" COLLATE ").append(schemaInfo.getCollation()).append(" ");
}
//reuse OID
if (schemaInfo.isReuseOid()) {
ddlBuffer.append(" REUSE_OID ");
}
String resolutionDDL = getResolutionsDDL(schemaInfo.getClassResolutions(), schemaInfo.getResolutions());
ddlBuffer.append(resolutionDDL);
ddlBuffer.append(endLineChar).append(StringUtil.NEWLINE);
if (!constraintList.isEmpty()) {
for (int i = 0; i < constraintList.size(); i++) {
Constraint constraint = constraintList.get(i);
List<SchemaInfo> superList = SuperClassUtil.getSuperClasses(databaseInfo, schemaInfo);
if (!schemaInfo.isInSuperClasses(superList, constraint.getName())) {
String type = constraint.getType();
if ("UNIQUE".equals(type) || "INDEX".equals(type) || "REVERSE INDEX".equals(type) || "REVERSE UNIQUE".equals(type)) {
String indexDDL = getCreateIndexDDL(tableName, constraint);
if (StringUtil.isNotEmpty(indexDDL)) {
ddlBuffer.append(indexDDL);
ddlBuffer.append(endLineChar).append(StringUtil.NEWLINE);
}
}
}
}
}
// partition DDL
List<PartitionInfo> partitionInfoList = schemaInfo.getPartitionList();
String transformToPartitionDDL = getTransformToPartitionDDL(partitionInfoList);
if (transformToPartitionDDL != null) {
ddlBuffer.append(transformToPartitionDDL);
ddlBuffer.append(endLineChar).append(StringUtil.NEWLINE);
}
return ddlBuffer.toString();
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ExportTableDefinitionProgress method getIndexList.
/**
*
* @param schemaInfo
* @return list
*/
public List<Constraint> getIndexList(SchemaInfo schemaInfo) {
// FIXME move this logic to core module
List<Constraint> list = new ArrayList<Constraint>();
List<Constraint> constraints = schemaInfo.getConstraints();
for (Constraint constraint : constraints) {
if (constraint.getType().equals(Constraint.ConstraintType.INDEX.getText()) || constraint.getType().equals(Constraint.ConstraintType.UNIQUE.getText()) || constraint.getType().equals(Constraint.ConstraintType.REVERSEINDEX.getText()) || constraint.getType().equals(Constraint.ConstraintType.REVERSEUNIQUE.getText()) || constraint.getType().equals(Constraint.ConstraintType.PRIMARYKEY.getText())) {
list.add(constraint);
}
}
return list;
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ExportTableDefinitionLayoutType1 method generateTableDetailSheets.
/**
* generate table name sheet
* @param wwb
* @param conn
* @param exportSchemaInfoList
* @param monitor
* @throws Exception
*/
public void generateTableDetailSheets(WritableWorkbook wwb, Connection conn, List<SchemaInfo> exportSchemaInfoList, IProgressMonitor monitor) throws Exception {
int sheetIndex = 1;
for (SchemaInfo schemaInfo : exportSchemaInfoList) {
String tableName = schemaInfo.getClassname();
monitor.subTask(Messages.bind(Messages.exportTableDefinitionProgressTaskWriteTable, tableName));
List<SchemaInfo> supers = SuperClassUtil.getSuperClasses(getProgressObject().getDatabase().getDatabaseInfo(), schemaInfo);
WritableSheet ws = wwb.createSheet(tableName, sheetIndex++);
int rowIndex = 0;
// Title
ws.addCell(new jxl.write.Label(0, rowIndex, Messages.exportTableDefinitionCell10, boldCellStyle));
ws.mergeCells(0, 0, 7, 0);
rowIndex++;
// System name
ws.addCell(new jxl.write.Label(0, rowIndex, Messages.exportTableDefinitionCell11, boldCellStyle));
ws.addCell(new jxl.write.Label(1, rowIndex, "", normalCellStyle));
// Date
ws.addCell(new jxl.write.Label(2, rowIndex, Messages.exportTableDefinitionCell4, boldCellStyle));
ws.addCell(new jxl.write.Label(3, rowIndex, dateString, normalCellStyle));
// Author
ws.addCell(new jxl.write.Label(5, rowIndex, Messages.exportTableDefinitionCell5, boldCellStyle));
ws.addCell(new jxl.write.Label(7, rowIndex, "", normalCellStyle));
ws.mergeCells(3, 1, 4, 1);
ws.mergeCells(5, 1, 6, 1);
rowIndex++;
String tableColumnText = "";
if (getProgressObject().isInstalledMetaTable()) {
SchemaComment tableComment = SchemaCommentHandler.find(getProgressObject().getSchemaCommentMap(), tableName, null);
if (tableComment != null) {
tableColumnText = tableComment.getDescription() == null ? "" : tableComment.getDescription();
}
}
// Table Name
ws.addCell(new jxl.write.Label(0, rowIndex, Messages.exportTableDefinitionCell6, boldCellStyle));
ws.addCell(new jxl.write.Label(1, rowIndex, tableName, normalLeftAlignCellStyle));
ws.mergeCells(1, 2, 7, 2);
rowIndex++;
// Table Description
ws.addCell(new jxl.write.Label(0, rowIndex, Messages.exportTableDefinitionCell27, boldCellStyle));
ws.addCell(new jxl.write.Label(1, rowIndex, tableColumnText, normalLeftAlignCellStyle));
ws.mergeCells(1, 3, 7, 3);
rowIndex++;
// Column ID
ws.addCell(new jxl.write.Label(0, rowIndex, Messages.exportTableDefinitionCell12, boldCellStyle));
// Data type
ws.addCell(new jxl.write.Label(1, rowIndex, Messages.exportTableDefinitionCell14, boldCellStyle));
// Size
ws.addCell(new jxl.write.Label(2, rowIndex, Messages.exportTableDefinitionCell15, boldCellStyle));
// Null
ws.addCell(new jxl.write.Label(3, rowIndex, Messages.exportTableDefinitionCell16, boldCellStyle));
// PK
ws.addCell(new jxl.write.Label(4, rowIndex, Messages.exportTableDefinitionCell17, boldCellStyle));
// FK
ws.addCell(new jxl.write.Label(5, rowIndex, Messages.exportTableDefinitionCell18, boldCellStyle));
// Default
ws.addCell(new jxl.write.Label(6, rowIndex, Messages.exportTableDefinitionCell26, boldCellStyle));
// Column description
ws.addCell(new jxl.write.Label(7, rowIndex, Messages.exportTableDefinitionCell25, boldCellStyle));
rowIndex++;
// column info
for (DBAttribute columnAtt : schemaInfo.getAttributes()) {
String attrName = columnAtt.getName();
String defaultValue = columnAtt.getDefault();
String columnText = "";
if (getProgressObject().isInstalledMetaTable()) {
SchemaComment columnComment = SchemaCommentHandler.find(getProgressObject().getSchemaCommentMap(), tableName, attrName);
if (columnComment != null) {
columnText = columnComment.getDescription() == null ? "" : columnComment.getDescription();
}
}
ws.addCell(new jxl.write.Label(0, rowIndex, attrName, normalLeftAlignCellStyle));
String showType = DataType.getShownType((columnAtt.getType()));
if (showType.indexOf("(") > -1 && showType.endsWith("")) {
showType = showType.substring(0, showType.indexOf("("));
}
ws.addCell(new jxl.write.Label(1, rowIndex, showType, normalLeftAlignCellStyle));
int size = DataType.getSize(columnAtt.getType());
int scale = DataType.getScale(columnAtt.getType());
if (size < 0 && scale < 0) {
ws.addCell(new jxl.write.Label(2, rowIndex, "", normalRightAlignCellStyle));
} else if (scale < 0) {
ws.addCell(new jxl.write.Number(2, rowIndex, size, normalRightAlignCellStyle));
} else {
ws.addCell(new jxl.write.Label(2, rowIndex, Integer.toString(size) + "," + Integer.toString(scale), normalRightAlignCellStyle));
}
//get nullable
boolean isNULL = true;
if (!columnAtt.isClassAttribute()) {
if (columnAtt.getInherit().equals(tableName)) {
Constraint pk = schemaInfo.getPK(supers);
if (null != pk && pk.getAttributes().contains(attrName)) {
isNULL = false;
}
} else {
List<Constraint> pkList = schemaInfo.getInheritPK(supers);
for (Constraint inheritPK : pkList) {
if (inheritPK.getAttributes().contains(attrName)) {
isNULL = false;
}
}
}
}
if (columnAtt.isNotNull()) {
isNULL = false;
}
ws.addCell(new jxl.write.Label(3, rowIndex, isNULL ? "Y" : "", normalCellStyle));
//get pk
boolean isPk = false;
if (!columnAtt.isClassAttribute()) {
if (columnAtt.getInherit().equals(tableName)) {
Constraint pk = schemaInfo.getPK(supers);
if (null != pk && pk.getAttributes().contains(attrName)) {
isPk = true;
}
} else {
List<Constraint> pkList = schemaInfo.getInheritPK(supers);
for (Constraint inheritPK : pkList) {
if (inheritPK.getAttributes().contains(attrName)) {
isPk = true;
}
}
}
}
ws.addCell(new jxl.write.Label(4, rowIndex, isPk ? "Y" : "", normalCellStyle));
//get fk
boolean isFk = false;
for (Constraint fk : schemaInfo.getFKConstraints()) {
for (String columns : fk.getAttributes()) {
if (columns.equals(attrName)) {
isFk = true;
break;
}
}
}
ws.addCell(new jxl.write.Label(5, rowIndex, isFk ? "Y" : "", normalCellStyle));
ws.addCell(new jxl.write.Label(6, rowIndex, defaultValue, normalCellStyle));
ws.addCell(new jxl.write.Label(7, rowIndex, columnText, normalLeftAlignCellStyle));
rowIndex++;
}
// blank
for (int i = 0; i < 8; i++) {
ws.addCell(new jxl.write.Label(i, rowIndex, "", normalCellStyle));
}
rowIndex++;
// index
ws.addCell(new jxl.write.Label(0, rowIndex, Messages.exportTableDefinitionCell20, boldCellStyle));
ws.mergeCells(0, rowIndex, 7, rowIndex);
rowIndex++;
// NO
ws.addCell(new jxl.write.Label(0, rowIndex, Messages.exportTableDefinitionCell21, boldCellStyle));
// Index name
ws.addCell(new jxl.write.Label(1, rowIndex, Messages.exportTableDefinitionCell22, boldCellStyle));
// Column ID
ws.addCell(new jxl.write.Label(3, rowIndex, Messages.exportTableDefinitionCell13, boldCellStyle));
// Order
ws.addCell(new jxl.write.Label(5, rowIndex, Messages.exportTableDefinitionCell23, boldCellStyle));
// Memo
ws.addCell(new jxl.write.Label(6, rowIndex, Messages.exportTableDefinitionCell19, boldCellStyle));
ws.mergeCells(1, rowIndex, 2, rowIndex);
ws.mergeCells(3, rowIndex, 4, rowIndex);
ws.mergeCells(6, rowIndex, 7, rowIndex);
rowIndex++;
List<Constraint> constraints = getProgressObject().getIndexList(schemaInfo);
for (int i = 0; i < constraints.size(); i++) {
Constraint constraint = constraints.get(i);
int columnSize = constraint.getAttributes().size();
// mark current row index
int currentRowIndex = rowIndex;
for (int j = 0; j < columnSize; j++) {
String columnName = constraint.getAttributes().get(j);
ws.addCell(new jxl.write.Number(0, rowIndex, i + 1, normalCellStyle));
ws.addCell(new jxl.write.Label(1, rowIndex, constraint.getName(), normalLeftAlignCellStyle));
ws.addCell(new jxl.write.Label(3, rowIndex, columnName, normalLeftAlignCellStyle));
ws.addCell(new jxl.write.Number(5, rowIndex, j + 1, normalCellStyle));
ws.addCell(new jxl.write.Label(6, rowIndex, "", normalCellStyle));
if (columnSize == 1) {
ws.mergeCells(1, rowIndex, 2, rowIndex);
}
ws.mergeCells(3, rowIndex, 4, rowIndex);
ws.mergeCells(6, rowIndex, 7, rowIndex);
rowIndex++;
}
//if multiple colulmn merge NO/Index Name CELL by vertical logic
if (columnSize > 1) {
ws.mergeCells(0, currentRowIndex, 0, currentRowIndex + columnSize - 1);
ws.mergeCells(1, currentRowIndex, 2, currentRowIndex + columnSize - 1);
}
}
// blank
ws.addCell(new jxl.write.Label(0, rowIndex, "", normalCellStyle));
ws.addCell(new jxl.write.Label(1, rowIndex, "", normalCellStyle));
ws.addCell(new jxl.write.Label(3, rowIndex, "", normalCellStyle));
ws.addCell(new jxl.write.Label(5, rowIndex, "", normalCellStyle));
ws.addCell(new jxl.write.Label(6, rowIndex, "", normalCellStyle));
ws.mergeCells(1, rowIndex, 2, rowIndex);
ws.mergeCells(3, rowIndex, 4, rowIndex);
ws.mergeCells(6, rowIndex, 7, rowIndex);
rowIndex++;
// DDL
ws.addCell(new jxl.write.Label(0, rowIndex, Messages.exportTableDefinitionCell24, boldCellStyle));
ws.mergeCells(0, rowIndex, 7, rowIndex);
rowIndex++;
String ddl = getProgressObject().getDDL(schemaInfo);
ws.addCell(new jxl.write.Label(0, rowIndex, ddl, normalLeftAlignCellStyle));
ws.mergeCells(0, rowIndex, 7, rowIndex);
ws.setRowView(0, 500);
int lineNumbner = ddl.split(StringUtil.NEWLINE).length;
ws.setRowView(rowIndex, lineNumbner * 350);
// column width
ws.setColumnView(0, 18);
ws.setColumnView(1, 20);
ws.setColumnView(2, 13);
ws.setColumnView(3, 9);
ws.setColumnView(4, 9);
ws.setColumnView(5, 9);
ws.setColumnView(6, 10);
ws.setColumnView(7, 29);
monitor.worked(1);
}
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ConstraintTest method testConstraint.
/**
* Test Constraint
*/
public final void testConstraint() {
String name = "name";
String type = "type";
int keyCount = 6;
// String
List<String> classAttributes = new ArrayList<String>();
// String
List<String> attributes = new ArrayList<String>();
// String
List<String> rules = new ArrayList<String>();
// test getters and setters
Constraint constraintNo = new Constraint(false);
Constraint constraintYes = new Constraint(name, type);
constraintYes.getAttributes();
constraintYes.setName(name);
constraintYes.setType(type);
constraintYes.setKeyCount(keyCount);
constraintYes.setAttributes(attributes);
constraintYes.setRules(rules);
assertEquals(constraintYes.getName(), name);
assertEquals(constraintYes.getType(), type);
assertEquals(constraintYes.getKeyCount(), keyCount);
assertEquals(constraintYes.getAttributes(), attributes);
assertEquals(constraintYes.getRules(), rules);
// test public boolean equals(Object obj)
assertTrue(constraintYes.equals(constraintYes));
assertFalse(constraintYes.equals(null));
assertFalse(constraintYes.equals("other object"));
constraintYes.setName(null);
Constraint constraintOther = new Constraint(name, type);
constraintOther.setName(name);
constraintOther.setType(type);
constraintOther.setKeyCount(keyCount);
constraintOther.setAttributes(attributes);
constraintOther.setRules(rules);
constraintYes.equals(constraintOther);
constraintYes.setName("name1");
constraintYes.equals(constraintOther);
constraintYes.setType(null);
constraintYes.equals(constraintOther);
constraintYes.setType("type1");
constraintYes.equals(constraintOther);
constraintYes.setAttributes(null);
constraintYes.equals(constraintOther);
constraintYes.setRules(null);
constraintYes.equals(constraintOther);
// test public int hashCode()
constraintYes.hashCode();
// test public SerialInfo clone()
Constraint clonedSerialInfo = constraintYes.clone();
assertEquals(constraintYes, clonedSerialInfo);
constraintYes.addAttribute("attributename");
constraintYes.addClassAttribute("classAttributeName");
constraintYes.addRule("ruleName");
constraintYes.toString();
constraintYes.getDefaultName("tableName");
constraintYes.getReferencedTable();
constraintNo.setName(name);
classAttributes.add("class");
}
Aggregations