Search in sources :

Example 1 with TableColumn

use of com.cubrid.cubridmanager.core.cubrid.table.model.TableColumn in project cubrid-manager by CUBRID.

the class GetUserClassColumnsTask method getColumns.

/**
	 * Get all column name and type info into list
	 *
	 * @param tableNames String the table name
	 * @return Map<String, List<TableColumn>>
	 */
public Map<String, List<TableColumn>> getColumns(List<String> tableNames) {
    Map<String, List<TableColumn>> columnsOfTable = new HashMap<String, List<TableColumn>>();
    try {
        if (errorMsg != null && errorMsg.trim().length() > 0) {
            return columnsOfTable;
        }
        if (connection == null || connection.isClosed()) {
            errorMsg = Messages.error_getConnection;
            return columnsOfTable;
        }
        // make talble_pk column hashed string list
        Set<String> hashSet = new HashSet<String>();
        Statement pkStmt = null;
        ResultSet pkRs = null;
        try {
            String sql = new StringBuilder().append("SELECT \n").append("    LOWER(CONCAT(i.class_name, '-', k.key_attr_name)) AS hashcode \n").append("FROM \n").append("    db_index i, \n").append("    db_index_key k \n").append("WHERE \n").append("    i.index_name = k.index_name \n").append("    AND \n").append("    i.class_name = k.class_name \n").append("    AND \n").append("    i.is_primary_key = 'YES'\n").toString();
            // [TOOLS-2425]Support shard broker
            sql = databaseInfo.wrapShardQuery(sql);
            pkStmt = connection.createStatement();
            pkRs = pkStmt.executeQuery(sql.toString());
            while (pkRs.next()) {
                hashSet.add(pkRs.getString(1));
            }
        } catch (SQLException e) {
            LOGGER.error(e.getMessage(), e);
        } finally {
            QueryUtil.freeQuery(pkStmt, pkRs);
        }
        for (String tableName : tableNames) {
            try {
                List<TableColumn> columns = SchemaUtil.getTableColumn(databaseInfo, connection, tableName);
                for (TableColumn dbColumn : columns) {
                    String columnName = dbColumn.getColumnName();
                    String tableColumnHash = (tableName + "-" + columnName).trim().toLowerCase();
                    if (hashSet.contains(tableColumnHash)) {
                        dbColumn.setPrimaryKey(true);
                    }
                }
                Collections.sort(columns);
                columnsOfTable.put(tableName, columns);
            } catch (SQLException e) {
                LOGGER.error(e.getMessage(), e);
                if (e.getErrorCode() == -74) {
                    isInTransation = true;
                }
                if (errorMsg == null) {
                    errorMsg = e.getMessage() + "\r\n";
                } else {
                    errorMsg += e.getMessage() + "\r\n";
                }
            }
        }
    } catch (SQLException e) {
        LOGGER.error(e.getMessage(), e);
        isInTransation = false;
        if (errorMsg == null) {
            errorMsg = e.getMessage() + "\r\n";
        } else {
            errorMsg += e.getMessage() + "\r\n";
        }
    } finally {
        finish();
    }
    return columnsOfTable;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) TableColumn(com.cubrid.cubridmanager.core.cubrid.table.model.TableColumn) HashSet(java.util.HashSet)

Example 2 with TableColumn

use of com.cubrid.cubridmanager.core.cubrid.table.model.TableColumn in project cubrid-manager by CUBRID.

the class CubridUserTableColumnLoader method load.

/**
	 * 
	 * Load children object for parent
	 * 
	 * @param parent the parent node
	 * @param monitor the IProgressMonitor object
	 */
public void load(ICubridNode parent, final IProgressMonitor monitor) {
    synchronized (this) {
        if (isLoaded()) {
            return;
        }
        CubridDatabase database = ((ISchemaNode) parent).getDatabase();
        if (!database.isLogined() || database.getRunningType() == DbRunningType.STANDALONE) {
            parent.removeAllChild();
            CubridNodeManager.getInstance().fireCubridNodeChanged(new CubridNodeChangedEvent((ICubridNode) parent, CubridNodeChangedEventType.CONTAINER_NODE_REFRESH));
            return;
        }
        if (columns == null && !getColumns(parent, monitor, database)) {
            return;
        }
        if (monitor.isCanceled()) {
            columns = null;
            setLoaded(true);
            return;
        }
        parent.removeAllChild();
        String parentId = parent.getId();
        if (columns != null && !columns.isEmpty()) {
            for (TableColumn column : columns) {
                String label = column.getColumnName() + ",";
                label += FieldHandlerUtils.getComleteType(column.getTypeName(), column.getSubElementTypeName(), column.getPrecision(), column.getScale());
                String nodeId = parentId + NODE_SEPARATOR + column.getColumnName();
                ICubridNode node = new DefaultSchemaNode(nodeId, label, "icons/navigator/table_column_item.png");
                if (column.isPrimaryKey()) {
                    node.setIconPath("icons/primary_key.png");
                }
                node.setType(NodeType.TABLE_COLUMN);
                node.setModelObj(column);
                node.setContainer(false);
                parent.addChild(node);
            }
        }
        columns = null;
        setLoaded(true);
        CubridNodeManager.getInstance().fireCubridNodeChanged(new CubridNodeChangedEvent((ICubridNode) parent, CubridNodeChangedEventType.CONTAINER_NODE_REFRESH));
    }
}
Also used : ISchemaNode(com.cubrid.common.ui.spi.model.ISchemaNode) CubridNodeChangedEvent(com.cubrid.common.ui.spi.event.CubridNodeChangedEvent) ICubridNode(com.cubrid.common.ui.spi.model.ICubridNode) CubridDatabase(com.cubrid.common.ui.spi.model.CubridDatabase) DefaultSchemaNode(com.cubrid.common.ui.spi.model.DefaultSchemaNode) TableColumn(com.cubrid.cubridmanager.core.cubrid.table.model.TableColumn)

Example 3 with TableColumn

use of com.cubrid.cubridmanager.core.cubrid.table.model.TableColumn in project cubrid-manager by CUBRID.

the class RenameColumnAction method run.

/**
	 * @see org.eclipse.jface.action.Action#run()
	 */
public void run() {
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window == null) {
        return;
    }
    Object[] obj = this.getSelectedObj();
    if (!isSupported(obj)) {
        setEnabled(false);
        return;
    }
    ISchemaNode cn = (ISchemaNode) obj[0];
    TableColumn tc = (TableColumn) cn.getAdapter(TableColumn.class);
    RenameColumnDialog dialog = new RenameColumnDialog(shell, cn.getParent().getParent().getName(), tc.getColumnName(), cn.getDatabase());
    if (dialog.open() == Dialog.OK) {
        CommonUITool.refreshNavigatorTree((TreeViewer) provider, cn.getParent().getParent());
        ICubridNode tableNode = cn.getParent().getParent();
        if (tableNode != null && tableNode instanceof ISchemaNode) {
            /* Broadcast the view changed */
            QueryEditorUtil.fireSchemaNodeChanged((ISchemaNode) tableNode);
        }
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ISchemaNode(com.cubrid.common.ui.spi.model.ISchemaNode) RenameColumnDialog(com.cubrid.common.ui.cubrid.table.dialog.RenameColumnDialog) ICubridNode(com.cubrid.common.ui.spi.model.ICubridNode) TableColumn(com.cubrid.cubridmanager.core.cubrid.table.model.TableColumn)

Example 4 with TableColumn

use of com.cubrid.cubridmanager.core.cubrid.table.model.TableColumn in project cubrid-manager by CUBRID.

the class CopyNodeAction method run.

public void run() {
    Object[] selectedObjs = this.getSelectedObj();
    if (selectedObjs == null || selectedObjs.length == 0) {
        return;
    }
    StringBuilder names = new StringBuilder();
    for (Object obj : selectedObjs) {
        if (!(obj instanceof ICubridNode)) {
            continue;
        }
        ICubridNode cubridNode = (ICubridNode) obj;
        String type = cubridNode.getType();
        if (names.length() > 0) {
            names.append(",");
        }
        if (type.equals(NodeType.TABLE_COLUMN)) {
            TableColumn tc = (TableColumn) cubridNode.getAdapter(TableColumn.class);
            names.append(tc.getColumnName());
        } else {
            names.append(cubridNode.getLabel());
        }
    }
    if (names.length() > 0) {
        TextTransfer textTransfer = TextTransfer.getInstance();
        Clipboard clipboard = CommonUITool.getClipboard();
        clipboard.setContents(new Object[] { names.toString() }, new Transfer[] { textTransfer });
    }
}
Also used : ICubridNode(com.cubrid.common.ui.spi.model.ICubridNode) Clipboard(org.eclipse.swt.dnd.Clipboard) TableColumn(com.cubrid.cubridmanager.core.cubrid.table.model.TableColumn) TextTransfer(org.eclipse.swt.dnd.TextTransfer)

Example 5 with TableColumn

use of com.cubrid.cubridmanager.core.cubrid.table.model.TableColumn in project cubrid-manager by CUBRID.

the class SchemaUtil method getTableColumn.

public static List<TableColumn> getTableColumn(DatabaseInfo databaseInfo, Connection connection, String tableName) throws SQLException {
    List<TableColumn> columns = new ArrayList<TableColumn>();
    if (connection == null || connection.isClosed()) {
        return columns;
    }
    ResultSet rs = null;
    DatabaseMetaData dbmd = connection.getMetaData();
    try {
        rs = dbmd.getColumns(null, null, tableName, null);
        while (rs.next()) {
            TableColumn dbColumn = new TableColumn();
            //$NON-NLS-1$		
            dbColumn.setColumnName(rs.getString("column_name"));
            String type = rs.getString("type_name");
            int size = rs.getInt("column_size");
            if (DataType.isNotSupportSizeOrPrecision(type)) {
                size = 0;
            }
            //$NON-NLS-1$
            dbColumn.setTypeName(type);
            //$NON-NLS-1$
            dbColumn.setPrecision(size);
            //$NON-NLS-1$
            dbColumn.setScale(rs.getInt("decimal_digits"));
            //$NON-NLS-1$
            dbColumn.setOrdinalPosition(rs.getInt("ordinal_position"));
            columns.add(dbColumn);
        }
    } catch (SQLException ex) {
        /*For bug TOOLS-2818*/
        PreparedStatement pstm = null;
        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 = ? ORDER BY def_order";
        sql = databaseInfo.wrapShardQuery(sql);
        try {
            pstm = connection.prepareStatement(sql);
            pstm.setString(1, tableName);
            rs = pstm.executeQuery();
            while (rs.next()) {
                TableColumn dbColumn = new TableColumn();
                dbColumn.setColumnName(rs.getString("attr_name"));
                String type = rs.getString("data_type");
                int size = rs.getInt("prec");
                if (DataType.isNotSupportSizeOrPrecision(type)) {
                    size = 0;
                }
                //$NON-NLS-1$
                dbColumn.setTypeName(type);
                //$NON-NLS-1$
                dbColumn.setPrecision(size);
                //$NON-NLS-1$
                dbColumn.setScale(rs.getInt("scale"));
                //$NON-NLS-1$
                dbColumn.setOrdinalPosition(rs.getInt("def_order"));
                columns.add(dbColumn);
            }
        } finally {
            QueryUtil.freeQuery(pstm);
        }
    } finally {
        QueryUtil.freeQuery(rs);
    }
    return columns;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DatabaseMetaData(java.sql.DatabaseMetaData) TableColumn(com.cubrid.cubridmanager.core.cubrid.table.model.TableColumn)

Aggregations

TableColumn (com.cubrid.cubridmanager.core.cubrid.table.model.TableColumn)6 ICubridNode (com.cubrid.common.ui.spi.model.ICubridNode)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 ISchemaNode (com.cubrid.common.ui.spi.model.ISchemaNode)2 ResultSet (java.sql.ResultSet)2 RenameColumnDialog (com.cubrid.common.ui.cubrid.table.dialog.RenameColumnDialog)1 CubridNodeChangedEvent (com.cubrid.common.ui.spi.event.CubridNodeChangedEvent)1 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)1 DefaultSchemaNode (com.cubrid.common.ui.spi.model.DefaultSchemaNode)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Clipboard (org.eclipse.swt.dnd.Clipboard)1 TextTransfer (org.eclipse.swt.dnd.TextTransfer)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1