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

The following examples show how to use java.sql.DatabaseMetaData#columnNoNulls() . 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: ColumnMetaData.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
/** Creates a ColumnMetaData for result sets that are not based on a struct
 * but need to have a single 'field' for purposes of
 * {@link java.sql.ResultSetMetaData}. */
public static ColumnMetaData dummy(AvaticaType type, boolean nullable) {
  return new ColumnMetaData(
      0,
      false,
      true,
      false,
      false,
      nullable
          ? DatabaseMetaData.columnNullable
          : DatabaseMetaData.columnNoNulls,
      true,
      -1,
      null,
      null,
      null,
      -1,
      -1,
      null,
      null,
      type,
      true,
      false,
      false,
      type.columnClassName());
}
 
Example 3
Source File: TestDBSetup.java    From development with Apache License 2.0 6 votes vote down vote up
private static void setFKColumnsNull(Connection conn, String user,
        String tableName) throws SQLException {
    ResultSet rs = conn.getMetaData()
            .getExportedKeys(null, null, tableName);
    Statement stmt = conn.createStatement();
    while (rs.next()) {
        String sourceTableName = rs.getString("FKTABLE_NAME");
        String columnName = rs.getString("FKCOLUMN_NAME");
        ResultSet columns = conn.getMetaData().getColumns(null, user,
                sourceTableName, columnName);
        while (columns.next()) {
            if (columns.getInt("NULLABLE") != DatabaseMetaData.columnNoNulls) {
                String queryString = String.format(
                        "UPDATE %s SET %s = NULL", sourceTableName,
                        columnName);
                stmt.executeUpdate(queryString);
            }
        }
    }
}
 
Example 4
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 5
Source File: CalcitePrepareImpl.java    From Quicksql with MIT License 5 votes vote down vote up
private ColumnMetaData metaData(JavaTypeFactory typeFactory, int ordinal,
    String fieldName, RelDataType type, RelDataType fieldType,
    List<String> origins) {
  final ColumnMetaData.AvaticaType avaticaType =
      avaticaType(typeFactory, type, fieldType);
  return new ColumnMetaData(
      ordinal,
      false,
      true,
      false,
      false,
      type.isNullable()
          ? DatabaseMetaData.columnNullable
          : DatabaseMetaData.columnNoNulls,
      true,
      type.getPrecision(),
      fieldName,
      origin(origins, 0),
      origin(origins, 2),
      getPrecision(type),
      getScale(type),
      origin(origins, 1),
      null,
      avaticaType,
      true,
      false,
      false,
      avaticaType.columnClassName());
}
 
Example 6
Source File: JdbcSchema.java    From Quicksql with MIT License 5 votes vote down vote up
RelProtoDataType getRelDataType(DatabaseMetaData metaData, String catalogName,
    String schemaName, String tableName) throws SQLException {
  final ResultSet resultSet =
      metaData.getColumns(catalogName, schemaName, tableName, null);

  // Temporary type factory, just for the duration of this method. Allowable
  // because we're creating a proto-type, not a type; before being used, the
  // proto-type will be copied into a real type factory.
  final RelDataTypeFactory typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
  while (resultSet.next()) {
    final String columnName = resultSet.getString(4);
    final int dataType = resultSet.getInt(5);
    final String typeString = resultSet.getString(6);
    final int precision;
    final int scale;
    switch (SqlType.valueOf(dataType)) {
    case TIMESTAMP:
    case TIME:
      precision = resultSet.getInt(9); // SCALE
      scale = 0;
      break;
    default:
      precision = resultSet.getInt(7); // SIZE
      scale = resultSet.getInt(9); // SCALE
      break;
    }
    RelDataType sqlType =
        sqlType(typeFactory, dataType, precision, scale, typeString);
    boolean nullable = resultSet.getInt(11) != DatabaseMetaData.columnNoNulls;
    fieldInfo.add(columnName, sqlType).nullable(nullable);
  }
  resultSet.close();
  return RelDataTypeImpl.proto(fieldInfo.build());
}
 
Example 7
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 8
Source File: CudHandler.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Object getValue(Row row, ReplicatorColumnMeta meta, boolean isBlobRow) {
    Object result = row.get(meta.hcolumnPos);
    if ((result == null) && isBlobRow) {
        return null;
    }
    // special logic to handle mysql 0000-00-00 00:00
    if (result==null && meta.dataType==Types.TIMESTAMP && meta.nullable==DatabaseMetaData.columnNoNulls) {
        if ("0000-00-00 00:00:00".equals(meta.defaultValue)) {
            result = "0000-00-00 00:00:00";
        }
    }
    if (result==null && meta.dataType==Types.DATE && meta.nullable==DatabaseMetaData.columnNoNulls) {
        if ("0000-00-00".equals(meta.defaultValue)) {
            result = "0000-00-00";
        }
    }
    if (result instanceof Date && ((Date)result).getTime() == Long.MIN_VALUE) {
        result = "0000-00-00";
    }
    if (result instanceof Timestamp && ((Timestamp)result).getTime() == Long.MIN_VALUE) {
        result = "0000-00-00 00:00:00";
    }
    if (result instanceof Duration) {
        result = UberFormatter.duration((Duration)result);
    }
    return result;
}
 
Example 9
Source File: PlanExecutor.java    From quark with Apache License 2.0 5 votes vote down vote up
private ColumnMetaData metaData(JavaTypeFactory typeFactory, int ordinal,
                                String fieldName, RelDataType type, RelDataType fieldType,
                                List<String> origins) {
  final ColumnMetaData.AvaticaType avaticaType =
      avaticaType(typeFactory, type, fieldType);
  return new ColumnMetaData(
      ordinal,
      false,
      true,
      false,
      false,
      type.isNullable()
          ? DatabaseMetaData.columnNullable
          : DatabaseMetaData.columnNoNulls,
      true,
      type.getPrecision(),
      fieldName,
      origin(origins, 0),
      origin(origins, 2),
      getPrecision(type),
      getScale(type),
      origin(origins, 1),
      null,
      avaticaType,
      true,
      false,
      false,
      avaticaType.columnClassName());
}
 
Example 10
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
private ColumnMetaData metaData(JavaTypeFactory typeFactory, int ordinal,
    String fieldName, RelDataType type, RelDataType fieldType,
    List<String> origins) {
  final ColumnMetaData.AvaticaType avaticaType =
      avaticaType(typeFactory, type, fieldType);
  return new ColumnMetaData(
      ordinal,
      false,
      true,
      false,
      false,
      type.isNullable()
          ? DatabaseMetaData.columnNullable
          : DatabaseMetaData.columnNoNulls,
      true,
      type.getPrecision(),
      fieldName,
      origin(origins, 0),
      origin(origins, 2),
      getPrecision(type),
      getScale(type),
      origin(origins, 1),
      null,
      avaticaType,
      true,
      false,
      false,
      avaticaType.columnClassName());
}
 
Example 11
Source File: JdbcSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
RelProtoDataType getRelDataType(DatabaseMetaData metaData, String catalogName,
    String schemaName, String tableName) throws SQLException {
  final ResultSet resultSet =
      metaData.getColumns(catalogName, schemaName, tableName, null);

  // Temporary type factory, just for the duration of this method. Allowable
  // because we're creating a proto-type, not a type; before being used, the
  // proto-type will be copied into a real type factory.
  final RelDataTypeFactory typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
  while (resultSet.next()) {
    final String columnName = resultSet.getString(4);
    final int dataType = resultSet.getInt(5);
    final String typeString = resultSet.getString(6);
    final int precision;
    final int scale;
    switch (SqlType.valueOf(dataType)) {
    case TIMESTAMP:
    case TIME:
      precision = resultSet.getInt(9); // SCALE
      scale = 0;
      break;
    default:
      precision = resultSet.getInt(7); // SIZE
      scale = resultSet.getInt(9); // SCALE
      break;
    }
    RelDataType sqlType =
        sqlType(typeFactory, dataType, precision, scale, typeString);
    boolean nullable = resultSet.getInt(11) != DatabaseMetaData.columnNoNulls;
    fieldInfo.add(columnName, sqlType).nullable(nullable);
  }
  resultSet.close();
  return RelDataTypeImpl.proto(fieldInfo.build());
}
 
Example 12
Source File: MetaImpl.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
private static int intForColumnNullable(boolean nullable) {
  return nullable ? DatabaseMetaData.columnNullable : DatabaseMetaData.columnNoNulls;
}
 
Example 13
Source File: KylinColumnMetaData.java    From Kylin with Apache License 2.0 4 votes vote down vote up
public static ColumnMetaData dummy(int ordinal, String label, String columnName, AvaticaType type, boolean nullable) {
    return new ColumnMetaData(ordinal, false, true, false, false, nullable ? DatabaseMetaData.columnNullable : DatabaseMetaData.columnNoNulls, true, -1, label, columnName, null, -1, -1, null, null, type, true, false, false, null);
}