1 package org.postgresql.largeobject;
3 import org.postgresql.Driver;
10 import org.postgresql.fastpath.*;
11 import org.postgresql.util.*;
14 * This class implements the large object interface to org.postgresql.
16 * <p>It provides methods that allow client code to create, open and delete
17 * large objects from the database. When opening an object, an instance of
18 * org.postgresql.largeobject.LargeObject is returned, and its methods then allow
19 * access to the object.
21 * <p>This class can only be created by org.postgresql.Connection
23 * <p>To get access to this class, use the following segment of code:
25 * import org.postgresql.largeobject.*;
28 * LargeObjectManager lobj;
30 * ... code that opens a connection ...
32 * lobj = ((org.postgresql.Connection)myconn).getLargeObjectAPI();
35 * <p>Normally, client code would use the getAsciiStream, getBinaryStream,
36 * or getUnicodeStream methods in ResultSet, or setAsciiStream,
37 * setBinaryStream, or setUnicodeStream methods in PreparedStatement to
38 * access Large Objects.
40 * <p>However, sometimes lower level access to Large Objects are required,
41 * that are not supported by the JDBC specification.
43 * <p>Refer to org.postgresql.largeobject.LargeObject on how to manipulate the
44 * contents of a Large Object.
46 * @see org.postgresql.largeobject.LargeObject
47 * @see org.postgresql.ResultSet#getAsciiStream
48 * @see org.postgresql.ResultSet#getBinaryStream
49 * @see org.postgresql.ResultSet#getUnicodeStream
50 * @see org.postgresql.PreparedStatement#setAsciiStream
51 * @see org.postgresql.PreparedStatement#setBinaryStream
52 * @see org.postgresql.PreparedStatement#setUnicodeStream
53 * @see java.sql.ResultSet#getAsciiStream
54 * @see java.sql.ResultSet#getBinaryStream
55 * @see java.sql.ResultSet#getUnicodeStream
56 * @see java.sql.PreparedStatement#setAsciiStream
57 * @see java.sql.PreparedStatement#setBinaryStream
58 * @see java.sql.PreparedStatement#setUnicodeStream
60 public class LargeObjectManager
62 // the fastpath api for this connection
66 * This mode indicates we want to write to an object
68 public static final int WRITE = 0x00020000;
71 * This mode indicates we want to read an object
73 public static final int READ = 0x00040000;
76 * This mode is the default. It indicates we want read and write access to
79 public static final int READWRITE = READ | WRITE;
82 * This prevents us being created by mere mortals
84 private LargeObjectManager()
88 * Constructs the LargeObject API.
90 * <p><b>Important Notice</b>
91 * <br>This method should only be called by org.postgresql.Connection
93 * <p>There should only be one LargeObjectManager per Connection. The
94 * org.postgresql.Connection class keeps track of the various extension API's
95 * and it's advised you use those to gain access, and not going direct.
97 public LargeObjectManager(Connection conn) throws SQLException
99 // We need Fastpath to do anything
100 this.fp = ((org.postgresql.PGConnection)conn).getFastpathAPI();
102 // Now get the function oid's for the api
104 // This is an example of Fastpath.addFunctions();
107 if (conn.getMetaData().supportsSchemasInTableDefinitions()) {
108 sql = "SELECT p.proname,p.oid "+
109 " FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n "+
110 " WHERE p.pronamespace=n.oid AND n.nspname='pg_catalog' AND ";
112 sql = "SELECT proname,oid FROM pg_proc WHERE ";
114 sql += " proname = 'lo_open'" +
115 " or proname = 'lo_close'" +
116 " or proname = 'lo_creat'" +
117 " or proname = 'lo_unlink'" +
118 " or proname = 'lo_lseek'" +
119 " or proname = 'lo_tell'" +
120 " or proname = 'loread'" +
121 " or proname = 'lowrite'";
123 ResultSet res = conn.createStatement().executeQuery(sql);
126 throw new PSQLException("postgresql.lo.init");
128 fp.addFunctions(res);
131 Driver.debug("Large Object initialised");
135 * This opens an existing large object, based on its OID. This method
136 * assumes that READ and WRITE access is required (the default).
138 * @param oid of large object
139 * @return LargeObject instance providing access to the object
140 * @exception SQLException on error
142 public LargeObject open(int oid) throws SQLException
144 return new LargeObject(fp, oid, READWRITE);
148 * This opens an existing large object, based on its OID
150 * @param oid of large object
151 * @param mode mode of open
152 * @return LargeObject instance providing access to the object
153 * @exception SQLException on error
155 public LargeObject open(int oid, int mode) throws SQLException
157 return new LargeObject(fp, oid, mode);
161 * This creates a large object, returning its OID.
163 * <p>It defaults to READWRITE for the new object's attributes.
165 * @return oid of new object
166 * @exception SQLException on error
168 public int create() throws SQLException
170 FastpathArg args[] = new FastpathArg[1];
171 args[0] = new FastpathArg(READWRITE);
172 return fp.getInteger("lo_creat", args);
176 * This creates a large object, returning its OID
178 * @param mode a bitmask describing different attributes of the new object
179 * @return oid of new object
180 * @exception SQLException on error
182 public int create(int mode) throws SQLException
184 FastpathArg args[] = new FastpathArg[1];
185 args[0] = new FastpathArg(mode);
186 return fp.getInteger("lo_creat", args);
190 * This deletes a large object.
192 * @param oid describing object to delete
193 * @exception SQLException on error
195 public void delete(int oid) throws SQLException
197 FastpathArg args[] = new FastpathArg[1];
198 args[0] = new FastpathArg(oid);
199 fp.fastpath("lo_unlink", false, args);
203 * This deletes a large object.
205 * <p>It is identical to the delete method, and is supplied as the C API uses
208 * @param oid describing object to delete
209 * @exception SQLException on error
211 public void unlink(int oid) throws SQLException