db-derby-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ян Программист <webautoma...@gmail.com>
Subject JDBC Metadata cursor
Date Wed, 14 Apr 2010 10:16:34 GMT
Following code gives an exception; a can't figure out how to get scrollable
cursor for metadata:

import static org.junit.Assert.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.derby.client.am.Cursor;
import org.apache.derby.client.am.ResultSet;
import org.apache.derby.iapi.sql.Statement;
import org.apache.derby.iapi.sql.execute.CursorResultSet;
import org.apache.derby.impl.jdbc.EmbedResultSet40;
import org.apache.derby.impl.jdbc.EmbedStatement40;
import org.apache.derbyTesting.functionTests.tests.lang.CursorTest;
import org.apache.derbyTesting.junit.Derby;
import org.hamcrest.core.Is;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class DatabaseMetaDataTest {
private static java.sql.Connection derby_conn;
/**
 * @throws java.lang.Exception
 */
private static java.sql.DatabaseMetaData metadata;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
derby_conn =
DriverManager.getConnection("jdbc:derby:repository;create=true");
}

/**
 * @throws java.lang.Exception
 */
@AfterClass
public static void tearDownAfterClass() throws Exception {
derby_conn.close();
}

/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
Statement stmt = (EmbedStatement40)
derby_conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
metadata = derby_conn.getMetaData();
}

/**
 * @throws java.lang.Exception
 */
@After
public void tearDown() throws Exception {
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getSchemaTerm()}.
 * @throws SQLException
 */
@Test
public final void testGetSchemaTerm() throws SQLException {
String schemaTerm = metadata.getSchemaTerm();
assertNotNull("...", schemaTerm);
assertTrue(schemaTerm.length() > 0);
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#isCatalogAtStart()}.
 * @throws SQLException
 */
@Test
public final void testIsCatalogAtStart() throws SQLException {
boolean isAtStart = metadata.isCatalogAtStart();
assertNotNull(isAtStart);
assertTrue(isAtStart);
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getMaxColumnNameLength()}.
 * @throws SQLException
 */
@Test
public final void testGetMaxColumnNameLength() throws SQLException {
int length = metadata.getMaxColumnNameLength();
assertNotNull(length);
assertTrue("", length > 0);
assertTrue("", length > 10);
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getMaxColumnsInGroupBy()}.
 */
@Test
public final void testGetMaxColumnsInGroupBy() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getMaxColumnsInIndex()}.
 */
@Test
public final void testGetMaxColumnsInIndex() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getMaxColumnsInSelect()}.
 */
@Test
public final void testGetMaxColumnsInSelect() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getMaxColumnsInTable()}.
 */
@Test
public final void testGetMaxColumnsInTable() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getSchemas()}.
 */
@Test
public final void testGetSchemas() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getCatalogs()}.
 */
@Test
public final void testGetCatalogs() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getColumns(java.lang.String,
java.lang.String, java.lang.String, java.lang.String)}.
 * @throws SQLException
 */
@Test
public final void testGetColumns() throws SQLException {
java.sql.ResultSet column_set = metadata.getColumns("APP", null,
"SERVER_SHUTDOWN_TYPE", null);
column_set.relative(2);
String column_name = column_set.getString("COLUMN_NAME");
assertNotNull(column_name);
assertTrue(column_name.length()>0);
//For scrollable result set:
//column_set.absolute(1);
//assertTrue(column_set.first());
//column_set.beforeFirst();
//assertTrue(column_set.next());
//assertTrue(column_set.isFirst());
//For updatable result set:
//column_set.moveToCurrentRow();
//assertTrue(column_set.next());

}
private static int getRowCount(java.sql.ResultSet columnSet) throws
SQLException
{
int rowCount;
   int currentRow = columnSet.getRow();            // Get current row
   rowCount = columnSet.last() ? columnSet.getRow() : 0; // Determine number
of rows
   if (currentRow == 0)                      // If there was no current row

      columnSet.beforeFirst();                     // We want next() to go
to first row
   else                                      // If there WAS a current row
      columnSet.absolute(currentRow);              // Restore it
   return rowCount;
}
/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getBestRowIdentifier(java.lang.String,
java.lang.String, java.lang.String, int, boolean)}.
 */
@Test
public final void testGetBestRowIdentifier() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getVersionColumns(java.lang.String,
java.lang.String, java.lang.String)}.
 */
@Test
public final void testGetVersionColumns() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getPrimaryKeys(java.lang.String,
java.lang.String, java.lang.String)}.
 * @throws SQLException
 */
@Test
public final void testGetPrimaryKeys() throws SQLException {
java.sql.ResultSet pk_set = metadata.getPrimaryKeys("APP", null,
"SERVER_SHUTDOWN_TYPE");
assertTrue(getRowCount(pk_set) > 0);
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getImportedKeys(java.lang.String,
java.lang.String, java.lang.String)}.
 */
@Test
public final void testGetImportedKeys() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getExportedKeys(java.lang.String,
java.lang.String, java.lang.String)}.
 */
@Test
public final void testGetExportedKeys() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getTypeInfo()}.
 */
@Test
public final void testGetTypeInfo() {
fail("Not yet implemented"); // TODO
}

}


And .next() throws an exception about undefined cursor state:

import static org.junit.Assert.*;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.derby.client.am.Cursor;
import org.apache.derby.client.am.ResultSet;
import org.apache.derby.iapi.sql.Statement;
import org.apache.derby.iapi.sql.execute.CursorResultSet;
import org.apache.derby.impl.jdbc.EmbedResultSet40;
import org.apache.derby.impl.jdbc.EmbedStatement40;
import org.apache.derby.jdbc.EmbeddedDriver;
import org.apache.derbyTesting.functionTests.tests.lang.CursorTest;
import org.apache.derbyTesting.junit.Derby;
import org.hamcrest.core.Is;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class DatabaseMetaDataTest {
private static EmbeddedDriver driver;
private static java.sql.Connection derby_conn;
/**
 * @throws java.lang.Exception
 */
private static java.sql.DatabaseMetaData metadata;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
 driver = new org.apache.derby.jdbc.EmbeddedDriver();
 derby_conn = driver.connect("jdbc:derby:repository;create=true", null);
}

/**
 * @throws java.lang.Exception
 */
@AfterClass
public static void tearDownAfterClass() throws Exception {
derby_conn.close();
}

/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
derby_conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
metadata = derby_conn.getMetaData();
}

/**
 * @throws java.lang.Exception
 */
@After
public void tearDown() throws Exception {
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getSchemaTerm()}.
 * @throws SQLException
 */
@Test
public final void testGetSchemaTerm() throws SQLException {
String schemaTerm = metadata.getSchemaTerm();
assertNotNull("...", schemaTerm);
assertTrue(schemaTerm.length() > 0);
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#isCatalogAtStart()}.
 * @throws SQLException
 */
@Test
public final void testIsCatalogAtStart() throws SQLException {
boolean isAtStart = metadata.isCatalogAtStart();
assertNotNull(isAtStart);
assertTrue(isAtStart);
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getMaxColumnNameLength()}.
 * @throws SQLException
 */
@Test
public final void testGetMaxColumnNameLength() throws SQLException {
int length = metadata.getMaxColumnNameLength();
assertNotNull(length);
assertTrue("", length > 0);
assertTrue("", length > 10);
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getMaxColumnsInGroupBy()}.
 */
@Test
public final void testGetMaxColumnsInGroupBy() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getMaxColumnsInIndex()}.
 */
@Test
public final void testGetMaxColumnsInIndex() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getMaxColumnsInSelect()}.
 */
@Test
public final void testGetMaxColumnsInSelect() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getMaxColumnsInTable()}.
 */
@Test
public final void testGetMaxColumnsInTable() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getSchemas()}.
 */
@Test
public final void testGetSchemas() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getCatalogs()}.
 */
@Test
public final void testGetCatalogs() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getColumns(java.lang.String,
java.lang.String, java.lang.String, java.lang.String)}.
 * @throws SQLException
 */
@Test
public final void testGetColumns() throws SQLException {
java.sql.ResultSet column_set = metadata.getColumns("APP", null,
"SERVER_SHUTDOWN_TYPE", null);
column_set.relative(2);
String column_name = column_set.getString("COLUMN_NAME");
assertNotNull(column_name);
assertTrue(column_name.length()>0);
//For scrollable result set:
//column_set.absolute(1);
//assertTrue(column_set.first());
//column_set.beforeFirst();
//assertTrue(column_set.next());
//assertTrue(column_set.isFirst());
//For updatable result set:
//column_set.moveToCurrentRow();
//assertTrue(column_set.next());

}
private static int getRowCount(java.sql.ResultSet columnSet) throws
SQLException
{
int rowCount;
   int currentRow = columnSet.getRow();            // Get current row
   rowCount = columnSet.last() ? columnSet.getRow() : 0; // Determine number
of rows
   if (currentRow == 0)                      // If there was no current row

      columnSet.beforeFirst();                     // We want next() to go
to first row
   else                                      // If there WAS a current row
      columnSet.absolute(currentRow);              // Restore it
   return rowCount;
}
/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getBestRowIdentifier(java.lang.String,
java.lang.String, java.lang.String, int, boolean)}.
 */
@Test
public final void testGetBestRowIdentifier() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getVersionColumns(java.lang.String,
java.lang.String, java.lang.String)}.
 */
@Test
public final void testGetVersionColumns() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getPrimaryKeys(java.lang.String,
java.lang.String, java.lang.String)}.
 * @throws SQLException
 */
@Test
public final void testGetPrimaryKeys() throws SQLException {
java.sql.ResultSet pk_set = metadata.getPrimaryKeys("APP", null,
"SERVER_SHUTDOWN_TYPE");
assertTrue(getRowCount(pk_set) > 0);
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getImportedKeys(java.lang.String,
java.lang.String, java.lang.String)}.
 */
@Test
public final void testGetImportedKeys() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getExportedKeys(java.lang.String,
java.lang.String, java.lang.String)}.
 */
@Test
public final void testGetExportedKeys() {
fail("Not yet implemented"); // TODO
}

/**
 * Test method for {@link
org.apache.derby.client.am.DatabaseMetaData#getTypeInfo()}.
 */
@Test
public final void testGetTypeInfo() {
fail("Not yet implemented"); // TODO
}

}

In the mean time this code from Derby tests works fine:

DatabaseMetaDataTest.java:
    public void testGetColumnsReadOnly() throws Exception
    {
        ResultSet[] rs = getColumns(null, null, null, null);
        for ( int j =0 ; j<2 ; j++) {
            checkColumnsShape(rs[j], j);
            crossCheckGetColumnsAndResultSetMetaData(rs[j], false, j);
        }
    }

Please help anyone. John

Mime
View raw message