Search in sources :

Example 36 with DBAttribute

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

the class AnalyseSqlTask method execute.

/**
	 * Execute the tasks
	 */
public void execute() {
    try {
        if (errorMsg != null && errorMsg.trim().length() > 0) {
            return;
        }
        if (connection == null || connection.isClosed()) {
            errorMsg = Messages.error_getConnection;
            return;
        }
        connection.setAutoCommit(false);
        if (sqls == null) {
            return;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < sqls.size(); i++) {
            sb.append(sqls.get(i));
            if (i != sqls.size() - 1) {
                sb.append(" UNION ALL ");
            }
        }
        stmt = connection.createStatement();
        rs = stmt.executeQuery(sb.toString());
        CUBRIDResultSetMetaDataProxy resultSetMeta = (CUBRIDResultSetMetaDataProxy) rs.getMetaData();
        for (int i = 1; i < resultSetMeta.getColumnCount() + 1; i++) {
            // "Name", "Data type", "Shared", "Default"
            Map<String, String> map = new HashMap<String, String>();
            String type = resultSetMeta.getColumnTypeName(i);
            int presion = resultSetMeta.getPrecision(i);
            int scale = resultSetMeta.getScale(i);
            if (type != null && type.equalsIgnoreCase("CLASS")) {
                String tableName = resultSetMeta.getTableName(i);
                String colName = resultSetMeta.getColumnName(i);
                DBAttribute bean = getColAttr(tableName, colName);
                if (bean == null || bean.getDomainClassName() == null || bean.getDomainClassName().equals("")) {
                    type = "OBJECT";
                } else {
                    type = bean.getDomainClassName();
                }
            } else if (type != null && (type.equalsIgnoreCase("CHAR") || type.equalsIgnoreCase("VARCHAR") || type.equalsIgnoreCase("NCHAR") || type.equalsIgnoreCase("BIT") || type.toUpperCase(Locale.getDefault()).indexOf("VARYING") >= 0)) {
                type += "(" + presion + ")";
            } else if (type != null && type.equalsIgnoreCase("NUMERIC")) {
                type += "(" + presion + "," + scale + ")";
            }
            map.put("0", resultSetMeta.getColumnName(i));
            map.put("1", type);
            map.put("2", "");
            map.put("3", "");
            map.put("4", "");
            result.add(map);
        }
    } catch (SQLException e) {
        this.errorMsg = e.getMessage();
    } finally {
        finish();
    }
}
Also used : CUBRIDResultSetMetaDataProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) DBAttribute(com.cubrid.common.core.common.model.DBAttribute)

Example 37 with DBAttribute

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

the class AnalyseSqlTask method getColAttr.

/**
	 * Get column attribute
	 * 
	 * @param className String
	 * @param colName String
	 * @return DBAttribute
	 */
private DBAttribute getColAttr(String className, String colName) {
    DBAttribute dbAttribute = null;
    pStmt = null;
    ResultSet rs = null;
    if (errorMsg != null && errorMsg.trim().length() > 0) {
        return dbAttribute;
    }
    try {
        if (connection == null || connection.isClosed()) {
            errorMsg = Messages.error_getConnection;
            return dbAttribute;
        }
        String sql = "SELECT attr_name, class_name, attr_type, def_order," + " from_class_name, from_attr_name, data_type, prec, scale," + " domain_class_name, default_value, is_nullable" + " FROM db_attribute" + " WHERE class_name=? AND attr_name=?";
        // [TOOLS-2425]Support shard broker
        sql = DatabaseInfo.wrapShardQuery(databaseInfo, sql);
        pStmt = connection.prepareStatement(sql);
        pStmt.setString(1, className);
        pStmt.setString(2, colName);
        rs = pStmt.executeQuery();
        if (rs.next()) {
            dbAttribute = new DBAttribute();
            dbAttribute.setName(rs.getString("attr_name"));
            dbAttribute.setType(rs.getString("data_type"));
            String attrType = rs.getString("attr_type");
            String domainClassName = rs.getString("domain_class_name");
            if (attrType.equalsIgnoreCase("SHARED")) {
                dbAttribute.setShared(true);
            } else {
                dbAttribute.setShared(false);
            }
            dbAttribute.setDomainClassName(domainClassName);
            dbAttribute.setDefault(rs.getString("default_value"));
            String isNull = rs.getString("is_nullable");
            if (isNull.equalsIgnoreCase("YES")) {
                dbAttribute.setNotNull(false);
            } else {
                dbAttribute.setNotNull(true);
            }
        }
    } catch (SQLException e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        QueryUtil.freeQuery(pStmt, rs);
    }
    return dbAttribute;
}
Also used : SQLException(java.sql.SQLException) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ResultSet(java.sql.ResultSet)

Example 38 with DBAttribute

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

the class GetAllAttrTask method getAttrList.

/**
	 * Get table or view all the attribute list
	 * 
	 */
public void getAttrList() {
    allAttrList = new ArrayList<DBAttribute>();
    try {
        if (errorMsg != null && errorMsg.trim().length() > 0) {
            return;
        }
        if (connection == null || connection.isClosed()) {
            errorMsg = Messages.error_getConnection;
            return;
        }
        String sql = "SELECT * FROM db_attribute WHERE class_name=?" + " ORDER BY def_order";
        // [TOOLS-2425]Support shard broker
        sql = databaseInfo.wrapShardQuery(sql);
        stmt = connection.prepareStatement(sql);
        ((PreparedStatement) stmt).setString(1, className.toLowerCase(Locale.getDefault()));
        rs = ((PreparedStatement) stmt).executeQuery();
        while (rs.next()) {
            DBAttribute dbAttribute = new DBAttribute();
            dbAttribute.setName(rs.getString("attr_name"));
            String attrType = rs.getString("attr_type");
            String dataType = rs.getString("data_type");
            int prec = rs.getInt("prec");
            int scale = rs.getInt("scale");
            String defaultValue = rs.getString("default_value");
            dbAttribute.setDomainClassName(rs.getString("domain_class_name"));
            if (attrType.equalsIgnoreCase("SHARED")) {
                dbAttribute.setShared(true);
            } else {
                dbAttribute.setShared(false);
            }
            if (attrType.equalsIgnoreCase("CLASS")) {
                dbAttribute.setClassAttribute(true);
            } else {
                dbAttribute.setClassAttribute(false);
            }
            String isNull = rs.getString("is_nullable");
            if (isNull.equalsIgnoreCase("YES")) {
                dbAttribute.setNotNull(false);
            } else {
                dbAttribute.setNotNull(true);
            }
            dataType = DataType.convertAttrTypeString(dataType, String.valueOf(prec), String.valueOf(scale));
            dbAttribute.setType(dataType);
            if (CompatibleUtil.isCommentSupports(databaseInfo)) {
                dbAttribute.setDescription(rs.getString("comment"));
            }
            //Fix bug TOOLS-3093
            defaultValue = DataType.convertDefaultValue(dataType, defaultValue, databaseInfo);
            dbAttribute.setDefault(defaultValue);
            allAttrList.add(dbAttribute);
        }
    } catch (SQLException e) {
        errorMsg = e.getMessage();
    } finally {
        finish();
    }
}
Also used : SQLException(java.sql.SQLException) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) PreparedStatement(java.sql.PreparedStatement)

Example 39 with DBAttribute

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

the class ConstraintComparator method getAlterDDL.

/**
	 * Return an alter DDL of schema, some changes stored in change
	 * logs(SchemaChangeManager), others are found by differing old and new
	 * schema objects
	 *
	 * @param oldSchemaInfo SchemaInfo the old reference of oldSchemaInfo
	 * @param newSchemaInfo SchemaInfo the new reference of oldSchemaInfo
	 * @return String a string indicates the info of DDL
	 */
public String getAlterDDL(SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo) {
    DDLGenerator generator = new DDLGenerator();
    if (oldSchemaInfo == null) {
        return null;
    }
    List<SchemaInfo> oldSupers = SuperClassUtil.getSuperClasses(databaseInfo, oldSchemaInfo);
    if (oldSupers == null) {
        return null;
    }
    List<SchemaInfo> newSupers = SuperClassUtil.getSuperClasses(databaseInfo, newSchemaInfo);
    if (newSupers == null) {
        return null;
    }
    // old --> new
    Map<String, String> attrMap = new HashMap<String, String>();
    //Generate the DDL for rename table
    String oldTableName = oldSchemaInfo.getClassname().toLowerCase();
    String newTableName = newSchemaInfo.getClassname().toLowerCase();
    String tableName = oldTableName;
    if (!oldTableName.equals(newTableName)) {
        String renameDDL = getRenameTableDDL(oldTableName, newTableName);
        generator.addSchemaDDLMode(DDLGenerator.TYPE_REBANE_TABLE, newSchemaInfo, renameDDL);
        tableName = newTableName;
    }
    String oldCollation = oldSchemaInfo.getCollation();
    String newCollation = newSchemaInfo.getCollation();
    if (!StringUtil.isEmpty(newCollation) && !StringUtil.isEqualNotIgnoreNull(oldCollation, newCollation)) {
        String alterCollationDDL = getAlterTableCollationDDL(oldSchemaInfo, newSchemaInfo);
        generator.addSchemaDDLMode(DDLGenerator.TYPE_CHANGE_TABLE_COLLATE, newSchemaInfo, alterCollationDDL);
    }
    //Generate the DDL for column attribute change
    List<SchemaChangeLog> allAttrChanges = changeLogMgr.getClassAttrChangeLogs();
    allAttrChanges.addAll(changeLogMgr.getAttrChangeLogs());
    // only new added attribute and after version 8.4.0 support to reorder
    boolean isSupportReorderColumn = CompatibleUtil.isSupportReorderColumn(databaseInfo);
    if (isSupportReorderColumn) {
        /*For the bug TOOLS-1258 After add column, change column name, it will pop error.*/
        /*Sort the change log first*/
        Collections.sort(allAttrChanges, new ChangeLogCompartor(newSchemaInfo));
        for (SchemaChangeLog log : allAttrChanges) {
            boolean isClassAttr = false;
            if (log.getType() == SchemeInnerType.TYPE_CLASSATTRIBUTE) {
                isClassAttr = true;
            } else {
                isClassAttr = false;
            }
            appendChangeAttributeDDL(oldSchemaInfo, newSchemaInfo, oldSupers, newSupers, attrMap, tableName, log, isClassAttr, generator);
        }
        List<SchemaChangeLog> allPosChangeLogs = changeLogMgr.getPositionChangeLogs();
        for (SchemaChangeLog log : allPosChangeLogs) {
            if (!generator.hasProcessedAttr(log.getOldValue())) {
                for (DBAttribute attr : newSchemaInfo.getAttributes()) {
                    if (attr.getName().equals(log.getOldValue())) {
                        appendChangeAttributeDDL(oldSchemaInfo, newSchemaInfo, oldSupers, newSupers, attrMap, tableName, log, attr.isClassAttribute(), generator);
                        break;
                    }
                }
            }
        }
    } else {
        for (SchemaChangeLog log : allAttrChanges) {
            appendAlterAttributeDDL(oldSchemaInfo, newSchemaInfo, oldSupers, newSupers, attrMap, tableName, log, generator);
        }
        if (isSupportReorderColumn) {
            generator.addSchemaDDLMode(DDLGenerator.TYPE_CHANGE_POS, newSchemaInfo, getAddReorderColumnDDL(oldSchemaInfo, newSchemaInfo, newSupers, tableName));
        }
    }
    //Generate the DDL for super class change
    List<String> oldSuperClasses = oldSchemaInfo.getSuperClasses();
    List<String> newSuperClasses = newSchemaInfo.getSuperClasses();
    List<List<String>> superChanges = getSuperclassChanges(oldSuperClasses, newSuperClasses);
    generator.addSchemaDDLMode(DDLGenerator.TYPE_CHANGE_SUPER, newSchemaInfo, appendChangedSuperDDL(oldSchemaInfo, newSchemaInfo, tableName, oldSuperClasses, newSuperClasses, superChanges));
    //Generate the DDL for PK change
    List<SchemaInfo> allSupers = SuperClassUtil.getSuperClasses(databaseInfo, newSchemaInfo);
    allSupers.addAll(newSupers);
    allSupers.addAll(oldSupers);
    Constraint newPK = newSchemaInfo.getPK(allSupers);
    Constraint oldPK = oldSchemaInfo.getPK(oldSupers);
    if (oldPK == null && newPK != null) {
        // add pk
        List<String> pkAttributes = newPK.getAttributes();
        if (pkAttributes != null && pkAttributes.size() > 0) {
            String addPKDDL = getAddPKDDL(tableName, pkAttributes, newPK.getName()) + endLineChar + StringUtil.NEWLINE;
            generator.addSchemaDDLMode(DDLGenerator.TYPE_ADD_INDEX, newPK, addPKDDL);
        }
    } else if (oldPK != null && newPK == null) {
        // del pk
        String dropPKDDL = dropPK(tableName, oldPK.getName()) + endLineChar + StringUtil.NEWLINE;
        generator.addPreDDLMode(DDLGenerator.TYPE_DROP_INDEX, oldPK, dropPKDDL);
    } else if (oldPK != null && newPK != null) {
        appendChangedPkDDL(attrMap, tableName, newPK, oldPK, generator);
    }
    //Generate the DDL for FK change
    List<SchemaChangeLog> fkChanges = changeLogMgr.getFKChangeLogs();
    for (SchemaChangeLog log : fkChanges) {
        appendChangedFkDDL(oldSchemaInfo, newSchemaInfo, attrMap, tableName, log, generator);
    }
    List<SchemaChangeLog> indexChanges = changeLogMgr.getIndexChangeLogs();
    for (SchemaChangeLog log : indexChanges) {
        appendChanedIndexDDL(oldSchemaInfo, newSchemaInfo, tableName, log, generator);
    }
    // Partitioning
    boolean isPartitionChanged = isPartitonChange(oldSchemaInfo.getPartitionList(), newSchemaInfo.getPartitionList());
    if ("YES".equals(oldSchemaInfo.isPartitionGroup()) && isPartitionChanged) {
        String sql = getTransformToGenericDDL(tableName) + endLineChar + StringUtil.NEWLINE;
        generator.addSchemaDDLMode(DDLGenerator.TYPE_DROP_PARTITON, oldSchemaInfo.getPartitionList(), sql);
    }
    if (isPartitionChanged) {
        List<PartitionInfo> partitionInfoList = newSchemaInfo.getPartitionList();
        String sql = getTransformToPartitionDDL(partitionInfoList);
        if (sql != null) {
            sql = sql + endLineChar + StringUtil.NEWLINE;
            generator.addSchemaDDLMode(DDLGenerator.TYPE_ADD_PARTITON, oldSchemaInfo.getPartitionList(), sql);
        }
    }
    return generator.generatorDDL();
}
Also used : HashMap(java.util.HashMap) Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) List(java.util.List) PartitionInfo(com.cubrid.common.core.common.model.PartitionInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 40 with DBAttribute

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

the class ConstraintComparator method appendAlterAttributeDDL.

/**
	 * Append the changed attribute DDL
	 *
	 * @param oldSchemaInfo the old instance of SchemaInfo
	 * @param newSchemaInfo the new instance of SchemaInfo
	 * @param oldSupers the list which includes the old instance of SchemaInfo
	 * @param newSupers the list which includes the new instance of SchemaInfo
	 * @param attrMap the map which includes all the attribute
	 * @param ddlBuffer the object of StringBuffer
	 * @param tableName the table name
	 * @param changeLog the object of SchemeChangeLog
	 */
private void appendAlterAttributeDDL(SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, List<SchemaInfo> oldSupers, List<SchemaInfo> newSupers, Map<String, String> attrMap, String tableName, SchemaChangeLog changeLog, DDLGenerator generator) {
    boolean isClassAttr = false;
    if (changeLog.getType() == SchemeInnerType.TYPE_CLASSATTRIBUTE) {
        isClassAttr = true;
    } else {
        isClassAttr = false;
    }
    if (StringUtil.isEmpty(changeLog.getOldValue())) {
        // add [class] column
        DBAttribute newAttr = newSchemaInfo.getDBAttributeByName(changeLog.getNewValue(), isClassAttr);
        String addDDL = null;
        if (isClassAttr) {
            addDDL = getAddClassColumnDDL(tableName, newAttr) + endLineChar + StringUtil.NEWLINE;
        } else {
            // only new added attribute and after version 8.4.0 support to
            // reorder
            boolean isSupportReorderColumn = CompatibleUtil.isSupportReorderColumn(databaseInfo);
            if (isSupportReorderColumn) {
                return;
            }
            Constraint pk = newSchemaInfo.getPK(newSupers);
            List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
            addDDL = getAddColumnDDL(tableName, newAttr, pkAttributes, newSchemaInfo) + endLineChar + StringUtil.NEWLINE;
        }
        generator.addSchemaDDLMode(DDLGenerator.TYPE_ADD_ATTR, newAttr, addDDL);
    } else if (StringUtil.isEmpty(changeLog.getNewValue())) {
        // drop [class] column
        DBAttribute oldAttr = oldSchemaInfo.getDBAttributeByName(changeLog.getOldValue(), isClassAttr);
        String attrName = oldAttr.getName();
        StringBuffer dropDDL = new StringBuffer();
        if (isClassAttr) {
            dropDDL.append(getDropClassColumnDDL(tableName, attrName)).append(endLineChar).append(StringUtil.NEWLINE);
        } else {
            dropDDL.append(getDropColumnDDL(tableName, attrName)).append(endLineChar).append(StringUtil.NEWLINE);
        }
        generator.addSchemaDDLMode(DDLGenerator.TYPE_DROP_ATTR, oldAttr, dropDDL.toString());
    } else {
        // edit column
        DBAttribute oldAttr = oldSchemaInfo.getDBAttributeByName(changeLog.getOldValue(), isClassAttr);
        DBAttribute newAttr = newSchemaInfo.getDBAttributeByName(changeLog.getNewValue(), isClassAttr);
        String editDDL = getAlterAttrDDL(oldAttr, newAttr, oldSchemaInfo, newSchemaInfo, oldSupers, newSupers, isClassAttr, attrMap, tableName, generator);
        generator.addSchemaDDLMode(DDLGenerator.TYPE_EDIT_ATTR, newAttr, editDDL);
    }
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute)

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