Search in sources :

Example 11 with DBResolution

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

the class SuperClassUtil method computingAttributeList.

/**
	 * computing which attributes will be remained
	 * 
	 * @param database DatabaseInfo
	 * @param newSchemaInfo SchemaInfo
	 * @param resolutions List<DBResolution>
	 * @param columnInheritSchemaMap Map<String, List<SchemaInfo>>
	 * @param isClassAttr boolean
	 * @return boolean
	 */
public static boolean computingAttributeList(DatabaseInfo database, SchemaInfo newSchemaInfo, List<DBResolution> resolutions, Map<String, List<SchemaInfo>> columnInheritSchemaMap, boolean isClassAttr) {
    for (Iterator<Entry<String, List<SchemaInfo>>> i = columnInheritSchemaMap.entrySet().iterator(); i.hasNext(); ) {
        Entry<String, List<SchemaInfo>> entry = i.next();
        String columnName = entry.getKey();
        List<SchemaInfo> schemaList = entry.getValue();
        if (schemaList.size() > 1) {
            //there should be a resolution
            List<NewAttribute> attrList = new ArrayList<NewAttribute>();
            //local attribute has the highest priority
            NewAttribute localAttr = null;
            for (SchemaInfo schema : schemaList) {
                DBAttribute attr = schema.getDBAttributeByName(columnName, isClassAttr);
                if (attr == null) {
                    continue;
                }
                if (attr.getInherit().equals(newSchemaInfo.getClassname())) {
                    localAttr = new NewAttribute(attr, schema);
                } else {
                    attrList.add(new NewAttribute(attr, schema));
                }
            }
            /**
				 * compute: <li>if local attribute exists, check whether it is
				 * the lowest in the class hierarchy <li>otherwise, return the
				 * lowest attributes, one or more in a list
				 */
            List<NewAttribute> lowestAttrList;
            try {
                lowestAttrList = getLowestAttributes(attrList, localAttr, database);
            } catch (Exception e) {
                LOGGER.error("", e);
                return false;
            }
            int size = lowestAttrList.size();
            if (size == 1) {
                //there is only one
                schemaList.clear();
                schemaList.add(lowestAttrList.get(0).schema);
            } else {
                //select one
                schemaList.clear();
                boolean found = false;
                for (int j = size - 1; j >= 0; j--) {
                    NewAttribute attr = lowestAttrList.get(j);
                    String column = attr.attr.getName();
                    String table = attr.schema.getClassname();
                    DBResolution r = getResolution(resolutions, column, table);
                    if (r != null && (r.getAlias() == null || "".equals(r.getAlias()))) {
                        found = true;
                        schemaList.add(attr.schema);
                    }
                }
                if (!found) {
                    schemaList.add(lowestAttrList.get(0).schema);
                }
            }
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) Constraint(com.cubrid.common.core.common.model.Constraint) Entry(java.util.Map.Entry) DBResolution(com.cubrid.common.core.common.model.DBResolution) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) List(java.util.List) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 12 with DBResolution

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

the class SuperClassUtil method resetAttribute.

/**
	 * when super classes change, attributes of a schema changes, not only the
	 * number of attributes, but also the order of attributes
	 * 
	 * @param newSchemaInfo SchemaInfo
	 * @param schemalist List<SchemaInfo>
	 * @param resolutions List<DBResolution>
	 * @param localAttributes List<DBAttribute>
	 * @param columnInheritSchemaMap Map<String, List<SchemaInfo>>
	 * @param isClassAttr boolean
	 */
private static void resetAttribute(SchemaInfo newSchemaInfo, List<SchemaInfo> schemalist, List<DBResolution> resolutions, List<DBAttribute> localAttributes, Map<String, List<SchemaInfo>> columnInheritSchemaMap, boolean isClassAttr) {
    //reorder attributes		
    List<DBAttribute> newAttrList = new ArrayList<DBAttribute>();
    //add inherit attributes at first
    for (int j = 0; j < schemalist.size(); j++) {
        SchemaInfo schema = schemalist.get(j);
        List<DBAttribute> attrList = null;
        if (isClassAttr) {
            attrList = schema.getClassAttributes();
        } else {
            attrList = schema.getAttributes();
        }
        for (int i = 0; i < attrList.size(); i++) {
            DBAttribute attr = attrList.get(i);
            String columnName = attr.getName();
            List<SchemaInfo> list = columnInheritSchemaMap.get(columnName);
            /**
				 * <li>if it is in statistic map, it should be remained; <li>if
				 * it is not in, but it has an alias name, it should be remained
				 * too;
				 */
            if (list.contains(schema)) {
                newAttrList.add(attr);
            } else {
                String tableName = schema.getClassname();
                DBResolution r = getResolution(resolutions, columnName, tableName);
                if (r != null && r.getAlias() != null && !r.getAlias().equals("")) {
                    //modify attr name
                    attr.setName(r.getAlias());
                    newAttrList.add(attr);
                }
            }
        }
    }
    //at last, add local attributes 		
    newAttrList.addAll(localAttributes);
    if (isClassAttr) {
        newSchemaInfo.setClassAttributes(newAttrList);
    } else {
        newSchemaInfo.setAttributes(newAttrList);
    }
}
Also used : DBResolution(com.cubrid.common.core.common.model.DBResolution) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 13 with DBResolution

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

the class SuperClassUtil method addDefaultResolution.

/**
	 * add default resolution aligning with the list of conflicts
	 * 
	 * @param oldResolutions List<DBResolution>
	 * @param newResolutions List<DBResolution>
	 * @param conflicts List<String[]>
	 * @param newSchemaInfo SchemaInfo
	 */
public static void addDefaultResolution(List<DBResolution> oldResolutions, List<DBResolution> newResolutions, List<String[]> conflicts, SchemaInfo newSchemaInfo) {
    List<String> localAttrList = new ArrayList<String>();
    String table = newSchemaInfo.getClassname();
    if (table == null) {
        table = "";
    }
    for (String[] strs : conflicts) {
        String tbl = strs[2];
        String col = strs[0];
        //local attribute has the highest priority
        if (table.equals(tbl)) {
            localAttrList.add(col);
            continue;
        } else {
            if (localAttrList.contains(col)) {
                continue;
            }
        }
        boolean found = false;
        boolean hasAlias = false;
        for (DBResolution r : newResolutions) {
            String columnName = r.getName();
            if (col.equals(columnName)) {
                if (r.getAlias().equals("")) {
                    found = true;
                } else {
                    if (tbl.equals(r.getClassName())) {
                        hasAlias = true;
                    }
                }
            }
        }
        //if current conflict has an alias, it should not be considered.
        if (hasAlias) {
            continue;
        }
        if (!found) {
            //if not found in conflicts, check whether an resolution exist
            for (DBResolution r : oldResolutions) {
                String columnName = r.getName();
                if (col.equals(columnName)) {
                    newResolutions.add(r.clone());
                    found = true;
                    break;
                }
            }
        }
        if (!found) {
            //give a default resolution
            newResolutions.add(new DBResolution(col, tbl, ""));
        }
    }
}
Also used : DBResolution(com.cubrid.common.core.common.model.DBResolution) ArrayList(java.util.ArrayList)

Example 14 with DBResolution

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

the class GetAllSchemaTask method getTableInfo.

/**
	 * Retieves the main information of table.
	 *
	 * @return schemaInfo SchemaInfo
	 * @throws SQLException the SQLException
	 */
private void getTableInfo() throws SQLException {
    boolean isSupportReuseOid = CompatibleUtil.isSupportReuseOID(databaseInfo);
    boolean isSupportCharset = CompatibleUtil.isSupportCreateDBByCharset(databaseInfo);
    String reuseOidCoulmn = isSupportReuseOid ? ",	c.is_reuse_oid_class\n" : "\n";
    String dbName = databaseInfo.getDbName();
    boolean isSupportComment = SchemaCommentHandler.isInstalledMetaTable(databaseInfo, connection);
    Map<String, SchemaComment> descriptions = null;
    if (isSupportComment) {
        descriptions = SchemaCommentHandler.loadDescriptions(databaseInfo, connection);
    }
    String sql = "SELECT a.attr_name, a.attr_type, a.from_class_name," + " a.data_type, a.prec, a.scale, a.is_nullable," + " a.domain_class_name, a.default_value, a.def_order," + " c.is_system_class, c.class_type, c.partitioned, c.owner_name, c.class_name," + " a.from_attr_name" + reuseOidCoulmn + " FROM db_attribute a, db_class c" + " WHERE c.class_name=a.class_name" + " ORDER BY a.class_name, a.def_order";
    // [TOOLS-2425]Support shard broker
    sql = databaseInfo.wrapShardQuery(sql);
    try {
        stmt = connection.prepareStatement(sql);
        rs = ((PreparedStatement) stmt).executeQuery();
        SchemaInfo schemaInfo = null;
        while (rs.next()) {
            String type = rs.getString("class_type");
            boolean isTable = "CLASS".equals(type);
            boolean isUserClass = "NO".equals(rs.getString("is_system_class"));
            String owner = rs.getString("owner_name");
            String className = rs.getString("class_name");
            String partitioned = rs.getString("partitioned");
            schemaInfo = schemas.get(className);
            if (schemaInfo == null) {
                schemaInfo = new SchemaInfo();
                schemas.put(className, schemaInfo);
            }
            if (isTable) {
                schemaInfo.setVirtual(VIRTUAL_NORMAL);
            } else {
                schemaInfo.setVirtual(VIRTUAL_VIEW);
            }
            if (isUserClass) {
                schemaInfo.setType("user");
            } else {
                schemaInfo.setType("system");
            }
            if (isSupportReuseOid) {
                String isReuseOid = rs.getString("is_reuse_oid_class");
                if ("NO".equals(isReuseOid)) {
                    schemaInfo.setReuseOid(false);
                } else {
                    schemaInfo.setReuseOid(true);
                }
            }
            SchemaComment tableComment = isSupportComment ? descriptions.get(className + "*") : null;
            if (tableComment != null) {
                schemaInfo.setDescription(tableComment.getDescription());
            }
            schemaInfo.setOwner(owner);
            schemaInfo.setClassname(className);
            schemaInfo.setDbname(dbName);
            schemaInfo.setPartitionGroup(partitioned);
            getColumnInfo(rs, schemaInfo, isSupportCharset, descriptions);
            String fromAttrName = rs.getString("from_attr_name");
            String attrName = rs.getString("attr_name");
            if (StringUtil.isNotEmpty(fromAttrName) && !fromAttrName.equals(attrName)) {
                DBResolution dbr = new DBResolution();
                dbr.setAlias(attrName);
                dbr.setClassName(rs.getString("from_class_name"));
                dbr.setClassResolution(!rs.getString("attr_type").equals("INSTANCE"));
                dbr.setName(fromAttrName);
                schemaInfo.addResolution(dbr);
            }
            if (isSupportCharset && isNeedCollationInfo) {
                GetSchemaTask.getTableCollation(connection, schemaInfo);
            }
        }
    } finally {
        QueryUtil.freeQuery(stmt, rs);
    }
}
Also used : DBResolution(com.cubrid.common.core.common.model.DBResolution) SchemaComment(com.cubrid.common.core.schemacomment.model.SchemaComment) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 15 with DBResolution

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

the class SuperClassUtil method removeUnusedResolutionForConflict.

/**
	 * remove unneeded resolution for there is no conflict
	 * 
	 * @param resolutions List<DBResolution> the given list that contains the
	 *        instances of DBResolution object
	 * @param conflicts List<String[]> the given list that contains the info of
	 *        conflicts
	 */
private static void removeUnusedResolutionForConflict(List<DBResolution> resolutions, List<String[]> conflicts) {
    //columnName, a.getType(), s.getClassname()
    for (int j = 0; j < resolutions.size(); ) {
        DBResolution resolution = resolutions.get(j);
        boolean found = false;
        for (String[] conflict : conflicts) {
            if (conflict[2].equals(resolution.getClassName()) && conflict[0].equals(resolution.getName())) {
                found = true;
            }
        }
        //if not found in conflicts, so should be removed
        if (found) {
            j++;
        } else {
            resolutions.remove(j);
        }
    }
}
Also used : DBResolution(com.cubrid.common.core.common.model.DBResolution) Constraint(com.cubrid.common.core.common.model.Constraint)

Aggregations

DBResolution (com.cubrid.common.core.common.model.DBResolution)18 Constraint (com.cubrid.common.core.common.model.Constraint)10 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)6 ArrayList (java.util.ArrayList)6 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)5 PartitionInfo (com.cubrid.common.core.common.model.PartitionInfo)2 PartitionType (com.cubrid.common.core.common.model.PartitionType)2 List (java.util.List)2 DBMethod (com.cubrid.common.core.common.model.DBMethod)1 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)1 SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)1 PreparedStatement (java.sql.PreparedStatement)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1