Java SQL Type columnDisplaySize(int columnType)

Here you can find the source of columnDisplaySize(int columnType)

Description

Column display size.

License

Apache License

Parameter

Parameter Description
columnType the column type

Exception

Parameter Description
SQLException the SQL exception

Return

the int

Declaration

static int columnDisplaySize(int columnType) throws SQLException 

Method Source Code

//package com.java2s;
/**/*from  w  w  w  .  j  a va 2 s  . com*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.sql.SQLException;

import java.sql.Types;

public class Main {
    /**
     * Column display size.
     *
     * @param columnType the column type
     * @return the int
     * @throws SQLException the SQL exception
     */
    static int columnDisplaySize(int columnType) throws SQLException {
        // according to hiveTypeToSqlType possible options are:
        switch (columnType) {
        case Types.BOOLEAN:
            return columnPrecision(columnType);
        case Types.CHAR:
        case Types.VARCHAR:
            return columnPrecision(columnType);
        case Types.BINARY:
            return Integer.MAX_VALUE; // hive has no max limit for binary
        case Types.TINYINT:
        case Types.SMALLINT:
        case Types.INTEGER:
        case Types.BIGINT:
            return columnPrecision(columnType) + 1; // allow +/-
        case Types.DATE:
            return 10;
        case Types.TIMESTAMP:
            return columnPrecision(columnType);

        // see http://download.oracle.com/javase/6/docs/api/constant-values.html#java.lang.Float.MAX_EXPONENT
        case Types.FLOAT:
            return 24; // e.g. -(17#).e-###
        // see http://download.oracle.com/javase/6/docs/api/constant-values.html#java.lang.Double.MAX_EXPONENT
        case Types.DOUBLE:
            return 25; // e.g. -(17#).e-####
        case Types.DECIMAL:
            return columnPrecision(columnType) + 2; // '-' sign and '.'
        case Types.JAVA_OBJECT:
        case Types.ARRAY:
        case Types.STRUCT:
            return Integer.MAX_VALUE;
        default:
            throw new SQLException("Invalid column type: " + columnType);
        }
    }

    /**
     * Column precision.
     *
     * @param columnType the column type
     * @return the int
     * @throws SQLException the SQL exception
     */
    static int columnPrecision(int columnType) throws SQLException {
        // according to hiveTypeToSqlType possible options are:
        switch (columnType) {
        case Types.BOOLEAN:
            return 1;
        case Types.CHAR:
        case Types.VARCHAR:
            return Integer.MAX_VALUE; // hive has no max limit for strings
        case Types.BINARY:
            return Integer.MAX_VALUE; // hive has no max limit for binary
        case Types.TINYINT:
            return 3;
        case Types.SMALLINT:
            return 5;
        case Types.INTEGER:
            return 10;
        case Types.BIGINT:
            return 19;
        case Types.FLOAT:
            return 7;
        case Types.DOUBLE:
            return 15;
        case Types.DATE:
            return 10;
        case Types.TIMESTAMP:
            return 29;
        case Types.DECIMAL:
            return 5;
        case Types.JAVA_OBJECT:
        case Types.ARRAY:
        case Types.STRUCT:
            return Integer.MAX_VALUE;
        default:
            throw new SQLException("Invalid column type: " + columnType);
        }
    }
}

Related

  1. addParameter(PreparedStatement smt, int index, Object value)
  2. adjustLevel(String attribute, int type)
  3. callPro2(String sql, String[] inparameters, Integer[] outparameters)
  4. columnClassName(int columnType)
  5. columnPrecision(int columnType)
  6. columnScale(int columnType)
  7. columnTypesDiffer(int t1, int t2)
  8. compressType(int type)