Search in sources :

Example 31 with DBAttribute

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

the class GetAllSchemaTask method getAutoIncrementInfo.

/**
	 * Get auto increment information
	 *
	 * @param schemaInfo the SchemaInfo
	 * @throws SQLException the exception
	 */
private void getAutoIncrementInfo() throws SQLException {
    List<SerialInfo> serialInfoList = new ArrayList<SerialInfo>();
    boolean isSupportCache = CompatibleUtil.isSupportCache(databaseInfo);
    String sql = "SELECT owner.name, db_serial.*" + " FROM db_serial" + " WHERE class_name IS NOT NULL";
    // [TOOLS-2425]Support shard broker
    sql = databaseInfo.wrapShardQuery(sql);
    try {
        stmt = connection.prepareStatement(sql);
        rs = ((PreparedStatement) stmt).executeQuery();
        while (rs.next()) {
            String name = rs.getString("name");
            String owner = rs.getString("owner.name");
            String currentVal = rs.getString("current_val");
            String incrementVal = rs.getString("increment_val");
            String maxVal = rs.getString("max_val");
            String minVal = rs.getString("min_val");
            String cyclic = rs.getString("cyclic");
            String startVal = rs.getString("started");
            String className = rs.getString("class_name");
            String attName = rs.getString("att_name");
            String cacheCount = null;
            if (isSupportCache) {
                cacheCount = rs.getString("cached_num");
            }
            boolean isCycle = false;
            if ("1".equals(cyclic)) {
                isCycle = true;
            }
            SerialInfo serialInfo = new SerialInfo(name, owner, currentVal, incrementVal, maxVal, minVal, isCycle, startVal, cacheCount, className, attName);
            serialInfoList.add(serialInfo);
        }
        for (SerialInfo autoIncrement : serialInfoList) {
            String className = autoIncrement.getClassName();
            String attrName = autoIncrement.getAttName();
            assert (null != attrName);
            SchemaInfo schemaInfo = schemas.get(className);
            if (schemaInfo == null) {
                LOGGER.error("Can't find a table with auto increment column : " + attrName);
                continue;
            }
            DBAttribute a = schemaInfo.getDBAttributeByName(attrName, false);
            if (a != null) {
                a.setAutoIncrement(autoIncrement);
            }
        }
    } finally {
        QueryUtil.freeQuery(stmt, rs);
    }
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 32 with DBAttribute

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

the class GetAllSchemaTask method getConstraintInfo.

/**
	 * Get constraint information
	 *
	 * @param schemaInfo the SchemaInfo
	 * @throws SQLException the exception
	 */
private void getConstraintInfo() throws SQLException {
    Map<String, Map<String, String>> foreignKeys = getForeignKeyInfo();
    boolean isSupportPrefixIndexLength = CompatibleUtil.isSupportPrefixIndexLength(databaseInfo);
    String extraColumns = null;
    if (isSupportPrefixIndexLength) {
        extraColumns = ", key_prefix_length";
    } else {
        extraColumns = "";
    }
    Map<String, Constraint> constraintMap = new HashMap<String, Constraint>();
    String sql = "SELECT i.class_name, i.index_name, i.is_unique, i.is_reverse," + " i.is_primary_key, i.is_foreign_key, i.key_count," + " k.key_attr_name, k.asc_desc, k.key_order" + extraColumns + " FROM db_index i, db_index_key k" + " WHERE i.class_name=k.class_name AND i.index_name=k.index_name" + " ORDER BY i.class_name, i.index_name, k.key_order";
    // [TOOLS-2425]Support shard broker
    sql = databaseInfo.wrapShardQuery(sql);
    try {
        stmt = connection.prepareStatement(sql);
        rs = ((PreparedStatement) stmt).executeQuery();
        Map<String, String> constraint2Unique = new HashMap<String, String>();
        while (rs.next()) {
            String className = rs.getString("class_name");
            String constraintName = rs.getString("index_name");
            String pk = rs.getString("is_primary_key");
            String fk = rs.getString("is_foreign_key");
            String unique = rs.getString("is_unique");
            String reverse = rs.getString("is_reverse");
            int keyCount = rs.getInt("key_count");
            SchemaInfo schemaInfo = schemas.get(className);
            if (schemaInfo == null) {
                LOGGER.error("Table " + className + " not found on the schema info.");
                continue;
            }
            String constraintKey = className + "_" + constraintName;
            Constraint constraint = constraintMap.get(constraintKey);
            if (constraint == null) {
                constraint = new Constraint(false);
                constraint.setName(constraintName);
                schemaInfo.addConstraint(constraint);
                constraintMap.put(constraintKey, constraint);
                if (StringUtil.booleanValueWithYN(pk)) {
                    constraint.setType(Constraint.ConstraintType.PRIMARYKEY.getText());
                } else if (StringUtil.booleanValueWithYN(fk)) {
                    constraint.setType(Constraint.ConstraintType.FOREIGNKEY.getText());
                } else {
                    if (StringUtil.booleanValueWithYN(unique) && !StringUtil.booleanValueWithYN(reverse)) {
                        constraint.setType(Constraint.ConstraintType.UNIQUE.getText());
                    } else if (!StringUtil.booleanValueWithYN(unique) && StringUtil.booleanValueWithYN(reverse)) {
                        constraint.setType(Constraint.ConstraintType.REVERSEINDEX.getText());
                    } else if (StringUtil.booleanValueWithYN(unique) && StringUtil.booleanValueWithYN(reverse)) {
                        constraint.setType(Constraint.ConstraintType.REVERSEUNIQUE.getText());
                    } else if (!StringUtil.booleanValueWithYN(unique) && !StringUtil.booleanValueWithYN(reverse)) {
                        constraint.setType(Constraint.ConstraintType.INDEX.getText());
                    }
                }
                constraint.setKeyCount(keyCount);
                constraint2Unique.put(constraintName, unique);
            }
            String attrName = rs.getString("key_attr_name");
            String ascDesc = rs.getString("asc_desc");
            String indexPrefix = "";
            if (isSupportPrefixIndexLength) {
                int indexLength = rs.getInt("key_prefix_length");
                if (indexLength > 0) {
                    indexPrefix = "(" + indexLength + ")";
                }
            }
            constraint.addAttribute(attrName);
            if (Constraint.ConstraintType.FOREIGNKEY.getText().equals(constraint.getType())) {
                String key = className + "." + attrName;
                Map<String, String> fkInfo = foreignKeys.get(key);
                if (null != fkInfo) {
                    //.append(" ON CACHE OBJECT {3}");
                    constraint.addRule("REFERENCES " + fkInfo.get("PKTABLE_NAME"));
                    constraint.addRule("ON DELETE " + fkInfo.get("DELETE_RULE"));
                    constraint.addRule("ON UPDATE " + fkInfo.get("UPDATE_RULE"));
                }
            } else {
                constraint.addRule(attrName + indexPrefix + " " + ascDesc);
            }
            //set the db attributes' unique property.
            if (StringUtil.booleanValueWithYN(constraint2Unique.get(constraint.getName()))) {
                DBAttribute dba = schemaInfo.getDBAttributeByName(attrName, true);
                if (dba == null) {
                    dba = schemaInfo.getDBAttributeByName(attrName, false);
                }
                if (null != dba) {
                    dba.setUnique(true);
                }
            }
        }
    } finally {
        QueryUtil.freeQuery(stmt, rs);
    }
}
Also used : HashMap(java.util.HashMap) Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) HashMap(java.util.HashMap) Map(java.util.Map) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 33 with DBAttribute

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

the class GetAllSchemaTask method getTypeInfo.

/**
	 * Get type information
	 *
	 * @param schemaInfo the SchemaInfo
	 * @throws SQLException the exception
	 */
private void getTypeInfo() throws SQLException {
    String sql = "SELECT a.class_name, a.attr_name, a.attr_type," + " a.data_type, a.prec, a.scale" + " FROM db_attr_setdomain_elm a" + " ORDER BY a.class_name, a.attr_name";
    // [TOOLS-2425]Support shard broker
    sql = databaseInfo.wrapShardQuery(sql);
    try {
        stmt = connection.prepareStatement(sql);
        rs = ((PreparedStatement) stmt).executeQuery();
        Map<String, Map<String, List<SubAttribute>>> schemaColumnMap = new HashMap<String, Map<String, List<SubAttribute>>>();
        while (rs.next()) {
            String className = rs.getString("class_name");
            String attrName = rs.getString("attr_name");
            String type = rs.getString("attr_type");
            String dateType = rs.getString("data_type");
            String prec = rs.getString("prec");
            String scale = rs.getString("scale");
            String subType = DataType.convertAttrTypeString(dateType, prec, scale);
            Map<String, List<SubAttribute>> columnMap = schemaColumnMap.get("className");
            if (columnMap == null) {
                columnMap = new HashMap<String, List<SubAttribute>>();
                schemaColumnMap.put(className, columnMap);
            }
            List<SubAttribute> subList = columnMap.get(attrName);
            if (subList == null) {
                subList = new ArrayList<SubAttribute>();
                columnMap.put(attrName, subList);
            }
            subList.add(new SubAttribute(type, subType));
        }
        for (Entry<String, Map<String, List<SubAttribute>>> entryMap : schemaColumnMap.entrySet()) {
            String className = entryMap.getKey();
            Map<String, List<SubAttribute>> columnMap = entryMap.getValue();
            SchemaInfo schemaInfo = schemas.get(className);
            if (schemaInfo == null) {
                LOGGER.error("Table " + className + " not found on the schema info.");
                continue;
            }
            for (Entry<String, List<SubAttribute>> entry : columnMap.entrySet()) {
                String name = entry.getKey();
                List<SubAttribute> subList = entry.getValue();
                DBAttribute attr = null;
                if ("INSTANCE".equals(subList.get(0).getParentInstanceType())) {
                    //INSTANCE
                    attr = schemaInfo.getDBAttributeByName(name, false);
                } else if ("CLASS".equals(subList.get(0).getParentInstanceType())) {
                    attr = schemaInfo.getDBAttributeByName(name, true);
                } else {
                    attr = schemaInfo.getDBAttributeByName(name, false);
                }
                StringBuilder sb = new StringBuilder();
                sb.append(attr.getType()).append("(");
                int size = subList.size();
                for (int i = 0; i < size; i++) {
                    SubAttribute subType = subList.get(i);
                    sb.append(subType.getsubDataType());
                    if (i + 1 < size) {
                        sb.append(",");
                    }
                }
                sb.append(")");
                attr.setType(sb.toString());
            }
        }
    } finally {
        QueryUtil.freeQuery(stmt, rs);
    }
}
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) HashMap(java.util.HashMap) Map(java.util.Map) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 34 with DBAttribute

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

the class SuperClassUtil method getColumnConflicts.

/**
	 * given a set of super classes, return the conflict attribute information,
	 * including: <li>attribute name <li>data type <li>name of the table which
	 * contains the attribute
	 * 
	 * @param database DatabaseInfo
	 * @param newSchemaInfo SchemaInfo
	 * @param superClasses List<String>
	 * @param isClassAttr boolean
	 * @return List<String[]>
	 */
public static List<String[]> getColumnConflicts(DatabaseInfo database, SchemaInfo newSchemaInfo, List<String> superClasses, boolean isClassAttr) {
    List<DBAttribute> localAttributes = new ArrayList<DBAttribute>();
    if (isClassAttr) {
        localAttributes.addAll(newSchemaInfo.getLocalClassAttributes());
    } else {
        localAttributes.addAll(newSchemaInfo.getLocalAttributes());
    }
    //		List<String> superClasses = newSchema.getSuperClasses();
    Map<String, List<SchemaInfo>> columnInheritSchemaMap = new HashMap<String, List<SchemaInfo>>();
    addAttributes2StatisticMap(columnInheritSchemaMap, newSchemaInfo, localAttributes, isClassAttr);
    addAttributes2StatisticMap(columnInheritSchemaMap, database, superClasses, isClassAttr);
    List<String[]> retList = new ArrayList<String[]>();
    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) {
            for (SchemaInfo s : schemaList) {
                DBAttribute a = s.getDBAttributeByName(columnName, isClassAttr);
                if (a == null) {
                    continue;
                }
                String[] strs = { columnName, a.getType(), s.getClassname() };
                retList.add(strs);
            }
        }
    }
    return retList;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Entry(java.util.Map.Entry) 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 35 with DBAttribute

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

the class SuperClassUtil method fireSuperClassChanged.

/**
	 * reset attributes and resolutions when super classes change, eg: <li>when
	 * adding a super class, some naming conflicts maybe occurs, so some
	 * resolution would be added, attributes to the schema should be reset and
	 * ordered
	 * 
	 * @param database DatabaseInfo the given reference of a DatabaseInfo object
	 * @param oldSchemaInfo SchemaInfo the given reference of old SchemaInfo
	 *        object
	 * @param newSchemaInfo SchemaInfo the given reference of new SchemaInfo
	 *        object
	 * @param newSupers List<String>
	 * @return boolean whether is succeed
	 */
public static boolean fireSuperClassChanged(DatabaseInfo database, SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, List<String> newSupers) {
    //checking attribute
    List<DBResolution> newResolutions = getCloneResolutions(newSchemaInfo.getResolutions());
    List<DBAttribute> localAttributes = newSchemaInfo.getLocalAttributes();
    Map<String, List<SchemaInfo>> columnInheritSchemaMap = new HashMap<String, List<SchemaInfo>>();
    boolean success = checkingOnSuperClassChanged(columnInheritSchemaMap, database, oldSchemaInfo, newSchemaInfo, newSupers, localAttributes, newResolutions, false);
    if (!success) {
        return false;
    }
    //checking class attributes
    List<DBResolution> newClassResolutions = getCloneResolutions(newSchemaInfo.getClassResolutions());
    List<DBAttribute> localClassAttributes = newSchemaInfo.getLocalClassAttributes();
    Map<String, List<SchemaInfo>> classColumnInheritSchemaMap = new HashMap<String, List<SchemaInfo>>();
    boolean classSuccess = checkingOnSuperClassChanged(classColumnInheritSchemaMap, database, oldSchemaInfo, newSchemaInfo, newSupers, localClassAttributes, newClassResolutions, true);
    if (!classSuccess) {
        return false;
    }
    if (database == null) {
        return false;
    }
    //reset resolution, super classes and resolution
    List<SchemaInfo> schemalist = new ArrayList<SchemaInfo>();
    for (String sup : newSupers) {
        schemalist.add(database.getSchemaInfo(sup).clone());
    }
    if (success && classSuccess) {
        //remove inherit constraint
        List<SchemaInfo> superList = getSuperClasses(database, newSchemaInfo);
        List<Constraint> constraints = newSchemaInfo.getConstraints();
        for (int j = constraints.size() - 1; j >= 0; j--) {
            Constraint constraint = constraints.get(j);
            String constraintType = constraint.getType();
            String constraintName = constraint.getName();
            boolean isConstraintInheritSupported = isConstraintTypeInherit(constraintType);
            if (isConstraintInheritSupported && newSchemaInfo.isInSuperClasses(superList, constraintName)) {
                constraints.remove(j);
            }
        }
        resetAttribute(newSchemaInfo, schemalist, newResolutions, localAttributes, columnInheritSchemaMap, false);
        newSchemaInfo.setResolutions(newResolutions);
        resetAttribute(newSchemaInfo, schemalist, newClassResolutions, localClassAttributes, classColumnInheritSchemaMap, true);
        newSchemaInfo.setClassResolutions(newClassResolutions);
        newSchemaInfo.setSuperClasses(newSupers);
        //add inherit constraint
        superList = getSuperClasses(database, newSupers);
        for (SchemaInfo schema : superList) {
            constraints = schema.getConstraints();
            for (int i = 0; i < constraints.size(); i++) {
                Constraint constraint = constraints.get(i);
                String constraintType = constraint.getType();
                boolean isConstraintInheritSupported = isConstraintTypeInherit(constraintType);
                if (isConstraintInheritSupported) {
                    newSchemaInfo.addConstraint(constraint);
                }
            }
        }
    }
    return true;
}
Also used : HashMap(java.util.HashMap) Constraint(com.cubrid.common.core.common.model.Constraint) ArrayList(java.util.ArrayList) Constraint(com.cubrid.common.core.common.model.Constraint) 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)

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