~ubuntu-branches/ubuntu/feisty/libpgjava/feisty

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/largeobject/LargeObjectManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-06 23:43:06 UTC
  • Revision ID: james.westby@ubuntu.com-20020206234306-hsg7suqr8q56qg40
Tags: upstream-7.2
ImportĀ upstreamĀ versionĀ 7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.postgresql.largeobject;
 
2
 
 
3
import java.io.*;
 
4
import java.lang.*;
 
5
import java.net.*;
 
6
import java.util.*;
 
7
import java.sql.*;
 
8
 
 
9
import org.postgresql.fastpath.*;
 
10
import org.postgresql.util.*;
 
11
 
 
12
/*
 
13
 * This class implements the large object interface to org.postgresql.
 
14
 *
 
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.
 
19
 *
 
20
 * <p>This class can only be created by org.postgresql.Connection
 
21
 *
 
22
 * <p>To get access to this class, use the following segment of code:
 
23
 * <br><pre>
 
24
 * import org.postgresql.largeobject.*;
 
25
 *
 
26
 * Connection  conn;
 
27
 * LargeObjectManager lobj;
 
28
 *
 
29
 * ... code that opens a connection ...
 
30
 *
 
31
 * lobj = ((org.postgresql.Connection)myconn).getLargeObjectAPI();
 
32
 * </pre>
 
33
 *
 
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.
 
38
 *
 
39
 * <p>However, sometimes lower level access to Large Objects are required,
 
40
 * that are not supported by the JDBC specification.
 
41
 *
 
42
 * <p>Refer to org.postgresql.largeobject.LargeObject on how to manipulate the
 
43
 * contents of a Large Object.
 
44
 *
 
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
 
58
 */
 
59
public class LargeObjectManager
 
60
{
 
61
        // the fastpath api for this connection
 
62
        private Fastpath fp;
 
63
 
 
64
        /*
 
65
         * This mode indicates we want to write to an object
 
66
         */
 
67
        public static final int WRITE = 0x00020000;
 
68
 
 
69
        /*
 
70
         * This mode indicates we want to read an object
 
71
         */
 
72
        public static final int READ = 0x00040000;
 
73
 
 
74
        /*
 
75
         * This mode is the default. It indicates we want read and write access to
 
76
         * a large object
 
77
         */
 
78
        public static final int READWRITE = READ | WRITE;
 
79
 
 
80
        /*
 
81
         * This prevents us being created by mere mortals
 
82
         */
 
83
        private LargeObjectManager()
 
84
        {}
 
85
 
 
86
        /*
 
87
         * Constructs the LargeObject API.
 
88
         *
 
89
         * <p><b>Important Notice</b>
 
90
         * <br>This method should only be called by org.postgresql.Connection
 
91
         *
 
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.
 
95
         */
 
96
        public LargeObjectManager(org.postgresql.Connection conn) throws SQLException
 
97
        {
 
98
                // We need Fastpath to do anything
 
99
                this.fp = conn.getFastpathAPI();
 
100
 
 
101
                // Now get the function oid's for the api
 
102
                //
 
103
                // This is an example of Fastpath.addFunctions();
 
104
                //
 
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'");
 
114
 
 
115
                if (res == null)
 
116
                        throw new PSQLException("postgresql.lo.init");
 
117
 
 
118
                fp.addFunctions(res);
 
119
                res.close();
 
120
                DriverManager.println("Large Object initialised");
 
121
        }
 
122
 
 
123
        /*
 
124
         * This opens an existing large object, based on its OID. This method
 
125
         * assumes that READ and WRITE access is required (the default).
 
126
         *
 
127
         * @param oid of large object
 
128
         * @return LargeObject instance providing access to the object
 
129
         * @exception SQLException on error
 
130
         */
 
131
        public LargeObject open(int oid) throws SQLException
 
132
        {
 
133
                return new LargeObject(fp, oid, READWRITE);
 
134
        }
 
135
 
 
136
        /*
 
137
         * This opens an existing large object, based on its OID
 
138
         *
 
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
 
143
         */
 
144
        public LargeObject open(int oid, int mode) throws SQLException
 
145
        {
 
146
                return new LargeObject(fp, oid, mode);
 
147
        }
 
148
 
 
149
        /*
 
150
         * This creates a large object, returning its OID.
 
151
         *
 
152
         * <p>It defaults to READWRITE for the new object's attributes.
 
153
         *
 
154
         * @return oid of new object
 
155
         * @exception SQLException on error
 
156
         */
 
157
        public int create() throws SQLException
 
158
        {
 
159
                FastpathArg args[] = new FastpathArg[1];
 
160
                args[0] = new FastpathArg(READWRITE);
 
161
                return fp.getInteger("lo_creat", args);
 
162
        }
 
163
 
 
164
        /*
 
165
         * This creates a large object, returning its OID
 
166
         *
 
167
         * @param mode a bitmask describing different attributes of the new object
 
168
         * @return oid of new object
 
169
         * @exception SQLException on error
 
170
         */
 
171
        public int create(int mode) throws SQLException
 
172
        {
 
173
                FastpathArg args[] = new FastpathArg[1];
 
174
                args[0] = new FastpathArg(mode);
 
175
                return fp.getInteger("lo_creat", args);
 
176
        }
 
177
 
 
178
        /*
 
179
         * This deletes a large object.
 
180
         *
 
181
         * @param oid describing object to delete
 
182
         * @exception SQLException on error
 
183
         */
 
184
        public void delete(int oid) throws SQLException
 
185
        {
 
186
                FastpathArg args[] = new FastpathArg[1];
 
187
                args[0] = new FastpathArg(oid);
 
188
                fp.fastpath("lo_unlink", false, args);
 
189
        }
 
190
 
 
191
        /*
 
192
         * This deletes a large object.
 
193
         *
 
194
         * <p>It is identical to the delete method, and is supplied as the C API uses
 
195
         * unlink.
 
196
         *
 
197
         * @param oid describing object to delete
 
198
         * @exception SQLException on error
 
199
         */
 
200
        public void unlink(int oid) throws SQLException
 
201
        {
 
202
                delete(oid);
 
203
        }
 
204
 
 
205
}