schema awareness patch provided by Kris Jurka

This commit is contained in:
Dave Cramer 2002-10-01 00:39:02 +00:00
parent 2a1e4a9006
commit 7c553901d7
9 changed files with 1358 additions and 901 deletions

View File

@ -14,7 +14,7 @@ import org.postgresql.largeobject.LargeObjectManager;
import org.postgresql.util.*;
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Connection.java,v 1.9 2002/09/11 05:38:44 barry Exp $
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Connection.java,v 1.10 2002/10/01 00:39:01 davec Exp $
* This class defines methods of the jdbc1 specification. This class is
* extended by org.postgresql.jdbc2.AbstractJdbc2Connection which adds the jdbc2
* methods. The real Connection class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Connection
@ -1147,6 +1147,11 @@ public abstract class AbstractJdbc1Connection implements org.postgresql.PGConnec
return dbVersionNumber;
}
/**
* Is the server we are connected to running at least this version?
* This comparison method will fail whenever a major or minor version
* goes to two digits (10.3.0) or (7.10.1).
*/
public boolean haveMinimumServerVersion(String ver) throws SQLException
{
return (getDBVersionNumber().compareTo(ver) >= 0);
@ -1184,16 +1189,29 @@ public abstract class AbstractJdbc1Connection implements org.postgresql.PGConnec
// it's not in the cache, so perform a query, and add the result to the cache
if (sqlType == null)
{
ResultSet result = ExecSQL("select typname from pg_type where oid = " + oid);
if (((AbstractJdbc1ResultSet)result).getColumnCount() != 1 || ((AbstractJdbc1ResultSet)result).getTupleCount() != 1)
throw new PSQLException("postgresql.unexpected");
result.next();
String pgType = result.getString(1);
String pgType;
// The opaque type does not exist in the system catalogs.
if (oid == 0) {
pgType = "opaque";
} else {
String sql;
if (haveMinimumServerVersion("7.3")) {
sql = "SELECT typname FROM pg_catalog.pg_type WHERE oid = " +oid;
} else {
sql = "SELECT typname FROM pg_type WHERE oid = " +oid;
}
ResultSet result = ExecSQL(sql);
if (((AbstractJdbc1ResultSet)result).getColumnCount() != 1 || ((AbstractJdbc1ResultSet)result).getTupleCount() != 1) {
throw new PSQLException("postgresql.unexpected");
}
result.next();
pgType = result.getString(1);
result.close();
}
Integer iOid = new Integer(oid);
sqlType = new Integer(getSQLType(result.getString(1)));
sqlType = new Integer(getSQLType(pgType));
sqlTypeCache.put(iOid, sqlType);
pgTypeCache.put(iOid, pgType);
result.close();
}
return sqlType.intValue();
@ -1217,8 +1235,13 @@ public abstract class AbstractJdbc1Connection implements org.postgresql.PGConnec
else
{
// it's not in the cache, so perform a query, and add the result to the cache
ResultSet result = ExecSQL("select oid from pg_type where typname='"
+ typeName + "'");
String sql;
if (haveMinimumServerVersion("7.3")) {
sql = "SELECT oid FROM pg_catalog.pg_type WHERE typname='" + typeName + "'";
} else {
sql = "SELECT oid FROM pg_type WHERE typname='" + typeName + "'";
}
ResultSet result = ExecSQL(sql);
if (((AbstractJdbc1ResultSet)result).getColumnCount() != 1 || ((AbstractJdbc1ResultSet)result).getTupleCount() != 1)
throw new PSQLException("postgresql.unexpected");
result.next();

View File

@ -8,7 +8,7 @@ import java.util.Vector;
import org.postgresql.largeobject.*;
import org.postgresql.util.*;
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.10 2002/09/14 03:52:56 barry Exp $
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.11 2002/10/01 00:39:02 davec Exp $
* This class defines methods of the jdbc1 specification. This class is
* extended by org.postgresql.jdbc2.AbstractJdbc2Statement which adds the jdbc2
* methods. The real Statement class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Statement
@ -1757,8 +1757,8 @@ public abstract class AbstractJdbc1Statement implements org.postgresql.PGStateme
*/
private void setSerialize(int parameterIndex, long x, String classname) throws SQLException
{
// converts . to _, toLowerCase, and ensures length<32
String tablename = Serialize.toPostgreSQL( classname );
// converts . to _, toLowerCase, and ensures length < max name length
String tablename = Serialize.toPostgreSQL((java.sql.Connection)connection, classname );
DriverManager.println("setSerialize: setting " + x + "::" + tablename );
// OID reference to tablerow-type must be cast like: <oid>::<tablename>

View File

@ -103,15 +103,24 @@ public class LargeObjectManager
//
// This is an example of Fastpath.addFunctions();
//
ResultSet res = conn.createStatement().executeQuery("select proname, oid from pg_proc" +
" where proname = 'lo_open'" +
" or proname = 'lo_close'" +
" or proname = 'lo_creat'" +
" or proname = 'lo_unlink'" +
" or proname = 'lo_lseek'" +
" or proname = 'lo_tell'" +
" or proname = 'loread'" +
" or proname = 'lowrite'");
String sql;
if (conn.getMetaData().supportsSchemasInTableDefinitions()) {
sql = "SELECT p.proname,p.oid "+
" FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n "+
" WHERE p.pronamespace=n.oid AND n.nspname='pg_catalog' AND ";
} else {
sql = "SELECT proname,oid FROM pg_proc WHERE ";
}
sql += " proname = 'lo_open'" +
" or proname = 'lo_close'" +
" or proname = 'lo_creat'" +
" or proname = 'lo_unlink'" +
" or proname = 'lo_lseek'" +
" or proname = 'lo_tell'" +
" or proname = 'loread'" +
" or proname = 'lowrite'";
ResultSet res = conn.createStatement().executeQuery(sql);
if (res == null)
throw new PSQLException("postgresql.lo.init");

View File

@ -110,7 +110,7 @@ public class TestUtil
try
{
String sql = "DROP TABLE " + table;
if (con instanceof org.postgresql.jdbc1.AbstractJdbc1Connection && ((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3")) {
if (haveMinimumServerVersion(con,"7.3")) {
sql += " CASCADE ";
}
stmt.executeUpdate(sql);
@ -190,4 +190,41 @@ public class TestUtil
String s = "0000000000".substring(0, l) + Integer.toString(v);
return s.substring(s.length() - l);
}
/**
* Determine if the given connection is connected to a server with
* a version of at least the given version.
* This is convenient because we are working with a java.sql.Connection,
* not an Postgres connection.
*/
public static boolean haveMinimumServerVersion(Connection con, String version) throws SQLException {
if (con instanceof org.postgresql.jdbc1.AbstractJdbc1Connection) {
return ((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion(version);
}
return false;
}
/**
* Print a ResultSet to System.out.
* This is useful for debugging tests.
*/
public static void printResultSet(ResultSet rs) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
for (int i=1; i<=rsmd.getColumnCount(); i++) {
if (i != 1) {
System.out.print(", ");
}
System.out.print(rsmd.getColumnName(i));
}
System.out.println();
while (rs.next()) {
for (int i=1; i<=rsmd.getColumnCount(); i++) {
if (i != 1) {
System.out.print(", ");
}
System.out.print(rs.getString(i));
}
System.out.println();
}
}
}

View File

@ -9,7 +9,7 @@ import java.sql.*;
*
* PS: Do you know how difficult it is to type on a train? ;-)
*
* $Id: DatabaseMetaDataTest.java,v 1.14 2002/09/11 05:38:45 barry Exp $
* $Id: DatabaseMetaDataTest.java,v 1.15 2002/10/01 00:39:02 davec Exp $
*/
public class DatabaseMetaDataTest extends TestCase
@ -35,10 +35,8 @@ public class DatabaseMetaDataTest extends TestCase
TestUtil.closeDB( con );
}
/*
* The spec says this may return null, but we always do!
*/
public void testGetMetaData()
public void testTables()
{
try
{
@ -50,7 +48,8 @@ public class DatabaseMetaDataTest extends TestCase
assertTrue( rs.next() );
String tableName = rs.getString("TABLE_NAME");
assertTrue( tableName.equals("testmetadata") );
String tableType = rs.getString("TABLE_TYPE");
assertTrue( tableType.equals("TABLE") );
rs.close();
rs = dbmd.getColumns("", "", "test%", "%" );
@ -76,162 +75,6 @@ public class DatabaseMetaDataTest extends TestCase
}
}
/*
* Test default capabilities
*/
public void testCapabilities()
{
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.allProceduresAreCallable());
assertTrue(dbmd.allTablesAreSelectable()); // not true all the time
// This should always be false for postgresql (at least for 7.x)
assertTrue(!dbmd.isReadOnly());
// does the backend support this yet? The protocol does...
assertTrue(!dbmd.supportsMultipleResultSets());
// yes, as multiple backends can have transactions open
assertTrue(dbmd.supportsMultipleTransactions());
assertTrue(dbmd.supportsMinimumSQLGrammar());
assertTrue(!dbmd.supportsCoreSQLGrammar());
assertTrue(!dbmd.supportsExtendedSQLGrammar());
if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3"))
assertTrue(dbmd.supportsANSI92EntryLevelSQL());
else
assertTrue(!dbmd.supportsANSI92EntryLevelSQL());
assertTrue(!dbmd.supportsANSI92IntermediateSQL());
assertTrue(!dbmd.supportsANSI92FullSQL());
assertTrue(!dbmd.supportsIntegrityEnhancementFacility());
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testJoins()
{
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.supportsOuterJoins());
assertTrue(dbmd.supportsFullOuterJoins());
assertTrue(dbmd.supportsLimitedOuterJoins());
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testCursors()
{
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(!dbmd.supportsPositionedDelete());
assertTrue(!dbmd.supportsPositionedUpdate());
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testNulls()
{
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
// We need to type cast the connection to get access to the
// PostgreSQL-specific method haveMinimumServerVersion().
// This is not available through the java.sql.Connection interface.
assertTrue( con instanceof org.postgresql.PGConnection );
assertTrue(!dbmd.nullsAreSortedAtStart());
assertTrue( dbmd.nullsAreSortedAtEnd() !=
((org.postgresql.jdbc2.AbstractJdbc2Connection)con).haveMinimumServerVersion("7.2"));
assertTrue( dbmd.nullsAreSortedHigh() ==
((org.postgresql.jdbc2.AbstractJdbc2Connection)con).haveMinimumServerVersion("7.2"));
assertTrue(!dbmd.nullsAreSortedLow());
assertTrue(dbmd.nullPlusNonNullIsNull());
assertTrue(dbmd.supportsNonNullableColumns());
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testLocalFiles()
{
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(!dbmd.usesLocalFilePerTable());
assertTrue(!dbmd.usesLocalFiles());
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testIdentifiers()
{
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(!dbmd.supportsMixedCaseIdentifiers()); // always false
assertTrue(dbmd.supportsMixedCaseQuotedIdentifiers()); // always true
assertTrue(!dbmd.storesUpperCaseIdentifiers()); // always false
assertTrue(dbmd.storesLowerCaseIdentifiers()); // always true
assertTrue(!dbmd.storesUpperCaseQuotedIdentifiers()); // always false
assertTrue(!dbmd.storesLowerCaseQuotedIdentifiers()); // always false
assertTrue(!dbmd.storesMixedCaseQuotedIdentifiers()); // always false
assertTrue(dbmd.getIdentifierQuoteString().equals("\""));
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testCrossReference()
{
try
@ -264,7 +107,7 @@ public class DatabaseMetaDataTest extends TestCase
assertTrue( fkColumnName.equals( "m" ) || fkColumnName.equals( "n" ) ) ;
String fkName = rs.getString( "FK_NAME" );
if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con1).haveMinimumServerVersion("7.3")) {
if (TestUtil.haveMinimumServerVersion(con1,"7.3")) {
assertTrue(fkName.startsWith("$1"));
} else {
assertTrue( fkName.startsWith( "<unnamed>") );
@ -354,120 +197,217 @@ public class DatabaseMetaDataTest extends TestCase
fail(ex.getMessage());
}
}
public void testTables()
public void testColumns()
{
// At the moment just test that no exceptions are thrown KJ
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
// we can add columns
assertTrue(dbmd.supportsAlterTableWithAddColumn());
// we can't drop columns (yet)
assertTrue(!dbmd.supportsAlterTableWithDropColumn());
}
catch (SQLException ex)
{
fail(ex.getMessage());
ResultSet rs = dbmd.getColumns(null,null,"pg_class",null);
rs.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
fail(sqle.getMessage());
}
}
public void testSelect()
public void testColumnPrivileges()
{
// At the moment just test that no exceptions are thrown KJ
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
// yes we can?: SELECT col a FROM a;
assertTrue(dbmd.supportsColumnAliasing());
// yes we can have expressions in ORDERBY
assertTrue(dbmd.supportsExpressionsInOrderBy());
// Yes, an ORDER BY clause can contain columns that are not in the
// SELECT clause.
assertTrue(dbmd.supportsOrderByUnrelated());
assertTrue(dbmd.supportsGroupBy());
assertTrue(dbmd.supportsGroupByUnrelated());
assertTrue(dbmd.supportsGroupByBeyondSelect()); // needs checking
}
catch (SQLException ex)
{
fail(ex.getMessage());
ResultSet rs = dbmd.getColumnPrivileges(null,null,"pg_statistic",null);
rs.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
fail(sqle.getMessage());
}
}
public void testDBParams()
public void testTablePrivileges()
{
// At the moment just test that no exceptions are thrown KJ
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.getURL().equals(TestUtil.getURL()));
assertTrue(dbmd.getUserName().equals(TestUtil.getUser()));
}
catch (SQLException ex)
{
fail(ex.getMessage());
ResultSet rs = dbmd.getTablePrivileges(null,null,"grantme");
rs.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
fail(sqle.getMessage());
}
}
public void testDbProductDetails()
public void testPrimaryKeys()
{
// At the moment just test that no exceptions are thrown KJ
try
{
assertTrue(con instanceof org.postgresql.PGConnection);
org.postgresql.jdbc2.AbstractJdbc2Connection pc = (org.postgresql.jdbc2.AbstractJdbc2Connection) con;
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.getDatabaseProductName().equals("PostgreSQL"));
//The test below doesn't make sense to me, it tests that
//the version of the driver = the version of the database it is connected to
//since the driver should be backwardly compatible this test is commented out
//assertTrue(dbmd.getDatabaseProductVersion().startsWith(
// Integer.toString(pc.getDriver().getMajorVersion())
// + "."
// + Integer.toString(pc.getDriver().getMinorVersion())));
assertTrue(dbmd.getDriverName().equals("PostgreSQL Native Driver"));
}
catch (SQLException ex)
{
fail(ex.getMessage());
ResultSet rs = dbmd.getPrimaryKeys(null,null,"pg_class");
rs.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
fail(sqle.getMessage());
}
}
public void testDriverVersioning()
public void testIndexInfo()
{
// At the moment just test that no exceptions are thrown KJ
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getIndexInfo(null,null,"pg_class",false,false);
rs.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
fail(sqle.getMessage());
}
}
public void testTableTypes()
{
// At the moment just test that no exceptions are thrown KJ
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getTableTypes();
rs.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
fail(sqle.getMessage());
}
}
public void testProcedureColumns()
{
// At the moment just test that no exceptions are thrown KJ
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getProcedureColumns(null,null,null,null);
rs.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
fail(sqle.getMessage());
}
}
public void testVersionColumns()
{
// At the moment just test that no exceptions are thrown KJ
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getVersionColumns(null,null,"pg_class");
rs.close();
} catch (SQLException sqle) {
fail(sqle.getMessage());
}
}
public void testBestRowIdentifier()
{
// At the moment just test that no exceptions are thrown KJ
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getBestRowIdentifier(null,null,"pg_type",dbmd.bestRowSession,false);
rs.close();
} catch (SQLException sqle) {
fail(sqle.getMessage());
}
}
public void testProcedures()
{
// At the moment just test that no exceptions are thrown KJ
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getProcedures(null,null,null);
rs.close();
} catch (SQLException sqle) {
fail(sqle.getMessage());
}
}
public void testCatalogs()
{
try
{
assertTrue(con instanceof org.postgresql.PGConnection);
org.postgresql.jdbc2.AbstractJdbc2Connection pc = (org.postgresql.jdbc2.AbstractJdbc2Connection) con;
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getCatalogs();
boolean foundTemplate0 = false;
boolean foundTemplate1 = false;
while(rs.next()) {
String database = rs.getString("TABLE_CAT");
if ("template0".equals(database)) {
foundTemplate0 = true;
} else if ("template1".equals(database)) {
foundTemplate1 = true;
}
}
rs.close();
assertTrue(foundTemplate0);
assertTrue(foundTemplate1);
} catch(SQLException sqle) {
fail(sqle.getMessage());
}
}
public void testSchemas()
{
try
{
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.getDriverVersion().equals(pc.getDriver().getVersion()));
assertTrue(dbmd.getDriverMajorVersion() == pc.getDriver().getMajorVersion());
assertTrue(dbmd.getDriverMinorVersion() == pc.getDriver().getMinorVersion());
}
catch (SQLException ex)
{
fail(ex.getMessage());
ResultSet rs = dbmd.getSchemas();
boolean foundPublic = false;
boolean foundEmpty = false;
boolean foundPGCatalog = false;
int count;
for(count=0; rs.next(); count++) {
String schema = rs.getString("TABLE_SCHEM");
if ("public".equals(schema)) {
foundPublic = true;
} else if ("".equals(schema)) {
foundEmpty = true;
} else if ("pg_catalog".equals(schema)) {
foundPGCatalog = true;
}
}
rs.close();
if (TestUtil.haveMinimumServerVersion(con,"7.3")) {
assertTrue(count >= 2);
assertTrue(foundPublic);
assertTrue(foundPGCatalog);
assertTrue(!foundEmpty);
} else {
assertEquals(count,1);
assertTrue(foundEmpty);
assertTrue(!foundPublic);
assertTrue(!foundPGCatalog);
}
} catch (SQLException sqle) {
fail(sqle.getMessage());
}
}
}

View File

@ -25,8 +25,7 @@ public class Jdbc2TestSuite extends TestSuite
// complexity.
// ANTTest should be first as it ensures that test parameters are
// being sent to the suite. It also initialises the database (if required)
// with some simple global tables (will make each testcase use its own later).
// being sent to the suite.
//
suite.addTestSuite(ANTTest.class);
@ -34,6 +33,7 @@ public class Jdbc2TestSuite extends TestSuite
suite.addTestSuite(DriverTest.class);
suite.addTestSuite(ConnectionTest.class);
suite.addTestSuite(DatabaseMetaDataTest.class);
suite.addTestSuite(DatabaseMetaDataPropertiesTest.class);
suite.addTestSuite(EncodingTest.class);
// Connectivity/Protocols
@ -48,13 +48,12 @@ public class Jdbc2TestSuite extends TestSuite
// PreparedStatement
// ServerSide Prepared Statements
suite.addTestSuite(ServerPreparedStmtTest.class);
// ServerSide Prepared Statements
suite.addTestSuite(ServerPreparedStmtTest.class);
// BatchExecute
suite.addTestSuite(BatchExecuteTest.class);
// MetaData
// Other misc tests, based on previous problems users have had or specific
// features some applications require.
@ -63,6 +62,8 @@ public class Jdbc2TestSuite extends TestSuite
// Fastpath/LargeObject
suite.addTestSuite(BlobTest.class);
suite.addTestSuite(SerializeTest.class);
suite.addTestSuite(UpdateableResultTest.class );
suite.addTestSuite(CallableStmtTest.class );

View File

@ -45,7 +45,7 @@ public class ServerPreparedStmtTest extends TestCase
{
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = 2");
((PGStatement)pstmt).setUseServerPrepare(true);
if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3")) {
if (TestUtil.haveMinimumServerVersion(con,"7.3")) {
assertTrue(((PGStatement)pstmt).isUseServerPrepare());
} else {
assertTrue(!((PGStatement)pstmt).isUseServerPrepare());
@ -85,7 +85,7 @@ public class ServerPreparedStmtTest extends TestCase
{
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = ?");
((PGStatement)pstmt).setUseServerPrepare(true);
if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3")) {
if (TestUtil.haveMinimumServerVersion(con,"7.3")) {
assertTrue(((PGStatement)pstmt).isUseServerPrepare());
} else {
assertTrue(!((PGStatement)pstmt).isUseServerPrepare());
@ -126,7 +126,7 @@ public class ServerPreparedStmtTest extends TestCase
{
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = ? or id = ?");
((PGStatement)pstmt).setUseServerPrepare(true);
if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3")) {
if (TestUtil.haveMinimumServerVersion(con,"7.3")) {
assertTrue(((PGStatement)pstmt).isUseServerPrepare());
} else {
assertTrue(!((PGStatement)pstmt).isUseServerPrepare());

View File

@ -124,14 +124,14 @@ public class Serialize
* This creates an instance that can be used to serialize or deserialize
* a Java object from a PostgreSQL table.
*/
public Serialize(Connection c, String type) throws SQLException
public Serialize(Connection conn, String type) throws SQLException
{
try
{
conn = c;
this.conn = conn;
if (Driver.logDebug)
Driver.debug("Serialize: initializing instance for type: " + type);
tableName = toPostgreSQL(type);
tableName = toPostgreSQL(conn,type);
className = type;
ourClass = Class.forName(className);
}
@ -144,7 +144,14 @@ public class Serialize
// Second check, the type must be a table
boolean status = false;
ResultSet rs = ((org.postgresql.jdbc1.AbstractJdbc1Connection)conn).ExecSQL("select typname from pg_type,pg_class where typname=relname and typname='" + tableName + "'");
String sql;
if (conn.getMetaData().supportsSchemasInTableDefinitions()) {
sql = "SELECT 1 FROM pg_catalog.pg_type t, pg_catalog.pg_class c WHERE t.typrelid=c.oid AND c.relkind='r' AND t.typname='" + tableName + "' AND pg_table_is_visible(c.oid) ";
} else {
sql = "SELECT 1 FROM pg_type t, pg_class c WHERE t.typrelid=c.oid AND c.relkind='r' AND t.typname='"+tableName+"'";
}
ResultSet rs = conn.createStatement().executeQuery(sql);
if (rs != null)
{
if (rs.next())
@ -187,7 +194,7 @@ public class Serialize
* @return Object relating to oid
* @exception SQLException on error
*/
public Object fetch(int oid) throws SQLException
public Object fetch(long oid) throws SQLException
{
try
{
@ -228,7 +235,7 @@ public class Serialize
if (Driver.logDebug)
Driver.debug("Serialize.fetch: " + sb.toString());
ResultSet rs = ((org.postgresql.jdbc1.AbstractJdbc1Connection)conn).ExecSQL(sb.toString());
ResultSet rs = conn.createStatement().executeQuery(sb.toString());
if (rs != null)
{
@ -493,15 +500,22 @@ public class Serialize
* @param o Class to base table on
* @exception SQLException on error
*/
public static void create(Connection con, Class c) throws SQLException
public static void create(Connection conn, Class c) throws SQLException
{
if (c.isInterface())
throw new PSQLException("postgresql.serial.interface");
// See if the table exists
String tableName = toPostgreSQL(c.getName());
String tableName = toPostgreSQL(conn,c.getName());
ResultSet rs = ((org.postgresql.jdbc1.AbstractJdbc1Connection)con).ExecSQL("select relname from pg_class where relname = '" + tableName + "'");
String sql;
if (conn.getMetaData().supportsSchemasInTableDefinitions()) {
sql = "SELECT 1 FROM pg_catalog.pg_class WHERE relkind='r' AND relname='" + tableName + "' AND pg_table_is_visible(oid) ";
} else {
sql = "SELECT 1 FROM pg_class WHERE relkind='r' AND relname='"+tableName+"'";
}
ResultSet rs = conn.createStatement().executeQuery(sql);
if ( rs.next() )
{
if (Driver.logDebug)
@ -549,8 +563,8 @@ public class Serialize
sb.append(tp[j][1]);
else
{
create(con, type);
sb.append(toPostgreSQL(n));
create(conn, type);
sb.append(toPostgreSQL(conn,n));
}
}
}
@ -560,7 +574,7 @@ public class Serialize
// Now create the table
if (Driver.logDebug)
Driver.debug("Serialize.create: " + sb );
((org.postgresql.jdbc1.AbstractJdbc1Connection)con).ExecSQL(sb.toString());
conn.createStatement().executeUpdate(sb.toString());
}
// This is used to translate between Java primitives and PostgreSQL types.
@ -582,35 +596,53 @@ public class Serialize
{"byte", "int2"}
};
/*
/**
* This converts a Java Class name to a org.postgresql table, by replacing . with
* _<p>
*
* Because of this, a Class name may not have _ in the name.<p>
* Another limitation, is that the entire class name (including packages)
* cannot be longer than 64 characters (a limit forced by PostgreSQL).
* cannot be longer than the maximum table name length.
*
* @param con The database connection
* @param name Class name
* @return PostgreSQL table name
* @exception SQLException on error
* @since 7.3
*/
public static String toPostgreSQL(String name) throws SQLException
public static String toPostgreSQL(Connection con, String name) throws SQLException
{
DatabaseMetaData dbmd = con.getMetaData();
int maxNameLength = dbmd.getMaxTableNameLength();
return toPostgreSQL(maxNameLength,name);
}
/**
* Convert a Java Class Name to an org.postgresql table name, by replacing .
* with _ <p>
*
* @deprecated Replaced by toPostgresql(connection, name) in 7.3
*/
public static String toPostgreSQL(String name) throws SQLException {
return toPostgreSQL(31,name);
}
private static String toPostgreSQL(int maxNameLength, String name) throws SQLException {
name = name.toLowerCase();
if (name.indexOf("_") > -1)
throw new PSQLException("postgresql.serial.underscore");
// Postgres table names can only be 64 character long.
// Reserve 1 char, so allow only up to 63 chars.
// Postgres table names can only be so many characters long.
// If the full class name with package is too long
// then just use the class name. If the class name is
// too long throw an exception.
//
if ( name.length() > 63 )
if ( name.length() > maxNameLength )
{
name = name.substring(name.lastIndexOf(".") + 1);
if ( name.length() > 63 )
if ( name.length() > maxNameLength )
throw new PSQLException("postgresql.serial.namelength", name, new Integer(name.length()));
}
return name.replace('.', '_');