Add libpq new API lo_import_with_oid() which is similar to lo_import()
authorTatsuo Ishii <ishii@postgresql.org>
Wed, 19 Mar 2008 00:39:33 +0000 (00:39 +0000)
committerTatsuo Ishii <ishii@postgresql.org>
Wed, 19 Mar 2008 00:39:33 +0000 (00:39 +0000)
except that lob's oid can be specified.

doc/src/sgml/lobj.sgml
src/interfaces/libpq/exports.txt
src/interfaces/libpq/fe-lobj.c
src/interfaces/libpq/libpq-fe.h

index dbabd75e1d105e25b01bca1d7ae75931d438c6f8..68cb257c7e4dc9a3ba318c02393644467322d378 100644 (file)
@@ -161,6 +161,28 @@ Oid lo_import(PGconn *conn, const char *filename);
      the server; so it must exist in the client file system and be readable
      by the client application.
     </para>
+
+    <para>
+     The function
+<synopsis>
+Oid lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);
+</synopsis>
+     <indexterm><primary>lo_import_with_oid</></>
+     also imports a new large object.  The OID to be assigned can be
+     specified by <replaceable class="parameter">lobjId</replaceable>;
+     if so, failure occurs if that OID is already in use for some large
+     object.  If <replaceable class="parameter">lobjId</replaceable>
+     is <symbol>InvalidOid</symbol> (zero) then <function>lo_import_with_oid</> assigns an unused
+     OID (this is the same behavior as <function>lo_import</>).
+     The return value is the OID that was assigned to the new large object,
+     or <symbol>InvalidOid</symbol> (zero) on failure.
+    </para>
+
+    <para>
+     <function>lo_import_with_oid</> is new as of <productname>PostgreSQL</productname>
+     8.4 and uses <function>lo_create</function> internally which is new in 8.1; if this function is run against 8.0 or before, it will
+     fail and return <symbol>InvalidOid</symbol>.
+    </para>
    </sect2>
 
    <sect2>
index d9951b2650be005b034dc164b5c3f7fabab534c2..b8bd30098ea2284111928306ec99a901f5411dae 100644 (file)
@@ -140,3 +140,4 @@ lo_truncate               137
 PQconnectionUsedPassword  138
 pg_valid_server_encoding_id 139
 PQconnectionNeedsPassword 140
+lo_import_with_oid               141
index e14c9d659ffc7aeddb1a9615513b1a2aa23a0a77..5609cf03e26607f6b8eacb432bdc2edc58d5f636 100644 (file)
@@ -41,6 +41,8 @@
 
 static int     lo_initialize(PGconn *conn);
 
+static Oid
+lo_import_internal(PGconn *conn, const char *filename, const Oid oid);
 
 /*
  * lo_open
@@ -483,6 +485,27 @@ lo_unlink(PGconn *conn, Oid lobjId)
 
 Oid
 lo_import(PGconn *conn, const char *filename)
+{
+       return lo_import_internal(conn, filename, InvalidOid);
+}
+
+/*
+ * lo_import_with_oid -
+ *       imports a file as an (inversion) large object.
+ *       large object id can be specified.
+ *
+ * returns the oid of that object upon success,
+ * returns InvalidOid upon failure
+ */
+
+Oid
+lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId)
+{
+       return lo_import_internal(conn, filename, lobjId);
+}
+
+static Oid
+lo_import_internal(PGconn *conn, const char *filename, Oid oid)
 {
        int                     fd;
        int                     nbytes,
@@ -507,10 +530,14 @@ lo_import(PGconn *conn, const char *filename)
        /*
         * create an inversion object
         */
-       lobjOid = lo_creat(conn, INV_READ | INV_WRITE);
+       if (oid == InvalidOid)
+               lobjOid = lo_creat(conn, INV_READ | INV_WRITE);
+       else
+               lobjOid = lo_create(conn, oid);
+
        if (lobjOid == InvalidOid)
        {
-               /* we assume lo_creat() already set a suitable error message */
+               /* we assume lo_create() already set a suitable error message */
                (void) close(fd);
                return InvalidOid;
        }
index 0d33488615504b4f0e67d622c8c0a15752162fa3..d7eca2fa23c73f28cb7e1af658a79c9f0e80489b 100644 (file)
@@ -495,6 +495,7 @@ extern int  lo_tell(PGconn *conn, int fd);
 extern int     lo_truncate(PGconn *conn, int fd, size_t len);
 extern int     lo_unlink(PGconn *conn, Oid lobjId);
 extern Oid     lo_import(PGconn *conn, const char *filename);
+extern Oid     lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);
 extern int     lo_export(PGconn *conn, Oid lobjId, const char *filename);
 
 /* === in fe-misc.c === */