Add libpq new API lo_import_with_oid() which is similar to lo_import()
authorTatsuo Ishii
Wed, 19 Mar 2008 00:39:33 +0000 (00:39 +0000)
committerTatsuo Ishii
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 0c60988f259e29876c5a6bfde1de27e6da51672c..69a21c99cf78f1bbe439168326e9bcc0091f9d04 100644 (file)
@@ -1,4 +1,4 @@
-
+
 
  
   Large Objects
@@ -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.
     
+
+    
+     The function
+
+Oid lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);
+
+     lo_import_with_oid
+     also imports a new large object.  The OID to be assigned can be
+     specified by lobjId;
+     if so, failure occurs if that OID is already in use for some large
+     object.  If lobjId
+     is InvalidOid (zero) then lo_import_with_oid assigns an unused
+     OID (this is the same behavior as lo_import).
+     The return value is the OID that was assigned to the new large object,
+     or InvalidOid (zero) on failure.
+    
+
+    
+     lo_import_with_oid is new as of PostgreSQL
+     8.4 and uses lo_create internally which is new in 8.1; if this function is run against 8.0 or before, it will
+     fail and return InvalidOid.
+    
    
 
    
index 2b7b8fe94b7407876c6968b2a42830601017eb01..f8809f841e4ad1a866380d692b11fbe5347f5712 100644 (file)
@@ -1,4 +1,4 @@
-# $PostgreSQL: pgsql/src/interfaces/libpq/exports.txt,v 1.18 2007/12/09 19:01:40 tgl Exp $
+# $PostgreSQL: pgsql/src/interfaces/libpq/exports.txt,v 1.19 2008/03/19 00:39:33 ishii Exp $
 # Functions to be exported by libpq DLLs
 PQconnectdb               1
 PQsetdbLogin              2
@@ -140,3 +140,4 @@ lo_truncate               137
 PQconnectionUsedPassword  138
 pg_valid_server_encoding_id 139
 PQconnectionNeedsPassword 140
+lo_import_with_oid       141
index dfe071987114b5f2bba75a76ede05dd3257b6bf0..1cf78d57d64cbb95c129504c02c2262fb0d0f43a 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/interfaces/libpq/fe-lobj.c,v 1.64 2008/01/01 19:46:00 momjian Exp $
+ *   $PostgreSQL: pgsql/src/interfaces/libpq/fe-lobj.c,v 1.65 2008/03/19 00:39:33 ishii Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -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 f51c6b38acdcdd2da4fe17f14cc601dc918bbc77..53d79b059f6228083cc397436c4d3e732e4c1357 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.141 2008/01/01 19:46:00 momjian Exp $
+ * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.142 2008/03/19 00:39:33 ishii Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -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 === */