use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ERAttributeCellModifier method replaceNewConstraintAttributeName.
/**
* Replace new constraint attribute name
*/
private void replaceNewConstraintAttributeName(SchemaInfo schemaInfo, String oldAttr, String newAttr) {
// FIXME move this logic to core module
for (Constraint constraint : schemaInfo.getConstraints()) {
constraint.replaceAttribute(oldAttr, newAttr);
constraint.replaceClassAttribute(oldAttr, newAttr);
/*Replace the rule*/
if (!Constraint.ConstraintType.FOREIGNKEY.equals(constraint.getType())) {
List<String> rulesList = constraint.getRules();
for (int i = 0; i < rulesList.size(); i++) {
String rule = rulesList.get(i);
int index = rule.indexOf(" ");
String attrName = rule.substring(0, index);
String rulePart = rule.substring(index);
if (StringUtil.isEqualNotIgnoreNull(attrName, oldAttr)) {
rulesList.set(i, newAttr + rulePart);
}
}
}
}
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ERAttributeCellModifier method getValue.
public Object getValue(Object element, String property) {
// FIXME move this logic to core module
ERTableColumn erColumn = (ERTableColumn) element;
DBAttribute attr = erColumn.getAttr();
if (StringUtil.isEqual(property, IAttributeColumn.COL_PK)) {
SchemaInfo schemaInfo = editor.getNewSchemaInfo();
if (schemaInfo == null) {
return false;
}
Constraint constraint = schemaInfo.getPK();
if (constraint == null) {
return false;
}
List<String> columns = constraint.getAttributes();
if (columns == null || columns.size() == 0) {
return false;
}
return columns.contains(attr.getName());
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_NAME)) {
return erColumn.getName(isPhysical);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_DATATYPE)) {
if (isPhysical) {
String dataType = attr.getType();
if (dataType.trim().toLowerCase().startsWith("enum")) {
return DataType.getShownType(attr.getType()) + attr.getEnumeration();
}
return DataType.getShownType(attr.getType());
} else {
return erColumn.getShowType(false);
}
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_DEFAULT)) {
String defaultValue = attr.getDefault();
if (defaultValue == null || attr.getAutoIncrement() != null || (StringUtil.isEmpty(defaultValue) && !DataType.isStringType(attr.getType()))) {
return DataType.NULL_EXPORT_FORMAT;
} else {
return defaultValue;
}
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_AUTO_INCREMENT)) {
DBAttribute aiAttr = editor.getNewSchemaInfo().getAutoIncrementColumn();
if (aiAttr != null && aiAttr != attr) {
CommonUITool.openErrorBox(Messages.errCanNotAddAutoincrementAlreadyExists);
return "";
}
SerialInfo serial = attr.getAutoIncrement();
if (serial == null) {
return "";
}
String defaultValue = attr.getDefault();
if (StringUtil.isNotEmpty(defaultValue)) {
return "";
}
return serial.getTableAutoIncrementString();
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_NOT_NULL)) {
return attr.isNotNull();
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_UK)) {
return attr.isUnique();
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_SHARED)) {
return attr.isShared();
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_COLLATION)) {
if (DataType.canUseCollation(attr.getType())) {
String collation = attr.getCollation();
return editor.getCollationIndex(collation);
}
return "";
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_MEMO)) {
return attr.getDescription();
}
return null;
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class ERAttributeCellModifier method modify.
public void modify(Object element, String property, Object value) {
// FIXME move this logic to core module
final TableItem item = (TableItem) element;
if (item == null) {
return;
}
ERTableColumn erColumn = (ERTableColumn) item.getData();
ERTable table = editor.getDialog().getNewERTable();
if (!StringUtil.isEmpty(erColumn.getName())) {
erColumn = table.getColumn(erColumn.getName());
}
DBAttribute attr = erColumn.getAttr();
String oldAttrName = attr.getName();
DBAttribute oldAttribute = null;
if (editor.getOldSchemaInfo() != null) {
oldAttribute = editor.getOldSchemaInfo().getDBAttributeByName(oldAttrName, false);
}
ERTableColumn oldColumn = editor.getDialog().getOldERTable().getColumn(oldAttrName);
if (StringUtil.isEqual(property, IAttributeColumn.COL_PK)) {
SchemaInfo schemaInfo = editor.getNewSchemaInfo();
if (schemaInfo == null) {
return;
}
boolean on = ((Boolean) value).booleanValue();
erColumn.setIsPrimaryKey(on);
if (on) {
Constraint constraint = schemaInfo.getPK();
if (constraint == null) {
constraint = new Constraint("pk", Constraint.ConstraintType.PRIMARYKEY.getText());
schemaInfo.addConstraint(constraint);
}
constraint.addAttribute(attr.getName());
} else {
Constraint constraint = schemaInfo.getPK();
if (constraint == null) {
return;
}
List<String> columns = constraint.getAttributes();
if (columns == null || columns.size() == 0) {
return;
}
boolean isContain = columns.remove(attr.getName());
/*For bug TOOLS-3972 The collumn's setting in Edit Table Inconsistent with the setting in Set Primary Key*/
if (isContain && columns.size() == 0) {
schemaInfo.removeConstraintByName(constraint.getName(), constraint.getType());
}
/*For bug TOOLS-3046 : deal with edit column*/
if (oldAttribute != null && isContain) {
attr.setNotNull(false);
}
}
editor.makeChangeLogForIndex(oldAttrName, attr, oldAttribute);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_NAME)) {
String newName = (String) value;
SchemaInfo schemaInfo = editor.getNewSchemaInfo();
if (schemaInfo == null) {
// TODO Improve error message
CommonUITool.openErrorBox(Messages.errEmptyNameOnEditTableColumn);
return;
}
if (!StringUtil.isEmpty(newName) && isPhysical && !ValidateUtil.isValidIdentifier(newName)) {
CommonUITool.openErrorBox(Messages.errColumnName);
return;
}
List<ERTableColumn> columns = table.getColumns();
for (ERTableColumn col : columns) {
if (StringUtil.isEqualIgnoreCase(col.getName(isPhysical), newName) && erColumn != col) {
CommonUITool.openErrorBox(Messages.errSameNameOnEditTableColumn);
return;
}
}
if (!StringUtil.isEqualIgnoreCase(erColumn.getName(isPhysical), newName)) {
if (isPhysical) {
replaceNewConstraintAttributeName(schemaInfo, attr.getName(), newName);
}
String oldName = erColumn.getName(isPhysical);
erColumn.setName(newName, isPhysical);
if (erColumn.isNew()) {
erColumn.setName(newName, !isPhysical);
for (ERTableColumn col : columns) {
if (StringUtil.isEqualIgnoreCase(col.getName(!isPhysical), newName) && erColumn != col) {
CommonUITool.openErrorBox(Messages.errSameNameOnEditTableColumn);
erColumn.setName(oldName, isPhysical);
erColumn.setName(oldName, !isPhysical);
return;
}
}
}
if (!hasAddedToSchemaInfo(attr)) {
if (!StringUtil.isEmpty(newName)) {
if (!columns.contains(erColumn)) {
erColumn.getAttr().setInherit(editor.getNewSchemaInfo().getClassname());
table.addColumn(erColumn);
editor.removeElementByName(newName);
}
if (!StringUtil.isEmpty(oldAttrName)) {
editor.makeChangeLogForIndex(oldAttrName, erColumn.getAttr(), oldAttribute);
}
}
} else {
editor.getDialog().changeForEditElement(oldAttrName, erColumn, oldColumn);
}
}
ERTableColumn col = null;
if (columns.size() > 0) {
Table columnsTable = editor.getColumnsTable();
col = (ERTableColumn) columnsTable.getItem(columnsTable.getItemCount() - 1).getData();
}
if (!StringUtil.isEmpty(newName) && (col == null || !StringUtil.isEmpty(col.getName(isPhysical)))) {
editor.addNewColumn();
}
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_DATATYPE)) {
String dataTypeRaw = (String) value;
ERSchema schema = erColumn.getERSchema();
if (isPhysical) {
if (dataTypeRaw.equalsIgnoreCase(DataType.getUpperEnumType())) {
dataTypeRaw += "('" + DataType.ENUM_DAFAULT_VALUE + "')";
}
erColumn.setPhysicalDataType(DataType.reviseDataType(dataTypeRaw));
if (!DataType.isIntegerType(attr.getType())) {
attr.setAutoIncrement(null);
}
if (!DataType.canUseCollation(attr.getType())) {
attr.setCollation("");
}
String physicalRealType = erColumn.getRealType();
if (erColumn.getERSchema().hasPhysicalTypeInMap(physicalRealType) || erColumn.isNew()) {
String logicalType = schema.convert2LogicalShowType(physicalRealType);
erColumn.setShowType(logicalType, false);
}
} else {
if (!DataType.DATATYPE_STRING.equalsIgnoreCase(dataTypeRaw)) {
dataTypeRaw = DataType.reviseDataType(dataTypeRaw);
}
erColumn.setShowType(dataTypeRaw, false);
if (erColumn.getERSchema().hasLogicalTypeInMap(dataTypeRaw) || erColumn.isNew()) {
String physicalType = schema.convert2UpPhysicalShowType(dataTypeRaw);
if (DataType.DATATYPE_STRING.equalsIgnoreCase(dataTypeRaw)) {
physicalType = DataType.reviseDataType(physicalType);
}
erColumn.setPhysicalDataType(physicalType);
}
}
editor.getDialog().changeForEditElement(oldAttrName, erColumn, oldColumn);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_DEFAULT)) {
String defaultVal = (String) value;
boolean isStringType = DataType.isStringType(attr.getType());
boolean isEmpty = StringUtil.isEmpty(defaultVal);
boolean isNull = false;
if (defaultVal == null || DataType.NULL_EXPORT_FORMAT.equals(defaultVal) || DataType.VALUE_NULL.equals(defaultVal) || (isEmpty && !isStringType)) {
isNull = true;
}
if (isNull) {
attr.setDefault(null);
} else {
if (attr.getAutoIncrement() != null) {
attr.setDefault(null);
CommonUITool.openErrorBox(Messages.errCanNotSetDefaultOnAI);
return;
}
boolean isConfirmReset = "".equals(defaultVal) && oldAttribute != null && !"".equals(oldAttribute.getDefault());
if (isConfirmReset) {
String confirmResetDef = Messages.confirmResetDef;
if (CommonUITool.openConfirmBox(confirmResetDef)) {
attr.setDefault(null);
} else {
attr.setDefault(defaultVal);
}
} else {
attr.setDefault(defaultVal);
}
}
editor.getDialog().changeForEditElement(oldAttrName, erColumn, oldColumn);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_AUTO_INCREMENT)) {
DBAttribute aiAttr = editor.getNewSchemaInfo().getAutoIncrementColumn();
if (aiAttr != null && aiAttr != attr) {
attr.setAutoIncrement(null);
return;
}
String param = (String) value;
if (StringUtil.isNotEmpty(param)) {
if (!param.matches("\\s*[0-9]+\\s*,\\s*[0-9]+\\s*")) {
CommonUITool.openErrorBox(Messages.errInvalidAutoIncrForm);
return;
}
String defaultValue = attr.getDefault();
if (StringUtil.isNotEmpty(defaultValue)) {
CommonUITool.openErrorBox(Messages.errCanNotSetAIOnDefault);
return;
}
String[] params = param.split(",");
String startVal = params[0].trim();
String incrVal = params[1].trim();
SchemaInfo schemaInfo = editor.getNewSchemaInfo();
SerialInfo serial = new SerialInfo();
serial.setOwner(schemaInfo.getOwner());
serial.setClassName(schemaInfo.getClassname());
serial.setAttName(oldAttrName);
serial.setCacheCount("1");
serial.setCurrentValue(startVal);
serial.setCyclic(false);
serial.setIncrementValue(incrVal);
serial.setMaxValue(String.valueOf(Integer.MAX_VALUE));
serial.setMinValue(startVal);
serial.setStartedValue(startVal);
if (attr.getAutoIncrement() != null && schemaInfo != null && schemaInfo.getAutoIncrementColumn() != null && schemaInfo.getAutoIncrementColumn().getAutoIncrement() != null) {
String oldAI = attr.getAutoIncrement().getTableAutoIncrementString();
String newAI = serial.getTableAutoIncrementString();
if (StringUtil.isEqual(oldAI, newAI)) {
return;
}
}
attr.setAutoIncrement(serial);
} else {
attr.setAutoIncrement(null);
}
editor.changeForEditElement(oldAttrName, attr, oldAttribute);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_NOT_NULL)) {
boolean on = ((Boolean) value).booleanValue();
attr.setNotNull(on);
editor.getDialog().changeForEditElement(oldAttrName, erColumn, oldColumn);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_UK)) {
boolean on = ((Boolean) value).booleanValue();
if (on && attr.isShared()) {
CommonUITool.openErrorBox(Messages.errCanNotUseUkAndSharedOnce);
return;
}
attr.setUnique(on);
editor.getDialog().changeForEditElement(oldAttrName, erColumn, oldColumn);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_SHARED)) {
boolean on = ((Boolean) value).booleanValue();
String defaultValue = attr.getDefault();
if (on && StringUtil.isEmpty(defaultValue)) {
CommonUITool.openErrorBox(Messages.msgInputSharedValue);
return;
}
if (on && attr.isUnique()) {
CommonUITool.openErrorBox(Messages.errCanNotUseUkAndSharedOnce);
return;
}
attr.setShared(on);
editor.getDialog().changeForEditElement(oldAttrName, erColumn, oldColumn);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_COLLATION)) {
String orignCollation = attr.getCollation();
Integer selection = StringUtil.intValue(value.toString(), 0);
if (selection > -1) {
String newCollation = editor.getCollationArray()[selection];
if (!StringUtil.isEqualNotIgnoreNull(orignCollation, newCollation)) {
attr.setCollation(newCollation);
}
}
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_MEMO)) {
attr.setDescription((String) value);
}
editor.loadColumnData();
editor.afterModifyColumn(oldColumn == null ? null : oldColumn.getName(isPhysical), erColumn.getName(isPhysical));
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class EditVirtualTableDialog method openEditIndexDialog.
/**
* Open the index edit dialog with the constraint object
*
* @param editedIndex Constraint
*/
private void openEditIndexDialog(Constraint editedIndex) {
boolean isNewConstraint = true;
for (int i = 0, len = originalConstraints.size(); i < len; i++) {
if (originalConstraints.get(i) == editedIndex) {
isNewConstraint = false;
}
}
SchemaInfo newSchemaInfo = getNewSchemaInfo();
AddIndexDialog dlg = new AddIndexDialog(getShell(), newSchemaInfo, database, editedIndex, isNewConstraint);
int returnCode = dlg.open();
if (returnCode == AddIndexDialog.OK) {
Constraint newIndex = dlg.getIndexConstraint();
if (newIndex == null) {
return;
}
newSchemaInfo.removeConstraintByName(editedIndex.getName(), editedIndex.getType());
newSchemaInfo.addConstraint(newIndex);
// For bug TOOLS-2394 Unique index can't be added again
if (Constraint.ConstraintType.UNIQUE.getText().equals(newIndex.getType()) && newIndex.getAttributes().size() == 1) {
DBAttribute attr = newSchemaInfo.getDBAttributeByName(newIndex.getAttributes().get(0), false);
attr.setUnique(true);
loadColumnData();
}
boolean modifiedUK = Constraint.ConstraintType.UNIQUE.getText().equals(editedIndex.getType()) && editedIndex.getAttributes().size() == 1;
boolean noNewUK = !Constraint.ConstraintType.UNIQUE.getText().equals(newIndex.getType()) || newIndex.getAttributes().size() != 1;
if (modifiedUK && noNewUK) {
String attrName = editedIndex.getAttributes().get(0);
DBAttribute attr = newSchemaInfo.getDBAttributeByName(attrName, false);
if (attr != null) {
attr.setUnique(false);
loadColumnData();
}
}
String key1 = editedIndex.getDefaultName(newSchemaInfo.getClassname()) + "$" + editedIndex.getName();
String key2 = newIndex.getDefaultName(newSchemaInfo.getClassname()) + "$" + newIndex.getName();
SchemaChangeLog changeLog = new SchemaChangeLog(key1, key2, SchemeInnerType.TYPE_INDEX);
schemaChangeMgr.addSchemeChangeLog(changeLog);
indexTableView.setInput(newSchemaInfo);
}
}
use of com.cubrid.common.core.common.model.Constraint in project cubrid-manager by CUBRID.
the class EditVirtualTableDialog method deleteColumn.
private void deleteColumn() {
if (!CommonUITool.openConfirmBox(Messages.msgDeleteColumnConfirm)) {
return;
}
TableItem[] tblItems = columnsTable.getSelection();
if (tblItems.length > 0) {
ERTableColumn column = (ERTableColumn) tblItems[0].getData();
List<ERTableColumn> items = newERTable.getColumns();
if (!items.contains(column)) {
return;
}
}
TableItem[] selection = columnsTable.getSelection();
int selectionIndex = columnsTable.getSelectionIndex();
if (selection != null && selection.length >= 1) {
List<String> physicalNames = new ArrayList<String>();
List<String> columnNames = new ArrayList<String>();
for (int m = 0; m < selection.length; m++) {
columnNames.add(m, selection[m].getText(2));
physicalNames.add(m, ((ERTableColumn) selection[m].getData()).getName());
}
List<SchemaInfo> allSupers = SuperClassUtil.getSuperClasses(database.getDatabaseInfo(), newERTable.getSchemaInfo());
Constraint pk = newERTable.getSchemaInfo().getPK(allSupers);
List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
boolean hasPk = false;
for (String pkAttribute : pkAttributes) {
if (physicalNames.contains(pkAttribute)) {
hasPk = true;
break;
}
}
if (hasPk && physicalNames.containsAll(pkAttributes)) {
newERTable.getSchemaInfo().removeConstraintByName(pk.getName(), Constraint.ConstraintType.PRIMARYKEY.getText());
}
SchemaInfo newSchemaInfo = getNewSchemaInfo();
for (TableItem selec : selection) {
ERTableColumn oldColumn = (ERTableColumn) selec.getData();
if (oldColumn == null) {
continue;
}
if (oldColumn.getAttr().isClassAttribute()) {
newSchemaInfo.getClassAttributes().remove(oldColumn.getAttr());
} else {
newSchemaInfo.getAttributes().remove(oldColumn.getAttr());
newSchemaInfo.removeAttrInConstraints(oldColumn.getAttr().getName());
indexTableView.setInput(newSchemaInfo);
fkTableView.setInput(newSchemaInfo);
}
afterDeleteColumn(oldColumn.getName(isPhysical));
newERTable.removeColumn(oldColumn.getName(isPhysical), isPhysical);
}
attrLabelProvider.setSchema(newSchemaInfo);
loadColumnData();
int itemCount = columnsTable.getItemCount();
columnsTable.select(selectionIndex < itemCount ? selectionIndex : selectionIndex - 1);
columnsTable.setFocus();
}
}
Aggregations