Example usage for java.sql DatabaseMetaData getProcedureColumns

List of usage examples for java.sql DatabaseMetaData getProcedureColumns

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData getProcedureColumns.

Prototype

ResultSet getProcedureColumns(String catalog, String schemaPattern, String procedureNamePattern,
        String columnNamePattern) throws SQLException;

Source Link

Document

Retrieves a description of the given catalog's stored procedure parameter and result columns.

Usage

From source file:org.seasar.dbflute.logic.jdbc.metadata.basic.DfProcedureExtractor.java

protected ResultSet doGetProcedureColumns(DatabaseMetaData metaData, DfProcedureMeta metaInfo)
        throws SQLException {
    final String catalogName = metaInfo.getProcedureCatalog();
    final String schemaName = metaInfo.getProcedureSchema().getPureSchema();
    final String procedurePureName = metaInfo.buildProcedurePureName();
    final String catalogArgName;
    final String procedureArgName;
    if (isDatabaseMySQL() && Srl.is_NotNull_and_NotTrimmedEmpty(catalogName)) {
        // getProcedureColumns() of MySQL requires qualified procedure name when other catalog
        catalogArgName = catalogName;//w  w  w .ja  v  a  2  s. c  o m
        procedureArgName = Srl.connectPrefix(procedurePureName, catalogName, ".");
    } else if (isDatabaseOracle() && metaInfo.isPackageProcdure()) {
        catalogArgName = metaInfo.getProcedurePackage();
        procedureArgName = procedurePureName; // needs to use pure name
    } else {
        catalogArgName = catalogName;
        procedureArgName = procedurePureName;
    }
    return metaData.getProcedureColumns(catalogArgName, schemaName, procedureArgName, null);
}

From source file:org.springframework.jdbc.core.metadata.GenericCallMetaDataProvider.java

/**
 * Process the procedure column metadata
 *///from   w  w w.  j a  v  a 2s  .  c om
private void processProcedureColumns(DatabaseMetaData databaseMetaData, @Nullable String catalogName,
        @Nullable String schemaName, @Nullable String procedureName) {

    String metaDataCatalogName = metaDataCatalogNameToUse(catalogName);
    String metaDataSchemaName = metaDataSchemaNameToUse(schemaName);
    String metaDataProcedureName = procedureNameToUse(procedureName);
    if (logger.isDebugEnabled()) {
        logger.debug("Retrieving metadata for " + metaDataCatalogName + '/' + metaDataSchemaName + '/'
                + metaDataProcedureName);
    }

    ResultSet procs = null;
    try {
        procs = databaseMetaData.getProcedures(metaDataCatalogName, metaDataSchemaName, metaDataProcedureName);
        List<String> found = new ArrayList<>();
        while (procs.next()) {
            found.add(procs.getString("PROCEDURE_CAT") + '.' + procs.getString("PROCEDURE_SCHEM") + '.'
                    + procs.getString("PROCEDURE_NAME"));
        }
        procs.close();

        if (found.size() > 1) {
            throw new InvalidDataAccessApiUsageException(
                    "Unable to determine the correct call signature - multiple "
                            + "procedures/functions/signatures for '" + metaDataProcedureName + "': found "
                            + found);
        } else if (found.isEmpty()) {
            if (metaDataProcedureName != null && metaDataProcedureName.contains(".")
                    && !StringUtils.hasText(metaDataCatalogName)) {
                String packageName = metaDataProcedureName.substring(0, metaDataProcedureName.indexOf("."));
                throw new InvalidDataAccessApiUsageException(
                        "Unable to determine the correct call signature for '" + metaDataProcedureName
                                + "' - package name should be specified separately using '.withCatalogName(\""
                                + packageName + "\")'");
            } else if ("Oracle".equals(databaseMetaData.getDatabaseProductName())) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Oracle JDBC driver did not return procedure/function/signature for '"
                            + metaDataProcedureName + "' - assuming a non-exposed synonym");
                }
            } else {
                throw new InvalidDataAccessApiUsageException(
                        "Unable to determine the correct call signature - no "
                                + "procedure/function/signature for '" + metaDataProcedureName + "'");
            }
        }

        procs = databaseMetaData.getProcedureColumns(metaDataCatalogName, metaDataSchemaName,
                metaDataProcedureName, null);
        while (procs.next()) {
            String columnName = procs.getString("COLUMN_NAME");
            int columnType = procs.getInt("COLUMN_TYPE");
            if (columnName == null && (columnType == DatabaseMetaData.procedureColumnIn
                    || columnType == DatabaseMetaData.procedureColumnInOut
                    || columnType == DatabaseMetaData.procedureColumnOut)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Skipping metadata for: " + columnType + " " + procs.getInt("DATA_TYPE") + " "
                            + procs.getString("TYPE_NAME") + " " + procs.getInt("NULLABLE")
                            + " (probably a member of a collection)");
                }
            } else {
                CallParameterMetaData meta = new CallParameterMetaData(columnName, columnType,
                        procs.getInt("DATA_TYPE"), procs.getString("TYPE_NAME"),
                        procs.getInt("NULLABLE") == DatabaseMetaData.procedureNullable);
                this.callParameterMetaData.add(meta);
                if (logger.isDebugEnabled()) {
                    logger.debug("Retrieved metadata: " + meta.getParameterName() + " "
                            + meta.getParameterType() + " " + meta.getSqlType() + " " + meta.getTypeName() + " "
                            + meta.isNullable());
                }
            }
        }
    } catch (SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Error while retrieving metadata for procedure columns: " + ex);
        }
    } finally {
        try {
            if (procs != null) {
                procs.close();
            }
        } catch (SQLException ex) {
            if (logger.isWarnEnabled()) {
                logger.warn("Problem closing ResultSet for procedure column metadata: " + ex);
            }
        }
    }
}

From source file:org.wso2.carbon.dataservices.core.description.query.SQLQuery.java

private Object[] getStoredProcFuncProps(String name) throws DataServiceFault, SQLException {
    Connection conn = this.getConfig().createConnection();
    DatabaseMetaData md = conn.getMetaData();
    ResultSet rs = null;/*from  ww w.j  a  va2  s  . com*/
    boolean error = true;
    try {
        rs = md.getProcedureColumns(null, null, name, "%");
        Object[] resultMap = new Object[2];
        if (!rs.next()) {
            rs.close();
            rs = md.getFunctionColumns(null, null, name, "%");
        } else {
            rs.close();
            rs = md.getProcedureColumns(null, null, name, "%");
        }
        resultMap[0] = conn;
        resultMap[1] = rs;
        error = false;
        return resultMap;
    } finally {
        if (error) {
            this.releaseResources(rs, null);
        }
    }
}

From source file:ro.nextreports.designer.dbviewer.DefaultDBViewer.java

public List<DBProcedureColumn> getProcedureColumns(String schema, String catalog, String procedure)
        throws NextSqlException {
    Connection con;//  ww  w . j a  v a2s  . c  o  m
    List<DBProcedureColumn> columns = new ArrayList<DBProcedureColumn>();
    String schemaName;

    try {
        con = Globals.getConnection();
        if (schema == null) {
            schemaName = Globals.getConnection().getMetaData().getUserName();
        } else {
            schemaName = schema;
        }
    } catch (Exception e) {
        throw new NextSqlException("Could not retrieve connection.", e);
    }

    ResultSet rs = null;
    Statement stmt = null;
    try {
        DatabaseMetaData dbmd = con.getMetaData();
        rs = dbmd.getProcedureColumns(catalog, schema, procedure, null);
        while (rs.next()) {
            String name = rs.getString("COLUMN_NAME");
            int returnType = rs.getShort("COLUMN_TYPE");
            String retType;
            if (DatabaseMetaData.procedureColumnIn == returnType) {
                retType = ProcUtil.IN;
            } else if (DatabaseMetaData.procedureColumnOut == returnType) {
                retType = ProcUtil.OUT;
            } else if (DatabaseMetaData.procedureColumnInOut == returnType) {
                retType = ProcUtil.INOUT;
            } else if (DatabaseMetaData.procedureColumnReturn == returnType) {
                retType = ProcUtil.VAL;
            } else {
                retType = ProcUtil.OTHER;
            }
            String dataType = rs.getString("TYPE_NAME");
            int length = rs.getInt("LENGTH");
            int precision = rs.getInt("PRECISION");
            int scale = rs.getInt("SCALE");
            DBProcedureColumn col = new DBProcedureColumn(schema, procedure, name, retType, dataType, length,
                    precision, scale);
            columns.add(col);
        }
        return columns;
    } catch (SQLException e) {
        LOG.error(e.getMessage(), e);
        e.printStackTrace();
        throw new NextSqlException("SQL Exception: " + e.getMessage(), e);
    } finally {
        closeResultSet(rs);
        closeStatement(stmt);
    }
}