Search in sources :

Example 16 with SchemaComment

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

the class SchemaCommentHandler method bindSchemaInfo.

/**
	 * Bind schema descriptions to SchemaInfo object.
	 *
	 * @param comments
	 * @param schema
	 */
public static void bindSchemaInfo(Map<String, SchemaComment> comments, SchemaInfo schema) {
    if (comments == null || schema == null) {
        return;
    }
    String tableName = schema.getClassname();
    SchemaComment cmt = find(comments, tableName, null);
    if (cmt != null) {
        schema.setDescription(cmt.getDescription());
    }
    if (schema.getAttributes() == null) {
        return;
    }
    for (DBAttribute attr : schema.getAttributes()) {
        if (attr.getName() == null) {
            continue;
        }
        cmt = find(comments, tableName, attr.getName());
        if (cmt != null) {
            attr.setDescription(cmt.getDescription());
        }
    }
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SchemaComment(com.cubrid.common.core.schemacomment.model.SchemaComment)

Example 17 with SchemaComment

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

the class SchemaCommentHandler method loadObjectDescription.

public static SchemaComment loadObjectDescription(IDatabaseSpec dbSpec, Connection conn, String objName, CommentType type) throws SQLException {
    String sql = null;
    switch(type) {
        case INDEX:
            sql = "SELECT index_name, comment " + "FROM db_index " + "WHERE index_name = ?";
            break;
        case VIEW:
            sql = "SELECT vclass_name, comment " + "FROM db_vclass " + "WHERE vclass_name = ?";
            break;
        case SP:
            sql = "SELECT sp_name, comment " + "FROM db_stored_procedure " + "WHERE sp_name = ?";
            break;
        case TRIGGER:
            sql = "SELECT name, comment " + "FROM db_trigger " + "WHERE name = ?";
            break;
        case SERIAL:
            sql = "SELECT name, comment " + "FROM db_serial " + "WHERE name = ?";
            break;
        case USER:
            sql = "SELECT name, comment " + "FROM db_user " + "WHERE name = ?";
            break;
        case PARTITION:
            sql = "SELECT partition_name, comment " + "FROM db_partition " + "WHERE partition_name = ?";
            break;
    }
    // [TOOLS-2425]Support shard broker
    if (dbSpec.isShard()) {
        sql = dbSpec.wrapShardQuery(sql);
    }
    SchemaComment schemaComment = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, objName);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            schemaComment = new SchemaComment();
            schemaComment.setType(type);
            schemaComment.setObjectName(rs.getString(1));
            schemaComment.setDescription(rs.getString(2));
        }
    } catch (SQLException e) {
        QueryUtil.rollback(conn);
        LOGGER.error(e.getMessage(), e);
        throw e;
    } finally {
        QueryUtil.freeQuery(pstmt, rs);
    }
    return schemaComment;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) SchemaComment(com.cubrid.common.core.schemacomment.model.SchemaComment) PreparedStatement(java.sql.PreparedStatement)

Example 18 with SchemaComment

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

the class TestSchemaComment method testGetSet.

public void testGetSet() {
    SchemaComment meta = new SchemaComment();
    meta.setTable("table");
    meta.setColumn("column");
    meta.setDescription("description");
    assertTrue(meta.isTable());
    assertFalse(meta.isColumn());
    assertEquals(meta.getTable(), "table");
    assertEquals(meta.getColumn(), "column");
    assertEquals(meta.getDescription(), "description");
    meta = new SchemaComment();
    meta.setTable("table");
    meta.setColumn(null);
    meta.setDescription("description");
    assertFalse(meta.isTable());
    assertTrue(meta.isColumn());
    assertEquals(meta.getTable(), "table");
    assertNull(meta.getColumn());
    assertEquals(meta.getDescription(), "description");
}
Also used : SchemaComment(com.cubrid.common.core.schemacomment.model.SchemaComment)

Example 19 with SchemaComment

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

the class TableEditorPart method init.

public void init(IEditorSite site, IEditorInput input) throws PartInitException {
    super.init(site, input);
    TableEditorInput tableInfoEditorInput = (TableEditorInput) input;
    this.database = tableInfoEditorInput.getDatabase();
    this.editedTableNode = tableInfoEditorInput.getEditedTableNode();
    this.isNewTableFlag = tableInfoEditorInput.isNewTableFlag();
    this.dbUserList = tableInfoEditorInput.getDbUserList();
    this.showDefaultType = tableInfoEditorInput.getType();
    this.collationList = tableInfoEditorInput.getCollationList();
    if (collationList != null) {
        Collation emptyCollation = new Collation();
        emptyCollation.setCharset("");
        emptyCollation.setName("");
        collationList.add(0, emptyCollation);
    }
    this.oldSchemaInfo = tableInfoEditorInput.getSchemaInfo();
    this.supportCharset = CompatibleUtil.isSupportCreateDBByCharset(database.getDatabaseInfo());
    if (isNewTableFlag) {
        newSchemaInfo = new SchemaInfo();
        //$NON-NLS-1$
        newSchemaInfo.setClassname("");
        newSchemaInfo.setOwner(database.getUserName());
        newSchemaInfo.setDbname(database.getName());
        newSchemaInfo.setType(Messages.userSchema);
        newSchemaInfo.setVirtual(Messages.schemaTypeClass);
        if (database.getDatabaseInfo() != null) {
            newSchemaInfo.setCollation(database.getDatabaseInfo().getCollation());
        }
    } else {
        newSchemaInfo = null;
        if (tableInfoEditorInput.getSchemaInfo() != null) {
            newSchemaInfo = tableInfoEditorInput.getSchemaInfo().clone();
            originalConstraints.addAll(newSchemaInfo.getConstraints());
        }
    }
    if (supportCharset) {
        columnProperites = new String[] { IAttributeColumn.COL_EMPTY, IAttributeColumn.COL_FLAG, IAttributeColumn.COL_NAME, IAttributeColumn.COL_DATATYPE, IAttributeColumn.COL_DEFAULT, IAttributeColumn.COL_AUTO_INCREMENT, IAttributeColumn.COL_NOT_NULL, IAttributeColumn.COL_PK, IAttributeColumn.COL_UK, IAttributeColumn.COL_SHARED, IAttributeColumn.COL_COLLATION, IAttributeColumn.COL_MEMO };
    } else {
        columnProperites = new String[] { IAttributeColumn.COL_EMPTY, IAttributeColumn.COL_FLAG, IAttributeColumn.COL_NAME, IAttributeColumn.COL_DATATYPE, IAttributeColumn.COL_DEFAULT, IAttributeColumn.COL_AUTO_INCREMENT, IAttributeColumn.COL_NOT_NULL, IAttributeColumn.COL_PK, IAttributeColumn.COL_UK, IAttributeColumn.COL_SHARED, IAttributeColumn.COL_MEMO };
    }
    Connection conn = null;
    try {
        conn = JDBCConnectionManager.getConnection(database.getDatabaseInfo(), false);
        IDatabaseSpec dbSpec = database.getDatabaseInfo();
        isSupportTableComment = SchemaCommentHandler.isInstalledMetaTable(dbSpec, conn);
        database.getDatabaseInfo().setSupportTableComment(isSupportTableComment);
        if (isSupportTableComment && !isNewTableFlag && newSchemaInfo != null) {
            Map<String, SchemaComment> map = SchemaCommentHandler.loadDescription(dbSpec, conn, newSchemaInfo.getClassname());
            for (DBAttribute attr : newSchemaInfo.getAttributes()) {
                SchemaComment schemaComment = SchemaCommentHandler.find(map, newSchemaInfo.getClassname(), attr.getName());
                if (schemaComment != null) {
                    attr.setDescription(schemaComment.getDescription());
                }
            }
            // get description for index
            for (Constraint cons : newSchemaInfo.getConstraints()) {
                if (CompatibleUtil.isCommentSupports(dbSpec)) {
                    String indexName = cons.getName();
                    SchemaComment indexComment = SchemaCommentHandler.loadObjectDescription(dbSpec, conn, indexName, CommentType.INDEX);
                    if (indexComment != null) {
                        cons.setDescription(indexComment.getDescription());
                    }
                }
            }
            SchemaComment schemaComment = SchemaCommentHandler.find(map, newSchemaInfo.getClassname(), null);
            if (schemaComment != null) {
                newSchemaInfo.setDescription(schemaComment.getDescription());
            }
        }
    } catch (SQLException e) {
        LOGGER.error("", e);
    } catch (Exception e) {
        LOGGER.error("", e);
    } finally {
        QueryUtil.freeQuery(conn);
    }
    schemaChangeMgr = new SchemaChangeManager(database.getDatabaseInfo(), isNewTableFlag);
    schemaDDL = new SchemaDDL(schemaChangeMgr, database.getDatabaseInfo());
    if (database != null) {
        isSupportChange = CompatibleUtil.isSupportChangeColumn(database.getDatabaseInfo());
    }
    setSite(site);
    setInput(input);
    setPartName(input.getName());
    setTitleToolTip(input.getName());
    setTitleImage(CommonUIPlugin.getImage("icons/navigator/schema_table.png"));
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) SQLException(java.sql.SQLException) SchemaChangeManager(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager) Connection(java.sql.Connection) Collation(com.cubrid.cubridmanager.core.cubrid.database.model.Collation) PartInitException(org.eclipse.ui.PartInitException) SQLException(java.sql.SQLException) IDatabaseSpec(com.cubrid.common.core.common.model.IDatabaseSpec) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) SchemaComment(com.cubrid.common.core.schemacomment.model.SchemaComment) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 20 with SchemaComment

use of com.cubrid.common.core.schemacomment.model.SchemaComment 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)

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