Connection

The Connection class, part of the org.postgresql package, is the Red Hat Database JDBC driver's implementation of java.sql.Connection. The org.postgresql.Connection defines three methods in addition to the methods required by the java.sql.Connection interface.

Connection Definition

package org.postgresql;
public class Connection implements java.sql.Connection {
   // Methods (new; java.sql.Connection methods not listed)
   public void addDataType(String type, String name);
   public Fastpath getFastpathAPI() throws SQLException;
   public LargeObjectManager getLargeObjectAPI() 
   throws SQLException;
}

Connection Methods

public void addDataType(String type, String name)

This method allows client code to add handlers for data types unique to Red Hat Database, including user-defined data types. Normally, a data type not known by the driver is returned by ResultSet.getObject() as a PGobject instance. The addDataType enables you to write a class that extends PGobject and to tell the driver the type name and class name to be used instead. Handler registration is per connection instance, so this method needs to be called each time a database connection is made.

Parameters.

type

The name of the data type.

name

The fully qualified name of the handler class that should be used for the type.

Example.
import org.postgresql.Connection;
...
((org.postgresql.Connection) con).addDataType("mytype", 
   "my.class.name");

public Fastpath getFastpathAPI() throws SQLException

This returns the Fastpath API of the connection.

Example.
import org.postgresql.Connection;
import org.postgresql.fastpath.*;
...
Fastpath fp = ((org.postgresql.Connection) con).getFastpathAPI();

public LargeObjectManager getLargeObjectAPI() throws SQLException

This returns the LargeObjectManager that the connection is using.

Example.
import org.postgresql.Connection;
import org.postgresql.largeobject.*;
...
LargeObjectManager lom = ((org.postgresql.Connection) con).getLargeObjectAPI();

Fastpath

Fastpath is an API that allows a client machine to execute a function on the database backend. Most client code will not need to use this method, but it is provided because the Large Object API uses it.

To use the Fastpath as an API, you need to import the org.postgresql.fastpath package in the preamble of the source file in which Fastpath will be used. A Fastpath object that is associated with a given database connection can be obtained by requesting one from the Connection object (cast to org.postgresql.Connection). For example:

import org.postgresql.Connection;
import org.postgresql.fastpath.*;
...
Fastpath fp = ((org.postgresql.Connection) con).getFastpathAPI();

Methods: addFunction

public void addFunction(String name, int fnid)

This method adds a function to the function name-to-OID look-up table used internally by the Fastpath API. User code should use the addFunctions method, which is based upon a query, rather than hard coding the OID as the OID for a function is not guaranteed to remain static even on different servers of the same version.

Parameters.

name

The name of the function.

fnid

The OID of the function.

Methods

public void addFunctions(ResultSet rs) throws SQLException

This method takes a ResultSet containing two columns as its argument. The first column contains a function name; the second contains that function's OID. The addFunctions method reads the entire ResultSet, loading the values into its internal function name-to-OID mapping table.

Parameters.

rs

The ResultSet.

public Object fastpath(int fnid, boolean resulttype, FastpathARG args[]) throws SQLException

This method sends a function call to the Red Hat Database backend that the Fastpath object is associated with. This version of Fastpath calls a function by OID.

Parameters.

fnid

The OID of the function to call.

resulttype

True if the result is an integer, false for other results.

args

Fastpath arguments.

Returns. If no data, null; if the result of the function call is an integer, an Integer; otherwise: byte[]

public Object fastpath(String name, boolean resulttype, FastpathARG args[]) throws SQLException

The fastpath method sends a function call to the Red Hat Database backend that the Fastpath object is associated with. This version of Fastpath calls a function by name.

Parameters.

name

The name of the function to call.

resulttype

True if the result is an integer, false for other results.

args

Fastpath arguments.

Returns. If no data, null; if the result of the function call is an integer, an Integer; otherwise: byte[]

public byte[] getData(String name, FastpathArg args[]) throws SQLException

getData is a wrapper method around Fastpath that assumes that the function to be called returns binary data.

Parameters.

name

Name of the function to call.

args

Fastpath arguments.

Returns. A byte array that contains the result.

public int getID(String name) throws SQLException

getID returns the OID of the function associated with the given function name. If the function name is not in the lookup table (that is: if either addFunction() or addFunctions() has not been called), then an SQLException is thrown.

Parameters.

name

Name of the function to get OID of.

public int getInteger(String name, FastpathArg args[]) throws SQLException

getInteger is a wrapper method for fastpath that assumes that the function to be called returns an integer value.

Parameters.

name

Name of the function to call.

args

Fastpath arguments.

Returns. A byte array that contains the result.

FastpathArg

Each fastpath call requires an array of arguments, the number and type being dependent on the function being called. This class implements the methods needed to provide this capability.

FastpathArg Definition

package org.postgresql.fastpath;
public class FastpathArg {
   public FastpathArg(int value);
   public FastpathArg(byte[] bytes);
   public FastpathArg(byte[] buf, int off, int len);
   public FastpathArg(String s);
}

Constructors

public FastpathArg(int value)

Constructs an argument that consists of a single integer.

Parameters.

value

The value that will be used as argument.

public FastpathArg(byte[] bytes)

Constructs an argument that consists of an array of bytes.

Parameters.

bytes

Array of bytes to use as argument.

public FastpathArg(byte[] buf, int off, int len)

Constructs an argument that consists of part of an array of bytes.

Parameters.

buf

source array

off

offset within array

len

length of data to include

public FastpathArg(String s)

Constructs an argument that consists of a String.

Parameters.

s

String that will be used as argument.