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

The following examples show how to use java.sql.DatabaseMetaData#getBestRowIdentifier() . 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: MetaDataRegressionTest.java    From r-course with MIT License 6 votes vote down vote up
/**
 * Tests fix for BUG#8803, 'DATA_TYPE' column from
 * DBMD.getBestRowIdentifier() causes ArrayIndexOutOfBoundsException when
 * accessed (and in fact, didn't return any value).
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug8803() throws Exception {
    String tableName = "testBug8803";
    createTable(tableName, "(field1 INT NOT NULL PRIMARY KEY)");
    DatabaseMetaData metadata = this.conn.getMetaData();
    try {
        this.rs = metadata.getBestRowIdentifier(this.conn.getCatalog(), null, tableName, DatabaseMetaData.bestRowNotPseudo, true);

        assertTrue(this.rs.next());

        this.rs.getInt("DATA_TYPE"); // **** Fails here *****
    } finally {
        if (this.rs != null) {
            this.rs.close();

            this.rs = null;
        }
    }

}
 
Example 2
Source File: MetaResultSetTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Test public void testGetBestRowIdentifier() throws SQLException {
  DatabaseMetaData metadata = getDatabaseMetadata();
  try (ResultSet rs = metadata.getBestRowIdentifier(null, null, null,
      DatabaseMetaData.bestRowUnknown, false)) {
    ResultSetMetaData rsMeta = rs.getMetaData();

    assertEquals(8, rsMeta.getColumnCount());
    assertColumn(rsMeta, 1, "SCOPE", Types.SMALLINT, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 2, "COLUMN_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 3, "DATA_TYPE", Types.INTEGER, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 4, "TYPE_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 5, "COLUMN_SIZE", Types.INTEGER, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 6, "BUFFER_LENGTH", Types.INTEGER,
        DatabaseMetaData.columnNullableUnknown);
    assertColumn(rsMeta, 7, "DECIMAL_DIGITS", Types.SMALLINT, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 8, "PSEUDO_COLUMN", Types.SMALLINT, DatabaseMetaData.columnNoNulls);
  }
}
 
Example 3
Source File: MetaDataRegressionTest.java    From Komondor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Tests fix for BUG#8803, 'DATA_TYPE' column from
 * DBMD.getBestRowIdentifier() causes ArrayIndexOutOfBoundsException when
 * accessed (and in fact, didn't return any value).
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug8803() throws Exception {
    String tableName = "testBug8803";
    createTable(tableName, "(field1 INT NOT NULL PRIMARY KEY)");
    DatabaseMetaData metadata = this.conn.getMetaData();
    try {
        this.rs = metadata.getBestRowIdentifier(this.conn.getCatalog(), null, tableName, DatabaseMetaData.bestRowNotPseudo, true);

        assertTrue(this.rs.next());

        this.rs.getInt("DATA_TYPE"); // **** Fails here *****
    } finally {
        if (this.rs != null) {
            this.rs.close();

            this.rs = null;
        }
    }

}
 
Example 4
Source File: TestDatabaseMetaData.java    From evosql with Apache License 2.0 5 votes vote down vote up
/**
 * Basic test of DatabaseMetaData functions that access system tables
 */
public void testTwo() throws Exception {

    Connection conn = newConnection();
    int        updateCount;

    try {
        TestUtil.testScript(conn, "testrun/hsqldb/TestSelf.txt");

        DatabaseMetaData dbmeta = conn.getMetaData();

        dbmeta.allProceduresAreCallable();
        dbmeta.getBestRowIdentifier(null, null, "T_1",
                                    DatabaseMetaData.bestRowTransaction,
                                    true);
        dbmeta.getCatalogs();
        dbmeta.getColumnPrivileges(null, "PUBLIC", "T_1", "%");
        dbmeta.getColumns("PUBLIC", "PUBLIC", "T_1", "%");
        dbmeta.getCrossReference(null, null, "T_1", null, null, "T_1");
        dbmeta.getExportedKeys(null, null, "T_1");
        dbmeta.getFunctionColumns(null, "%", "%", "%");
        dbmeta.getFunctions(null, "%", "%");
        dbmeta.getImportedKeys("PUBLIC", "PUBLIC", "T_1");
        dbmeta.getIndexInfo("PUBLIC", "PUBLIC", "T1", true, true);
        dbmeta.getPrimaryKeys("PUBLIC", "PUBLIC", "T_1");
        dbmeta.getProcedureColumns(null, null, "%", "%");
        dbmeta.getProcedures("PUBLIC", "%", "%");
        dbmeta.getSchemas(null, "#");
        dbmeta.getTablePrivileges(null, "%", "%");
        dbmeta.getUDTs(null, "%", "%", new int[]{ Types.DISTINCT });
    } catch (Exception e) {
        assertTrue("unable to prepare or execute DDL", false);
    } finally {
        conn.close();
    }
}
 
Example 5
Source File: DatabaseMetaDataTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method for testing getBestRowIdentifier - calls
 * dmd.getBestRowIdentifier for the JDBC call, and getBestRowIdentifierODBC
 * for the ODBC procedure
 * @throws SQLException
 */
private ResultSet[] getBestRowIdentifier(String catalog, String schema, String table,
    int scope, boolean nullable) throws SQLException
{
    ResultSet[] rss = new ResultSet[2];
    DatabaseMetaData dmd = getDMD();
    rss[0]= dmd.getBestRowIdentifier(catalog, schema, table, scope, nullable);
    rss[1]= getBestRowIdentifierODBC(catalog, schema, table, scope, nullable);

    String[] columnNames = {
            "SCOPE", "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME",
            "COLUMN_SIZE", "BUFFER_LENGTH", "DECIMAL_DIGITS",
            "PSEUDO_COLUMN"};
    int[] columnTypes = {
            Types.SMALLINT, Types.VARCHAR, Types.INTEGER, Types.VARCHAR,
            Types.INTEGER, Types.INTEGER, Types.SMALLINT, Types.SMALLINT};
    int[] odbcColumnTypes = {
            Types.SMALLINT, Types.VARCHAR, Types.SMALLINT, Types.VARCHAR,
            Types.INTEGER, Types.INTEGER, Types.SMALLINT, Types.SMALLINT};
    boolean [] nullability = {
            true, false, true, true, true, true, true, true};

    // column nullability is opposite to with scope=1 or 2...DERBY-3181
    // When running the ODBC version, the datatype returned for
    // column 3 is SMALLINT, vs. INTEGER when scope=1 or 2...
    // So, in this case, the columnTypes are the same for ODBC and JDBC,
    // but with calls with a valid scope, they are different.

    if (scope != DatabaseMetaData.bestRowTemporary &&
        scope != DatabaseMetaData.bestRowTransaction &&
        scope != DatabaseMetaData.bestRowSession)
    {
      nullability = new boolean [] {
                false, false, false, false, false, false, false, false};

      odbcColumnTypes = columnTypes;
    }

    assertMetaDataResultSet(rss[0], columnNames, columnTypes, nullability);
    assertMetaDataResultSet(rss[1], columnNames, odbcColumnTypes, nullability);

    return rss;
}
 
Example 6
Source File: DatabaseMetaDataTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method for testing getBestRowIdentifier - calls
 * dmd.getBestRowIdentifier for the JDBC call, and getBestRowIdentifierODBC
 * for the ODBC procedure
 * @throws SQLException
 */
private ResultSet[] getBestRowIdentifier(String catalog, String schema, String table,
    int scope, boolean nullable) throws SQLException
{
    ResultSet[] rss = new ResultSet[2];
    DatabaseMetaData dmd = getDMD();
    rss[0]= dmd.getBestRowIdentifier(catalog, schema, table, scope, nullable);
    rss[1]= getBestRowIdentifierODBC(catalog, schema, table, scope, nullable);

    String[] columnNames = {
            "SCOPE", "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME",
            "COLUMN_SIZE", "BUFFER_LENGTH", "DECIMAL_DIGITS",
            "PSEUDO_COLUMN"};
    int[] columnTypes = {
            Types.SMALLINT, Types.VARCHAR, Types.INTEGER, Types.VARCHAR,
            Types.INTEGER, Types.INTEGER, Types.SMALLINT, Types.SMALLINT};
    int[] odbcColumnTypes = {
            Types.SMALLINT, Types.VARCHAR, Types.SMALLINT, Types.VARCHAR,
            Types.INTEGER, Types.INTEGER, Types.SMALLINT, Types.SMALLINT};
    boolean [] nullability = {
            true, false, true, true, true, true, true, true};

    // column nullability is opposite to with scope=1 or 2...DERBY-3181
    // When running the ODBC version, the datatype returned for
    // column 3 is SMALLINT, vs. INTEGER when scope=1 or 2...
    // So, in this case, the columnTypes are the same for ODBC and JDBC,
    // but with calls with a valid scope, they are different.

    if (scope != DatabaseMetaData.bestRowTemporary &&
        scope != DatabaseMetaData.bestRowTransaction &&
        scope != DatabaseMetaData.bestRowSession)
    {
      nullability = new boolean [] {
                false, false, false, false, false, false, false, false};

      odbcColumnTypes = columnTypes;
    }

    assertMetaDataResultSet(rss[0], columnNames, columnTypes, nullability);
    assertMetaDataResultSet(rss[1], columnNames, odbcColumnTypes, nullability);

    return rss;
}