2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Npgsql / NpgsqlTypes / LargeObjectManager.cs
blob26d082fce7714fa1bded1312a42db0ab90ba6953
1 /*-------------------------------------------------------------------------
3 LargeObjectManager.cs
4 This class is a port of the class LargeObjectManager.java implemented by
5 PostgreSQL Global Development Group
8 Copyright (c) 2004, Emiliano Necciari
9 Original Code: Copyright (c) 2003, PostgreSQL Global Development Group
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
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.
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.
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 -------------------------------------------------------------------------
31 using System;
32 using System.Data;
33 using Npgsql;
35 namespace NpgsqlTypes
37 /// <summary>
38 /// Summary description for LargeObjectManager.
39 /// </summary>
40 public class LargeObjectManager
42 // the fastpath api for this connection
43 private Fastpath fp;
46 * This mode indicates we want to write to an object
48 public const Int32 WRITE = 0x00020000;
51 * This mode indicates we want to read an object
53 public static Int32 READ = 0x00040000;
56 * This mode is the default. It indicates we want read and write access to
57 * a large object
59 public static Int32 READWRITE = READ | WRITE;
62 * This prevents us being created by mere mortals
64 private LargeObjectManager()
68 * Constructs the LargeObject API.
70 * <p><b>Important Notice</b>
71 * <br>This method should only be called by org.postgresql.Connection
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.
77 public LargeObjectManager(NpgsqlConnection conn)
79 // We need Fastpath to do anything
80 // Now get the function oid's for the api
82 // This is an example of Fastpath.addFunctions();
84 String sql;
85 if (conn.ServerVersion > new ServerVersion(7,3,0) )
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 (";
92 else
94 sql = "SELECT proname,oid FROM pg_proc WHERE ";
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'";
105 if (conn.ServerVersion > new ServerVersion(7,3,0) )
107 sql += ")";
110 IDbCommand cmd = new NpgsqlCommand(sql);
111 cmd.Connection = conn;
113 this.fp = new Fastpath(conn,conn.Connector.Stream);
115 IDataReader res = cmd.ExecuteReader(CommandBehavior.CloseConnection);
118 if (res == null)
119 throw new NpgsqlException("postgresql.lo.init");
122 fp.AddFunctions(res);
126 * This opens an existing large object, based on its OID. This method
127 * assumes that READ and WRITE access is required (the default).
129 * @param oid of large object
130 * @return LargeObject instance providing access to the object
131 * @exception NpgsqlException on error
133 public LargeObject Open(Int32 oid)
135 return new LargeObject(fp, oid, READWRITE);
139 * This opens an existing large object, based on its OID
141 * @param oid of large object
142 * @param mode mode of open
143 * @return LargeObject instance providing access to the object
144 * @exception NpgsqlException on error
146 public LargeObject Open(Int32 oid, Int32 mode)
148 return new LargeObject(fp, oid, mode);
152 * This creates a large object, returning its OID.
154 * <p>It defaults to READWRITE for the new object's attributes.
156 * @return oid of new object
157 * @exception NpgsqlException on error
159 public Int32 Create()
161 FastpathArg[] args = new FastpathArg[1];
162 args[0] = new FastpathArg(READWRITE);
163 return fp.GetInteger("lo_creat", args);
167 * This creates a large object, returning its OID
169 * @param mode a bitmask describing different attributes of the new object
170 * @return oid of new object
171 * @exception NpgsqlException on error
173 public Int32 Create(Int32 mode)
175 FastpathArg[] args = new FastpathArg[1];
176 args[0] = new FastpathArg(mode);
177 return fp.GetInteger("lo_creat", args);
181 * This deletes a large object.
183 * @param oid describing object to delete
184 * @exception NpgsqlException on error
186 public void Delete(Int32 oid)
188 FastpathArg[] args = new FastpathArg[1];
189 args[0] = new FastpathArg(oid);
190 fp.FastpathCall("lo_unlink", false, args);
194 * This deletes a large object.
196 * <p>It is identical to the delete method, and is supplied as the C API uses
197 * unlink.
199 * @param oid describing object to delete
200 * @exception NpgsqlException on error
202 public void Unlink(Int32 oid)
204 Delete(oid);