Search in sources :

Example 71 with DBAttribute

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

the class TableEditorPart method deleteColumn.

private void deleteColumn() {
    if (!CommonUITool.openConfirmBox(Messages.msgDeleteColumnConfirm)) {
        return;
    }
    TableItem[] tblItems = columnsTable.getSelection();
    if (tblItems.length > 0) {
        DBAttribute attr = (DBAttribute) tblItems[0].getData();
        List<DBAttribute> items = newSchemaInfo.getAttributes();
        if (!items.contains(attr)) {
            return;
        }
    }
    TableItem[] selection = columnsTable.getSelection();
    int selectionIndex = columnsTable.getSelectionIndex();
    if (selection != null && selection.length >= 1) {
        List<String> attrNames = new ArrayList<String>();
        for (int m = 0; m < selection.length; m++) {
            attrNames.add(m, selection[m].getText(1));
        }
        List<SchemaInfo> allSupers = SuperClassUtil.getSuperClasses(database.getDatabaseInfo(), newSchemaInfo);
        Constraint pk = newSchemaInfo.getPK(allSupers);
        List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
        boolean hasPk = false;
        for (String pkAttribute : pkAttributes) {
            if (attrNames.contains(pkAttribute)) {
                hasPk = true;
                break;
            }
        }
        if (hasPk) {
            if (attrNames.containsAll(pkAttributes)) {
                newSchemaInfo.removeConstraintByName(pk.getName(), Constraint.ConstraintType.PRIMARYKEY.getText());
            } else {
                CommonUITool.openErrorBox(Messages.errColumnNotDropForPk);
                return;
            }
        }
        for (PartitionInfo partitionInfo : newSchemaInfo.getPartitionList()) {
            String partitionExpr = partitionInfo.getPartitionExpr();
            if (StringUtil.isNotEmpty(partitionExpr)) {
                if (partitionExpr.startsWith("[") && partitionExpr.endsWith("]")) {
                    partitionExpr = partitionExpr.substring(1, partitionExpr.length() - 1);
                    if (attrNames.contains(partitionExpr)) {
                        CommonUITool.openErrorBox(Messages.errDropForPartitonColumn);
                        return;
                    }
                } else {
                    if (attrNames.contains(partitionExpr)) {
                        CommonUITool.openErrorBox(Messages.errDropForPartitonColumn);
                        return;
                    }
                }
            }
        }
        for (TableItem selec : selection) {
            DBAttribute oldAttribute = (DBAttribute) selec.getData();
            if (oldAttribute == null) {
                continue;
            }
            if (!oldAttribute.getInherit().equals(newSchemaInfo.getClassname())) {
                CommonUITool.openErrorBox(Messages.errColumnNotDrop);
                return;
            }
            if (oldAttribute.isClassAttribute()) {
                newSchemaInfo.getClassAttributes().remove(oldAttribute);
            } else {
                newSchemaInfo.getAttributes().remove(oldAttribute);
                // newSchemaInfo.removeUniqueByAttrName(selec.getText(1));
                // For bug TOOLS-2390 After delete a column of a table,some
                // related index doesn't been deleted.
                newSchemaInfo.removeConstraintByAttrName(oldAttribute.getName());
                indexTableView.setInput(newSchemaInfo);
                fkTableView.setInput(newSchemaInfo);
            }
            SuperClassUtil.fireSuperClassChanged(database.getDatabaseInfo(), oldSchemaInfo, newSchemaInfo, newSchemaInfo.getSuperClasses());
            String oldAttrName = oldAttribute.getName();
            addDropAttrLog(oldAttrName, oldAttribute.isClassAttribute());
        }
        attrLabelProvider.setSchema(newSchemaInfo);
        loadColumnData();
        int itemCount = columnsTable.getItemCount();
        columnsTable.select(selectionIndex < itemCount ? selectionIndex : selectionIndex - 1);
        columnsTable.setFocus();
    }
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) TableItem(org.eclipse.swt.widgets.TableItem) ArrayList(java.util.ArrayList) Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) PartitionInfo(com.cubrid.common.core.common.model.PartitionInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 72 with DBAttribute

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

the class AttributeLabelProvider method getColumnText.

public String getColumnText(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_FLAG)) {
        if (StringUtil.isEmpty(dbAttribute.getName())) {
            return "*";
        }
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_NAME)) {
        return dbAttribute.getName();
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_DATATYPE)) {
        if (DataType.DATATYPE_ENUM.equalsIgnoreCase(dbAttribute.getType())) {
            String type = StringUtil.toUpper(dbAttribute.getType()) + dbAttribute.getEnumeration();
            return DataType.getShownType(type);
        } else {
            return DataType.getShownType(dbAttribute.getType());
        }
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_DEFAULT)) {
        String defaultValue = dbAttribute.getDefault();
        if (defaultValue == null) {
            return DataType.NULL_EXPORT_FORMAT;
        }
        if (defaultValue.length() == 0 && DataType.isStringType(dbAttribute.getType())) {
            return "";
        }
        return defaultValue;
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_AUTO_INCREMENT)) {
        SerialInfo serial = dbAttribute.getAutoIncrement();
        if (serial == null) {
            return "";
        }
        return serial.getMinValue() + "," + serial.getIncrementValue();
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_MEMO)) {
        return dbAttribute.getDescription();
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_COLLATION)) {
        return dbAttribute.getCollation();
    }
    return null;
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SerialInfo(com.cubrid.common.core.common.model.SerialInfo)

Example 73 with DBAttribute

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

the class TableEditorPart method init.

public void init(IEditorSite site, IEditorInput input) throws PartInitException {
    super.init(site, input);
    TableEditorInput tableInfoEditorInput = (TableEditorInput) input;
    this.database = tableInfoEditorInput.getDatabase();
    this.editedTableNode = tableInfoEditorInput.getEditedTableNode();
    this.isNewTableFlag = tableInfoEditorInput.isNewTableFlag();
    this.dbUserList = tableInfoEditorInput.getDbUserList();
    this.showDefaultType = tableInfoEditorInput.getType();
    this.collationList = tableInfoEditorInput.getCollationList();
    if (collationList != null) {
        Collation emptyCollation = new Collation();
        emptyCollation.setCharset("");
        emptyCollation.setName("");
        collationList.add(0, emptyCollation);
    }
    this.oldSchemaInfo = tableInfoEditorInput.getSchemaInfo();
    this.supportCharset = CompatibleUtil.isSupportCreateDBByCharset(database.getDatabaseInfo());
    if (isNewTableFlag) {
        newSchemaInfo = new SchemaInfo();
        //$NON-NLS-1$
        newSchemaInfo.setClassname("");
        newSchemaInfo.setOwner(database.getUserName());
        newSchemaInfo.setDbname(database.getName());
        newSchemaInfo.setType(Messages.userSchema);
        newSchemaInfo.setVirtual(Messages.schemaTypeClass);
        if (database.getDatabaseInfo() != null) {
            newSchemaInfo.setCollation(database.getDatabaseInfo().getCollation());
        }
    } else {
        newSchemaInfo = null;
        if (tableInfoEditorInput.getSchemaInfo() != null) {
            newSchemaInfo = tableInfoEditorInput.getSchemaInfo().clone();
            originalConstraints.addAll(newSchemaInfo.getConstraints());
        }
    }
    if (supportCharset) {
        columnProperites = new String[] { IAttributeColumn.COL_EMPTY, IAttributeColumn.COL_FLAG, IAttributeColumn.COL_NAME, IAttributeColumn.COL_DATATYPE, IAttributeColumn.COL_DEFAULT, IAttributeColumn.COL_AUTO_INCREMENT, IAttributeColumn.COL_NOT_NULL, IAttributeColumn.COL_PK, IAttributeColumn.COL_UK, IAttributeColumn.COL_SHARED, IAttributeColumn.COL_COLLATION, IAttributeColumn.COL_MEMO };
    } else {
        columnProperites = new String[] { IAttributeColumn.COL_EMPTY, IAttributeColumn.COL_FLAG, IAttributeColumn.COL_NAME, IAttributeColumn.COL_DATATYPE, IAttributeColumn.COL_DEFAULT, IAttributeColumn.COL_AUTO_INCREMENT, IAttributeColumn.COL_NOT_NULL, IAttributeColumn.COL_PK, IAttributeColumn.COL_UK, IAttributeColumn.COL_SHARED, IAttributeColumn.COL_MEMO };
    }
    Connection conn = null;
    try {
        conn = JDBCConnectionManager.getConnection(database.getDatabaseInfo(), false);
        IDatabaseSpec dbSpec = database.getDatabaseInfo();
        isSupportTableComment = SchemaCommentHandler.isInstalledMetaTable(dbSpec, conn);
        database.getDatabaseInfo().setSupportTableComment(isSupportTableComment);
        if (isSupportTableComment && !isNewTableFlag && newSchemaInfo != null) {
            Map<String, SchemaComment> map = SchemaCommentHandler.loadDescription(dbSpec, conn, newSchemaInfo.getClassname());
            for (DBAttribute attr : newSchemaInfo.getAttributes()) {
                SchemaComment schemaComment = SchemaCommentHandler.find(map, newSchemaInfo.getClassname(), attr.getName());
                if (schemaComment != null) {
                    attr.setDescription(schemaComment.getDescription());
                }
            }
            // get description for index
            for (Constraint cons : newSchemaInfo.getConstraints()) {
                if (CompatibleUtil.isCommentSupports(dbSpec)) {
                    String indexName = cons.getName();
                    SchemaComment indexComment = SchemaCommentHandler.loadObjectDescription(dbSpec, conn, indexName, CommentType.INDEX);
                    if (indexComment != null) {
                        cons.setDescription(indexComment.getDescription());
                    }
                }
            }
            SchemaComment schemaComment = SchemaCommentHandler.find(map, newSchemaInfo.getClassname(), null);
            if (schemaComment != null) {
                newSchemaInfo.setDescription(schemaComment.getDescription());
            }
        }
    } catch (SQLException e) {
        LOGGER.error("", e);
    } catch (Exception e) {
        LOGGER.error("", e);
    } finally {
        QueryUtil.freeQuery(conn);
    }
    schemaChangeMgr = new SchemaChangeManager(database.getDatabaseInfo(), isNewTableFlag);
    schemaDDL = new SchemaDDL(schemaChangeMgr, database.getDatabaseInfo());
    if (database != null) {
        isSupportChange = CompatibleUtil.isSupportChangeColumn(database.getDatabaseInfo());
    }
    setSite(site);
    setInput(input);
    setPartName(input.getName());
    setTitleToolTip(input.getName());
    setTitleImage(CommonUIPlugin.getImage("icons/navigator/schema_table.png"));
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) SQLException(java.sql.SQLException) SchemaChangeManager(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager) Connection(java.sql.Connection) Collation(com.cubrid.cubridmanager.core.cubrid.database.model.Collation) PartInitException(org.eclipse.ui.PartInitException) SQLException(java.sql.SQLException) IDatabaseSpec(com.cubrid.common.core.common.model.IDatabaseSpec) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) SchemaComment(com.cubrid.common.core.schemacomment.model.SchemaComment) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 74 with DBAttribute

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

the class ExportSchemaThread method getViewDDL.

/**
	 * Return DDL of a view
	 *
	 * @param viewInfo SchemaInfo the given reference of a SChemaInfo object
	 * @param getDatabaseInfo DatabaseInfo
	 * @param querySpec String the view querySpec
	 * @return
	 */
private String getViewDDL(SchemaInfo viewInfo, DatabaseInfo getDatabaseInfo, String querySpec) {
    // FIXME move this logic to core module
    StringBuffer sb = new StringBuffer();
    if (CompatibleUtil.isSupportReplaceView(getDatabaseInfo)) {
        sb.append("CREATE OR REPLACE VIEW ");
    } else {
        sb.append("CREATE VIEW ");
    }
    sb.append(QuerySyntax.escapeKeyword(viewInfo.getClassname()));
    sb.append("(");
    for (DBAttribute addr : viewInfo.getAttributes()) {
        // "Name", "Data
        // type", "Shared",
        // "Default","Default value"
        String type = addr.getType();
        sb.append(StringUtil.NEWLINE).append(QuerySyntax.escapeKeyword(addr.getName())).append(" ").append(type);
        String defaultType = addr.isShared() ? "shared" : "default";
        String defaultValue = addr.getDefault();
        if (defaultType != null && !"".equals(defaultType) && defaultValue != null && !"".equals(defaultValue)) {
            if (type != null && (DataType.DATATYPE_CHAR.equalsIgnoreCase(type) || DataType.DATATYPE_STRING.equalsIgnoreCase(type) || DataType.DATATYPE_VARCHAR.equalsIgnoreCase(type))) {
                sb.append(" " + defaultType).append(" '" + defaultValue + "'");
            } else {
                sb.append(" " + defaultType).append(" " + defaultValue);
            }
        }
        sb.append(",");
    }
    if (!viewInfo.getAttributes().isEmpty() && sb.length() > 0) {
        sb.deleteCharAt(sb.length() - 1);
    }
    sb.append(")").append(StringUtil.NEWLINE);
    sb.append("    AS ").append(StringUtil.NEWLINE);
    if (querySpec != null) {
        sb.append(querySpec);
    }
    sb.append(";").append(StringUtil.NEWLINE);
    return sb.toString();
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute)

Example 75 with DBAttribute

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

the class TaskTest method testSetFieldValue.

public void testSetFieldValue() throws Exception {
    String filepath = this.getFilePathInPlugin("/com/cubrid/cubridmanager/core/cubrid/table/task/attribute/test.message/updateattribute");
    String msg = Tool.getFileContent(filepath);
    TreeNode node = MessageUtil.parseResponse(msg, true);
    TreeNode node2 = node.getChildren().get(0).getChildren().get(0);
    DBAttribute attribute = new DBAttribute();
    SocketTask.setFieldValue(node2, attribute);
    assertEquals(node2.getValue("name"), attribute.getName());
    assertEquals(node2.getValue("type"), attribute.getType());
    assertEquals(node2.getValue("inherit"), attribute.getInherit());
    assertEquals(StringUtil.strYN2Boolean(node2.getValue("indexed")), attribute.isIndexed());
    assertEquals(StringUtil.strYN2Boolean(node2.getValue("notnull")), attribute.isNotNull());
    assertEquals(StringUtil.strYN2Boolean(node2.getValue("shared")), attribute.isShared());
    assertEquals(StringUtil.strYN2Boolean(node2.getValue("unique")), attribute.isUnique());
    assertEquals(node2.getValue("default"), attribute.getDefault());
    SocketTask socketTask = new SocketTask("viewlog", serverInfo) {
    };
    socketTask.getWarningMsg();
    socketTask.setWarningMsg(null);
    socketTask.getWarningMsg();
    socketTask.setClientService(new ClientSocket(host, Integer.valueOf(port), "admin"));
    socketTask.setWarningMsg("err");
    socketTask.getWarningMsg();
    socketTask.setAppendSendMsg("msg");
    socketTask.execute();
    socketTask.setTaskname(null);
    socketTask.execute();
    socketTask.setTaskname("");
    socketTask.execute();
    socketTask.setNeedServerConnected(true);
    socketTask.setServerInfo(new ServerInfo());
    socketTask.execute();
    socketTask.getResponsedMsg();
    socketTask.setClientService(null);
    socketTask.execute();
    socketTask.getResponsedMsg();
    socketTask.setServerInfo(null);
    socketTask.execute();
    socketTask.setUsingMonPort(true);
    socketTask.setServerInfo(serverInfo);
    socketTask.setUsingMonPort(false);
    socketTask.setServerInfo(serverInfo);
    socketTask.setResponse(node2);
    socketTask.getResponse();
    String[] values1 = { "aa", "bb" };
    String[] values2 = {};
    SocketTask.fillSet(new ArrayList<String>(), null);
    SocketTask.fillSet(new ArrayList<String>(), values1);
    SocketTask.fillSet(new ArrayList<String>(), values2);
    socketTask.cancel();
    socketTask.setClientService(null);
    socketTask.cancel();
    socketTask.finish();
    socketTask.setClientService(new ClientSocket(host, Integer.valueOf(port), "admin"));
    socketTask.finish();
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ServerInfo(com.cubrid.cubridmanager.core.common.model.ServerInfo)

Aggregations

DBAttribute (com.cubrid.common.core.common.model.DBAttribute)130 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)57 Constraint (com.cubrid.common.core.common.model.Constraint)53 ArrayList (java.util.ArrayList)46 HashMap (java.util.HashMap)16 List (java.util.List)15 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)14 TableItem (org.eclipse.swt.widgets.TableItem)13 ERTableColumn (com.cubrid.common.ui.er.model.ERTableColumn)11 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)11 GetAllAttrTask (com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask)10 SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)8 Map (java.util.Map)8 Point (org.eclipse.swt.graphics.Point)8 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)7 SQLException (java.sql.SQLException)7 PreparedStatement (java.sql.PreparedStatement)6 DBResolution (com.cubrid.common.core.common.model.DBResolution)5 PartitionInfo (com.cubrid.common.core.common.model.PartitionInfo)5 SqlFormattingStrategy (com.cubrid.common.ui.query.format.SqlFormattingStrategy)5