⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dumpmetadatatask.java

📁 OBPM是一个开源
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    /**
     * Dumps all procedures.
     * 
     * @param parent   The parent element
     * @param metaData The database metadata
     */
    private void dumpProcedures(Element parent, DatabaseMetaData metaData) throws SQLException
    {
        ResultSet result = null;

        try
        {
            result = metaData.getProcedures(_catalogPattern, _schemaPattern, _procedurePattern);
        }
        catch (SQLException ex)
        {
            log("Could not determine the procedures: "+ex.getMessage(), Project.MSG_ERR);
            return;
        }

        Element proceduresElem = parent.addElement("procedures");
        Set     columns        = getColumnsInResultSet(result);

        try
        {
            while (result.next())
            {
                String procedureName = getString(result, "PROCEDURE_NAME");
    
                if ((procedureName == null) || (procedureName.length() == 0))
                {
                    continue;
                }

                Element procedureElem = proceduresElem.addElement("procedure");
                String  catalog       = getString(result, "PROCEDURE_CAT");
                String  schema        = getString(result, "PROCEDURE_SCHEM");
    
                log("Reading procedure " + ((schema != null) && (schema.length() > 0) ? schema + "." : "") + procedureName, Project.MSG_INFO);

                procedureElem.addAttribute("name", procedureName);
                if (catalog != null)
                {
                    procedureElem.addAttribute("catalog", catalog);
                }
                if (schema != null)
                {
                    procedureElem.addAttribute("schema", schema);
                }
                addStringAttribute(result, columns, "REMARKS", procedureElem, "remarks");
                if (columns.contains("PROCEDURE_TYPE"))
                {
                    switch (result.getShort("PROCEDURE_TYPE"))
                    {
                        case DatabaseMetaData.procedureReturnsResult:
                            procedureElem.addAttribute("type", "returns result");
                            break;
                        case DatabaseMetaData.procedureNoResult:
                            procedureElem.addAttribute("type", "doesn't return result");
                            break;
                        case DatabaseMetaData.procedureResultUnknown:
                            procedureElem.addAttribute("type", "may return result");
                            break;
                        default:
                            procedureElem.addAttribute("type", "unknown");
                            break;
                    }
                }
    
                dumpProcedure(procedureElem, metaData, "%", "%", procedureName);
            }
        }
        finally
        {
            if (result != null)
            {
                result.close();
            }
        }
    }

    /**
     * Dumps the contents of the indicated procedure.
     * 
     * @param procedureElem The XML element for the procedure
     * @param metaData      The database metadata
     * @param catalogName   The catalog name
     * @param schemaName    The schema name
     * @param procedureName The procedure name
     */
    private void dumpProcedure(Element procedureElem, DatabaseMetaData metaData, String catalogName, String schemaName, String procedureName) throws SQLException
    {
        ResultSet result = null;

        try
        {
            result = metaData.getProcedureColumns(catalogName, schemaName, procedureName, _columnPattern);
        }
        catch (SQLException ex)
        {
            log("Could not determine the columns for procedure '"+procedureName+"': "+ex.getMessage(), Project.MSG_ERR);
            return;
        }

        Set columns = getColumnsInResultSet(result);

        try
        {
            while (result.next())
            {
                String columnName = getString(result, "COLUMN_NAME");
    
                if ((columnName == null) || (columnName.length() == 0))
                {
                    continue;
                }
    
                Element columnElem = procedureElem.addElement("column");
    
                columnElem.addAttribute("name", columnName);
                if (columns.contains("COLUMN_TYPE"))
                {
                    switch (result.getShort("COLUMN_TYPE"))
                    {
                        case DatabaseMetaData.procedureColumnIn:
                            columnElem.addAttribute("type", "in parameter");
                            break;
                        case DatabaseMetaData.procedureColumnInOut:
                            columnElem.addAttribute("type", "in/out parameter");
                            break;
                        case DatabaseMetaData.procedureColumnOut:
                            columnElem.addAttribute("type", "out parameter");
                            break;
                        case DatabaseMetaData.procedureColumnReturn:
                            columnElem.addAttribute("type", "return value");
                            break;
                        case DatabaseMetaData.procedureColumnResult:
                            columnElem.addAttribute("type", "result column in ResultSet");
                            break;
                        default:
                            columnElem.addAttribute("type", "unknown");
                            break;
                    }
                }
    
                addIntAttribute(result, columns, "DATA_TYPE", columnElem, "typeCode");
                addStringAttribute(result, columns, "TYPE_NAME", columnElem, "type");
                addIntAttribute(result, columns, "LENGTH", columnElem, "length");
                addIntAttribute(result, columns, "PRECISION", columnElem, "precision");
                addShortAttribute(result, columns, "SCALE", columnElem, "short");
                addShortAttribute(result, columns, "RADIX", columnElem, "radix");
                if (columns.contains("NULLABLE"))
                {
                    switch (result.getInt("NULLABLE"))
                    {
                        case DatabaseMetaData.procedureNoNulls:
                            columnElem.addAttribute("nullable", "false");
                            break;
                        case DatabaseMetaData.procedureNullable:
                            columnElem.addAttribute("nullable", "true");
                            break;
                        default:
                            columnElem.addAttribute("nullable", "unknown");
                            break;
                    }
                }
                addStringAttribute(result, columns, "REMARKS", columnElem, "remarks");
            }
        }
        finally
        {
            if (result != null)
            {
                result.close();
            }
        }
    }

    /**
     * If the result set contains the indicated column, extracts its value and sets an attribute at the given element.
     * 
     * @param result     The result set
     * @param columns    The columns in the result set
     * @param columnName The name of the column in the result set
     * @param element    The element to add the attribute
     * @param attrName   The name of the attribute to set
     * @return The string value or <code>null</code>
     */
    private String addStringAttribute(ResultSet result, Set columns, String columnName, Element element, String attrName) throws SQLException
    {
        String value = null;

        if (columns.contains(columnName))
        {
            value = getString(result, columnName);
            element.addAttribute(attrName, value);
        }
        return value;
    }

    /**
     * If the result set contains the indicated column, extracts its int value and sets an attribute at the given element.
     * 
     * @param result     The result set
     * @param columns    The columns in the result set
     * @param columnName The name of the column in the result set
     * @param element    The element to add the attribute
     * @param attrName   The name of the attribute to set
     * @return The string value or <code>null</code>
     */
    private String addIntAttribute(ResultSet result, Set columns, String columnName, Element element, String attrName) throws SQLException
    {
        String value = null;

        if (columns.contains(columnName))
        {
        	try
        	{
                value = String.valueOf(result.getInt(columnName));
        	}
        	catch (SQLException ex)
        	{
        		// A few databases do not comply with the jdbc spec and return a string (or null),
        		// so lets try this just in case
        		value = result.getString(columnName);

        		if (value != null)
        		{
	        		try
	        		{
	        			Integer.parseInt(value);
	        		}
	        		catch (NumberFormatException parseEx)
	        		{
	        			// its no int returned as a string, so lets re-throw the original exception
	        			throw ex;
	        		}
        		}
        	}
            element.addAttribute(attrName, value);
        }
        return value;
    }

    /**
     * If the result set contains the indicated column, extracts its short value and sets an attribute at the given element.
     * 
     * @param result     The result set
     * @param columns    The columns in the result set
     * @param columnName The name of the column in the result set
     * @param element    The element to add the attribute
     * @param attrName   The name of the attribute to set
     * @return The string value or <code>null</code>
     */
    private String addShortAttribute(ResultSet result, Set columns, String columnName, Element element, String attrName) throws SQLException
    {
        String value = null;

        if (columns.contains(columnName))
        {
        	try
        	{
                value = String.valueOf(result.getShort(columnName));
        	}
        	catch (SQLException ex)
        	{
        		// A few databases do not comply with the jdbc spec and return a string (or null),
        		// so lets try strings this just in case
        		value = result.getString(columnName);

        		if (value != null)
        		{
	        		try
	        		{
	        			Short.parseShort(value);
	        		}
	        		catch (NumberFormatException parseEx)
	        		{
	        			// its no short returned as a string, so lets re-throw the original exception
	        			throw ex;
	        		}
        		}
        	}
            element.addAttribute(attrName, value);
        }
        return value;
    }

    /**
     * If the result set contains the indicated column, extracts its boolean value and sets an attribute at the given element.
     * 
     * @param result     The result set
     * @param columns    The columns in the result set
     * @param columnName The name of the column in the result set
     * @param element    The element to add the attribute
     * @param attrName   The name of the attribute to set
     * @return The string value or <code>null</code>
     */
    private String addBooleanAttribute(ResultSet result, Set columns, String columnName, Element element, String attrName) throws SQLException
    {
        String value = null;

        if (columns.contains(columnName))
        {
            value = String.valueOf(result.getBoolean(columnName));
            element.addAttribute(attrName, value);
        }
        return value;
    }

    /**
     * Extracts a string from the result set.
     * 
     * @param result     The result set
     * @param columnName The name of the column in the result set
     * @return The string value
     */
    private String getString(ResultSet result, String columnName) throws SQLException
    {
        return result.getString(columnName);
    }
    
    /**
     * Determines the columns that are present in the given result set.
     * 
     * @param resultSet The result set
     * @return The columns
     */
    private Set getColumnsInResultSet(ResultSet resultSet) throws SQLException
    {
        ListOrderedSet    result   = new ListOrderedSet();
        ResultSetMetaData metaData = resultSet.getMetaData();

        for (int idx = 1; idx <= metaData.getColumnCount(); idx++)
        {
            result.add(metaData.getColumnName(idx).toUpperCase());
        }
        
        return result;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -