use of com.cubrid.common.core.common.model.SerialInfo 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.SerialInfo 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.SerialInfo in project cubrid-manager by CUBRID.
the class ConstraintComparator method getAlterAttrDDL.
/**
* Get alter DBAttribute ddl
*
* @param oldAttr
* @param newAttr
* @param oldSchemaInfo
* @param newSchemaInfo
* @param oldSupers
* @param newSupers
* @param isClassAttr
* @param attrMap
* @param tableName
* @return
*/
private String getAlterAttrDDL(DBAttribute oldAttr, DBAttribute newAttr, SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, List<SchemaInfo> oldSupers, List<SchemaInfo> newSupers, boolean isClassAttr, Map<String, String> attrMap, String tableName, DDLGenerator generator) {
StringBuffer ddlBuffer = new StringBuffer();
String oldColumnName = oldAttr.getName().toLowerCase();
String columnName = oldColumnName;
String newColumnName = newAttr.getName().toLowerCase();
if (!newColumnName.equals(oldColumnName)) {
String ddl = getRenameColumnNameDDL(tableName, oldColumnName, newColumnName, isClassAttr);
ddlBuffer.append(ddl).append(endLineChar).append(StringUtil.NEWLINE);
columnName = newColumnName;
attrMap.put(oldColumnName, newColumnName);
}
boolean oldNotNull = oldAttr.isNotNull();
boolean newNotNull = newAttr.isNotNull();
boolean notNullChanged = oldNotNull != newNotNull;
boolean hasNotNullDDL = false;
if (notNullChanged) {
boolean isChangedByPK = false;
if (newNotNull) {
// add a new PK
Constraint pk = newSchemaInfo.getPK(newSupers);
List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
if (pkAttributes.contains(newColumnName)) {
isChangedByPK = true;
}
} else {
// drop an old PK
Constraint pk = oldSchemaInfo.getPK(oldSupers);
List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
if (pkAttributes.contains(newColumnName)) {
isChangedByPK = true;
}
}
if (!isChangedByPK) {
hasNotNullDDL = true;
// null constraint changed,it is not support for 8.2.2
String editDDL = null;
if (isClassAttr) {
editDDL = getChangeAttributeDDL(tableName, oldAttr, newAttr, oldSchemaInfo, newSchemaInfo, newSupers);
} else {
editDDL = getChangeColumnDDL(tableName, oldAttr, newAttr, oldSchemaInfo, newSchemaInfo, oldSupers, newSupers);
}
return editDDL;
}
}
String oldDefault = oldAttr.getDefault();
String newDefault = newAttr.getDefault();
boolean defaultChanged = oldDefault == null ? newDefault != null : !oldDefault.equals(newDefault);
if (defaultChanged && !hasNotNullDDL) {
if (newDefault == null) {
newDefault = "null";
} else {
FormatDataResult result = DBAttrTypeFormatter.formatForInput(newAttr.getType(), newDefault, false);
if (result.isSuccess()) {
newDefault = result.getFormatResult();
}
}
String ddl = getChangeDefaultValueDDL(tableName, columnName, newDefault, isClassAttr);
ddlBuffer.append(ddl).append(endLineChar).append(StringUtil.NEWLINE);
}
SerialInfo oldAutoIncrement = oldAttr.getAutoIncrement();
SerialInfo newAutoIncrement = newAttr.getAutoIncrement();
if (null != newAutoIncrement && !newAutoIncrement.equals(oldAutoIncrement)) {
String increment = getAlterAutoIncrementDDL(tableName, newColumnName);
ddlBuffer.append(increment);
}
return ddlBuffer.toString();
}
use of com.cubrid.common.core.common.model.SerialInfo in project cubrid-manager by CUBRID.
the class ConstraintComparator method getChangeColumnDDL.
/**
* Get the column change DDL
*
* @param tableName
* @param oldAttr
* @param newAttr
* @param oldSchemaInfo
* @param newSchemaInfo
* @param newSupers
* @return
*/
private String getChangeColumnDDL(String tableName, DBAttribute oldAttr, DBAttribute newAttr, SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, List<SchemaInfo> oldSupers, List<SchemaInfo> newSupers) {
Constraint newPK = newSchemaInfo.getPK(newSupers);
List<String> pkAttributes = newPK == null ? new ArrayList<String>() : newPK.getAttributes();
StringBuffer sb = new StringBuffer();
sb.append("ALTER TABLE ");
sb.append(QuerySyntax.escapeKeyword(tableName));
sb.append(" CHANGE COLUMN ");
sb.append(QuerySyntax.escapeKeyword(oldAttr.getName())).append(" ");
sb.append(getInstanceAttributeDDL(newAttr, pkAttributes, newSchemaInfo, false));
String reorderString = getReorderString(newAttr, newSchemaInfo);
if (reorderString != null) {
sb.append(" " + reorderString);
}
sb.append(endLineChar);
sb.append(StringUtil.NEWLINE);
SerialInfo oldAutoIncrement = oldAttr.getAutoIncrement();
SerialInfo newAutoIncrement = newAttr.getAutoIncrement();
if (null != newAutoIncrement && !newAutoIncrement.equals(oldAutoIncrement)) {
String increment = getAlterAutoIncrementDDL(tableName, newAttr.getName());
sb.append(increment);
}
return sb.toString();
}
use of com.cubrid.common.core.common.model.SerialInfo in project cubrid-manager by CUBRID.
the class ConstraintComparator method getAutoIncrementList.
/**
* get auto increament info
*
* @param schemaInfo
* @return List<SerialInfo>
*/
public List<SerialInfo> getAutoIncrementList(SchemaInfo schemaInfo) {
List<DBAttribute> nlist = schemaInfo.getAttributes();
List<SerialInfo> autoIncrementList = new ArrayList<SerialInfo>();
if (!nlist.isEmpty()) {
for (int i = 0; i < nlist.size(); i++) {
DBAttribute instanceAttr = nlist.get(i);
if (instanceAttr.getAutoIncrement() != null) {
autoIncrementList.add(instanceAttr.getAutoIncrement());
}
}
}
return autoIncrementList;
}
Aggregations