Java Code Examples for java.sql.DatabaseMetaData#columnNullableUnknown()

The following examples show how to use java.sql.DatabaseMetaData#columnNullableUnknown() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetaImpl.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
protected static int getColumnNullability(Field field) {
  // Check annotations first
  if (field.isAnnotationPresent(ColumnNoNulls.class)) {
    return DatabaseMetaData.columnNoNulls;
  }

  if (field.isAnnotationPresent(ColumnNullable.class)) {
    return DatabaseMetaData.columnNullable;
  }

  if (field.isAnnotationPresent(ColumnNullableUnknown.class)) {
    return DatabaseMetaData.columnNullableUnknown;
  }

  // check the field type to decide if annotated, as a fallback
  if (field.getType().isPrimitive()) {
    return DatabaseMetaData.columnNoNulls;
  }

  return DatabaseMetaData.columnNullable;
}
 
Example 2
Source File: SqlDataSourceUtils.java    From AuthMeReloaded with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns whether the given column has a NOT NULL constraint.
 *
 * @param metaData the database meta data
 * @param tableName the name of the table in which the column is
 * @param columnName the name of the column to check
 * @return true if the column is NOT NULL, false otherwise
 * @throws SQLException :)
 */
public static boolean isNotNullColumn(DatabaseMetaData metaData, String tableName,
                                      String columnName) throws SQLException {
    try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) {
        if (!rs.next()) {
            throw new IllegalStateException("Did not find meta data for column '"
                + columnName + "' while checking for not-null constraint");
        }

        int nullableCode = rs.getInt("NULLABLE");
        if (nullableCode == DatabaseMetaData.columnNoNulls) {
            return true;
        } else if (nullableCode == DatabaseMetaData.columnNullableUnknown) {
            logger.warning("Unknown nullable status for column '" + columnName + "'");
        }
    }
    return false;
}
 
Example 3
Source File: JDBCUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static Nullable getColumnNullable(int dbmdColumnNullable) {
    switch (dbmdColumnNullable) {
        case DatabaseMetaData.columnNoNulls:
            return Nullable.NOT_NULLABLE;
        case DatabaseMetaData.columnNullable:
            return Nullable.NULLABLE;
        case DatabaseMetaData.columnNullableUnknown:
        default:
            return Nullable.UNKNOWN;
    }
}
 
Example 4
Source File: DBFBuiltInMemoryResultSetForColumnsListing.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * @see java.sql.ResultSet#getInt(java.lang.String)
 * @throws SQLNoSuchFieldException if the column does not exist.
 */
@Override public int getInt(String columnLabel) throws SQLNoSuchFieldException {
    logStep("getInt", columnLabel);

    switch(columnLabel) {
        // int => SQL type from java.sql.Types
        case "DATA_TYPE": {
            this.wasNull = false;
            return toSQLDataType();
        }

        // int => column size.
        case "COLUMN_SIZE": {
            this.wasNull = false;
            return toPrecision();
        }

        // int => the number of fractional digits. Null is returned for data types where DECIMAL_DIGITS is not applicable.
        case "DECIMAL_DIGITS": {
            int scale = toScale();
            this.wasNull = toScale() == -1;
            return scale == -1 ? 0 : scale;
        }

        // int => Radix (typically either 10 or 2)
        case "NUM_PREC_RADIX": {
            return 10;
        }

        /**
         * int => is NULL allowed.
         * columnNoNulls - might not allow NULL values
         * columnNullable - definitely allows NULL values
         * columnNullableUnknown - nullability unknown
         */
        case "NULLABLE": {
            this.wasNull = false;
            return DatabaseMetaData.columnNullableUnknown;
        }

        // int => unused
        case "SQL_DATA_TYPE": {
            this.wasNull = false;
            return toSQLDataType();
        }

        // int => for char types the maximum number of bytes in the column
        case "CHAR_OCTET_LENGTH": {
            if (toSQLDataType() == Types.CHAR) {
                return toPrecision();
            }

            return 0;
        }

        // int => index of column in table (starting at 1)
        case "ORDINAL_POSITION": {
            return this.columnIndex;
        }

        /**
         * Columns responding to features that aren't handled by DBase 3.
         * and return always a default value NULL or "NO"...
         */

        // short => source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types (null if DATA_TYPE isn't DISTINCT or user-generated REF)
        case "SOURCE_DATA_TYPE": {
            this.wasNull = true;
            return 0;
        }

        // is not used.
        case "BUFFER_LENGTH": {
            this.wasNull = false;
            return 0;
        }

        // int => unused
        case "SQL_DATETIME_SUB": {
            this.wasNull = false;
            return 0;
        }

        default:
            // FIXME : this function is not perfect. It a column label is given that refers to a field described in getString(..) this function
            // will tell that the field doesn't exist. It's not true : the field is not numeric. But as getString(..) defaults to getInt(...),
            // getInt(..) cannot default to getString(..), else the function will run in a cycle.
            String message = format(Level.WARNING, "excp.no_desc_field", columnLabel, getTableName());
            throw new SQLNoSuchFieldException(message, "asking columns desc", getFile(), columnLabel);
    }
}