Search in sources :

Example 1 with DBSProcedureParameterKind

use of org.jkiss.dbeaver.model.struct.rdb.DBSProcedureParameterKind in project dbeaver by serge-rider.

the class GenericProcedure method loadProcedureColumns.

private void loadProcedureColumns(DBRProgressMonitor monitor) throws DBException {
    Collection<? extends GenericProcedure> procedures = getContainer().getProcedures(monitor, getName());
    if (procedures == null || !procedures.contains(this)) {
        throw new DBException("Internal error - cannot read columns for procedure '" + getName() + "' because its not found in container");
    }
    Iterator<? extends GenericProcedure> procIter = procedures.iterator();
    GenericProcedure procedure = null;
    final GenericMetaObject pcObject = getDataSource().getMetaObject(GenericConstants.OBJECT_PROCEDURE_COLUMN);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load procedure columns")) {
        final JDBCResultSet dbResult;
        if (functionResultType == null) {
            dbResult = session.getMetaData().getProcedureColumns(getCatalog() == null ? this.getPackage() == null || !this.getPackage().isNameFromCatalog() ? null : this.getPackage().getName() : getCatalog().getName(), getSchema() == null ? null : getSchema().getName(), getName(), getDataSource().getAllObjectsPattern());
        } else {
            dbResult = session.getMetaData().getFunctionColumns(getCatalog() == null ? null : getCatalog().getName(), getSchema() == null ? null : getSchema().getName(), getName(), getDataSource().getAllObjectsPattern());
        }
        try {
            int previousPosition = -1;
            while (dbResult.next()) {
                String columnName = GenericUtils.safeGetString(pcObject, dbResult, JDBCConstants.COLUMN_NAME);
                int columnTypeNum = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.COLUMN_TYPE);
                int valueType = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.DATA_TYPE);
                String typeName = GenericUtils.safeGetString(pcObject, dbResult, JDBCConstants.TYPE_NAME);
                int columnSize = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.LENGTH);
                boolean notNull = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.NULLABLE) == DatabaseMetaData.procedureNoNulls;
                int scale = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.SCALE);
                int precision = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.PRECISION);
                //int radix = GenericUtils.safeGetInt(dbResult, JDBCConstants.RADIX);
                String remarks = GenericUtils.safeGetString(pcObject, dbResult, JDBCConstants.REMARKS);
                int position = GenericUtils.safeGetInt(pcObject, dbResult, JDBCConstants.ORDINAL_POSITION);
                DBSProcedureParameterKind parameterType;
                if (functionResultType == null) {
                    switch(columnTypeNum) {
                        case DatabaseMetaData.procedureColumnIn:
                            parameterType = DBSProcedureParameterKind.IN;
                            break;
                        case DatabaseMetaData.procedureColumnInOut:
                            parameterType = DBSProcedureParameterKind.INOUT;
                            break;
                        case DatabaseMetaData.procedureColumnOut:
                            parameterType = DBSProcedureParameterKind.OUT;
                            break;
                        case DatabaseMetaData.procedureColumnReturn:
                            parameterType = DBSProcedureParameterKind.RETURN;
                            break;
                        case DatabaseMetaData.procedureColumnResult:
                            parameterType = DBSProcedureParameterKind.RESULTSET;
                            break;
                        default:
                            parameterType = DBSProcedureParameterKind.UNKNOWN;
                            break;
                    }
                } else {
                    switch(columnTypeNum) {
                        case DatabaseMetaData.functionColumnIn:
                            parameterType = DBSProcedureParameterKind.IN;
                            break;
                        case DatabaseMetaData.functionColumnInOut:
                            parameterType = DBSProcedureParameterKind.INOUT;
                            break;
                        case DatabaseMetaData.functionColumnOut:
                            parameterType = DBSProcedureParameterKind.OUT;
                            break;
                        case DatabaseMetaData.functionReturn:
                            parameterType = DBSProcedureParameterKind.RETURN;
                            break;
                        case DatabaseMetaData.functionColumnResult:
                            parameterType = DBSProcedureParameterKind.RESULTSET;
                            break;
                        default:
                            parameterType = DBSProcedureParameterKind.UNKNOWN;
                            break;
                    }
                }
                if (CommonUtils.isEmpty(columnName) && parameterType == DBSProcedureParameterKind.RETURN) {
                    columnName = "RETURN";
                }
                if (position == 0) {
                    // Some drivers do not return ordinal position (PostgreSQL) but
                    // position is contained in column name
                    Matcher numberMatcher = PATTERN_COL_NAME_NUMERIC.matcher(columnName);
                    if (numberMatcher.matches()) {
                        position = Integer.parseInt(numberMatcher.group(1));
                    }
                }
                if (procedure == null || (previousPosition >= 0 && position <= previousPosition && procIter.hasNext())) {
                    procedure = procIter.next();
                }
                GenericProcedureParameter column = new GenericProcedureParameter(procedure, columnName, typeName, valueType, position, columnSize, scale, precision, notNull, remarks, parameterType);
                procedure.addColumn(column);
                previousPosition = position;
            }
        } finally {
            dbResult.close();
        }
    } catch (SQLException e) {
        throw new DBException(e, getDataSource());
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) GenericMetaObject(org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject) Matcher(java.util.regex.Matcher) SQLException(java.sql.SQLException) DBSProcedureParameterKind(org.jkiss.dbeaver.model.struct.rdb.DBSProcedureParameterKind) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Aggregations

SQLException (java.sql.SQLException)1 Matcher (java.util.regex.Matcher)1 DBException (org.jkiss.dbeaver.DBException)1 GenericMetaObject (org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject)1 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)1 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)1 DBSProcedureParameterKind (org.jkiss.dbeaver.model.struct.rdb.DBSProcedureParameterKind)1