* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Npgsql / NpgsqlTypes / LargeObjectManager.cs
1 /*-------------------------------------------------------------------------
2  
3   LargeObjectManager.cs
4       This class is a port of the class LargeObjectManager.java implemented by 
5       PostgreSQL Global Development Group
6  
7  
8  Copyright (c) 2004, Emiliano Necciari
9  Original Code: Copyright (c) 2003, PostgreSQL Global Development Group
10  
11  Note: (Francisco Figueiredo Jr.)
12         Changed case of method names to conform to .Net names standard.
13         Also changed type names to their true names. i.e. int -> Int32
14     
15  This library is free software; you can redistribute it and/or
16  modify it under the terms of the GNU Lesser General Public
17  License as published by the Free Software Foundation; either
18  version 2.1 of the License, or (at your option) any later version.
19  
20  This library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  Lesser General Public License for more details.
24  
25  You should have received a copy of the GNU Lesser General Public
26  License along with this library; if not, write to the Free Software
27  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 -------------------------------------------------------------------------
29 */
30
31 using System;
32 using System.Data;
33 using Npgsql;
34
35 namespace NpgsqlTypes
36 {
37     /// <summary>
38     /// Summary description for LargeObjectManager.
39     /// </summary>
40     public class LargeObjectManager
41     {
42         // the fastpath api for this connection
43         private Fastpath fp;
44
45         /*
46          * This mode indicates we want to write to an object
47          */
48         public const Int32 WRITE = 0x00020000;
49
50         /*
51          * This mode indicates we want to read an object
52          */
53         public static  Int32 READ = 0x00040000;
54
55         /*
56          * This mode is the default. It indicates we want read and write access to
57          * a large object
58          */
59         public static Int32 READWRITE = READ | WRITE;
60
61         /*
62          * This prevents us being created by mere mortals
63          */
64         private LargeObjectManager()
65         {}
66
67         /*
68          * Constructs the LargeObject API.
69          *
70          * <p><b>Important Notice</b>
71          * <br>This method should only be called by org.postgresql.Connection
72          *
73          * <p>There should only be one LargeObjectManager per Connection. The
74          * org.postgresql.Connection class keeps track of the various extension API's
75          * and it's advised you use those to gain access, and not going direct.
76          */
77         public LargeObjectManager(NpgsqlConnection conn)
78         {
79             // We need Fastpath to do anything
80             // Now get the function oid's for the api
81             //
82             // This is an example of Fastpath.addFunctions();
83             //
84             String sql;
85             if (conn.ServerVersion > new ServerVersion(7,3,0) )
86             {
87
88                 sql = "SELECT p.proname,p.oid "+
89                       " FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n "+
90                       " WHERE p.pronamespace=n.oid AND n.nspname='pg_catalog' AND (";
91             }
92             else
93             {
94                 sql = "SELECT proname,oid FROM pg_proc WHERE ";
95             }
96             sql += " proname = 'lo_open'" +
97                    " or proname = 'lo_close'" +
98                    " or proname = 'lo_creat'" +
99                    " or proname = 'lo_unlink'" +
100                    " or proname = 'lo_lseek'" +
101                    " or proname = 'lo_tell'" +
102                    " or proname = 'loread'" +
103                    " or proname = 'lowrite'";
104
105             if (conn.ServerVersion > new ServerVersion(7,3,0) )
106             {
107                 sql += ")";
108             }
109
110             IDbCommand cmd = new NpgsqlCommand(sql);
111             cmd.Connection = conn;
112
113             this.fp = new Fastpath(conn,conn.Connector.Stream);
114
115             IDataReader res = cmd.ExecuteReader(CommandBehavior.CloseConnection);
116
117
118             if (res == null)
119                 throw new Exception("postgresql.lo.init");
120
121
122             fp.AddFunctions(res);
123         }
124
125         /*
126          * This opens an existing large object, based on its OID. This method
127          * assumes that READ and WRITE access is required (the default).
128          *
129          * @param oid of large object
130          * @return LargeObject instance providing access to the object
131          * @exception SQLException on error
132          */
133         public LargeObject Open(Int32 oid)
134         {
135             return new LargeObject(fp, oid, READWRITE);
136         }
137
138         /*
139          * This opens an existing large object, based on its OID
140          *
141          * @param oid of large object
142          * @param mode mode of open
143          * @return LargeObject instance providing access to the object
144          * @exception SQLException on error
145          */
146         public LargeObject Open(Int32 oid, Int32 mode)
147         {
148             return new LargeObject(fp, oid, mode);
149         }
150
151         /*
152          * This creates a large object, returning its OID.
153          *
154          * <p>It defaults to READWRITE for the new object's attributes.
155          *
156          * @return oid of new object
157          * @exception SQLException on error
158          */
159         public Int32 Create()
160         {
161             FastpathArg[] args = new FastpathArg[1];
162             args[0] = new FastpathArg(READWRITE);
163             return fp.GetInteger("lo_creat", args);
164         }
165
166         /*
167          * This creates a large object, returning its OID
168          *
169          * @param mode a bitmask describing different attributes of the new object
170          * @return oid of new object
171          * @exception SQLException on error
172          */
173         public Int32 Create(Int32 mode)
174         {
175             FastpathArg[] args = new FastpathArg[1];
176             args[0] = new FastpathArg(mode);
177             return fp.GetInteger("lo_creat", args);
178         }
179
180         /*
181          * This deletes a large object.
182          *
183          * @param oid describing object to delete
184          * @exception SQLException on error
185          */
186         public void Delete(Int32 oid)
187         {
188             FastpathArg[] args = new FastpathArg[1];
189             args[0] = new FastpathArg(oid);
190             fp.FastpathCall("lo_unlink", false, args);
191         }
192
193         /*
194          * This deletes a large object.
195          *
196          * <p>It is identical to the delete method, and is supplied as the C API uses
197          * unlink.
198          *
199          * @param oid describing object to delete
200          * @exception SQLException on error
201          */
202         public void Unlink(Int32 oid)
203         {
204             Delete(oid);
205         }
206
207     }
208
209 }