1
package org.postgresql.largeobject;
9
import org.postgresql.fastpath.*;
10
import org.postgresql.util.*;
13
* This class implements the large object interface to org.postgresql.
15
* <p>It provides methods that allow client code to create, open and delete
16
* large objects from the database. When opening an object, an instance of
17
* org.postgresql.largeobject.LargeObject is returned, and its methods then allow
18
* access to the object.
20
* <p>This class can only be created by org.postgresql.Connection
22
* <p>To get access to this class, use the following segment of code:
24
* import org.postgresql.largeobject.*;
27
* LargeObjectManager lobj;
29
* ... code that opens a connection ...
31
* lobj = ((org.postgresql.Connection)myconn).getLargeObjectAPI();
34
* <p>Normally, client code would use the getAsciiStream, getBinaryStream,
35
* or getUnicodeStream methods in ResultSet, or setAsciiStream,
36
* setBinaryStream, or setUnicodeStream methods in PreparedStatement to
37
* access Large Objects.
39
* <p>However, sometimes lower level access to Large Objects are required,
40
* that are not supported by the JDBC specification.
42
* <p>Refer to org.postgresql.largeobject.LargeObject on how to manipulate the
43
* contents of a Large Object.
45
* @see org.postgresql.largeobject.LargeObject
46
* @see org.postgresql.ResultSet#getAsciiStream
47
* @see org.postgresql.ResultSet#getBinaryStream
48
* @see org.postgresql.ResultSet#getUnicodeStream
49
* @see org.postgresql.PreparedStatement#setAsciiStream
50
* @see org.postgresql.PreparedStatement#setBinaryStream
51
* @see org.postgresql.PreparedStatement#setUnicodeStream
52
* @see java.sql.ResultSet#getAsciiStream
53
* @see java.sql.ResultSet#getBinaryStream
54
* @see java.sql.ResultSet#getUnicodeStream
55
* @see java.sql.PreparedStatement#setAsciiStream
56
* @see java.sql.PreparedStatement#setBinaryStream
57
* @see java.sql.PreparedStatement#setUnicodeStream
59
public class LargeObjectManager
61
// the fastpath api for this connection
65
* This mode indicates we want to write to an object
67
public static final int WRITE = 0x00020000;
70
* This mode indicates we want to read an object
72
public static final int READ = 0x00040000;
75
* This mode is the default. It indicates we want read and write access to
78
public static final int READWRITE = READ | WRITE;
81
* This prevents us being created by mere mortals
83
private LargeObjectManager()
87
* Constructs the LargeObject API.
89
* <p><b>Important Notice</b>
90
* <br>This method should only be called by org.postgresql.Connection
92
* <p>There should only be one LargeObjectManager per Connection. The
93
* org.postgresql.Connection class keeps track of the various extension API's
94
* and it's advised you use those to gain access, and not going direct.
96
public LargeObjectManager(org.postgresql.Connection conn) throws SQLException
98
// We need Fastpath to do anything
99
this.fp = conn.getFastpathAPI();
101
// Now get the function oid's for the api
103
// This is an example of Fastpath.addFunctions();
105
java.sql.ResultSet res = (java.sql.ResultSet)conn.createStatement().executeQuery("select proname, oid from pg_proc" +
106
" where proname = 'lo_open'" +
107
" or proname = 'lo_close'" +
108
" or proname = 'lo_creat'" +
109
" or proname = 'lo_unlink'" +
110
" or proname = 'lo_lseek'" +
111
" or proname = 'lo_tell'" +
112
" or proname = 'loread'" +
113
" or proname = 'lowrite'");
116
throw new PSQLException("postgresql.lo.init");
118
fp.addFunctions(res);
120
DriverManager.println("Large Object initialised");
124
* This opens an existing large object, based on its OID. This method
125
* assumes that READ and WRITE access is required (the default).
127
* @param oid of large object
128
* @return LargeObject instance providing access to the object
129
* @exception SQLException on error
131
public LargeObject open(int oid) throws SQLException
133
return new LargeObject(fp, oid, READWRITE);
137
* This opens an existing large object, based on its OID
139
* @param oid of large object
140
* @param mode mode of open
141
* @return LargeObject instance providing access to the object
142
* @exception SQLException on error
144
public LargeObject open(int oid, int mode) throws SQLException
146
return new LargeObject(fp, oid, mode);
150
* This creates a large object, returning its OID.
152
* <p>It defaults to READWRITE for the new object's attributes.
154
* @return oid of new object
155
* @exception SQLException on error
157
public int create() throws SQLException
159
FastpathArg args[] = new FastpathArg[1];
160
args[0] = new FastpathArg(READWRITE);
161
return fp.getInteger("lo_creat", args);
165
* This creates a large object, returning its OID
167
* @param mode a bitmask describing different attributes of the new object
168
* @return oid of new object
169
* @exception SQLException on error
171
public int create(int mode) throws SQLException
173
FastpathArg args[] = new FastpathArg[1];
174
args[0] = new FastpathArg(mode);
175
return fp.getInteger("lo_creat", args);
179
* This deletes a large object.
181
* @param oid describing object to delete
182
* @exception SQLException on error
184
public void delete(int oid) throws SQLException
186
FastpathArg args[] = new FastpathArg[1];
187
args[0] = new FastpathArg(oid);
188
fp.fastpath("lo_unlink", false, args);
192
* This deletes a large object.
194
* <p>It is identical to the delete method, and is supplied as the C API uses
197
* @param oid describing object to delete
198
* @exception SQLException on error
200
public void unlink(int oid) throws SQLException