mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-02-05 19:09:58 +08:00
Add server side lo_import(filename, oid) function.
This commit is contained in:
parent
bc49703d91
commit
325c0a39e4
@ -1,4 +1,4 @@
|
|||||||
<!-- $PostgreSQL: pgsql/doc/src/sgml/lobj.sgml,v 1.47 2008/03/19 00:39:33 ishii Exp $ -->
|
<!-- $PostgreSQL: pgsql/doc/src/sgml/lobj.sgml,v 1.48 2008/03/22 01:55:14 ishii Exp $ -->
|
||||||
|
|
||||||
<chapter id="largeObjects">
|
<chapter id="largeObjects">
|
||||||
<title id="largeObjects-title">Large Objects</title>
|
<title id="largeObjects-title">Large Objects</title>
|
||||||
@ -422,6 +422,9 @@ SELECT lo_unlink(173454); -- deletes large object with OID 173454
|
|||||||
INSERT INTO image (name, raster)
|
INSERT INTO image (name, raster)
|
||||||
VALUES ('beautiful image', lo_import('/etc/motd'));
|
VALUES ('beautiful image', lo_import('/etc/motd'));
|
||||||
|
|
||||||
|
INSERT INTO image (name, raster) -- same as above, but specify OID to use
|
||||||
|
VALUES ('beautiful image', lo_import('/etc/motd', 68583));
|
||||||
|
|
||||||
SELECT lo_export(image.raster, '/tmp/motd') FROM image
|
SELECT lo_export(image.raster, '/tmp/motd') FROM image
|
||||||
WHERE name = 'beautiful image';
|
WHERE name = 'beautiful image';
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $PostgreSQL: pgsql/src/backend/libpq/be-fsstubs.c,v 1.87 2008/01/01 19:45:49 momjian Exp $
|
* $PostgreSQL: pgsql/src/backend/libpq/be-fsstubs.c,v 1.88 2008/03/22 01:55:14 ishii Exp $
|
||||||
*
|
*
|
||||||
* NOTES
|
* NOTES
|
||||||
* This should be moved to a more appropriate place. It is here
|
* This should be moved to a more appropriate place. It is here
|
||||||
@ -79,6 +79,7 @@ static MemoryContext fscxt = NULL;
|
|||||||
|
|
||||||
static int newLOfd(LargeObjectDesc *lobjCookie);
|
static int newLOfd(LargeObjectDesc *lobjCookie);
|
||||||
static void deleteLOfd(int fd);
|
static void deleteLOfd(int fd);
|
||||||
|
static Oid lo_import_internal(text *filename, Oid lobjOid);
|
||||||
|
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
@ -320,13 +321,33 @@ Datum
|
|||||||
lo_import(PG_FUNCTION_ARGS)
|
lo_import(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
text *filename = PG_GETARG_TEXT_P(0);
|
text *filename = PG_GETARG_TEXT_P(0);
|
||||||
|
|
||||||
|
PG_RETURN_OID(lo_import_internal(filename, InvalidOid));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* lo_import_with_oid -
|
||||||
|
* imports a file as an (inversion) large object specifying oid.
|
||||||
|
*/
|
||||||
|
Datum
|
||||||
|
lo_import_with_oid(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
text *filename = PG_GETARG_TEXT_P(0);
|
||||||
|
Oid oid = PG_GETARG_OID(1);
|
||||||
|
|
||||||
|
PG_RETURN_OID(lo_import_internal(filename, oid));
|
||||||
|
}
|
||||||
|
|
||||||
|
static Oid
|
||||||
|
lo_import_internal(text *filename, Oid lobjOid)
|
||||||
|
{
|
||||||
File fd;
|
File fd;
|
||||||
int nbytes,
|
int nbytes,
|
||||||
tmp;
|
tmp;
|
||||||
char buf[BUFSIZE];
|
char buf[BUFSIZE];
|
||||||
char fnamebuf[MAXPGPATH];
|
char fnamebuf[MAXPGPATH];
|
||||||
LargeObjectDesc *lobj;
|
LargeObjectDesc *lobj;
|
||||||
Oid lobjOid;
|
Oid oid;
|
||||||
|
|
||||||
#ifndef ALLOW_DANGEROUS_LO_FUNCTIONS
|
#ifndef ALLOW_DANGEROUS_LO_FUNCTIONS
|
||||||
if (!superuser())
|
if (!superuser())
|
||||||
@ -356,12 +377,12 @@ lo_import(PG_FUNCTION_ARGS)
|
|||||||
/*
|
/*
|
||||||
* create an inversion object
|
* create an inversion object
|
||||||
*/
|
*/
|
||||||
lobjOid = inv_create(InvalidOid);
|
oid = inv_create(lobjOid);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* read in from the filesystem and write to the inversion object
|
* read in from the filesystem and write to the inversion object
|
||||||
*/
|
*/
|
||||||
lobj = inv_open(lobjOid, INV_WRITE, fscxt);
|
lobj = inv_open(oid, INV_WRITE, fscxt);
|
||||||
|
|
||||||
while ((nbytes = FileRead(fd, buf, BUFSIZE)) > 0)
|
while ((nbytes = FileRead(fd, buf, BUFSIZE)) > 0)
|
||||||
{
|
{
|
||||||
@ -378,7 +399,7 @@ lo_import(PG_FUNCTION_ARGS)
|
|||||||
inv_close(lobj);
|
inv_close(lobj);
|
||||||
FileClose(fd);
|
FileClose(fd);
|
||||||
|
|
||||||
PG_RETURN_OID(lobjOid);
|
return oid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -37,7 +37,7 @@
|
|||||||
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
||||||
* Portions Copyright (c) 1994, Regents of the University of California
|
* Portions Copyright (c) 1994, Regents of the University of California
|
||||||
*
|
*
|
||||||
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.442 2008/03/10 13:53:35 mha Exp $
|
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.443 2008/03/22 01:55:14 ishii Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -53,6 +53,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* yyyymmddN */
|
/* yyyymmddN */
|
||||||
#define CATALOG_VERSION_NO 200803101
|
#define CATALOG_VERSION_NO 200803221
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
||||||
* Portions Copyright (c) 1994, Regents of the University of California
|
* Portions Copyright (c) 1994, Regents of the University of California
|
||||||
*
|
*
|
||||||
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.482 2008/01/01 19:45:57 momjian Exp $
|
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.483 2008/03/22 01:55:14 ishii Exp $
|
||||||
*
|
*
|
||||||
* NOTES
|
* NOTES
|
||||||
* The script catalog/genbki.sh reads this file and generates .bki
|
* The script catalog/genbki.sh reads this file and generates .bki
|
||||||
@ -1027,6 +1027,8 @@ DESCR("storage manager");
|
|||||||
|
|
||||||
DATA(insert OID = 764 ( lo_import PGNSP PGUID 12 1 0 f f t f v 1 26 "25" _null_ _null_ _null_ lo_import - _null_ _null_ ));
|
DATA(insert OID = 764 ( lo_import PGNSP PGUID 12 1 0 f f t f v 1 26 "25" _null_ _null_ _null_ lo_import - _null_ _null_ ));
|
||||||
DESCR("large object import");
|
DESCR("large object import");
|
||||||
|
DATA(insert OID = 767 ( lo_import PGNSP PGUID 12 1 0 f f t f v 2 26 "25 26" _null_ _null_ _null_ lo_import_with_oid - _null_ _null_ ));
|
||||||
|
DESCR("large object import");
|
||||||
DATA(insert OID = 765 ( lo_export PGNSP PGUID 12 1 0 f f t f v 2 23 "26 25" _null_ _null_ _null_ lo_export - _null_ _null_ ));
|
DATA(insert OID = 765 ( lo_export PGNSP PGUID 12 1 0 f f t f v 2 23 "26 25" _null_ _null_ _null_ lo_export - _null_ _null_ ));
|
||||||
DESCR("large object export");
|
DESCR("large object export");
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
||||||
* Portions Copyright (c) 1994, Regents of the University of California
|
* Portions Copyright (c) 1994, Regents of the University of California
|
||||||
*
|
*
|
||||||
* $PostgreSQL: pgsql/src/include/libpq/be-fsstubs.h,v 1.30 2008/01/01 19:45:58 momjian Exp $
|
* $PostgreSQL: pgsql/src/include/libpq/be-fsstubs.h,v 1.31 2008/03/22 01:55:14 ishii Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* LO functions available via pg_proc entries
|
* LO functions available via pg_proc entries
|
||||||
*/
|
*/
|
||||||
extern Datum lo_import(PG_FUNCTION_ARGS);
|
extern Datum lo_import(PG_FUNCTION_ARGS);
|
||||||
|
extern Datum lo_import_with_oid(PG_FUNCTION_ARGS);
|
||||||
extern Datum lo_export(PG_FUNCTION_ARGS);
|
extern Datum lo_export(PG_FUNCTION_ARGS);
|
||||||
|
|
||||||
extern Datum lo_creat(PG_FUNCTION_ARGS);
|
extern Datum lo_creat(PG_FUNCTION_ARGS);
|
||||||
|
Loading…
Reference in New Issue
Block a user