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

The following examples show how to use java.sql.DatabaseMetaData#getAttributes() . 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: MetaResultSetTest.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
@Test public void testGetAttributes() throws SQLException {
  DatabaseMetaData metadata = getDatabaseMetadata();
  try (ResultSet rs = metadata.getAttributes(null, null, null, null)) {
    ResultSetMetaData rsMeta = rs.getMetaData();

    assertEquals(21, rsMeta.getColumnCount());
    assertColumn(rsMeta, 1, "TYPE_CAT", Types.VARCHAR, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 2, "TYPE_SCHEM", Types.VARCHAR, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 3, "TYPE_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 4, "ATTR_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 5, "DATA_TYPE", Types.INTEGER, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 6, "ATTR_TYPE_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 7, "ATTR_SIZE", Types.INTEGER, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 8, "DECIMAL_DIGITS", Types.INTEGER, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 9, "NUM_PREC_RADIX", Types.INTEGER, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 10, "NULLABLE", Types.INTEGER, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 11, "REMARKS", Types.VARCHAR, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 12, "ATTR_DEF", Types.VARCHAR, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 13, "SQL_DATA_TYPE", Types.INTEGER,
        DatabaseMetaData.columnNullableUnknown);
    assertColumn(rsMeta, 14, "SQL_DATETIME_SUB", Types.INTEGER,
        DatabaseMetaData.columnNullableUnknown);
    assertColumn(rsMeta, 15, "CHAR_OCTET_LENGTH", Types.INTEGER, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 16, "ORDINAL_POSITION", Types.INTEGER, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 17, "IS_NULLABLE", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 18, "SCOPE_CATALOG", Types.VARCHAR, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 19, "SCOPE_SCHEMA", Types.VARCHAR, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 20, "SCOPE_TABLE", Types.VARCHAR, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 21, "SOURCE_DATA_TYPE", Types.SMALLINT, DatabaseMetaData.columnNullable);
  }
}
 
Example 2
Source File: DatabaseMetaDataJDBC3Test.java    From jTDS with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Test meta data functions that return result sets.
 */
public void testResultSets() throws Exception
{
    DatabaseMetaData dbmd = con.getMetaData();
    ResultSet rs;
    //
    // JDBC3 Methods
    //
    rs = dbmd.getAttributes(null, null, null, null);
    assertTrue(checkColumnNames(rs, new String[]{"TYPE_CAT", "TYPE_SCHEM","TYPE_NAME","ATTR_NAME",
            "DATA_TYPE","ATTR_TYPE_NAME","ATTR_SIZE","DECIMAL_DIGITS","NUM_PREC_RADIX","NULLABLE",
            "REMARKS","ATTR_DEF","SQL_DATA_TYPE","SQL_DATETIME_SUB","CHAR_OCTET_LENGTH",
            "ORDINAL_POSITION","IS_NULLABLE","SCOPE_CATALOG","SCOPE_SCHEMA","SCOPE_TABLE","SOURCE_DATA_TYPE"}));
    assertFalse(rs.next());
    rs.close();
    //
    rs = dbmd.getSuperTables(null, null, "%");
    assertTrue(checkColumnNames(rs, new String[]{"TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME","SUPERTABLE_NAME"}));
    assertFalse(rs.next());
    rs.close();
    //
    rs = dbmd.getSuperTypes(null, null, "%");
    assertTrue(checkColumnNames(rs, new String[]{"TYPE_CAT", "TYPE_SCHEM", "TYPE_NAME",
            "SUPERTYPE_CAT", "SUPERTYPE_SCHEM", "SUPERTYPE_NAME"}));
    assertFalse(rs.next());
    rs.close();
}
 
Example 3
Source File: Show.java    From jsqsh with Apache License 2.0 5 votes vote down vote up
private ResultSet doAttributes(Session session, Connection con, Options options)
    throws SQLException {
    
    SQLObjectName name = null;
    SQLConnectionContext ctx = (SQLConnectionContext) session.getConnectionContext();
    
    if (options.arguments.size() == 1) {
        
        name = new SQLObjectName(ctx, "%");
    }
    else if (options.arguments.size() == 2) {
        
        name = new SQLObjectName(ctx, options.arguments.get(1));
    }
    else if (options.arguments.size() > 2) {
        
        session.err.println(
            "Use: \\show attributes [-p pattern] [[[catalog.]schema-pattern.]type-pattern]");
        return null;
    }
    
    DatabaseMetaData meta = con.getMetaData();
    return meta.getAttributes(
        (options.catalog != null ? options.catalog : name.getCatalog()),
        (options.schemaPattern != null ? options.schemaPattern : name.getSchema()),
        (options.tablePattern != null ? options.tablePattern : name.getName()),
        ctx.normalizeIdentifier(options.pattern));
}
 
Example 4
Source File: DatabaseMetaDataTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Test methods that describe attributes of SQL Objects
 * that are not supported by derby. In each case the
 * metadata should return an empty ResultSet of the
 * correct shape, and with correct names, datatypes and
 * nullability for the columns in the ResultSet.
 *
 */
public void testUnimplementedSQLObjectAttributes() throws SQLException
{
    DatabaseMetaData dmd = getDMD();

    ResultSet rs;

    rs = dmd.getAttributes(null,null,null,null);

    String [] columnNames = {
            "TYPE_CAT", "TYPE_SCHEM", "TYPE_NAME", "ATTR_NAME", "DATA_TYPE",
            "ATTR_TYPE_NAME", "ATTR_SIZE", "DECIMAL_DIGITS", "NUM_PREC_RADIX",
            "NULLABLE", "REMARKS", "ATTR_DEF", "SQL_DATA_TYPE",
            "SQL_DATETIME_SUB", "CHAR_OCTET_LENGTH", "ORDINAL_POSITION",
            "IS_NULLABLE", "SCOPE_CATALOG", "SCOPE_SCHEMA", "SCOPE_TABLE",
            "SOURCE_DATA_TYPE"
    };
    int [] columnTypes = {
            Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
            Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER,
            Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.INTEGER,
            Types.INTEGER, Types.INTEGER, Types.INTEGER,
            Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.SMALLINT
    };
    // DERBY-3171; we get a different value back for nullability for
    // a number of the columns with networkserver/client vs. embedded
    boolean nullval = true;
    if (usingDerbyNetClient())
        nullval = false;
    boolean [] nullability = {
            true, true, false, nullval, nullval, nullval, nullval,
            nullval, nullval, nullval, true, true, nullval, nullval,
            nullval, nullval, nullval, true, true, true, true
    };

    assertMetaDataResultSet(rs, columnNames, columnTypes, nullability);
    JDBC.assertEmpty(rs);

    rs = dmd.getCatalogs();
    checkCatalogsShape(rs);
    JDBC.assertEmpty(rs);

    rs = dmd.getSuperTables(null,null,null);
    columnNames = new String[] {
            "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "SUPERTABLE_NAME"};
    columnTypes = new int[] {
            Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
    nullability = new boolean[] {
            true, true, false, false};
    assertMetaDataResultSet(rs, columnNames, columnTypes, nullability);
    JDBC.assertEmpty(rs);

    rs = dmd.getSuperTypes(null,null,null);
    columnNames = new String[] {
            "TYPE_CAT", "TYPE_SCHEM", "TYPE_NAME", "SUPERTYPE_CAT",
            "SUPERTYPE_SCHEM", "SUPERTYPE_NAME"};
    columnTypes = new int[] {
            Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
            Types.VARCHAR, Types.VARCHAR};
    nullability = new boolean[] {
            true, true, false, true, true, false};
    assertMetaDataResultSet(rs, columnNames, columnTypes, nullability);
    JDBC.assertEmpty(rs);

    ResultSet rss[] = getVersionColumns(null,null, "No_such_table");
    checkVersionColumnsShape(rss);
    JDBC.assertEmpty(rss[0]);
    JDBC.assertEmpty(rss[1]);

    rs.close();
    rss[0].close();
    rss[1].close();
}
 
Example 5
Source File: DatabaseMetaDataTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Test methods that describe attributes of SQL Objects
 * that are not supported by derby. In each case the
 * metadata should return an empty ResultSet of the
 * correct shape, and with correct names, datatypes and
 * nullability for the columns in the ResultSet.
 *
 */
public void testUnimplementedSQLObjectAttributes() throws SQLException
{
    DatabaseMetaData dmd = getDMD();

    ResultSet rs;

    rs = dmd.getAttributes(null,null,null,null);

    String [] columnNames = {
            "TYPE_CAT", "TYPE_SCHEM", "TYPE_NAME", "ATTR_NAME", "DATA_TYPE",
            "ATTR_TYPE_NAME", "ATTR_SIZE", "DECIMAL_DIGITS", "NUM_PREC_RADIX",
            "NULLABLE", "REMARKS", "ATTR_DEF", "SQL_DATA_TYPE",
            "SQL_DATETIME_SUB", "CHAR_OCTET_LENGTH", "ORDINAL_POSITION",
            "IS_NULLABLE", "SCOPE_CATALOG", "SCOPE_SCHEMA", "SCOPE_TABLE",
            "SOURCE_DATA_TYPE"
    };
    int [] columnTypes = {
            Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
            Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER,
            Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.INTEGER,
            Types.INTEGER, Types.INTEGER, Types.INTEGER,
            Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.SMALLINT
    };
    // DERBY-3171; we get a different value back for nullability for
    // a number of the columns with networkserver/client vs. embedded
    boolean nullval = true;
    if (usingDerbyNetClient())
        nullval = false;
    boolean [] nullability = {
            true, true, false, nullval, nullval, nullval, nullval,
            nullval, nullval, nullval, true, true, nullval, nullval,
            nullval, nullval, nullval, true, true, true, true
    };

    assertMetaDataResultSet(rs, columnNames, columnTypes, nullability);
    JDBC.assertEmpty(rs);

    rs = dmd.getCatalogs();
    checkCatalogsShape(rs);
    JDBC.assertEmpty(rs);

    rs = dmd.getSuperTables(null,null,null);
    columnNames = new String[] {
            "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "SUPERTABLE_NAME"};
    columnTypes = new int[] {
            Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
    nullability = new boolean[] {
            true, true, false, false};
    assertMetaDataResultSet(rs, columnNames, columnTypes, nullability);
    JDBC.assertEmpty(rs);

    rs = dmd.getSuperTypes(null,null,null);
    columnNames = new String[] {
            "TYPE_CAT", "TYPE_SCHEM", "TYPE_NAME", "SUPERTYPE_CAT",
            "SUPERTYPE_SCHEM", "SUPERTYPE_NAME"};
    columnTypes = new int[] {
            Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
            Types.VARCHAR, Types.VARCHAR};
    nullability = new boolean[] {
            true, true, false, true, true, false};
    assertMetaDataResultSet(rs, columnNames, columnTypes, nullability);
    JDBC.assertEmpty(rs);

    ResultSet rss[] = getVersionColumns(null,null, "No_such_table");
    checkVersionColumnsShape(rss);
    JDBC.assertEmpty(rss[0]);
    JDBC.assertEmpty(rss[1]);

    rs.close();
    rss[0].close();
    rss[1].close();
}