Search in sources :

Example 1 with DBAttribute

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

the class ERXmlContainer method parseAttributes.

private void parseAttributes() {
    NodeList attributes = doc.getElementsByTagName("Attribute");
    for (int i = 0; i < attributes.getLength(); i++) {
        Node attribute = attributes.item(i);
        ERWinDBAttribute erwinAttribute = new ERWinDBAttribute();
        String attrId = attribute.getAttributes().getNamedItem("id").getNodeValue().trim();
        //logical name
        String attrName = attribute.getAttributes().getNamedItem("Name").getNodeValue().trim();
        erwinAttribute.setLogicalName(attrName);
        attributeMap.put(attrId, erwinAttribute);
        String physicalName = "";
        String fkAttrName = "";
        int attributeType = -1;
        boolean duplicate = false;
        Node pNode = findPNode(attribute, "Entity");
        if (pNode == null) {
            continue;
        }
        String pId = pNode.getAttributes().getNamedItem("id").getNodeValue().trim();
        String tableName = physicalNameMap.get(pId);
        if (tableName == null) {
            tableName = pNode.getAttributes().getNamedItem("Name").getNodeValue().trim();
        }
        ERWinSchemaInfo schemaInfo = schemaInfos.get(tableName);
        for (int ii = 0; ii < attribute.getChildNodes().getLength(); ii++) {
            Node tempNode = attribute.getChildNodes().item(ii);
            if (tempNode.getNodeName().equals("AttributeProps")) {
                physicalName = handler.getChildValueByProperty(tempNode, "Physical_Name");
                if (physicalName == null) {
                    physicalName = attrName;
                }
                DBAttribute duplicateAttr = schemaInfo.getDBAttributeByName(physicalName, false);
                if (duplicateAttr != null) {
                    duplicate = true;
                    break;
                }
                erwinAttribute.setName(physicalName);
                columnIdMap.put(attrId, physicalName);
                Node attributePropsChild = tempNode.getFirstChild();
                while (attributePropsChild != null) {
                    String attributeName = attributePropsChild.getNodeName();
                    if (attributeName.equals("Datatype")) {
                        String dataType = attributePropsChild.getFirstChild().getNodeValue().trim();
                        if (dataType.startsWith(DataType.getUpperEnumType())) {
                            //convert to show type
                            dataType = dataType.replaceFirst(DataType.getUpperEnumType(), DataType.getLowerEnumType());
                        }
                        String enumeration = DataType.getEnumeration(dataType);
                        if (!StringUtil.isEmpty(enumeration)) {
                            dataType = DataType.getUpperEnumType();
                            erwinAttribute.setEnumeration(enumeration);
                        }
                        erwinAttribute.setType(dataType);
                        erwinAttribute.setDefault(updateData(dataType, erwinAttribute.getDefault()));
                    } else if (attributeName.equals("Type")) {
                        attributeType = Integer.parseInt(attributePropsChild.getFirstChild().getNodeValue().trim());
                    } else if (attributeName.equals("Null_Option")) {
                        int value = Integer.parseInt(attributePropsChild.getFirstChild().getNodeValue().trim());
                        switch(value) {
                            case 1:
                            case 8:
                                erwinAttribute.setNotNull(true);
                                break;
                            default:
                                break;
                        }
                    } else if (attributeName.equals("Default")) {
                        String id = attributePropsChild.getFirstChild().getNodeValue().trim();
                        Node defaultValueNode = getNodeById(id, "Default_Value");
                        if (defaultValueNode == null) {
                            attributePropsChild = attributePropsChild.getNextSibling();
                            continue;
                        }
                        String value = handler.getChildValueByProperty(defaultValueNode, "Default_ValueProps.Server_Value");
                        if (erwinAttribute.getType() != null) {
                            value = updateData(erwinAttribute.getType(), value);
                        }
                        erwinAttribute.setDefault(value);
                    } else if (attributeName.equals("Identity_Seed")) {
                        SerialInfo serialInfo = new SerialInfo();
                        serialInfo.setIncrementValue(attributePropsChild.getFirstChild().getNodeValue().trim());
                        erwinAttribute.setAutoIncrement(serialInfo);
                    } else if (attributeName.equals("Parent_Domain")) {
                        String id = attributePropsChild.getFirstChild().getNodeValue();
                        String value = handler.getNodeChildValueById("Domain", id, "DomainProps.Datatype");
                        if (value == null) {
                            // if it has reference-id (DomainProps.Parent_Domain)
                            String refId = handler.getNodeChildValueById("Domain", id, "DomainProps.Parent_Domain");
                            value = handler.getNodeChildValueById("Domain", refId, "DomainProps.Datatype");
                        }
                        if (StringUtil.isEmpty(erwinAttribute.getType())) {
                            erwinAttribute.setType(DataType.getRealType(StringUtil.toUpper(value)));
                        }
                    } else if (attributeName.equals("Parent_Relationship")) {
                        String id = attributePropsChild.getFirstChild().getNodeValue().trim();
                        Constraint relType = foreignKeyMap.get(id);
                        if (relType != null) {
                            boolean isPk = relType.getType().equals(Constraint.ConstraintType.PRIMARYKEY.getText());
                            boolean isFk = relType.getType().equals(Constraint.ConstraintType.FOREIGNKEY.getText());
                            if (isPk || isFk) {
                                erwinAttribute.setNotNull(true);
                                erwinAttribute.setUnique(true);
                            }
                        }
                    } else if (attributeName.equals("Physical_Name")) {
                        physicalName = attributePropsChild.getFirstChild().getNodeValue().trim();
                    } else if (attributeName.equals("Logical_Datatype")) {
                        String logicalDataType = attributePropsChild.getFirstChild().getNodeValue().trim();
                        erwinAttribute.setLogicalDataType(logicalDataType);
                    } else if (attributeName.equals("Parent_Attribute")) {
                        String id = attributePropsChild.getFirstChild().getNodeValue().trim();
                        Node fkAttrNode = getNodeById(id, "Attribute");
                        if (fkAttrNode == null) {
                            attributePropsChild = attributePropsChild.getNextSibling();
                            continue;
                        }
                        fkAttrName = handler.getChildValueByProperty(fkAttrNode, "AttributeProps.Physical_Name");
                        if (fkAttrName == null) {
                            fkAttrName = fkAttrNode.getAttributes().getNamedItem("Name").getNodeValue().trim();
                        }
                        Node fkTableNode = findPNode(fkAttrNode, "Entity");
                        String fkTableId = fkTableNode.getAttributes().getNamedItem("id").getNodeValue().trim();
                        String fkTableName = physicalNameMap.get(fkTableId);
                        erwinAttribute.setInherit(fkTableName);
                    // SchemaInfo schemaInfo = schemaInfos.get(tName);
                    // schemaInfo.addSuperClass(tableName);
                    } else if (attributeName.equals("View_Expression")) {
                        fkAttrName = attributePropsChild.getFirstChild().getNodeValue().trim();
                    }
                    attributePropsChild = attributePropsChild.getNextSibling();
                }
            }
        }
        if (duplicate) {
            continue;
        }
        if (attributeType != ERXmlModelConstant.ATTRIBUTE_TYPE_VIEW) {
            if (attributeType == ERXmlModelConstant.ATTRIBUTE_TYPE_PK) {
                erwinAttribute.setUnique(true);
                erwinAttribute.setNotNull(true);
            }
            erwinAttribute.setInherit(tableName);
            schemaInfo.addAttribute(erwinAttribute);
        } else {
            String viewName = tableName;
            if (viewName == null) {
                viewName = pNode.getAttributes().getNamedItem("Name").getNodeValue().trim();
            }
            ViewModel model = viewModelMap.get(viewName);
            String pTable = erwinAttribute.getInherit();
            if (pTable == null) {
                pTable = "DEFAULT";
            }
            model.addTableColumnAlias(pTable, fkAttrName, erwinAttribute.getName());
        }
    }
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ERWinDBAttribute(com.cubrid.common.ui.cubrid.database.erwin.model.ERWinDBAttribute) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ERWinSchemaInfo(com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo) ERWinDBAttribute(com.cubrid.common.ui.cubrid.database.erwin.model.ERWinDBAttribute) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) Constraint(com.cubrid.common.core.common.model.Constraint)

Example 2 with DBAttribute

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

the class TableSchemaCompareComposite method getTableSchema.

private String getTableSchema(CubridDatabase db, Map<String, SchemaInfo> schemas, String tableName) {
    // FIXME logic code move to core module
    String tableSchemaInfo = "";
    try {
        SchemaInfo schemaInfo = schemas.get(tableName);
        if (schemaInfo == null) {
            return "";
        }
        if (schemaInfo.getVirtual().equals(ClassType.VIEW.getText())) {
            GetAllClassListTask getAllClassListTask = new GetAllClassListTask(db.getDatabaseInfo());
            getAllClassListTask.setTableName(tableName);
            getAllClassListTask.getClassInfoTaskExcute();
            ClassInfo classInfo = getAllClassListTask.getClassInfo();
            GetAllAttrTask getAllAttrTask = new GetAllAttrTask(db.getDatabaseInfo());
            getAllAttrTask.setClassName(tableName);
            getAllAttrTask.getAttrList();
            List<DBAttribute> attrList = getAllAttrTask.getAllAttrList();
            List<Map<String, String>> viewColListData = GetInfoDataUtil.getViewColMapList(attrList);
            /*Get view column*/
            GetViewAllColumnsTask getAllDBVclassTask = new GetViewAllColumnsTask(db.getDatabaseInfo());
            getAllDBVclassTask.setClassName(tableName);
            getAllDBVclassTask.getAllVclassListTaskExcute();
            /*Get query list*/
            List<String> vclassList = getAllDBVclassTask.getAllVclassList();
            List<Map<String, String>> queryListData = new ArrayList<Map<String, String>>();
            for (String sql : vclassList) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("0", sql);
                queryListData.add(map);
            }
            tableSchemaInfo = GetInfoDataUtil.getViewCreateSQLScript(true, db, classInfo, tableName, viewColListData, queryListData);
        } else {
            SchemaDDL schemaDDL = null;
            schemaDDL = new SchemaDDL(null, db.getDatabaseInfo());
            tableSchemaInfo = schemaDDL.getSchemaDDL(schemaInfo);
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        return "";
    }
    return tableSchemaInfo;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GetAllClassListTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllClassListTask) GetViewAllColumnsTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetViewAllColumnsTask) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) HashMap(java.util.HashMap) Map(java.util.Map) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo) ClassInfo(com.cubrid.cubridmanager.core.cubrid.table.model.ClassInfo) GetAllAttrTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask)

Example 3 with DBAttribute

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

the class TableSchemaCompareUpdateDDL method setPositionAlterDDL.

/**
	 * Compare table Attributes Positions
	 */
public void setPositionAlterDDL() {
    // FIXME logic code move to core module
    List<DBAttribute> targetDBAttributes = targetTableSchema.getAttributes();
    List<DBAttribute> sourceDBAttributes = sourceTableSchema.getAttributes();
    for (DBAttribute targetAttr : targetDBAttributes) {
        String targetAttrName = targetAttr.getName();
        int targetPosition = targetDBAttributes.indexOf(targetAttr);
        DBAttribute sourceAttr = sourceTableSchema.getDBAttributeByName(targetAttrName, false);
        int sourcePosition = sourceDBAttributes.indexOf(sourceAttr);
        if (sourceAttr != null && sourcePosition != targetPosition) {
            changeManager.addSchemeChangeLog(new SchemaChangeLog(sourceAttr.getName(), targetAttr.getName(), SchemeInnerType.TYPE_POSITION));
        }
    }
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaChangeLog(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeLog)

Example 4 with DBAttribute

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

the class TableSchemaCompareUpdateDDL method setAttributesAlterDDL.

/**
	 * Compare table attributes
	 */
public void setAttributesAlterDDL() {
    // FIXME logic code move to core module
    List<DBAttribute> sourceDBAttributes = sourceTableSchema.getAttributes();
    List<DBAttribute> targetDBAttributes = targetTableSchema.getAttributes();
    for (DBAttribute targetAttr : targetDBAttributes) {
        String targetAttrName = targetAttr.getName().toLowerCase();
        DBAttribute sourceAttr = sourceTableSchema.getDBAttributeByName(targetAttrName, false);
        if (sourceAttr != null) {
            if (!targetAttr.equals(sourceAttr)) {
                changeManager.addSchemeChangeLog(new SchemaChangeLog(sourceAttr.getName(), targetAttrName, SchemeInnerType.TYPE_ATTRIBUTE));
                changeManager.addSchemeChangeLog(new SchemaChangeLog(sourceAttr.getName(), targetAttrName, SchemeInnerType.TYPE_POSITION));
            }
        } else {
            changeManager.addSchemeChangeLog(new SchemaChangeLog(null, targetAttrName, SchemeInnerType.TYPE_ATTRIBUTE));
            changeManager.addSchemeChangeLog(new SchemaChangeLog(targetAttrName, targetAttrName, SchemeInnerType.TYPE_POSITION));
        }
    }
    for (DBAttribute sourceAttr : sourceDBAttributes) {
        String sourceAttrName = sourceAttr.getName();
        DBAttribute targetAttr = targetTableSchema.getDBAttributeByName(sourceAttrName, false);
        if (targetAttr == null) {
            changeManager.addSchemeChangeLog(new SchemaChangeLog(sourceAttrName, null, SchemeInnerType.TYPE_ATTRIBUTE));
        }
    }
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SchemaChangeLog(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeLog)

Example 5 with DBAttribute

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

the class FileToTableMappingComposite method parseData.

@SuppressWarnings("unchecked")
private void parseData(final Map<String, String> tableToFile) throws Exception {
    allTableList = (List<ICubridNode>) treeViewer.getInput();
    final List<ICubridNode> newMappedNodeList = new ArrayList<ICubridNode>();
    final ImportConfig importConfig = importSettingPage.getImportDataWizard().getImportConfig();
    ProgressMonitorDialog progress = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
    progress.setCancelable(true);
    progress.run(true, true, new IRunnableWithProgress() {

        public void run(IProgressMonitor monitor) throws InvocationTargetException {
            monitor.beginTask(Messages.taskParsingData, tableToFile.size());
            for (Entry<String, String> entry : tableToFile.entrySet()) {
                // FIXME move this logic to core module
                String tableName = entry.getKey();
                String filePath = entry.getValue();
                File file = new File(filePath);
                String fileName = file.getName();
                monitor.subTask(Messages.bind(Messages.taskParsingFile, fileName));
                List<String> fileColumnList = new ArrayList<String>();
                List<String> firstRowColsLst = new ArrayList<String>();
                List<String> colNameList = new ArrayList<String>();
                List<String> colTypeList = new ArrayList<String>();
                int totalLine = 0;
                List<Integer> itemsNumberOfSheets = null;
                /*Find the class node*/
                ICubridNode classNode = getTableNode(tableName);
                /*Process file*/
                ImportFileHandler importFileHandler = ImportFileHandlerFactory.getHandler(file.getAbsolutePath(), importConfig);
                ImportFileDescription ifd = getFileDescription(importFileHandler);
                firstRowColsLst.addAll(ifd.getFirstRowCols());
                totalLine = ifd.getTotalCount();
                itemsNumberOfSheets = ifd.getItemsNumberOfSheets();
                boolean isFirstLineAsColumnName = isFirstLineAsColumnName(tableName);
                fillInFromList(fileColumnList, firstRowColsLst, isFirstLineAsColumnName);
                if (isFirstLineAsColumnName) {
                    handleSelectEventForFirstRowAsColBtn(itemsNumberOfSheets, fileColumnList, firstRowColsLst, totalLine, isFirstLineAsColumnName);
                }
                if (classNode != null) {
                    Object isNew = classNode.getData(ImportObjectLabelProvider.IS_NEW);
                    if (isNew == null || !(Boolean) isNew) {
                        SchemaInfo schemaInfo = database.getDatabaseInfo().getSchemaInfo(tableName);
                        List<DBAttribute> attributes = schemaInfo == null ? null : schemaInfo.getAttributes();
                        if (attributes == null) {
                            return;
                        }
                        for (DBAttribute attr : attributes) {
                            String column = attr.getName();
                            String dataType = attr.getType();
                            colNameList.add(column);
                            colTypeList.add(dataType);
                        }
                    } else {
                        removeClassNode(classNode);
                        classNode = createClassNode(tableName, isFirstLineAsColumnName(tableName), firstRowColsLst, colNameList, colTypeList);
                    }
                } else if (importConfig.isCreateTableAccordingData()) {
                    classNode = createClassNode(tableName, isFirstLineAsColumnName(tableName), firstRowColsLst, colNameList, colTypeList);
                } else {
                    failedFileList.add(file.getAbsolutePath());
                    continue;
                }
                if (fileColumnList.size() == colNameList.size()) {
                    classNode.setData(ImportObjectLabelProvider.IS_MAPPED, true);
                    classNode.setData(ImportObjectLabelProvider.FILE_PAHT, fileName);
                    classNode.setData(ImportObjectLabelProvider.ROW_COUNT, totalLine);
                    List<PstmtParameter> parameterList = new ArrayList<PstmtParameter>();
                    for (int i = 0; i < fileColumnList.size(); i++) {
                        PstmtParameter pstmtParameter = new PstmtParameter(colNameList.get(i), i + 1, colTypeList.get(i), String.valueOf(i));
                        pstmtParameter.setFileColumnName(fileColumnList.get(i));
                        parameterList.add(pstmtParameter);
                    }
                    TableConfig tableConfig = new TableConfig(classNode.getName());
                    tableConfig.setPstmList(parameterList);
                    tableConfig.setFilePath(file.getAbsolutePath());
                    tableConfig.setInsertDML(getInsertDML(classNode, colNameList, colTypeList));
                    tableConfig.setLineCount(totalLine);
                    tableConfig.setMapped(true);
                    Object isNew = classNode.getData(ImportObjectLabelProvider.IS_NEW);
                    if (isNew != null && (Boolean) isNew) {
                        tableConfig.setCreateDDL(classNode.getData(ImportObjectLabelProvider.CREATE_DDL).toString());
                    }
                    importConfig.addTableConfig(tableConfig);
                    newMappedNodeList.add(classNode);
                } else {
                    classNode.setData(ImportObjectLabelProvider.IS_MAPPED, false);
                    classNode.setData(ImportObjectLabelProvider.FILE_PAHT, fileName);
                    classNode.setData(ImportObjectLabelProvider.ROW_COUNT, totalLine);
                    TableConfig tableConfig = importConfig.getSelectedMap().get(classNode.getName());
                    if (tableConfig != null) {
                        tableConfig.setMapped(false);
                        tableConfig.setFilePath(filePath);
                        tableConfig.setLineCount(totalLine);
                        tableConfig.getPstmList().clear();
                        tableConfig.setInsertDML("");
                    }
                    failedFileList.add(fileName);
                }
                monitor.worked(1);
                if (monitor.isCanceled()) {
                    return;
                }
            }
        }
    });
    for (ICubridNode node : allTableList) {
        if (newMappedNodeList.contains(node)) {
            treeViewer.setChecked(node, true);
        }
    }
    treeViewer.refresh();
}
Also used : ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) ImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileHandler) ArrayList(java.util.ArrayList) ICubridNode(com.cubrid.common.ui.spi.model.ICubridNode) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Entry(java.util.Map.Entry) PstmtParameter(com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ImportFileDescription(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileDescription) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

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