Search in sources :

Example 21 with SchemaComment

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

the class GetAllSchemaTask method getColumnInfo.

/**
	 * Get column information
	 *
	 * @param schemaInfo the SchemaInfo
	 * @throws SQLException the exception
	 */
private void getColumnInfo(ResultSet rs, SchemaInfo schemaInfo, boolean supportCharset, Map<String, SchemaComment> descriptions) throws SQLException {
    if (schemaInfo == null) {
        return;
    }
    Map<String, String> columnCollMap = null;
    if (supportCharset && isNeedCollationInfo) {
        columnCollMap = GetSchemaTask.extractColumnCollationMap(connection, schemaInfo);
    }
    String attrName = rs.getString("attr_name");
    String type = rs.getString("attr_type");
    String inherit = rs.getString("from_class_name");
    String dataType = rs.getString("data_type");
    String prec = rs.getString("prec");
    String scale = rs.getString("scale");
    String isNull = rs.getString("is_nullable");
    String defaultValue = rs.getString("default_value");
    DBAttribute attr = new DBAttribute();
    attr.setName(attrName);
    if (inherit == null) {
        attr.setInherit(schemaInfo.getClassname());
    } else {
        attr.setInherit(inherit);
    }
    if ("YES".equals(isNull)) {
        //null
        attr.setNotNull(false);
    } else {
        attr.setNotNull(true);
    }
    dataType = DataType.convertAttrTypeString(dataType, prec, scale);
    attr.setType(dataType);
    //Fix bug TOOLS-3093
    defaultValue = DataType.convertDefaultValue(dataType, defaultValue, databaseInfo);
    attr.setDefault(defaultValue);
    //Fixe bug TOOLS-259
    //Different solution between CQB and CM on default value  make some problem
    attr.resetDefault();
    // after 9.1
    if (supportCharset && columnCollMap != null) {
        String collation = columnCollMap.get(attrName);
        attr.setCollation(collation);
    }
    SchemaComment columnSchema = descriptions != null ? descriptions.get(schemaInfo.getClassname() + "*" + attrName) : null;
    if (columnSchema != null) {
        attr.setDescription(columnSchema.getDescription());
    }
    if ("INSTANCE".equals(type)) {
        //INSTANCE
        schemaInfo.addAttribute(attr);
    } else if ("CLASS".equals(type)) {
        schemaInfo.addClassAttribute(attr);
    } else {
        attr.setShared(true);
        schemaInfo.addAttribute(attr);
    }
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SchemaComment(com.cubrid.common.core.schemacomment.model.SchemaComment)

Example 22 with SchemaComment

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

the class SubAttribute method getTableInfo.

/**
	 * Retieves the main information of table.
	 * 
	 * @return schemaInfo SchemaInfo
	 * @throws SQLException the SQLException
	 */
private SchemaInfo getTableInfo() throws SQLException {
    boolean supportCharset = CompatibleUtil.isSupportCreateDBByCharset(databaseInfo);
    // get table comment
    boolean supportComment = SchemaCommentHandler.isInstalledMetaTable(databaseInfo, connection);
    SchemaComment schemaComment = null;
    if (supportComment) {
        schemaComment = SchemaCommentHandler.loadDescription(databaseInfo, connection, tableName).get(tableName + "*");
    }
    //get table information
    String sql = "SELECT * FROM db_class WHERE class_name=?";
    // [TOOLS-2425]Support shard broker
    sql = databaseInfo.wrapShardQuery(sql);
    SchemaInfo schemaInfo = null;
    try {
        stmt = connection.prepareStatement(sql);
        ((PreparedStatement) stmt).setString(1, tableName);
        rs = ((PreparedStatement) stmt).executeQuery();
        // databaseInfo.getServerInfo().compareVersionKey("8.2.2") >= 0;
        boolean isSupportReuseOid = CompatibleUtil.isSupportReuseOID(databaseInfo);
        if (rs.next()) {
            String type = rs.getString("class_type");
            String isSystemClass = rs.getString("is_system_class");
            String owner = rs.getString("owner_name");
            schemaInfo = new SchemaInfo();
            if ("CLASS".equals(type)) {
                schemaInfo.setVirtual(SchemaInfo.VIRTUAL_NORMAL);
            } else {
                schemaInfo.setVirtual(SchemaInfo.VIRTUAL_VIEW);
            }
            if ("NO".equals(isSystemClass)) {
                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);
                }
            }
            if (schemaComment != null) {
                schemaInfo.setDescription(schemaComment.getDescription());
            }
            schemaInfo.setOwner(owner);
            schemaInfo.setClassname(tableName);
            schemaInfo.setDbname(databaseInfo.getDbName());
            schemaInfo.setPartitionGroup(rs.getString("partitioned"));
        }
        // after cubrid 9.1
        if (supportCharset && schemaInfo != null && StringUtil.isEqual(SchemaInfo.VIRTUAL_NORMAL, schemaInfo.getVirtual())) {
            getTableCollation(connection, schemaInfo);
        }
    } finally {
        QueryUtil.freeQuery(stmt, rs);
    }
    return schemaInfo;
}
Also used : SchemaComment(com.cubrid.common.core.schemacomment.model.SchemaComment) PreparedStatement(java.sql.PreparedStatement) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 23 with SchemaComment

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

the class SubAttribute method getColumnInfo.

/**
	 * Get column information
	 * 
	 * @param schemaInfo the SchemaInfo
	 * @throws SQLException the exception
	 */
private void getColumnInfo(SchemaInfo schemaInfo) throws SQLException {
    if (schemaInfo != null) {
        String sql = null;
        Map<String, String> columnCollMap = null;
        boolean supportCharset = CompatibleUtil.isSupportCreateDBByCharset(databaseInfo);
        if (supportCharset && isNeedCollationInfo && StringUtil.isEqual(schemaInfo.getVirtual(), SchemaInfo.VIRTUAL_NORMAL)) {
            columnCollMap = extractColumnCollationMap(connection, schemaInfo);
        }
        // get table comment
        boolean supportComment = SchemaCommentHandler.isInstalledMetaTable(databaseInfo, connection);
        Map<String, SchemaComment> comments = null;
        if (supportComment) {
            comments = SchemaCommentHandler.loadDescriptions(databaseInfo, connection);
        }
        sql = "SELECT *" + " FROM db_attribute" + " WHERE class_name=? " + " ORDER BY def_order";
        // [TOOLS-2425]Support shard broker
        sql = databaseInfo.wrapShardQuery(sql);
        List<String> enumColumnList = new ArrayList<String>();
        try {
            stmt = connection.prepareStatement(sql);
            ((PreparedStatement) stmt).setString(1, tableName);
            rs = ((PreparedStatement) stmt).executeQuery();
            while (rs.next()) {
                String attrName = rs.getString("attr_name");
                String type = rs.getString("attr_type");
                String inherit = rs.getString("from_class_name");
                String dataType = rs.getString("data_type");
                String prec = rs.getString("prec");
                String scale = rs.getString("scale");
                String isNull = rs.getString("is_nullable");
                String defaultValue = rs.getString("default_value");
                DBAttribute attr = new DBAttribute();
                attr.setName(attrName);
                if (inherit == null) {
                    attr.setInherit(tableName);
                } else {
                    attr.setInherit(inherit);
                }
                if ("YES".equals(isNull)) {
                    //null
                    attr.setNotNull(false);
                } else {
                    attr.setNotNull(true);
                }
                if (DataType.DATATYPE_ENUM.equalsIgnoreCase(dataType)) {
                    enumColumnList.add(attrName);
                }
                dataType = DataType.convertAttrTypeString(dataType, prec, scale);
                attr.setType(dataType);
                //Fix bug TOOLS-3093
                defaultValue = DataType.convertDefaultValue(dataType, defaultValue, databaseInfo);
                attr.setDefault(defaultValue);
                //Fixe bug TOOLS-259
                //Different solution between CQB and CM on default value  make some problem
                attr.resetDefault();
                // after 9.1
                if (supportCharset && columnCollMap != null && DataType.canUseCollation(attr.getType())) {
                    String collation = columnCollMap.get(attrName);
                    attr.setCollation(collation);
                }
                SchemaComment schemaComment = comments.get(tableName + "*" + attrName);
                if (schemaComment != null) {
                    attr.setDescription(schemaComment.getDescription());
                }
                if ("INSTANCE".equals(type)) {
                    //INSTANCE
                    schemaInfo.addAttribute(attr);
                } else if ("CLASS".equals(type)) {
                    schemaInfo.addClassAttribute(attr);
                } else {
                    attr.setShared(true);
                    schemaInfo.addAttribute(attr);
                }
            }
        } finally {
            QueryUtil.freeQuery(stmt, rs);
        }
        // Get enumeration
        if (CompatibleUtil.isSupportEnumVersion(databaseInfo) && enumColumnList.size() > 0) {
            String escapedTableName = QuerySyntax.escapeKeyword(schemaInfo.getClassname());
            StringBuilder sb = new StringBuilder();
            sb.append("SHOW COLUMNS FROM ").append(escapedTableName).append(" WHERE FIELD IN (");
            try {
                stmt = connection.createStatement();
                for (int i = 0; i < enumColumnList.size(); i++) {
                    sb.append("'").append(enumColumnList.get(i)).append("'");
                    if (i + 1 < enumColumnList.size()) {
                        sb.append(",");
                    }
                }
                sb.append(");");
                rs = stmt.executeQuery(sb.toString());
                while (rs.next()) {
                    String name = rs.getString("Field");
                    String type = rs.getString("Type");
                    DBAttribute attr = schemaInfo.getDBAttributeByName(name, false);
                    attr.setEnumeration(StringUtil.getEnumeration(type));
                }
            } finally {
                QueryUtil.freeQuery(stmt, rs);
            }
        }
    }
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) SchemaComment(com.cubrid.common.core.schemacomment.model.SchemaComment) PreparedStatement(java.sql.PreparedStatement) Constraint(com.cubrid.common.core.common.model.Constraint)

Aggregations

SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)23 SQLException (java.sql.SQLException)11 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)8 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)8 Constraint (com.cubrid.common.core.common.model.Constraint)6 PreparedStatement (java.sql.PreparedStatement)6 Connection (java.sql.Connection)5 IDatabaseSpec (com.cubrid.common.core.common.model.IDatabaseSpec)4 ResultSet (java.sql.ResultSet)4 HashMap (java.util.HashMap)4 WritableSheet (jxl.write.WritableSheet)4 Statement (java.sql.Statement)3 ArrayList (java.util.ArrayList)3 TableDetailInfo (com.cubrid.common.core.common.model.TableDetailInfo)2 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 PartInitException (org.eclipse.ui.PartInitException)2 DBResolution (com.cubrid.common.core.common.model.DBResolution)1 Trigger (com.cubrid.common.core.common.model.Trigger)1 ITask (com.cubrid.common.core.task.ITask)1