Example usage for java.sql DatabaseMetaData columnNullable

List of usage examples for java.sql DatabaseMetaData columnNullable

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData columnNullable.

Prototype

int columnNullable

To view the source code for java.sql DatabaseMetaData columnNullable.

Click Source Link

Document

Indicates that the column definitely allows NULL values.

Usage

From source file:com.nextep.designer.sqlgen.generic.impl.JDBCCapturer.java

/**
 * Returns a <code>IBasicColumn</code> object representing the column
 * described in the currently selected row of the specified
 * <code>ResultSet</code>.//from   w ww. j a  va  2 s  .com
 * 
 * @param rset
 *            a {@link ResultSet} where each line is a description of a
 *            column; must not be <code>null</code>
 * @param dbObjName
 *            the name of the database object for which the
 *            {@link IBasicColumn} must be created
 * @return a {@link IBasicColumn} object if the column name in the currently
 *         selected row of the specified <code>ResultSet</code> is not
 *         <code>null</code> or empty, <code>null</code> otherwise
 * @throws SQLException
 *             if a database access error occurs
 */
private IBasicColumn getColumnFromResultSet(ICaptureContext context, ResultSet rset, String dbObjName)
        throws SQLException {
    IBasicColumn column = null;

    final String columnName = rset.getString(COLUMN_NAME_COLUMN_NAME);
    final int position = rset.getInt(COLUMN_NAME_ORDINAL_POSITION);
    final String dsTypeName = rset.getString(COLUMN_NAME_TYPE_NAME);
    // final String sqlTypeName = rset.getString(COLUMN_NAME_DATA_TYPE);
    final int length = rset.getInt(COLUMN_NAME_COLUMN_SIZE);
    final int precision = rset.getInt(COLUMN_NAME_DECIMAL_DIGITS);
    final boolean nullable = (rset.getInt(COLUMN_NAME_NULLABLE) == DatabaseMetaData.columnNullable);

    String desc = ""; //$NON-NLS-1$
    try {
        desc = rset.getString(COLUMN_NAME_COMMENT);
    } catch (SQLException sqle) {
        LOGGER.warn("Table column [" + dbObjName + "][" //$NON-NLS-2$
                + (null == columnName ? "" : columnName) + "]" //$NON-NLS-1$ //$NON-NLS-2$
                + " comment could not be fetched from database", sqle);
    }

    String defaultValue = ""; //$NON-NLS-1$
    try {
        defaultValue = rset.getString(COLUMN_NAME_COLUMN_DEF);
    } catch (SQLException sqle) {
        LOGGER.warn("Table column [" + dbObjName + "][" //$NON-NLS-2$
                + (null == columnName ? "" : columnName) + "]" //$NON-NLS-1$ //$NON-NLS-2$
                + " default value could not be fetched from database", sqle);
    }

    if (columnName != null && !"".equals(columnName.trim())) { //$NON-NLS-1$
        if (LOGGER.isDebugEnabled()) {
            String logPrefix = "[" + dbObjName + "][" + columnName + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            LOGGER.debug("= " + logPrefix + " Column Metadata ="); //$NON-NLS-1$ //$NON-NLS-2$
            LOGGER.debug(logPrefix + "[" + COLUMN_NAME_ORDINAL_POSITION + "] " + position); //$NON-NLS-1$ //$NON-NLS-2$
            LOGGER.debug(logPrefix + "[" + COLUMN_NAME_TYPE_NAME + "] " + dsTypeName); //$NON-NLS-1$ //$NON-NLS-2$
            LOGGER.debug(logPrefix + "[" + COLUMN_NAME_COLUMN_SIZE + "] " + length); //$NON-NLS-1$ //$NON-NLS-2$
            LOGGER.debug(logPrefix + "[" + COLUMN_NAME_DECIMAL_DIGITS + "] " + precision); //$NON-NLS-1$ //$NON-NLS-2$
            LOGGER.debug(logPrefix + "[" + COLUMN_NAME_NULLABLE + "] " + nullable); //$NON-NLS-1$ //$NON-NLS-2$
            LOGGER.debug(logPrefix + "[" + COLUMN_NAME_COMMENT + "] " + desc); //$NON-NLS-1$ //$NON-NLS-2$
            LOGGER.debug(logPrefix + "[" + COLUMN_NAME_COLUMN_DEF + "] " + defaultValue); //$NON-NLS-1$ //$NON-NLS-2$
        }

        column = typedObjFactory.create(IBasicColumn.class);
        column.setName(getConnectionVendor(context).getNameFormatter().format(columnName));
        column.setDescription(desc);
        column.setRank(position - 1);
        column.setNotNull(!nullable);
        column.setDefaultExpr(defaultValue != null ? defaultValue.trim() : ""); //$NON-NLS-1$

        final IDatatype datatype = new Datatype(dsTypeName);
        if (!DBGMHelper.getDatatypeProvider(getConnectionVendor(context)).getUnsizableDatatypes()
                .contains(datatype.getName())) {
            datatype.setLength(length);
            datatype.setPrecision(precision);
        }
        column.setDatatype(datatype);
    }

    return column;
}

From source file:com.jaxio.celerio.configuration.database.support.MetadataExtractor.java

private boolean isNullable(int nullable) {
    switch (nullable) {
    case DatabaseMetaData.columnNoNulls:
        return false;
    case DatabaseMetaData.columnNullableUnknown:
    case DatabaseMetaData.columnNullable:
    default:/*from w  w  w . j  a va2 s  .co  m*/
        return true;
    }
}

From source file:org.apache.openjpa.jdbc.schema.Column.java

/**
 * Sets nullability of this receiver by the given flag.
 * @param flag one of the JDBC nullability flag namely
 * <LI> {@link DatabaseMetaData#columnNullableUnknown} : not known if the column can be set to null value
 * <LI> {@link DatabaseMetaData#columnNullable} : the column can be set to null value
 * <LI> {@link DatabaseMetaData#columnNoNulls} : the column can not be set to null value
 *//*from w w w . ja va 2 s. c  o m*/
public void setNullability(short flag) {
    switch (flag) {
    case DatabaseMetaData.columnNullableUnknown:
        _notNull = null;
        break;
    case DatabaseMetaData.columnNullable:
        _notNull = false;
        break;
    case DatabaseMetaData.columnNoNulls:
        _notNull = true;
        break;

    }
}

From source file:ca.sqlpower.sqlobject.TestSQLColumn.java

public void testIsDefinitelyNullable() throws Exception {
    SQLColumn tmpCol = new SQLColumn();
    assertEquals(false, tmpCol.isDefinitelyNullable());
    tmpCol.setNullable(DatabaseMetaData.columnNullable);
    assertEquals(true, tmpCol.isDefinitelyNullable());
    tmpCol.setNullable(DatabaseMetaData.columnNoNulls);
    assertEquals(false, tmpCol.isDefinitelyNullable());
    tmpCol.setNullable(DatabaseMetaData.columnNullableUnknown);
    assertEquals(false, tmpCol.isDefinitelyNullable());

    SQLColumn cowCol = table1pk.getColumn(0);

    assertEquals(false, cowCol.isDefinitelyNullable());
    cowCol.setNullable(DatabaseMetaData.columnNullable);
    assertEquals(true, cowCol.isDefinitelyNullable());
    cowCol.setNullable(DatabaseMetaData.columnNoNulls);
    assertEquals(false, cowCol.isDefinitelyNullable());
    cowCol.setNullable(DatabaseMetaData.columnNullableUnknown);
    assertEquals(false, cowCol.isDefinitelyNullable());

}

From source file:ca.sqlpower.sqlobject.TestSQLColumn.java

public void testGetNullable() throws Exception {
    SQLColumn tmpCol = new SQLColumn();
    assertEquals(DatabaseMetaData.columnNoNulls, tmpCol.getNullable());
    tmpCol.setNullable(DatabaseMetaData.columnNullable);
    assertEquals(DatabaseMetaData.columnNullable, tmpCol.getNullable());
    tmpCol.setNullable(DatabaseMetaData.columnNullableUnknown);
    assertEquals(DatabaseMetaData.columnNullableUnknown, tmpCol.getNullable());
    tmpCol.setNullable(DatabaseMetaData.columnNoNulls);
    assertEquals(DatabaseMetaData.columnNoNulls, tmpCol.getNullable());

    SQLColumn cowCol = table1pk.getColumn(0);
    assertEquals(DatabaseMetaData.columnNoNulls, cowCol.getNullable());
    cowCol.setNullable(DatabaseMetaData.columnNullable);
    assertEquals(DatabaseMetaData.columnNullable, cowCol.getNullable());
    cowCol.setNullable(DatabaseMetaData.columnNullableUnknown);
    assertEquals(DatabaseMetaData.columnNullableUnknown, cowCol.getNullable());
    cowCol.setNullable(DatabaseMetaData.columnNoNulls);
    assertEquals(DatabaseMetaData.columnNoNulls, cowCol.getNullable());
}

From source file:org.apache.ddlutils.task.DumpMetadataTask.java

/**
 * Dumps the columns of the indicated table.
 * //from ww  w  .  ja  v a2 s. c o  m
 * @param xmlWriter   The xml writer to write to
 * @param metaData    The database metadata
 * @param catalogName The catalog name
 * @param schemaName  The schema name
 * @param tableName   The table name
 */
private void dumpColumns(PrettyPrintingXmlWriter xmlWriter, final DatabaseMetaData metaData,
        final String catalogName, final String schemaName, final String tableName) throws SQLException {
    performResultSetXmlOperation(xmlWriter, null, new ResultSetXmlOperation() {
        public ResultSet getResultSet() throws SQLException {
            return metaData.getColumns(catalogName, schemaName, tableName, _columnPattern);
        }

        public void handleRow(PrettyPrintingXmlWriter xmlWriter, ResultSet result) throws SQLException {
            Set columns = getColumnsInResultSet(result);
            String columnName = result.getString("COLUMN_NAME");

            if ((columnName != null) && (columnName.length() > 0)) {
                xmlWriter.writeElementStart(null, "column");
                xmlWriter.writeAttribute(null, "name", columnName);

                addIntAttribute(xmlWriter, "typeCode", result, columns, "DATA_TYPE");
                addStringAttribute(xmlWriter, "type", result, columns, "TYPE_NAME");
                addIntAttribute(xmlWriter, "size", result, columns, "COLUMN_SIZE");
                addIntAttribute(xmlWriter, "digits", result, columns, "DECIMAL_DIGITS");
                addIntAttribute(xmlWriter, "precision", result, columns, "NUM_PREC_RADIX");
                if (columns.contains("NULLABLE")) {
                    try {
                        switch (result.getInt("NULLABLE")) {
                        case DatabaseMetaData.columnNoNulls:
                            xmlWriter.writeAttribute(null, "nullable", "false");
                            break;
                        case DatabaseMetaData.columnNullable:
                            xmlWriter.writeAttribute(null, "nullable", "true");
                            break;
                        default:
                            xmlWriter.writeAttribute(null, "nullable", "unknown");
                            break;
                        }
                    } catch (SQLException ex) {
                        log("Could not read the NULLABLE value for colum '" + columnName + "' of table '"
                                + tableName + "' from the result set: " + ex.getStackTrace(), Project.MSG_ERR);
                    }
                }
                addStringAttribute(xmlWriter, "remarks", result, columns, "REMARKS");
                addStringAttribute(xmlWriter, "defaultValue", result, columns, "COLUMN_DEF");
                addIntAttribute(xmlWriter, "maxByteLength", result, columns, "CHAR_OCTET_LENGTH");
                addIntAttribute(xmlWriter, "index", result, columns, "ORDINAL_POSITION");
                if (columns.contains("IS_NULLABLE")) {
                    try {
                        String value = result.getString("IS_NULLABLE");

                        if ("no".equalsIgnoreCase(value)) {
                            xmlWriter.writeAttribute(null, "isNullable", "false");
                        } else if ("yes".equalsIgnoreCase(value)) {
                            xmlWriter.writeAttribute(null, "isNullable", "true");
                        } else {
                            xmlWriter.writeAttribute(null, "isNullable", "unknown");
                        }
                    } catch (SQLException ex) {
                        log("Could not read the IS_NULLABLE value for colum '" + columnName + "' of table '"
                                + tableName + "' from the result set: " + ex.getStackTrace(), Project.MSG_ERR);
                    }
                }
                addStringAttribute(xmlWriter, "refCatalog", result, columns, "SCOPE_CATLOG");
                addStringAttribute(xmlWriter, "refSchema", result, columns, "SCOPE_SCHEMA");
                addStringAttribute(xmlWriter, "refTable", result, columns, "SCOPE_TABLE");
                addShortAttribute(xmlWriter, "sourceTypeCode", result, columns, "SOURCE_DATA_TYPE");

                xmlWriter.writeElementEnd();
            }
        }

        public void handleError(SQLException ex) {
            log("Could not read the colums for table '" + tableName + "' from the result set: "
                    + ex.getStackTrace(), Project.MSG_ERR);
        }
    });
}

From source file:ca.sqlpower.sqlobject.TestSQLColumn.java

/**
 * Tests the SQLColumn and {@link UserDefinedSQLType} at the same time. If
 * you set the column's type through its setter the persister should see the
 * events come through correctly and not throw an exception. Previously the
 * old value of the last property change was not matching the new value of
 * the first property change when persisting the object.
 *//*from w w w .jav a 2  s .  co  m*/
public void testSetNullabilityAfterCreation() throws Exception {
    final CountingSPPersister persister = new CountingSPPersister();
    SPPersisterListener listener = new SPPersisterListener(persister, getConverter());
    SQLTable table = (SQLTable) createNewValueMaker(getRootObject(), new PlDotIni())
            .makeNewValue(SQLTable.class, null, "");
    table.addSPListener(listener);

    UserDefinedSQLType underlyingType = new UserDefinedSQLType();
    underlyingType.setMyAutoIncrement(false);
    underlyingType.setMyNullability(DatabaseMetaData.columnNullableUnknown);
    underlyingType.setType(Types.VARCHAR);

    table.begin("Transaction for testing");
    SQLColumn col = new SQLColumn(underlyingType);
    table.addColumn(col);

    int nullable = col.getNullable();
    if (nullable == DatabaseMetaData.columnNullable) {
        nullable = DatabaseMetaData.columnNullableUnknown;
    } else {
        nullable = DatabaseMetaData.columnNullable;
    }
    col.setNullable(nullable);

    table.commit();

}