Example usage for java.sql DatabaseMetaData attributeNullable

List of usage examples for java.sql DatabaseMetaData attributeNullable

Introduction

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

Prototype

short attributeNullable

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

Click Source Link

Document

Indicates that NULL values are definitely allowed.

Usage

From source file:net.starschema.clouddb.jdbc.BQDatabaseMetadata.java

private String nullableAsInt(String mode) {
    if (mode == null) {
        return String.valueOf(DatabaseMetaData.attributeNullableUnknown);
    }/*from   w w w.ja  va 2s .c om*/
    if ("NULLABLE".equals(mode)) {
        return String.valueOf(DatabaseMetaData.attributeNullable);
    }
    if ("REQUIRED".equals(mode)) {
        return String.valueOf(DatabaseMetaData.attributeNoNulls);
    }
    if ("REPEATED".equals(mode)) {
        return String.valueOf(DatabaseMetaData.attributeNullableUnknown);
    }
    return String.valueOf(DatabaseMetaData.attributeNullableUnknown);
}

From source file:org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.java

@Override
public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern,
        String columnNamePattern) throws SQLException {
    StringBuilder buf = new StringBuilder("select \n " + TENANT_ID + " " + TABLE_CAT + "," + // use this for tenant id
            TABLE_SCHEM + "," + TABLE_NAME + " ," + COLUMN_NAME + "," + ExternalSqlTypeIdFunction.NAME + "("
            + DATA_TYPE + ") AS " + DATA_TYPE + "," + SqlTypeNameFunction.NAME + "(" + DATA_TYPE + ") AS "
            + TYPE_NAME + "," + COLUMN_SIZE + "," + BUFFER_LENGTH + "," + DECIMAL_DIGITS + "," + NUM_PREC_RADIX
            + "," + NULLABLE + "," + COLUMN_DEF + "," + SQL_DATA_TYPE + "," + SQL_DATETIME_SUB + ","
            + CHAR_OCTET_LENGTH + "," + "CASE WHEN " + TENANT_POS_SHIFT + " THEN " + ORDINAL_POSITION
            + "-1 ELSE " + ORDINAL_POSITION + " END AS " + ORDINAL_POSITION + "," + "CASE " + NULLABLE
            + " WHEN " + DatabaseMetaData.attributeNoNulls + " THEN '" + Boolean.FALSE.toString() + "' WHEN "
            + DatabaseMetaData.attributeNullable + " THEN '" + Boolean.TRUE.toString() + "' END AS "
            + IS_NULLABLE + "," + SCOPE_CATALOG + "," + SCOPE_SCHEMA + "," + SCOPE_TABLE + ","
            + SOURCE_DATA_TYPE + "," + IS_AUTOINCREMENT + "," + ARRAY_SIZE + "," + COLUMN_FAMILY + ","
            + DATA_TYPE + " " + TYPE_ID + "," + // raw type id for potential internal consumption
            VIEW_CONSTANT + "," + MULTI_TENANT + "," + "CASE WHEN " + TENANT_POS_SHIFT + " THEN " + KEY_SEQ
            + "-1 ELSE " + KEY_SEQ + " END AS " + KEY_SEQ + " from " + SYSTEM_CATALOG + " "
            + SYSTEM_CATALOG_ALIAS + "(" + TENANT_POS_SHIFT + " BOOLEAN)");
    StringBuilder where = new StringBuilder();
    addTenantIdFilter(where, catalog);// w  ww .  j  a  v  a 2 s .  c o m
    if (schemaPattern != null) {
        appendConjunction(where);
        where.append(TABLE_SCHEM
                + (schemaPattern.length() == 0 ? " is null" : " like '" + escapePattern(schemaPattern) + "'"));
    }
    if (tableNamePattern != null && tableNamePattern.length() > 0) {
        appendConjunction(where);
        where.append(TABLE_NAME + " like '" + escapePattern(tableNamePattern) + "'");
    }
    // Allow a "." in columnNamePattern for column family match
    String colPattern = null;
    if (columnNamePattern != null && columnNamePattern.length() > 0) {
        String cfPattern = null;
        int index = columnNamePattern.indexOf('.');
        if (index <= 0) {
            colPattern = columnNamePattern;
        } else {
            cfPattern = columnNamePattern.substring(0, index);
            if (columnNamePattern.length() > index + 1) {
                colPattern = columnNamePattern.substring(index + 1);
            }
        }
        if (cfPattern != null && cfPattern.length() > 0) { // if null or empty, will pick up all columns
            // Will pick up only KV columns
            appendConjunction(where);
            where.append(COLUMN_FAMILY + " like '" + escapePattern(cfPattern) + "'");
        }
        if (colPattern != null && colPattern.length() > 0) {
            appendConjunction(where);
            where.append(COLUMN_NAME + " like '" + escapePattern(colPattern) + "'");
        }
    }
    if (colPattern == null) {
        appendConjunction(where);
        where.append(COLUMN_NAME + " is not null");
    }
    boolean isTenantSpecificConnection = connection.getTenantId() != null;
    if (isTenantSpecificConnection) {
        buf.append(" where (" + where + ") OR (" + COLUMN_FAMILY + " is null AND " + COLUMN_NAME + " is null)");
    } else {
        buf.append(" where " + where);
    }
    buf.append(" order by " + TENANT_ID + "," + TABLE_SCHEM + "," + TABLE_NAME + "," + SYSTEM_CATALOG_ALIAS
            + "." + ORDINAL_POSITION);

    Statement stmt;
    if (isTenantSpecificConnection) {
        stmt = connection.createStatement(new PhoenixStatementFactory() {
            @Override
            public PhoenixStatement newStatement(PhoenixConnection connection) {
                return new PhoenixStatement(connection) {
                    @Override
                    protected PhoenixResultSet newResultSet(ResultIterator iterator, RowProjector projector)
                            throws SQLException {
                        return new PhoenixResultSet(new TenantColumnFilteringIterator(iterator, projector),
                                projector, this);
                    }
                };
            }
        });
    } else {
        stmt = connection.createStatement();
    }
    return stmt.executeQuery(buf.toString());
}