Search in sources :

Example 26 with Constraint

use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.

the class ConstraintComparator method getIndexDDL.

/**
	 * Get the index DDL
	 *
	 * @param schemaInfo SchemaInfo
	 * @return The index related DDL
	 */
public String getIndexDDL(SchemaInfo schemaInfo) {
    String tableName = schemaInfo.getClassname();
    StringBuffer ddlBuffer = new StringBuffer();
    //Get the PK
    List<SchemaInfo> allSupers = SuperClassUtil.getSuperClasses(databaseInfo, schemaInfo);
    Constraint pk = schemaInfo.getPK(allSupers);
    if (pk != null) {
        List<String> pkAttributes = pk.getAttributes();
        if (pkAttributes != null && pkAttributes.size() > 0) {
            ddlBuffer.append(getAddPKDDL(tableName, pkAttributes, pk.getName()));
            ddlBuffer.append(endLineChar);
            ddlBuffer.append(StringUtil.NEWLINE);
            ddlBuffer.append(StringUtil.NEWLINE);
        }
    }
    //Get the FK
    List<Constraint> fkList = schemaInfo.getFKConstraints();
    if (fkList != null) {
        for (Constraint fk : fkList) {
            ddlBuffer.append(getAddFKDDL(tableName, fk));
            ddlBuffer.append(endLineChar);
            ddlBuffer.append(StringUtil.NEWLINE);
            ddlBuffer.append(StringUtil.NEWLINE);
        }
    }
    //Get the index
    List<Constraint> constaintList = schemaInfo.getConstraints();
    if (!constaintList.isEmpty()) {
        for (int i = 0; i < constaintList.size(); i++) {
            Constraint constraint = constaintList.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);
                        ddlBuffer.append(StringUtil.NEWLINE);
                        ddlBuffer.append(StringUtil.NEWLINE);
                    }
                }
            }
        }
    }
    return ddlBuffer.toString();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 27 with Constraint

use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.

the class ConstraintComparator method getCreateIndexDDL.

/**
	 * Return DDL of adding index, unique, reverse index or reverse unique
	 *
	 * @param tableName String the given table name
	 * @param indexConstaint Constraint the give reference of the Constraint
	 *        object
	 * @return String a string indicates the info of index
	 */
private String getCreateIndexDDL(String tableName, Constraint indexConstaint) {
    String type = indexConstaint.getType();
    String defaultName = indexConstaint.getDefaultName(tableName);
    StringBuffer bf = new StringBuffer();
    bf.append("CREATE ");
    if ("INDEX".equals(type)) {
        bf.append("INDEX");
    } else if ("REVERSE INDEX".equals(type)) {
        bf.append("REVERSE INDEX");
    } else if ("UNIQUE".equals(type)) {
        bf.append("UNIQUE INDEX");
    } else if ("REVERSE UNIQUE".equals(type)) {
        bf.append("REVERSE UNIQUE INDEX");
    }
    //TODO: it is needed to handle an exception if the type is not in above conditions.
    if (StringUtil.isNotEmpty(indexConstaint.getName())) {
        bf.append(" ").append(QuerySyntax.escapeKeyword(indexConstaint.getName()));
    } else {
        bf.append(" ").append(QuerySyntax.escapeKeyword(defaultName));
    }
    List<String> rules = indexConstaint.getRules();
    String[][] columnsRuleArray = new String[rules.size()][2];
    if ("UNIQUE".equals(type) || "INDEX".equals(type) || "REVERSE UNIQUE".equals(type) || "REVERSE INDEX".equals(type)) {
        for (int i = 0; i < rules.size(); i++) {
            String rule = rules.get(i);
            String[] strs = rule.trim().split(" ");
            if (strs[0].indexOf("(") > 0) {
                strs[0] = QuerySyntax.escapeKeyword(strs[0].substring(0, strs[0].indexOf("("))) + strs[0].substring(strs[0].indexOf("("));
            } else {
                strs[0] = QuerySyntax.escapeKeyword(strs[0]);
            }
            if (strs.length > 1) {
                columnsRuleArray[i][0] = strs[0];
                columnsRuleArray[i][1] = strs[1];
            } else {
                columnsRuleArray[i][0] = strs[0];
                columnsRuleArray[i][1] = "";
            }
        }
    }
    makeIndexColumns(bf, tableName, columnsRuleArray);
    String description = indexConstaint.getDescription();
    if (description != null) {
        description = StringUtil.escapeQuotes("'" + description + "'");
        bf.append(String.format(" COMMENT %s", description));
    }
    return bf.toString();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint)

Example 28 with Constraint

use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.

the class ConstraintComparator method appendAlterAttributeDDL.

/**
	 * Append the changed attribute DDL
	 *
	 * @param oldSchemaInfo the old instance of SchemaInfo
	 * @param newSchemaInfo the new instance of SchemaInfo
	 * @param oldSupers the list which includes the old instance of SchemaInfo
	 * @param newSupers the list which includes the new instance of SchemaInfo
	 * @param attrMap the map which includes all the attribute
	 * @param ddlBuffer the object of StringBuffer
	 * @param tableName the table name
	 * @param changeLog the object of SchemeChangeLog
	 */
private void appendAlterAttributeDDL(SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, List<SchemaInfo> oldSupers, List<SchemaInfo> newSupers, Map<String, String> attrMap, String tableName, SchemaChangeLog changeLog, DDLGenerator generator) {
    boolean isClassAttr = false;
    if (changeLog.getType() == SchemeInnerType.TYPE_CLASSATTRIBUTE) {
        isClassAttr = true;
    } else {
        isClassAttr = false;
    }
    if (StringUtil.isEmpty(changeLog.getOldValue())) {
        // add [class] column
        DBAttribute newAttr = newSchemaInfo.getDBAttributeByName(changeLog.getNewValue(), isClassAttr);
        String addDDL = null;
        if (isClassAttr) {
            addDDL = getAddClassColumnDDL(tableName, newAttr) + endLineChar + StringUtil.NEWLINE;
        } else {
            // only new added attribute and after version 8.4.0 support to
            // reorder
            boolean isSupportReorderColumn = CompatibleUtil.isSupportReorderColumn(databaseInfo);
            if (isSupportReorderColumn) {
                return;
            }
            Constraint pk = newSchemaInfo.getPK(newSupers);
            List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
            addDDL = getAddColumnDDL(tableName, newAttr, pkAttributes, newSchemaInfo) + endLineChar + StringUtil.NEWLINE;
        }
        generator.addSchemaDDLMode(DDLGenerator.TYPE_ADD_ATTR, newAttr, addDDL);
    } else if (StringUtil.isEmpty(changeLog.getNewValue())) {
        // drop [class] column
        DBAttribute oldAttr = oldSchemaInfo.getDBAttributeByName(changeLog.getOldValue(), isClassAttr);
        String attrName = oldAttr.getName();
        StringBuffer dropDDL = new StringBuffer();
        if (isClassAttr) {
            dropDDL.append(getDropClassColumnDDL(tableName, attrName)).append(endLineChar).append(StringUtil.NEWLINE);
        } else {
            dropDDL.append(getDropColumnDDL(tableName, attrName)).append(endLineChar).append(StringUtil.NEWLINE);
        }
        generator.addSchemaDDLMode(DDLGenerator.TYPE_DROP_ATTR, oldAttr, dropDDL.toString());
    } else {
        // edit column
        DBAttribute oldAttr = oldSchemaInfo.getDBAttributeByName(changeLog.getOldValue(), isClassAttr);
        DBAttribute newAttr = newSchemaInfo.getDBAttributeByName(changeLog.getNewValue(), isClassAttr);
        String editDDL = getAlterAttrDDL(oldAttr, newAttr, oldSchemaInfo, newSchemaInfo, oldSupers, newSupers, isClassAttr, attrMap, tableName, generator);
        generator.addSchemaDDLMode(DDLGenerator.TYPE_EDIT_ATTR, newAttr, editDDL);
    }
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute)

Example 29 with Constraint

use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.

the class ConstraintComparator method getIndexsDDL.

/**
	 * get the pk DDL
	 *
	 * @param schemaInfo SchemaInfo
	 * @return pk DDL
	 */
public String getIndexsDDL(SchemaInfo schemaInfo) {
    String tableName = schemaInfo.getClassname();
    StringBuffer ddlBuffer = new StringBuffer();
    //Get the index
    List<Constraint> constaintList = schemaInfo.getConstraints();
    if (!constaintList.isEmpty()) {
        for (int i = 0; i < constaintList.size(); i++) {
            Constraint constraint = constaintList.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);
                        ddlBuffer.append(StringUtil.NEWLINE);
                        ddlBuffer.append(StringUtil.NEWLINE);
                    }
                }
            }
        }
    }
    return ddlBuffer.toString();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 30 with Constraint

use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.

the class FKTableViewerLabelProvider method getColumnText.

/**
	 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object,
	 *      int)
	 * @param element the object representing the entire row, or
	 *        <code>null</code> indicating that no input object is set in the
	 *        viewer
	 * @param columnIndex the zero-based index of the column in which the label
	 *        appears
	 * @return String or or <code>null</code> if there is no text for the given
	 *         object at columnIndex
	 */
public String getColumnText(Object element, int columnIndex) {
    Constraint fk = (Constraint) element;
    String refTable = null;
    String delRule = null;
    String updateRule = null;
    String cacheRule = null;
    List<String> rules = fk.getRules();
    for (String rule : rules) {
        String refStr = "REFERENCES ";
        String delStr = "ON DELETE ";
        String updStr = "ON UPDATE ";
        String cacheStr = "ON CACHE OBJECT ";
        if (rule.startsWith(refStr)) {
            refTable = rule.replace(refStr, "");
        } else if (rule.startsWith(delStr)) {
            delRule = rule.replace(delStr, "");
        } else if (rule.startsWith(updStr)) {
            updateRule = rule.replace(updStr, "");
        } else if (rule.startsWith(cacheStr)) {
            cacheRule = rule.replace(cacheStr, "");
        }
    }
    switch(columnIndex) {
        case 0:
            return fk.getName() == null ? "" : fk.getName();
        case 1:
            List<String> columns = fk.getAttributes();
            StringBuffer bf = new StringBuffer();
            int count = 0;
            for (String column : columns) {
                if (count != 0) {
                    bf.append(",");
                }
                bf.append(column);
                count++;
            }
            return bf.toString();
        case 2:
            return refTable;
        case 3:
            //get reference table's PK
            if (null == refTable) {
                return null;
            }
            SchemaInfo refSchema = getRefedTable(refTable);
            List<SchemaInfo> refSupers = getRefedSupper(refSchema);
            Constraint refPK = refSchema.getPK(refSupers);
            if (refPK == null) {
                return null;
            } else {
                List<String> refPKAttrs = refPK.getAttributes();
                StringBuffer bf2 = new StringBuffer();
                int count2 = 0;
                for (String column : refPKAttrs) {
                    if (count2 != 0) {
                        bf2.append(",");
                    }
                    bf2.append(column);
                    count2++;
                }
                return bf2.toString();
            }
        case 4:
            return updateRule;
        case 5:
            return delRule;
        case 6:
            return cacheRule;
        default:
            break;
    }
    return null;
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Aggregations

Constraint (com.cubrid.common.core.common.model.Constraint)90 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)47 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)37 ArrayList (java.util.ArrayList)27 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)11 TableItem (org.eclipse.swt.widgets.TableItem)10 ERTableColumn (com.cubrid.common.ui.er.model.ERTableColumn)9 List (java.util.List)9 SchemaChangeLog (com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeLog)7 HashMap (java.util.HashMap)6 PartitionInfo (com.cubrid.common.core.common.model.PartitionInfo)5 ERWinSchemaInfo (com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo)4 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)4 Node (org.w3c.dom.Node)4 DBResolution (com.cubrid.common.core.common.model.DBResolution)3 SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)3 ERSchema (com.cubrid.common.ui.er.model.ERSchema)3 IOException (java.io.IOException)3 SQLException (java.sql.SQLException)3 NodeList (org.w3c.dom.NodeList)3