Example usage for java.sql DatabaseMetaData procedureColumnUnknown

List of usage examples for java.sql DatabaseMetaData procedureColumnUnknown

Introduction

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

Prototype

int procedureColumnUnknown

To view the source code for java.sql DatabaseMetaData procedureColumnUnknown.

Click Source Link

Document

Indicates that type of the column is unknown.

Usage

From source file:org.executequery.databasemediators.spi.DefaultStatementExecutor.java

/** <p>Executes the specified procedure.
 *
 *  @param  the SQL procedure to execute
 *  @return the query result/*www. j a  va 2  s.c o m*/
 */
public SqlStatementResult execute(DatabaseExecutable databaseExecutable) throws SQLException {

    if (!prepared()) {

        return statementResult;
    }

    ProcedureParameter[] param = databaseExecutable.getParametersArray();
    Arrays.sort(param, new ProcedureParameterSorter());

    String procQuery = null;
    boolean hasOut = false;
    boolean hasParameters = (param != null && param.length > 0);

    List<ProcedureParameter> outs = null;
    List<ProcedureParameter> ins = null;

    if (hasParameters) {

        // split the params into ins and outs
        outs = new ArrayList<ProcedureParameter>();
        ins = new ArrayList<ProcedureParameter>();

        int type = -1;
        for (int i = 0; i < param.length; i++) {
            type = param[i].getType();
            if (type == DatabaseMetaData.procedureColumnIn || type == DatabaseMetaData.procedureColumnInOut) {

                // add to the ins list
                ins.add(param[i]);

            } else if (type == DatabaseMetaData.procedureColumnOut
                    || type == DatabaseMetaData.procedureColumnResult
                    || type == DatabaseMetaData.procedureColumnReturn
                    || type == DatabaseMetaData.procedureColumnUnknown
                    || type == DatabaseMetaData.procedureColumnInOut) {

                // add to the outs list
                outs.add(param[i]);

            }
        }

        char QUESTION_MARK = '?';
        String COMMA = ", ";

        // init the string buffer
        StringBuilder sb = new StringBuilder("{ ");
        if (!outs.isEmpty()) {

            // build the out params place holders
            for (int i = 0, n = outs.size(); i < n; i++) {

                sb.append(QUESTION_MARK);

                if (i < n - 1) {

                    sb.append(COMMA);
                }

            }

            sb.append(" = ");
        }

        sb.append(" call ");

        if (databaseExecutable.supportCatalogOrSchemaInFunctionOrProcedureCalls()) {

            String namePrefix = null;
            if (databaseExecutable.supportCatalogInFunctionOrProcedureCalls()) {

                namePrefix = databaseExecutable.getCatalogName();

            }
            if (databaseExecutable.supportSchemaInFunctionOrProcedureCalls()) {

                namePrefix = databaseExecutable.getSchemaName();

            }

            if (namePrefix != null) {

                sb.append(namePrefix).append('.');
            }
        }

        sb.append(databaseExecutable.getName()).append("( ");

        // build the ins params place holders
        for (int i = 0, n = ins.size(); i < n; i++) {
            sb.append(QUESTION_MARK);
            if (i < n - 1) {
                sb.append(COMMA);
            }
        }

        sb.append(" ) }");

        // determine if we have out params
        hasOut = !(outs.isEmpty());
        procQuery = sb.toString();
    } else {
        StringBuilder sb = new StringBuilder();
        sb.append("{ call ");

        if (databaseExecutable.getSchemaName() != null) {
            sb.append(databaseExecutable.getSchemaName()).append('.');
        }

        sb.append(databaseExecutable.getName()).append("( ) }");

        procQuery = sb.toString();
    }

    //Log.debug(procQuery);

    // null value literal
    String NULL = "null";

    // clear any warnings
    conn.clearWarnings();

    Log.info("Executing: " + procQuery);

    CallableStatement cstmnt = null;
    try {
        // prepare the statement
        cstmnt = conn.prepareCall(procQuery);
        stmnt = cstmnt;
    } catch (SQLException e) {
        handleException(e);
        statementResult.setSqlException(e);
        return statementResult;
    }

    // check if we are passing parameters
    if (hasParameters) {
        // the parameter index counter
        int index = 1;

        // the java.sql.Type value
        int dataType = -1;

        // the parameter input value
        String value = null;

        // register the out params
        for (int i = 0, n = outs.size(); i < n; i++) {
            //Log.debug("setting out at index: " + index);
            cstmnt.registerOutParameter(index, outs.get(i).getDataType());
            index++;
        }

        try {

            // register the in params
            for (int i = 0, n = ins.size(); i < n; i++) {

                ProcedureParameter procedureParameter = ins.get(i);
                value = procedureParameter.getValue();
                dataType = procedureParameter.getDataType();

                // try infer a type if OTHER
                if (dataType == Types.OTHER) {

                    // checking only for bit/bool for now

                    if (isTrueFalse(value)) {

                        dataType = Types.BOOLEAN;

                    } else if (isBit(value)) {

                        dataType = Types.BIT;
                        value = value.substring(2, value.length() - 1);
                    }

                }

                if (MiscUtils.isNull(value) || value.equalsIgnoreCase(NULL)) {

                    cstmnt.setNull(index, dataType);

                } else {

                    switch (dataType) {

                    case Types.TINYINT:
                        byte _byte = Byte.valueOf(value).byteValue();
                        cstmnt.setShort(index, _byte);
                        break;

                    case Types.SMALLINT:
                        short _short = Short.valueOf(value).shortValue();
                        cstmnt.setShort(index, _short);
                        break;

                    case Types.LONGVARCHAR:
                    case Types.CHAR:
                    case Types.VARCHAR:
                        cstmnt.setString(index, value);
                        break;

                    case Types.BIT:
                    case Types.BOOLEAN:

                        boolean _boolean = false;
                        if (NumberUtils.isNumber(value)) {

                            int number = Integer.valueOf(value);
                            if (number > 0) {

                                _boolean = true;
                            }

                        } else {

                            _boolean = Boolean.valueOf(value).booleanValue();
                        }

                        cstmnt.setBoolean(index, _boolean);
                        break;

                    case Types.BIGINT:
                        long _long = Long.valueOf(value).longValue();
                        cstmnt.setLong(index, _long);
                        break;

                    case Types.INTEGER:
                        int _int = Integer.valueOf(value).intValue();
                        cstmnt.setInt(index, _int);
                        break;

                    case Types.REAL:
                        float _float = Float.valueOf(value).floatValue();
                        cstmnt.setFloat(index, _float);
                        break;

                    case Types.NUMERIC:
                    case Types.DECIMAL:
                        cstmnt.setBigDecimal(index, new BigDecimal(value));
                        break;
                    /*
                                      case Types.DATE:
                                      case Types.TIMESTAMP:
                                      case Types.TIME:
                                        cstmnt.setTimestamp(index, new Timestamp( BigDecimal(value));
                    */
                    case Types.FLOAT:
                    case Types.DOUBLE:
                        double _double = Double.valueOf(value).doubleValue();
                        cstmnt.setDouble(index, _double);
                        break;

                    default:
                        cstmnt.setObject(index, value);

                    }

                }

                // increment the index
                index++;
            }

        } catch (Exception e) {

            statementResult.setOtherErrorMessage(e.getClass().getName() + ": " + e.getMessage());
            return statementResult;
        }

    }

    /*
    test creating function for postgres:
            
    CREATE FUNCTION concat_lower_or_upper(a text, b text, uppercase boolean DEFAULT false)
    RETURNS text
    AS
    $$
     SELECT CASE
        WHEN $3 THEN UPPER($1 || ' ' || $2)
        ELSE LOWER($1 || ' ' || $2)
        END;
    $$
    LANGUAGE SQL IMMUTABLE STRICT;
    */

    try {
        cstmnt.clearWarnings();
        boolean hasResultSet = cstmnt.execute();
        Map<String, Object> results = new HashMap<String, Object>();

        if (hasOut) {
            // incrementing index
            int index = 1;

            // return value from each registered out
            String returnValue = null;

            for (int i = 0; i < param.length; i++) {

                int type = param[i].getType();
                int dataType = param[i].getDataType();

                if (type == DatabaseMetaData.procedureColumnOut
                        || type == DatabaseMetaData.procedureColumnResult
                        || type == DatabaseMetaData.procedureColumnReturn
                        || type == DatabaseMetaData.procedureColumnUnknown
                        || type == DatabaseMetaData.procedureColumnInOut) {

                    switch (dataType) {

                    case Types.TINYINT:
                        returnValue = Byte.toString(cstmnt.getByte(index));
                        break;

                    case Types.SMALLINT:
                        returnValue = Short.toString(cstmnt.getShort(index));
                        break;

                    case Types.LONGVARCHAR:
                    case Types.CHAR:
                    case Types.VARCHAR:
                        returnValue = cstmnt.getString(index);
                        break;

                    case Types.BIT:
                    case Types.BOOLEAN:
                        returnValue = Boolean.toString(cstmnt.getBoolean(index));
                        break;

                    case Types.INTEGER:
                        returnValue = Integer.toString(cstmnt.getInt(index));
                        break;

                    case Types.BIGINT:
                        returnValue = Long.toString(cstmnt.getLong(index));
                        break;

                    case Types.REAL:
                        returnValue = Float.toString(cstmnt.getFloat(index));
                        break;

                    case Types.NUMERIC:
                    case Types.DECIMAL:
                        returnValue = cstmnt.getBigDecimal(index).toString();
                        break;

                    case Types.DATE:
                    case Types.TIME:
                    case Types.TIMESTAMP:
                        returnValue = cstmnt.getDate(index).toString();
                        break;

                    case Types.FLOAT:
                    case Types.DOUBLE:
                        returnValue = Double.toString(cstmnt.getDouble(index));
                        break;

                    }

                    if (returnValue == null) {
                        returnValue = "NULL";
                    }

                    results.put(param[i].getName(), returnValue);
                    index++;
                }

            }

        }

        if (!hasResultSet) {

            statementResult.setUpdateCount(cstmnt.getUpdateCount());

        } else {

            statementResult.setResultSet(cstmnt.getResultSet());
        }

        useCount++;
        statementResult.setOtherResult(results);

    } catch (SQLException e) {

        statementResult.setSqlException(e);

    } catch (Exception e) {

        statementResult.setMessage(e.getMessage());
    }

    return statementResult;
}

From source file:org.openadaptor.auxil.connector.jdbc.writer.AbstractSQLWriter.java

private static final String spTypeToString(int type) {
    String result;/*from  w  ww.j a v  a 2s .  c  o m*/
    switch (type) {
    case DatabaseMetaData.procedureColumnUnknown: // 0
        result = "unknown";
        break;
    case DatabaseMetaData.procedureColumnIn: // 1
        result = "IN";
        break;
    case DatabaseMetaData.procedureColumnInOut: // 2
        result = "INOUT";
        break;
    case DatabaseMetaData.procedureColumnOut: //3
        result = "OUT";
        break;
    case DatabaseMetaData.procedureColumnReturn: // 4
        result = "RETURN";
        break;
    case DatabaseMetaData.procedureColumnResult: // 5
        result = "RESULT";
        break;
    default: //This *should* never arise
        result = "Illegal value for procedureColumn type";
        break;
    }
    return result;
}

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

protected void setupProcedureColumnMetaInfo(DfProcedureMeta procedureMetaInfo, ResultSet columnRs)
        throws SQLException {
    final Set<String> uniqueSet = new HashSet<String>();
    while (columnRs.next()) {
        // /- - - - - - - - - - - - - - - - - - - - - - - -
        // same policy as table process about JDBC handling
        // (see DfTableHandler.java)
        // - - - - - - - - - -/

        final String columnName = columnRs.getString("COLUMN_NAME");

        // filter duplicated informations
        // because Oracle package procedure may return them
        if (uniqueSet.contains(columnName)) {
            continue;
        }/* w w w  .j a  v a  2  s.c o m*/
        uniqueSet.add(columnName);

        final Integer procedureColumnType;
        {
            final String columnType = columnRs.getString("COLUMN_TYPE");
            final int unknowType = DatabaseMetaData.procedureColumnUnknown;
            if (Srl.is_NotNull_and_NotTrimmedEmpty(columnType)) {
                procedureColumnType = toInt("columnType", columnType);
            } else {
                procedureColumnType = unknowType;
            }
        }

        final int jdbcType;
        {
            int tmpJdbcType = Types.OTHER;
            String dataType = null;
            try {
                dataType = columnRs.getString("DATA_TYPE");
            } catch (RuntimeException ignored) { // pinpoint patch
                // for example, SQLServer throws an exception
                // if the procedure is a function that returns table type
                final String procdureName = procedureMetaInfo.getProcedureFullQualifiedName();
                log("*Failed to get data type: " + procdureName + "." + columnName);
                tmpJdbcType = Types.OTHER;
            }
            if (Srl.is_NotNull_and_NotTrimmedEmpty(dataType)) {
                tmpJdbcType = toInt("dataType", dataType);
            }
            jdbcType = tmpJdbcType;
        }

        final String dbTypeName = columnRs.getString("TYPE_NAME");

        // uses getString() to get null value
        // (getInt() returns zero when a value is no defined)
        final Integer columnSize;
        {
            final String precision = columnRs.getString("PRECISION");
            if (Srl.is_NotNull_and_NotTrimmedEmpty(precision)) {
                columnSize = toInt("precision", precision);
            } else {
                final String length = columnRs.getString("LENGTH");
                if (Srl.is_NotNull_and_NotTrimmedEmpty(length)) {
                    columnSize = toInt("length", length);
                } else {
                    columnSize = null;
                }
            }
        }
        final Integer decimalDigits;
        {
            final String scale = columnRs.getString("SCALE");
            if (Srl.is_NotNull_and_NotTrimmedEmpty(scale)) {
                decimalDigits = toInt("scale", scale);
            } else {
                decimalDigits = null;
            }
        }
        final String columnComment = columnRs.getString("REMARKS");

        final DfProcedureColumnMeta procedureColumnMetaInfo = new DfProcedureColumnMeta();
        procedureColumnMetaInfo.setColumnName(columnName);
        if (procedureColumnType == DatabaseMetaData.procedureColumnUnknown) {
            procedureColumnMetaInfo.setProcedureColumnType(DfProcedureColumnType.procedureColumnUnknown);
        } else if (procedureColumnType == DatabaseMetaData.procedureColumnIn) {
            procedureColumnMetaInfo.setProcedureColumnType(DfProcedureColumnType.procedureColumnIn);
        } else if (procedureColumnType == DatabaseMetaData.procedureColumnInOut) {
            procedureColumnMetaInfo.setProcedureColumnType(DfProcedureColumnType.procedureColumnInOut);
        } else if (procedureColumnType == DatabaseMetaData.procedureColumnOut) {
            procedureColumnMetaInfo.setProcedureColumnType(DfProcedureColumnType.procedureColumnOut);
        } else if (procedureColumnType == DatabaseMetaData.procedureColumnReturn) {
            procedureColumnMetaInfo.setProcedureColumnType(DfProcedureColumnType.procedureColumnReturn);
        } else if (procedureColumnType == DatabaseMetaData.procedureColumnResult) {
            procedureColumnMetaInfo.setProcedureColumnType(DfProcedureColumnType.procedureColumnResult);
        } else {
            throw new IllegalStateException("Unknown procedureColumnType: " + procedureColumnType);
        }
        procedureColumnMetaInfo.setJdbcDefType(jdbcType);
        procedureColumnMetaInfo.setDbTypeName(dbTypeName);
        procedureColumnMetaInfo.setColumnSize(columnSize);
        procedureColumnMetaInfo.setDecimalDigits(decimalDigits);
        procedureColumnMetaInfo.setColumnComment(columnComment);
        procedureMetaInfo.addProcedureColumn(procedureColumnMetaInfo);
    }
    adjustProcedureColumnList(procedureMetaInfo);
}