Example usage for java.sql Types SMALLINT

List of usage examples for java.sql Types SMALLINT

Introduction

In this page you can find the example usage for java.sql Types SMALLINT.

Prototype

int SMALLINT

To view the source code for java.sql Types SMALLINT.

Click Source Link

Document

The constant in the Java programming language, sometimes referred to as a type code, that identifies the generic SQL type SMALLINT.

Usage

From source file:org.nuxeo.ecm.core.storage.sql.db.dialect.DialectOracle.java

@Override
@SuppressWarnings("boxing")
public Serializable getFromResultSet(ResultSet rs, int index, Column column) throws SQLException {
    switch (column.getJdbcType()) {
    case Types.VARCHAR:
        String string = rs.getString(index);
        if (column.getType() == ColumnType.BLOBID && string != null) {
            return column.getModel().getBinary(string);
        } else {//from  www.  j  a  v a  2  s .  c  o  m
            return string;
        }
    case Types.CLOB:
        // Oracle cannot read CLOBs using rs.getString when the ResultSet is
        // a ScrollableResultSet (the standard OracleResultSetImpl works
        // fine).
        Reader r = rs.getCharacterStream(index);
        if (r == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        char[] buffer = new char[4096];
        try {
            int n;
            while ((n = r.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, n));
            }
        } catch (IOException e) {
            log.error("Cannot read CLOB", e);
        }
        return sb.toString();
    case Types.BIT:
        return rs.getBoolean(index);
    case Types.TINYINT:
    case Types.SMALLINT:
    case Types.INTEGER:
    case Types.BIGINT:
        return rs.getLong(index);
    case Types.DOUBLE:
        return rs.getDouble(index);
    case Types.TIMESTAMP:
        Timestamp ts = rs.getTimestamp(index);
        if (ts == null) {
            return null;
        } else {
            Serializable cal = new GregorianCalendar(); // XXX timezone
            ((Calendar) cal).setTimeInMillis(ts.getTime());
            return cal;
        }
    }
    throw new SQLException("Unhandled JDBC type: " + column.getJdbcType());
}

From source file:com.squid.core.domain.operators.ExtendedType.java

/**
 * compute a type "order" that can be used to compare types and promote types.
 * The order is a couple (x,y), where x represent a family type (string,date,numbers) and y an order in that family
 * A type should be promoted to the higher order.
 * @return// w ww  .  j  a va2  s .com
 */
public int[] computeTypeOrder() {
    switch (getDataType()) {
    case Types.BIT:
        return new int[] { NUMBER_ORDER, 0 };
    case Types.BOOLEAN:
        return new int[] { NUMBER_ORDER, 1 };
    case Types.TINYINT:
        return new int[] { NUMBER_ORDER, 2 };
    case Types.SMALLINT:
        return new int[] { NUMBER_ORDER, 3 };
    case Types.INTEGER:
        return new int[] { NUMBER_ORDER, 4 };
    case Types.BIGINT:
        return new int[] { NUMBER_ORDER, 5 };
    ///////////////////////////
    case Types.REAL:
        return new int[] { NUMBER_ORDER, 6 };
    case Types.DOUBLE:
    case Types.FLOAT:
        return new int[] { NUMBER_ORDER, 7 };
    case Types.DECIMAL:
        return new int[] { NUMBER_ORDER, 8 };
    case Types.NUMERIC:
        return new int[] { NUMBER_ORDER, 9 };
    //////////////////////////
    case Types.CHAR:
        return new int[] { STRING_ORDER, 0 };
    case Types.VARCHAR:
        return new int[] { STRING_ORDER, 1 };
    case Types.LONGVARCHAR:
        return new int[] { STRING_ORDER, 2 };
    case Types.CLOB:
        return new int[] { STRING_ORDER, 3 };
    ///////////////////////////
    case Types.TIME:
        return new int[] { DATE_ORDER, 1 };
    case Types.DATE:
        return new int[] { DATE_ORDER, 2 };
    case Types.TIMESTAMP:
        return new int[] { DATE_ORDER, 3 };
    case CustomTypes.INTERVAL:
        return new int[] { DATE_ORDER, 4 };
    ///////////////////////////
    default:
        return new int[] { UNKNOWN_ORDER, 0 };
    }
}

From source file:org.jumpmind.vaadin.ui.common.CommonUiUtils.java

public static Table putResultsInTable(final ResultSet rs, int maxResultSize, final boolean showRowNumbers,
        String... excludeValues) throws SQLException {

    final Table table = createTable();
    table.setImmediate(true);//from ww  w . j av a2 s . c  om
    table.setSortEnabled(true);
    table.setSelectable(true);
    table.setMultiSelect(true);
    table.setColumnReorderingAllowed(true);
    table.setColumnReorderingAllowed(true);
    table.setColumnCollapsingAllowed(true);

    final ResultSetMetaData meta = rs.getMetaData();
    int columnCount = meta.getColumnCount();
    table.addContainerProperty("#", Integer.class, null);
    Set<String> columnNames = new HashSet<String>();
    Set<Integer> skipColumnIndexes = new HashSet<Integer>();
    int[] types = new int[columnCount];
    for (int i = 1; i <= columnCount; i++) {
        String realColumnName = meta.getColumnName(i);
        String columnName = realColumnName;
        if (!Arrays.asList(excludeValues).contains(columnName)) {

            int index = 1;
            while (columnNames.contains(columnName)) {
                columnName = realColumnName + "_" + index++;
            }
            columnNames.add(columnName);

            Class<?> typeClass = Object.class;
            int type = meta.getColumnType(i);
            types[i - 1] = type;
            switch (type) {
            case Types.FLOAT:
            case Types.DOUBLE:
            case Types.NUMERIC:
            case Types.REAL:
            case Types.DECIMAL:
                typeClass = BigDecimal.class;
                break;
            case Types.TINYINT:
            case Types.SMALLINT:
            case Types.BIGINT:
            case Types.INTEGER:
                typeClass = Long.class;
                break;
            case Types.VARCHAR:
            case Types.CHAR:
            case Types.NVARCHAR:
            case Types.NCHAR:
            case Types.CLOB:
                typeClass = String.class;
            default:
                break;
            }
            table.addContainerProperty(i, typeClass, null);
            table.setColumnHeader(i, columnName);
        } else {
            skipColumnIndexes.add(i - 1);
        }

    }
    int rowNumber = 1;
    while (rs.next() && rowNumber <= maxResultSize) {
        Object[] row = new Object[columnNames.size() + 1];
        row[0] = new Integer(rowNumber);
        int rowIndex = 1;
        for (int i = 0; i < columnCount; i++) {
            if (!skipColumnIndexes.contains(i)) {
                Object o = getObject(rs, i + 1);
                int type = types[i];
                switch (type) {
                case Types.FLOAT:
                case Types.DOUBLE:
                case Types.REAL:
                case Types.NUMERIC:
                case Types.DECIMAL:
                    if (o == null) {
                        o = new BigDecimal(-1);
                    }
                    if (!(o instanceof BigDecimal)) {
                        o = new BigDecimal(castToNumber(o.toString()));
                    }
                    break;
                case Types.TINYINT:
                case Types.SMALLINT:
                case Types.BIGINT:
                case Types.INTEGER:
                    if (o == null) {
                        o = new Long(-1);
                    }

                    if (!(o instanceof Long)) {
                        o = new Long(castToNumber(o.toString()));
                    }
                    break;
                default:
                    break;
                }
                row[rowIndex] = o == null ? NULL_TEXT : o;
                rowIndex++;
            }
        }
        table.addItem(row, rowNumber);
        rowNumber++;
    }

    if (rowNumber < 100) {
        table.setColumnWidth("#", 18);
    } else if (rowNumber < 1000) {
        table.setColumnWidth("#", 25);
    } else {
        table.setColumnWidth("#", 30);
    }

    if (!showRowNumbers) {
        table.setColumnCollapsed("#", true);
    }

    return table;
}

From source file:br.bookmark.db.util.ResultSetUtils.java

/**
 * Map JDBC objects to Java equivalents.
 * Used by getBean() and getBeans()./* w w  w. j a  va2s .  com*/
 * <p>
 * Some types not supported.
 * Many not work with all drivers.
 * <p>
 * Makes binary conversions of BIGINT, DATE, DECIMAL, DOUBLE, FLOAT, INTEGER,
 * REAL, SMALLINT, TIME, TIMESTAMP, TINYINT.
 * Makes Sting conversions of CHAR, CLOB, VARCHAR, LONGVARCHAR, BLOB, LONGVARBINARY,
 * VARBINARY.
 * <p>
 * DECIMAL, INTEGER, SMALLINT, TIMESTAMP, CHAR, VARCHAR tested with MySQL and Poolman.
 * Others not guaranteed.
 * @param classeDestino 
 * @throws NoSuchFieldException 
 * @throws SecurityException 
 */
private static void putEntry(Map properties, ResultSetMetaData metaData, ResultSet resultSet, int i,
        Class classeDestino) throws Exception {

    /*
    In a perfect universe, this would be enough
    properties.put(
        metaData.getColumnName(i),
        resultSet.getObject(i));
    But only String, Timestamp, and Integer seem to get through that way.
    */

    String columnName = metaData.getColumnName(i);

    // Testa se  uma FK
    /*Field[] fields = classeDestino.getDeclaredFields();
    for (int j = 0; j < fields.length; j++) {
    if (fields[j].getAnnotation(DBFK.class) != null) {
        properties.put(columnName, resultSet.getString(i));
    }
    }*/
    //System.out.println(i+"-"+metaData.getColumnType(i));
    switch (metaData.getColumnType(i)) {

    // http://java.sun.com/j2se/1.3.0/docs/api/java/sql/Types.html

    case Types.BIGINT:
        properties.put(columnName, new Long(resultSet.getLong(i)));
        break;

    case Types.DATE:
        properties.put(columnName, resultSet.getDate(i));
        break;

    case Types.DECIMAL:
    case Types.DOUBLE:
        properties.put(columnName, new Double(resultSet.getDouble(i)));
        break;

    case Types.FLOAT:
        properties.put(columnName, new Float(resultSet.getFloat(i)));
        break;

    case Types.INTEGER:
        int valor = 0;
        try { // Se o campo esta vazio d erro
            valor = resultSet.getInt(i);
        } catch (SQLException e) {
        }
        properties.put(columnName, new Integer(valor));
        break;

    case Types.REAL:
        properties.put(columnName, new Double(resultSet.getString(i)));
        break;

    case Types.SMALLINT:
        properties.put(columnName, new Short(resultSet.getShort(i)));
        break;

    case Types.TIME:
        properties.put(columnName, resultSet.getTime(i));
        break;

    case Types.TIMESTAMP:
        properties.put(columnName, resultSet.getTimestamp(i));
        break;

    // :FIXME: Throws java.lang.ClassCastException: java.lang.Integer
    // :FIXME: with Poolman and MySQL unless use getString.
    case Types.TINYINT:
        properties.put(columnName, new Byte(resultSet.getString(i)));
        break;

    case Types.CHAR:
    case Types.CLOB:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
        // :FIXME: Handle binaries differently?
    case Types.BLOB:
    case Types.LONGVARBINARY:
    case Types.VARBINARY:
        properties.put(columnName, resultSet.getString(i));
        break;

    /*
        :FIXME: Add handlers for
        ARRAY
        BINARY
        BIT
        DISTINCT
        JAVA_OBJECT
        NULL
        NUMERIC
        OTHER
        REF
        STRUCT
    */

    // Otherwise, pass as *String property to be converted
    default:
        properties.put(columnName + "String", resultSet.getString(i));
        break;
    } // end switch

}

From source file:org.apache.hadoop.sqoop.manager.SqlManager.java

public String toJavaType(int sqlType) {
    // mappings from http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
    if (sqlType == Types.INTEGER) {
        return "Integer";
    } else if (sqlType == Types.VARCHAR) {
        return "String";
    } else if (sqlType == Types.CHAR) {
        return "String";
    } else if (sqlType == Types.LONGVARCHAR) {
        return "String";
    } else if (sqlType == Types.NUMERIC) {
        return "java.math.BigDecimal";
    } else if (sqlType == Types.DECIMAL) {
        return "java.math.BigDecimal";
    } else if (sqlType == Types.BIT) {
        return "Boolean";
    } else if (sqlType == Types.BOOLEAN) {
        return "Boolean";
    } else if (sqlType == Types.TINYINT) {
        return "Integer";
    } else if (sqlType == Types.SMALLINT) {
        return "Integer";
    } else if (sqlType == Types.BIGINT) {
        return "Long";
    } else if (sqlType == Types.REAL) {
        return "Float";
    } else if (sqlType == Types.FLOAT) {
        return "Double";
    } else if (sqlType == Types.DOUBLE) {
        return "Double";
    } else if (sqlType == Types.DATE) {
        return "java.sql.Date";
    } else if (sqlType == Types.TIME) {
        return "java.sql.Time";
    } else if (sqlType == Types.TIMESTAMP) {
        return "java.sql.Timestamp";
    } else {/*w w w  .  j av  a 2s.co m*/
        // TODO(aaron): Support BINARY, VARBINARY, LONGVARBINARY, DISTINCT, CLOB, BLOB, ARRAY,
        // STRUCT, REF, JAVA_OBJECT.
        return null;
    }
}

From source file:com.jaspersoft.jrx.query.PlSqlQueryExecuter.java

private void createStatement() throws JRException {
    String queryString = getQueryString();

    if (connection != null && queryString != null && queryString.trim().length() > 0) {

        try {//  ww  w . ja  va 2  s  .c  o  m
            isStoredProcedure = isOracleStoredProcedure(queryString);

            if (isStoredProcedure) {
                statement = connection.prepareCall(queryString);
            } else {
                statement = connection.prepareStatement(queryString);
            }

            int fetchSize = JRPropertiesUtil.getInstance(getJasperReportsContext()).getIntegerProperty(
                    dataset.getPropertiesMap(), JRJdbcQueryExecuterFactory.PROPERTY_JDBC_FETCH_SIZE, 0);
            if (fetchSize > 0) {
                statement.setFetchSize(fetchSize);
            }

            List<String> parameterNames = getCollectedParameterNames();
            if (!parameterNames.isEmpty()) {
                for (int i = 0; i < parameterNames.size(); i++) {
                    String parameterName = (String) parameterNames.get(i);
                    JRValueParameter parameter = getValueParameter(parameterName);
                    Class<?> clazz = parameter.getValueClass();
                    Object parameterValue = parameter.getValue();

                    if (clazz.equals(java.lang.Object.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.JAVA_OBJECT);
                        } else {
                            statement.setObject(i + 1, parameterValue);
                        }
                    } else if (clazz.equals(java.lang.Boolean.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.BIT);
                        } else {
                            statement.setBoolean(i + 1, ((Boolean) parameterValue).booleanValue());
                        }
                    } else if (clazz.equals(java.lang.Byte.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.TINYINT);
                        } else {
                            statement.setByte(i + 1, ((Byte) parameterValue).byteValue());
                        }
                    } else if (clazz.equals(java.lang.Double.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.DOUBLE);
                        } else {
                            statement.setDouble(i + 1, ((Double) parameterValue).doubleValue());
                        }
                    } else if (clazz.equals(java.lang.Float.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.FLOAT);
                        } else {
                            statement.setFloat(i + 1, ((Float) parameterValue).floatValue());
                        }
                    } else if (clazz.equals(java.lang.Integer.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.INTEGER);
                        } else {
                            statement.setInt(i + 1, ((Integer) parameterValue).intValue());
                        }
                    } else if (clazz.equals(java.lang.Long.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.BIGINT);
                        } else {
                            statement.setLong(i + 1, ((Long) parameterValue).longValue());
                        }
                    } else if (clazz.equals(java.lang.Short.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.SMALLINT);
                        } else {
                            statement.setShort(i + 1, ((Short) parameterValue).shortValue());
                        }
                    } else if (clazz.equals(java.math.BigDecimal.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.DECIMAL);
                        } else {
                            statement.setBigDecimal(i + 1, (BigDecimal) parameterValue);
                        }
                    } else if (clazz.equals(java.lang.String.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.VARCHAR);
                        } else {
                            statement.setString(i + 1, parameterValue.toString());
                        }
                    } else if (clazz.equals(java.util.Date.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.DATE);
                        } else {
                            statement.setDate(i + 1,
                                    new java.sql.Date(((java.util.Date) parameterValue).getTime()));
                        }
                    } else if (clazz.equals(java.sql.Timestamp.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.TIMESTAMP);
                        } else {
                            statement.setTimestamp(i + 1, (java.sql.Timestamp) parameterValue);
                        }
                    } else if (clazz.equals(java.sql.Time.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.TIME);
                        } else {
                            statement.setTime(i + 1, (java.sql.Time) parameterValue);
                        }
                    } else if (clazz.equals(java.sql.ResultSet.class)) {
                        if (!isStoredProcedure) {
                            throw new JRException("OUT paramater used in non-stored procedure call : "
                                    + parameterName + " class " + clazz.getName());
                        } else if (cursorParameter > 0) {
                            throw new JRException("A stored procedure can have at most one cursor parameter : "
                                    + parameterName + " class " + clazz.getName());
                        }

                        ((CallableStatement) statement).registerOutParameter(i + 1, ORACLE_CURSOR_TYPE);
                        cursorParameter = i + 1;
                    } else {
                        throw new JRException("Parameter type not supported in query : " + parameterName
                                + " class " + clazz.getName());
                    }
                }
            }
        } catch (SQLException e) {
            throw new JRException("Error preparing statement for executing the report query : " + "\n\n"
                    + queryString + "\n\n", e);
        }
    }
}

From source file:org.jfree.data.jdbc.JDBCXYDataset.java

/**
 * ExecuteQuery will attempt execute the query passed to it against the
 * provided database connection.  If connection is null then no action is
 * taken./*from  w  w w  .  j a  v  a 2  s .c  o  m*/
 *
 * The results from the query are extracted and cached locally, thus
 * applying an upper limit on how many rows can be retrieved successfully.
 *
 * @param  query  the query to be executed.
 * @param  con  the connection the query is to be executed against.
 *
 * @throws SQLException if there is a problem executing the query.
 */
public void executeQuery(Connection con, String query) throws SQLException {

    if (con == null) {
        throw new SQLException("There is no database to execute the query.");
    }

    ResultSet resultSet = null;
    Statement statement = null;
    try {
        statement = con.createStatement();
        resultSet = statement.executeQuery(query);
        ResultSetMetaData metaData = resultSet.getMetaData();

        int numberOfColumns = metaData.getColumnCount();
        int numberOfValidColumns = 0;
        int[] columnTypes = new int[numberOfColumns];
        for (int column = 0; column < numberOfColumns; column++) {
            try {
                int type = metaData.getColumnType(column + 1);
                switch (type) {

                case Types.NUMERIC:
                case Types.REAL:
                case Types.INTEGER:
                case Types.DOUBLE:
                case Types.FLOAT:
                case Types.DECIMAL:
                case Types.BIT:
                case Types.DATE:
                case Types.TIME:
                case Types.TIMESTAMP:
                case Types.BIGINT:
                case Types.SMALLINT:
                    ++numberOfValidColumns;
                    columnTypes[column] = type;
                    break;
                default:
                    columnTypes[column] = Types.NULL;
                    break;
                }
            } catch (SQLException e) {
                columnTypes[column] = Types.NULL;
                throw e;
            }
        }

        if (numberOfValidColumns <= 1) {
            throw new SQLException("Not enough valid columns where generated by query.");
        }

        /// First column is X data
        this.columnNames = new String[numberOfValidColumns - 1];
        /// Get the column names and cache them.
        int currentColumn = 0;
        for (int column = 1; column < numberOfColumns; column++) {
            if (columnTypes[column] != Types.NULL) {
                this.columnNames[currentColumn] = metaData.getColumnLabel(column + 1);
                ++currentColumn;
            }
        }

        // Might need to add, to free memory from any previous result sets
        if (this.rows != null) {
            for (int column = 0; column < this.rows.size(); column++) {
                ArrayList row = (ArrayList) this.rows.get(column);
                row.clear();
            }
            this.rows.clear();
        }

        // Are we working with a time series.
        switch (columnTypes[0]) {
        case Types.DATE:
        case Types.TIME:
        case Types.TIMESTAMP:
            this.isTimeSeries = true;
            break;
        default:
            this.isTimeSeries = false;
            break;
        }

        // Get all rows.
        // rows = new ArrayList();
        while (resultSet.next()) {
            ArrayList newRow = new ArrayList();
            for (int column = 0; column < numberOfColumns; column++) {
                Object xObject = resultSet.getObject(column + 1);
                switch (columnTypes[column]) {
                case Types.NUMERIC:
                case Types.REAL:
                case Types.INTEGER:
                case Types.DOUBLE:
                case Types.FLOAT:
                case Types.DECIMAL:
                case Types.BIGINT:
                case Types.SMALLINT:
                    newRow.add(xObject);
                    break;

                case Types.DATE:
                case Types.TIME:
                case Types.TIMESTAMP:
                    newRow.add(new Long(((Date) xObject).getTime()));
                    break;
                case Types.NULL:
                    break;
                default:
                    System.err.println("Unknown data");
                    columnTypes[column] = Types.NULL;
                    break;
                }
            }
            this.rows.add(newRow);
        }

        /// a kludge to make everything work when no rows returned
        if (this.rows.size() == 0) {
            ArrayList newRow = new ArrayList();
            for (int column = 0; column < numberOfColumns; column++) {
                if (columnTypes[column] != Types.NULL) {
                    newRow.add(new Integer(0));
                }
            }
            this.rows.add(newRow);
        }

        /// Determine max and min values.
        if (this.rows.size() < 1) {
            this.maxValue = 0.0;
            this.minValue = 0.0;
        } else {
            ArrayList row = (ArrayList) this.rows.get(0);
            this.maxValue = Double.NEGATIVE_INFINITY;
            this.minValue = Double.POSITIVE_INFINITY;
            for (int rowNum = 0; rowNum < this.rows.size(); ++rowNum) {
                row = (ArrayList) this.rows.get(rowNum);
                for (int column = 1; column < numberOfColumns; column++) {
                    Object testValue = row.get(column);
                    if (testValue != null) {
                        double test = ((Number) testValue).doubleValue();

                        if (test < this.minValue) {
                            this.minValue = test;
                        }
                        if (test > this.maxValue) {
                            this.maxValue = test;
                        }
                    }
                }
            }
        }

        fireDatasetChanged(new DatasetChangeInfo());
        //TODO: fill in real change info
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (Exception e) {
                // TODO: is this a good idea?
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (Exception e) {
                // TODO: is this a good idea?
            }
        }
    }

}

From source file:org.waarp.common.database.data.AbstractDbData.java

/**
 * Set Value into PreparedStatement/*from www  .  j ava  2  s.  c  o  m*/
 * 
 * @param ps
 * @param value
 * @param rank
 *            >= 1
 * @throws WaarpDatabaseSqlException
 */
static public void setTrueValue(PreparedStatement ps, DbValue value, int rank)
        throws WaarpDatabaseSqlException {
    try {
        switch (value.type) {
        case Types.VARCHAR:
            if (value.value == null) {
                ps.setNull(rank, Types.VARCHAR);
                break;
            }
            ps.setString(rank, (String) value.value);
            break;
        case Types.LONGVARCHAR:
            if (value.value == null) {
                ps.setNull(rank, Types.LONGVARCHAR);
                break;
            }
            ps.setString(rank, (String) value.value);
            break;
        case Types.BIT:
            if (value.value == null) {
                ps.setNull(rank, Types.BIT);
                break;
            }
            ps.setBoolean(rank, (Boolean) value.value);
            break;
        case Types.TINYINT:
            if (value.value == null) {
                ps.setNull(rank, Types.TINYINT);
                break;
            }
            ps.setByte(rank, (Byte) value.value);
            break;
        case Types.SMALLINT:
            if (value.value == null) {
                ps.setNull(rank, Types.SMALLINT);
                break;
            }
            ps.setShort(rank, (Short) value.value);
            break;
        case Types.INTEGER:
            if (value.value == null) {
                ps.setNull(rank, Types.INTEGER);
                break;
            }
            ps.setInt(rank, (Integer) value.value);
            break;
        case Types.BIGINT:
            if (value.value == null) {
                ps.setNull(rank, Types.BIGINT);
                break;
            }
            ps.setLong(rank, (Long) value.value);
            break;
        case Types.REAL:
            if (value.value == null) {
                ps.setNull(rank, Types.REAL);
                break;
            }
            ps.setFloat(rank, (Float) value.value);
            break;
        case Types.DOUBLE:
            if (value.value == null) {
                ps.setNull(rank, Types.DOUBLE);
                break;
            }
            ps.setDouble(rank, (Double) value.value);
            break;
        case Types.VARBINARY:
            if (value.value == null) {
                ps.setNull(rank, Types.VARBINARY);
                break;
            }
            ps.setBytes(rank, (byte[]) value.value);
            break;
        case Types.DATE:
            if (value.value == null) {
                ps.setNull(rank, Types.DATE);
                break;
            }
            ps.setDate(rank, (Date) value.value);
            break;
        case Types.TIMESTAMP:
            if (value.value == null) {
                ps.setNull(rank, Types.TIMESTAMP);
                break;
            }
            ps.setTimestamp(rank, (Timestamp) value.value);
            break;
        case Types.CLOB:
            if (value.value == null) {
                ps.setNull(rank, Types.CLOB);
                break;
            }
            ps.setClob(rank, (Reader) value.value);
            break;
        case Types.BLOB:
            if (value.value == null) {
                ps.setNull(rank, Types.BLOB);
                break;
            }
            ps.setBlob(rank, (InputStream) value.value);
            break;
        default:
            throw new WaarpDatabaseSqlException("Type not supported: " + value.type + " at " + rank);
        }
    } catch (ClassCastException e) {
        throw new WaarpDatabaseSqlException("Setting values casting error: " + value.type + " at " + rank, e);
    } catch (SQLException e) {
        DbSession.error(e);
        throw new WaarpDatabaseSqlException("Setting values in error: " + value.type + " at " + rank, e);
    }
}

From source file:org.apache.ddlutils.io.RoundtripTestBase.java

/**
 * Returns the original model adjusted for type changes because of the native type mappings
 * which when read back from the database will map to different types.
 * //from  w ww . j a va  2 s. co  m
 * @return The adjusted model
 */
protected Database getAdjustedModel() {
    try {
        Database model = (Database) getModel().clone();

        for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++) {
            Table table = model.getTable(tableIdx);

            for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++) {
                Column column = table.getColumn(columnIdx);
                int origType = column.getTypeCode();
                int targetType = getPlatformInfo().getTargetJdbcType(origType);

                // we adjust the column types if the native type would back-map to a
                // different jdbc type
                if (targetType != origType) {
                    column.setTypeCode(targetType);
                    // we should also adapt the default value
                    if (column.getDefaultValue() != null) {
                        DefaultValueHelper helper = getPlatform().getSqlBuilder().getDefaultValueHelper();

                        column.setDefaultValue(helper.convert(column.getDefaultValue(), origType, targetType));
                    }
                }
                // we also promote the default size if the column has no size
                // spec of its own
                if ((column.getSize() == null) && getPlatformInfo().hasSize(targetType)) {
                    Integer defaultSize = getPlatformInfo().getDefaultSize(targetType);

                    if (defaultSize != null) {
                        column.setSize(defaultSize.toString());
                    }
                }
                // finally the platform might return a synthetic default value if the column
                // is a primary key column
                if (getPlatformInfo().isSyntheticDefaultValueForRequiredReturned()
                        && (column.getDefaultValue() == null) && column.isRequired()
                        && !column.isAutoIncrement()) {
                    switch (column.getTypeCode()) {
                    case Types.TINYINT:
                    case Types.SMALLINT:
                    case Types.INTEGER:
                    case Types.BIGINT:
                        column.setDefaultValue("0");
                        break;
                    case Types.REAL:
                    case Types.FLOAT:
                    case Types.DOUBLE:
                        column.setDefaultValue("0.0");
                        break;
                    case Types.BIT:
                        column.setDefaultValue("false");
                        break;
                    default:
                        column.setDefaultValue("");
                        break;
                    }
                }
            }
            // we also add the default names to foreign keys that are initially unnamed
            for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) {
                ForeignKey fk = table.getForeignKey(fkIdx);

                if (fk.getName() == null) {
                    fk.setName(getPlatform().getSqlBuilder().getForeignKeyName(table, fk));
                }
            }
        }
        return model;
    } catch (CloneNotSupportedException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.jdbc.dialect.DialectPostgreSQL.java

@Override
public JDBCInfo getJDBCTypeAndString(ColumnType type) {
    switch (type.spec) {
    case STRING://ww w  .j av a2  s . c  om
        if (type.isUnconstrained()) {
            return jdbcInfo("varchar", Types.VARCHAR);
        } else if (type.isClob()) {
            return jdbcInfo("text", Types.CLOB);
        } else {
            return jdbcInfo("varchar(%d)", type.length, Types.VARCHAR);
        }
    case ARRAY_STRING:
        if (type.isUnconstrained()) {
            return jdbcInfo("varchar[]", Types.ARRAY, "varchar", Types.VARCHAR);
        } else if (type.isClob()) {
            return jdbcInfo("text[]", Types.ARRAY, "text", Types.CLOB);
        } else {
            return jdbcInfo("varchar(%d)[]", type.length, Types.ARRAY, "varchar", Types.VARCHAR);
        }
    case BOOLEAN:
        return jdbcInfo("bool", Types.BIT);
    case ARRAY_BOOLEAN:
        return jdbcInfo("bool[]", Types.ARRAY, "bool", Types.BOOLEAN);
    case LONG:
        return jdbcInfo("int8", Types.BIGINT);
    case ARRAY_LONG:
        return jdbcInfo("int8[]", Types.ARRAY, "int8", Types.BIGINT);
    case DOUBLE:
        return jdbcInfo("float8", Types.DOUBLE);
    case ARRAY_DOUBLE:
        return jdbcInfo("float8[]", Types.ARRAY, "float8", Types.DOUBLE);
    case TIMESTAMP:
        return jdbcInfo("timestamp", Types.TIMESTAMP);
    case ARRAY_TIMESTAMP:
        return jdbcInfo("timestamp[]", Types.ARRAY, "timestamp", Types.TIMESTAMP);
    case BLOBID:
        return jdbcInfo("varchar(40)", Types.VARCHAR);
    case ARRAY_BLOBID:
        return jdbcInfo("varchar(40)[]", Types.ARRAY, "varchar", Types.VARCHAR);
    // -----
    case NODEID:
    case NODEIDFK:
    case NODEIDFKNP:
    case NODEIDFKMUL:
    case NODEIDFKNULL:
    case NODEIDPK:
    case NODEVAL:
        switch (idType) {
        case VARCHAR:
            return jdbcInfo("varchar(36)", Types.VARCHAR);
        case UUID:
            return jdbcInfo("uuid", Types.OTHER);
        case SEQUENCE:
            return jdbcInfo("int8", Types.BIGINT);
        }
    case NODEARRAY:
        switch (idType) {
        case VARCHAR:
            return jdbcInfo("varchar(36)[]", Types.ARRAY, "varchar", Types.VARCHAR);
        case UUID:
            return jdbcInfo("uuid[]", Types.ARRAY, "uuid", Types.OTHER);
        case SEQUENCE:
            return jdbcInfo("int8[]", Types.ARRAY, "int8", Types.BIGINT);
        }
    case SYSNAME:
        return jdbcInfo("varchar(250)", Types.VARCHAR);
    case SYSNAMEARRAY:
        return jdbcInfo("varchar(250)[]", Types.ARRAY, "varchar", Types.VARCHAR);
    case TINYINT:
        return jdbcInfo("int2", Types.SMALLINT);
    case INTEGER:
        return jdbcInfo("int4", Types.INTEGER);
    case ARRAY_INTEGER:
        return jdbcInfo("int4[]", Types.ARRAY, "int4", Types.INTEGER);
    case AUTOINC:
        return jdbcInfo("serial", Types.INTEGER);
    case FTINDEXED:
        if (compatibilityFulltextTable) {
            return jdbcInfo("tsvector", Types.OTHER);
        } else {
            return jdbcInfo("text", Types.CLOB);
        }
    case FTSTORED:
        if (compatibilityFulltextTable) {
            return jdbcInfo("tsvector", Types.OTHER);
        } else {
            return jdbcInfo("text", Types.CLOB);
        }
    case CLUSTERNODE:
        return jdbcInfo("int4", Types.INTEGER);
    case CLUSTERFRAGS:
        return jdbcInfo("varchar[]", Types.ARRAY, "varchar", Types.VARCHAR);
    }
    throw new AssertionError(type);
}