class SqlResultSetReader {

  protected static ColumnType getColumnType(int columnType, int scale, int precision) {
    ColumnType type = SQL_TYPE_TO_TABLESAW_TYPE.get(columnType);
    // Try to improve on the initial type assigned to 'type' to minimize size/space of type needed.
    // For all generic numeric columns inspect closer, checking the precision and
    // scale to more accurately determine the appropriate java type to use.
    if (columnType == Types.NUMERIC || columnType == Types.DECIMAL) {
      // When scale is 0 then column is a type of integer
      if (scale == 0) {
        /* Mapping to java integer types based on integer precision defined:

        Java type           TypeMinVal              TypeMaxVal          p               MaxIntVal
        -----------------------------------------------------------------------------------------
        byte, Byte:         -128                    127                 NUMBER(2)       99
        short, Short:       -32768                  32767               NUMBER(4)       9_999
        int, Integer:       -2147483648             2147483647          NUMBER(9)       999_999_999
        long, Long:         -9223372036854775808    9223372036854775807 NUMBER(18)      999_999_999_999_999_999

        */
        if (precision > 0) {
          if (precision <= 4) {
            // Start with SHORT (since ColumnType.BYTE isn't supported yet)
            // and find the smallest java integer type that fits
            type = ColumnType.SHORT;
          } else if (precision <= 9) {
            type = ColumnType.INTEGER;
          } else if (precision <= 18) {
            type = ColumnType.LONG;
          }
        }
      } else { // s is not zero, so a decimal value is expected. First try float, then double
        if (scale <= 7) {
          type = ColumnType.FLOAT;
        } else if (scale <= 16) {
          type = ColumnType.DOUBLE;
        }
      }
    }
    return type;
  }

}