Skip to content

Commit

Permalink
This patch is the culmination of Jan-Andre le Roux's work on enhancing
Browse files Browse the repository at this point in the history
the ResultSetMetaData methods with new information available in the V3
protocol.  He wandered off of the verge of completing it and I've just
done some minor reworking and editing.

This implements getSchemaName, getTableName, getColumnName,
getColumnLabel, and isNullable.
  • Loading branch information
kjurka committed Jan 13, 2004
1 parent 231f337 commit 30ef6f3
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 129 deletions.
2 changes: 2 additions & 0 deletions org/postgresql/core/BaseStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

public interface BaseStatement extends org.postgresql.PGStatement
{
public BaseResultSet createDriverResultSet(Field[] fields, Vector tuples) throws SQLException;

public BaseResultSet createResultSet(Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor) throws SQLException;
public PGRefCursorResultSet createRefCursorResultSet(String cursorName) throws SQLException;

Expand Down
145 changes: 137 additions & 8 deletions org/postgresql/core/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.postgresql.core;

import java.sql.*;

import org.postgresql.core.BaseConnection;

/*
Expand All @@ -22,7 +23,14 @@ public class Field
private int length; // Internal Length of this field
private int oid; // OID of the type
private int mod; // type modifier of this field
private String name; // Name of this field
private String name; // Name of this field (the column label)
private int tableOid; // OID of table ( zero if no table )
private int positionInTable;
private boolean fromServer; // Did this field come from a query?

// cache-fields
private Integer nullable;
private String columnName;

private BaseConnection conn; // Connection Instantation

Expand All @@ -37,11 +45,7 @@ public class Field
*/
public Field(BaseConnection conn, String name, int oid, int length, int mod)
{
this.conn = conn;
this.name = name;
this.oid = oid;
this.length = length;
this.mod = mod;
this(conn, name, oid, length, mod, 0, 0);
}

/*
Expand All @@ -57,6 +61,28 @@ public Field(BaseConnection conn, String name, int oid, int length)
this(conn, name, oid, length, 0);
}

/*
* Construct a field based on the information fed to it.
*
* @param conn the connection this field came from
* @param name the name of the field
* @param oid the OID of the field
* @param length the length of the field
* @param tableOid the OID of the columns' table
* @param positionInTable the position of column in the table (first column is 1, second column is 2, etc...)
*/
public Field(BaseConnection conn, String name, int oid, int length, int mod, int tableOid, int positionInTable)
{
this.conn = conn;
this.name = name;
this.oid = oid;
this.length = length;
this.mod = mod;
this.tableOid = tableOid;
this.positionInTable = positionInTable;
this.fromServer = true;
}

/*
* @return the oid of this Field's data type
*/
Expand Down Expand Up @@ -101,14 +127,117 @@ public String getPGType() throws SQLException
}

/*
* We also need to get the java.sql.types type.
* We also need to get the java.sql.Types type.
*
* @return the int representation of the java.sql.types type of this field
* @return the int representation of the java.sql.Types type of this field
* @exception SQLException if a database access error occurs
*/
public int getSQLType() throws SQLException
{
return conn.getSQLType(oid);
}

/**
* Specify if this field was created from a server query
* or the driver manually creating a ResultSet.
*/
public void setFromServer(boolean fromServer)
{
this.fromServer = fromServer;
}

/*
* @return the columns' table oid, zero if no oid available
*/
public int getTableOid()
{
return tableOid;
}

/*
* @return instantiated connection
*/
public BaseConnection getConn()
{
return conn;
}

public int getPositionInTable()
{
return positionInTable;
}

public int getNullable() throws SQLException
{
if (nullable != null)
{
return nullable.intValue();
}
if (tableOid == 0)
{
nullable = new Integer(ResultSetMetaData.columnNullableUnknown);
return nullable.intValue();
}
Connection con = (Connection) conn;
ResultSet res = null;
PreparedStatement ps = null;
try
{
ps = con.prepareStatement("SELECT attnotnull FROM pg_catalog.pg_attribute WHERE attrelid = ? AND attnum = ?;");
ps.setInt(1, tableOid);
ps.setInt(2, positionInTable);
res = ps.executeQuery();
int nullResult = ResultSetMetaData.columnNullableUnknown;
if (res.next())
{
nullResult = res.getBoolean(1) ? ResultSetMetaData.columnNoNulls : ResultSetMetaData.columnNullable;
}
nullable = new Integer(nullResult);
return nullResult;
} finally
{
if (res != null)
res.close();
if (ps != null)
ps.close();
}
}

public String getColumnName() throws SQLException
{
if (conn.getPGProtocolVersionMajor() < 3 || !fromServer) {
return name;
}
if (columnName != null)
{
return columnName;
}
if (tableOid == 0)
{
return columnName = "";
}
Connection con = (Connection) conn;
ResultSet res = null;
PreparedStatement ps = null;
try
{
ps = con.prepareStatement("SELECT attname FROM pg_catalog.pg_attribute WHERE attrelid = ? AND attnum = ?");
ps.setInt(1, tableOid);
ps.setInt(2, positionInTable);
res = ps.executeQuery();
String columnName = "";
if (res.next())
{
columnName = res.getString(1);
}
return columnName;
} finally
{
if (res != null)
res.close();
if (ps != null)
ps.close();
}
}

}
12 changes: 5 additions & 7 deletions org/postgresql/core/QueryExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,6 @@ private void receiveCommandStatusV2() throws SQLException
private void receiveFieldsV3() throws SQLException
{
//TODO: use the msgSize
//TODO: use the tableOid, and tablePosition
if (fields != null)
throw new PSQLException("postgresql.con.multres", PSQLState.CONNECTION_FAILURE);
int l_msgSize = pgStream.ReceiveIntegerR(4);
Expand All @@ -477,15 +476,14 @@ private void receiveFieldsV3() throws SQLException

for (int i = 0; i < fields.length; i++)
{
String typeName = pgStream.ReceiveString(connection.getEncoding());
String columnLabel = pgStream.ReceiveString(connection.getEncoding());
int tableOid = pgStream.ReceiveIntegerR(4);
int tablePosition = pgStream.ReceiveIntegerR(2);
short positionInTable = (short)pgStream.ReceiveIntegerR(2);
int typeOid = pgStream.ReceiveIntegerR(4);
int typeLength = pgStream.ReceiveIntegerR(2);
int typeModifier = pgStream.ReceiveIntegerR(4);
int formatType = pgStream.ReceiveIntegerR(2);
//TODO: use the extra values coming back
fields[i] = new Field(connection, typeName, typeOid, typeLength, typeModifier);
fields[i] = new Field(connection, columnLabel, typeOid, typeLength, typeModifier, tableOid, positionInTable);
}
}
/*
Expand All @@ -501,11 +499,11 @@ private void receiveFieldsV2() throws SQLException

for (int i = 0; i < fields.length; i++)
{
String typeName = pgStream.ReceiveString(connection.getEncoding());
String columnLabel = pgStream.ReceiveString(connection.getEncoding());
int typeOid = pgStream.ReceiveIntegerR(4);
int typeLength = pgStream.ReceiveIntegerR(2);
int typeModifier = pgStream.ReceiveIntegerR(4);
fields[i] = new Field(connection, typeName, typeOid, typeLength, typeModifier);
fields[i] = new Field(connection, columnLabel, typeOid, typeLength, typeModifier);
}
}
}
18 changes: 9 additions & 9 deletions org/postgresql/jdbc1/AbstractJdbc1DatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -1926,7 +1926,7 @@ public java.sql.ResultSet getProcedureColumns(String catalog, String schemaPatte
}
rs.close();

return (ResultSet) ((BaseStatement)connection.createStatement()).createResultSet(f, v, "OK", 1, 0, false);
return (ResultSet) ((BaseStatement)connection.createStatement()).createDriverResultSet(f, v);
}

/*
Expand Down Expand Up @@ -2218,7 +2218,7 @@ public java.sql.ResultSet getTableTypes() throws SQLException
v.addElement(tuple);
}

return (ResultSet) ((BaseStatement)connection.createStatement()).createResultSet(f, v, "OK", 1, 0, false);
return (ResultSet) ((BaseStatement)connection.createStatement()).createDriverResultSet(f, v);
}

/*
Expand Down Expand Up @@ -2392,7 +2392,7 @@ else if (pgType.equals("bit") || pgType.equals("varbit")) {
}
rs.close();

return (ResultSet) ((BaseStatement)connection.createStatement()).createResultSet(f, v, "OK", 1, 0, false);
return (ResultSet) ((BaseStatement)connection.createStatement()).createDriverResultSet(f, v);
}

/*
Expand Down Expand Up @@ -2505,7 +2505,7 @@ public java.sql.ResultSet getColumnPrivileges(String catalog, String schema, Str
}
rs.close();

return (ResultSet) ((BaseStatement)connection.createStatement()).createResultSet(f, v, "OK", 1, 0, false);
return (ResultSet) ((BaseStatement)connection.createStatement()).createDriverResultSet(f, v);
}

/*
Expand Down Expand Up @@ -2607,7 +2607,7 @@ public java.sql.ResultSet getTablePrivileges(String catalog, String schemaPatter
}
rs.close();

return (ResultSet) ((BaseStatement)connection.createStatement()).createResultSet(f, v, "OK", 1, 0, false);
return (ResultSet) ((BaseStatement)connection.createStatement()).createDriverResultSet(f, v);
}

private static void sortStringArray(String s[]) {
Expand Down Expand Up @@ -2805,7 +2805,7 @@ public java.sql.ResultSet getBestRowIdentifier(String catalog, String schema, St
v.addElement(tuple);
}

return (ResultSet) ((BaseStatement)connection.createStatement()).createResultSet(f, v, "OK", 1, 0, false);
return (ResultSet) ((BaseStatement)connection.createStatement()).createDriverResultSet(f, v);
}

/*
Expand Down Expand Up @@ -2875,7 +2875,7 @@ public java.sql.ResultSet getVersionColumns(String catalog, String schema, Strin
/* Perhaps we should check that the given
* catalog.schema.table actually exists. -KJ
*/
return (ResultSet) ((BaseStatement)connection.createStatement()).createResultSet(f, v, "OK", 1, 0, false);
return (ResultSet) ((BaseStatement)connection.createStatement()).createDriverResultSet(f, v);
}

/*
Expand Down Expand Up @@ -3249,7 +3249,7 @@ else if ("restrict".equals(rule))
tuples.addElement(tuple);
}

return (ResultSet) ((BaseStatement)connection.createStatement()).createResultSet(f, tuples, "OK", 1, 0, false);
return (ResultSet) ((BaseStatement)connection.createStatement()).createDriverResultSet(f, tuples);
}

/*
Expand Down Expand Up @@ -3534,7 +3534,7 @@ public java.sql.ResultSet getTypeInfo() throws SQLException
}
rs.close();

return (ResultSet) ((BaseStatement)connection.createStatement()).createResultSet(f, v, "OK", 1, 0, false);
return (ResultSet) ((BaseStatement)connection.createStatement()).createDriverResultSet(f, v);
}

/*
Expand Down

0 comments on commit 30ef6f3

Please sign in to comment.