use of com.cubrid.common.core.common.model.DBAttribute in project cubrid-manager by CUBRID.
the class TableEditorPart method okPressed.
protected void okPressed() {
if (!verifyTableName()) {
return;
}
if (columnsTable.getItemCount() == 0) {
CommonUITool.openErrorBox(Messages.noAttributes);
return;
}
String message = (oldSchemaInfo == null) ? Messages.msgCreateTableConfirm : Messages.msgAlterTableConfirm;
if (!CommonUITool.openConfirmBox(message)) {
return;
}
tableName = tableNameText.getText();
owner = ownerCombo.getText();
String tableDesc = tableDescText.getText();
newSchemaInfo.setClassname(tableName);
newSchemaInfo.setOwner(owner);
newSchemaInfo.setDescription(tableDesc);
if (reuseOIDBtn != null) {
newSchemaInfo.setReuseOid(reuseOIDBtn.getSelection());
}
DatabaseInfo dbInfo = database.getDatabaseInfo();
CommonSQLExcuterTask commonSqlTask = new CommonSQLExcuterTask(dbInfo);
schemaDDL.setEndLineChar("$$$$");
String ddlStr = null;
if (isNewTableFlag) {
ddlStr = schemaDDL.getSchemaDDL(newSchemaInfo);
} else {
ddlStr = schemaDDL.getSchemaDDL(oldSchemaInfo, newSchemaInfo);
}
boolean isExecuteCommonSqlTask = false;
String[] sqlStr = ddlStr.split("\\$\\$\\$\\$");
for (String sql : sqlStr) {
String trimSql = sql.trim();
if (trimSql.length() > 0 && !trimSql.startsWith("--")) {
if (dbInfo.isShard()) {
sql = dbInfo.wrapShardQuery(sql);
}
commonSqlTask.addSqls(sql);
isExecuteCommonSqlTask = true;
}
}
// do with table user change
String changeOwnerDDL = getChangeOwnerDDL();
if (StringUtil.isNotEmpty(changeOwnerDDL)) {
changeOwnerDDL = dbInfo.wrapShardQuery(changeOwnerDDL);
commonSqlTask.addCallSqls(changeOwnerDDL);
isExecuteCommonSqlTask = true;
}
schemaDDL.setEndLineChar(";");
// do with column null attribute change
List<String[]> nullAttrChangedColumnList = getNotNullChangedColumn();
// if the column is null value, when set this column for not null,need
// change these null value for default value
List<String> nullToDefaultChangedColumnList = new ArrayList<String>();
List<String> defaultValList = new ArrayList<String>();
if (ApplicationType.CUBRID_MANAGER.equals(PerspectiveManager.getInstance().getCurrentMode())) {
for (Iterator<String[]> it = nullAttrChangedColumnList.iterator(); it.hasNext(); ) {
String[] column = it.next();
if (!Boolean.parseBoolean(column[1])) {
continue;
}
nullToDefaultChangedColumnList.add(column[0]);
}
// if the column is null value, when set this column for not null,do
// not need change these null value for default value
List<String> keepNullValueColList = new ArrayList<String>();
for (Iterator<String> it = nullToDefaultChangedColumnList.iterator(); it.hasNext(); ) {
String nullColumn = it.next();
DBAttribute dBAttribute = newSchemaInfo.getDBAttributeByName(nullColumn, false);
if (dBAttribute == null) {
continue;
}
String defaultVal = dBAttribute.getDefault();
boolean isUnique = dBAttribute.isUnique();
if (isUnique) {
keepNullValueColList.add(nullColumn);
it.remove();
} else {
if (defaultVal == null) {
keepNullValueColList.add(nullColumn);
it.remove();
continue;
} else {
FormatDataResult result = DBAttrTypeFormatter.formatForInput(dBAttribute.getType(), defaultVal, false);
if (result.isSuccess()) {
defaultValList.add(result.getFormatResult());
}
}
}
}
String msg = Messages.bind(Messages.confirmSetDef, nullToDefaultChangedColumnList);
if (!nullToDefaultChangedColumnList.isEmpty() && (!CommonUITool.openConfirmBox(msg))) {
return;
}
msg = Messages.bind(Messages.confirmKeepNull, keepNullValueColList);
if (!keepNullValueColList.isEmpty() && (!CommonUITool.openConfirmBox(msg))) {
return;
}
}
TaskJobExecutor taskJobExec = new CommonTaskJobExec(this);
boolean hasChanges = isExecuteCommonSqlTask || !nullAttrChangedColumnList.isEmpty() || !nullToDefaultChangedColumnList.isEmpty();
if (hasChanges) {
if (isExecuteCommonSqlTask) {
taskJobExec.addTask(commonSqlTask);
}
if (database == null || newSchemaInfo == null) {
return;
}
// change all table data from null value to default value
int nullColSize = nullToDefaultChangedColumnList.size();
for (int colIndex = 0; colIndex < nullColSize; colIndex++) {
UpdateNullToDefault updateNullToDefault = new UpdateNullToDefault(dbInfo);
updateNullToDefault.setTable(tableName);
updateNullToDefault.setColumn(nullToDefaultChangedColumnList.get(colIndex));
updateNullToDefault.setDefaultValue(defaultValList.get(colIndex));
taskJobExec.addTask(updateNullToDefault);
}
}
List<UpdateDescriptionTask> updateDescriptionTaskList = getUpdateDescriptionTaskList(dbInfo);
for (UpdateDescriptionTask task : updateDescriptionTaskList) {
taskJobExec.addTask(task);
}
if (taskJobExec.getTaskCount() > 0) {
String serverName = database.getServer().getName();
String dbName = database.getDatabaseInfo().getDbName();
String title = getSite().getShell().getText();
jobName = title + " - " + tableName + "@" + dbName;
JobFamily jobFamily = new JobFamily();
jobFamily.setServerName(serverName);
jobFamily.setDbName(dbName);
taskJobExec.schedule(jobName, jobFamily, true, Job.SHORT);
} else {
getSite().getWorkbenchWindow().getActivePage().closeEditor(editor, false);
}
}
use of com.cubrid.common.core.common.model.DBAttribute in project cubrid-manager by CUBRID.
the class AttributeCellModifier 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;
}
DBAttribute attr = (DBAttribute) item.getData();
String attrName = attr.getName();
DBAttribute oldAttribute = null;
if (editor.getOldSchemaInfo() != null) {
oldAttribute = editor.getOldSchemaInfo().getDBAttributeByName(attrName, false);
}
if (StringUtil.isEqual(property, IAttributeColumn.COL_PK)) {
SchemaInfo schemaInfo = editor.getNewSchemaInfo();
if (schemaInfo == null) {
return;
}
boolean on = ((Boolean) value).booleanValue();
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.changeForEditElement(attrName, attr, oldAttribute);
}
}
editor.makeChangeLogForIndex(attrName, 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) && !ValidateUtil.isValidIdentifier(newName)) {
CommonUITool.openErrorBox(Messages.errColumnName);
return;
}
List<DBAttribute> lastAttrs = schemaInfo.getAttributes();
if (StringUtil.isEmpty(newName) && lastAttrs.contains(attr)) {
CommonUITool.openErrorBox(Messages.errEmptyNameOnEditTableColumn);
return;
}
for (DBAttribute lastAttr : lastAttrs) {
if (StringUtil.isEqualIgnoreCase(lastAttr.getName(), newName) && attr != lastAttr) {
CommonUITool.openErrorBox(Messages.errSameNameOnEditTableColumn);
return;
}
}
if (!StringUtil.isEqualIgnoreCase(attr.getName(), newName)) {
replaceNewConstraintAttributeName(schemaInfo, attr.getName(), newName);
DBAttribute newAttribute = attr.clone();
if (newAttribute != null) {
newAttribute.setName(newName);
}
if (attr != null) {
attr.setName(newName);
}
if (!hasAddedToSchemaInfo(attr)) {
attr.setName(newName);
if (!StringUtil.isEmpty(newName)) {
if (!lastAttrs.contains(newAttribute)) {
newAttribute.setInherit(editor.getNewSchemaInfo().getClassname());
schemaInfo.addAttribute(newAttribute);
editor.removeElementByName(newName);
}
editor.addNewAttrLog(newName, newAttribute.isClassAttribute());
if (!StringUtil.isEmpty(newAttribute.getName())) {
editor.makeChangeLogForIndex(attrName, newAttribute, oldAttribute);
}
}
} else {
editor.changeForEditElement(attrName, newAttribute, oldAttribute);
}
}
DBAttribute lastDBAttribute = null;
if (lastAttrs.size() > 0) {
Table columnsTable = editor.getColumnsTable();
lastDBAttribute = (DBAttribute) columnsTable.getItem(columnsTable.getItemCount() - 1).getData();
}
if (!StringUtil.isEmpty(newName) && (lastDBAttribute == null || !StringUtil.isEmpty(lastDBAttribute.getName()))) {
editor.addNewColumn();
}
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_DATATYPE)) {
String dataTypeRaw = (String) value;
if (dataTypeRaw != null && dataTypeRaw.trim().toLowerCase().startsWith("enum")) {
int sp = dataTypeRaw.indexOf("(");
if (sp != -1) {
String dataType = dataTypeRaw.substring(0, sp).toLowerCase().trim();
attr.setType(dataType);
String enumeration = dataTypeRaw.substring(sp).trim();
attr.setEnumeration(enumeration);
}
} else {
attr.setType(dataTypeRaw);
}
if (!DataType.isIntegerType(attr.getType())) {
attr.setAutoIncrement(null);
}
if (!DataType.canUseCollation(attr.getType())) {
attr.setCollation("");
}
editor.changeForEditElement(attrName, attr, oldAttribute);
} 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.changeForEditElement(attrName, attr, oldAttribute);
} 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(attrName);
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(attrName, attr, oldAttribute);
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_NOT_NULL)) {
boolean on = ((Boolean) value).booleanValue();
attr.setNotNull(on);
editor.changeForEditElement(attrName, attr, oldAttribute);
} 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.changeForEditElement(attrName, attr, oldAttribute);
} 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.changeForEditElement(attrName, attr, oldAttribute);
} 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();
}
use of com.cubrid.common.core.common.model.DBAttribute in project cubrid-manager by CUBRID.
the class AttributeCellModifier method getValue.
public Object getValue(Object element, String property) {
// FIXME move this logic to core module
DBAttribute attr = (DBAttribute) element;
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 attr.getName();
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_DATATYPE)) {
String dataType = attr.getType();
if (dataType == null) {
return "";
}
String physicalType = getShownPhysicalType(attr);
return physicalType;
} 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.DBAttribute in project cubrid-manager by CUBRID.
the class AttributeLabelProvider method getBackground.
public Color getBackground(Object element, int columnIndex) {
if (schema == null || element == null || !editableMode) {
return null;
}
DBAttribute attr = (DBAttribute) element;
String property = editorAdaptor.getColumnProperty(columnIndex);
if (StringUtil.isEqual(property, IAttributeColumn.COL_AUTO_INCREMENT)) {
DBAttribute aiAttr = schema.getAutoIncrementColumn();
if (aiAttr != null && aiAttr != attr) {
return DISABLED_COLOR;
} else if (!DataType.isIntegerType(attr.getType())) {
return DISABLED_COLOR;
}
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_MEMO)) {
return editorAdaptor.isSupportTableComment() ? null : DISABLED_COLOR;
}
return null;
}
use of com.cubrid.common.core.common.model.DBAttribute in project cubrid-manager by CUBRID.
the class AttributeLabelProvider method getColumnImage.
public Image getColumnImage(Object element, int columnIndex) {
if (element == null) {
return null;
}
DBAttribute dbAttribute = (DBAttribute) element;
if (dbAttribute == null || dbAttribute.getInherit() == null || schema == null) {
return null;
}
String property = editorAdaptor.getColumnProperty(columnIndex);
if (StringUtil.isEqual(property, IAttributeColumn.COL_PK)) {
String attrName = dbAttribute.getName();
Constraint pk = schema.getPK(supers);
if (null != pk && pk.getAttributes().contains(attrName)) {
return PK_IMAGE;
}
return editableMode ? UNCHECK_IMAGE : null;
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_NOT_NULL)) {
String attrName = dbAttribute.getName();
Constraint pk = schema.getPK(supers);
if (null != pk && pk.getAttributes().contains(attrName)) {
return DISABLED_CHECK_IMAGE;
}
if (dbAttribute.isNotNull()) {
return editableMode ? CHECK_IMAGE : DISABLED_CHECK_IMAGE;
} else {
return editableMode ? UNCHECK_IMAGE : null;
}
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_UK)) {
String attrName = dbAttribute.getName();
Constraint pk = schema.getPK(supers);
if (null != pk && pk.getAttributes().contains(attrName)) {
return DISABLED_CHECK_IMAGE;
}
if (dbAttribute.isUnique() && schema.isAttributeUnique(dbAttribute, supers)) {
return editableMode ? CHECK_IMAGE : DISABLED_CHECK_IMAGE;
} else {
return editableMode ? UNCHECK_IMAGE : DISABLED_UNCHECK_IMAGE;
}
} else if (StringUtil.isEqual(property, IAttributeColumn.COL_SHARED)) {
if (dbAttribute.isShared()) {
return editableMode ? CHECK_IMAGE : DISABLED_CHECK_IMAGE;
} else {
return editableMode ? UNCHECK_IMAGE : null;
}
}
return null;
}
Aggregations