1998-06-16 15:07:11 +08:00
|
|
|
/*
|
|
|
|
* PostgreSQL type definitions for managed LargeObjects.
|
|
|
|
*
|
2003-11-30 03:52:15 +08:00
|
|
|
* $PostgreSQL: pgsql/contrib/lo/lo.c,v 1.14 2003/11/29 19:51:35 pgsql Exp $
|
1998-06-16 15:07:11 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2000-11-22 05:51:58 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1998-06-16 15:07:11 +08:00
|
|
|
/* Required for largeobjects */
|
2000-11-22 05:51:58 +08:00
|
|
|
#include "libpq/libpq-fs.h"
|
|
|
|
#include "libpq/be-fsstubs.h"
|
1998-06-16 15:07:11 +08:00
|
|
|
|
|
|
|
/* Required for SPI */
|
2000-11-22 05:51:58 +08:00
|
|
|
#include "executor/spi.h"
|
1998-06-16 15:07:11 +08:00
|
|
|
|
|
|
|
/* Required for triggers */
|
2000-11-22 05:51:58 +08:00
|
|
|
#include "commands/trigger.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
|
1998-06-16 15:07:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the internal storage format for managed large objects
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef Oid Blob;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Various forward declarations:
|
|
|
|
*/
|
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
Blob *lo_in(char *str); /* Create from String */
|
2001-10-28 14:26:15 +08:00
|
|
|
char *lo_out(Blob * addr); /* Output oid as String */
|
|
|
|
Oid lo_oid(Blob * addr); /* Return oid as an oid */
|
1999-05-26 00:15:34 +08:00
|
|
|
Blob *lo(Oid oid); /* Return Blob based on oid */
|
2001-03-22 12:01:46 +08:00
|
|
|
Datum lo_manage(PG_FUNCTION_ARGS); /* Trigger handler */
|
1998-06-16 15:07:11 +08:00
|
|
|
|
|
|
|
/*
|
2000-11-22 05:51:58 +08:00
|
|
|
* This creates a large object, and sets its OID to the value in the
|
1998-06-16 15:07:11 +08:00
|
|
|
* supplied string.
|
|
|
|
*
|
|
|
|
* If the string is empty, then a new LargeObject is created, and its oid
|
|
|
|
* is placed in the resulting lo.
|
|
|
|
*/
|
|
|
|
Blob *
|
|
|
|
lo_in(char *str)
|
|
|
|
{
|
1999-05-26 00:15:34 +08:00
|
|
|
Blob *result;
|
|
|
|
Oid oid;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
if (strlen(str) > 0)
|
1998-06-16 15:07:11 +08:00
|
|
|
{
|
2000-11-22 05:51:58 +08:00
|
|
|
count = sscanf(str, "%u", &oid);
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
if (count < 1)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("error in parsing \"%s\"", str)));
|
1999-05-26 00:15:34 +08:00
|
|
|
|
2000-11-22 05:51:58 +08:00
|
|
|
if (oid == InvalidOid)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("illegal oid: \"%s\"", str)));
|
1998-06-16 15:07:11 +08:00
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
else
|
1998-06-16 15:07:11 +08:00
|
|
|
{
|
1999-05-26 00:15:34 +08:00
|
|
|
/*
|
|
|
|
* There is no Oid passed, so create a new one
|
|
|
|
*/
|
2000-06-09 09:11:16 +08:00
|
|
|
oid = DatumGetObjectId(DirectFunctionCall1(lo_creat,
|
2001-03-22 12:01:46 +08:00
|
|
|
Int32GetDatum(INV_READ | INV_WRITE)));
|
1999-05-26 00:15:34 +08:00
|
|
|
if (oid == InvalidOid)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "InvalidOid returned from lo_creat");
|
1998-06-16 15:07:11 +08:00
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
result = (Blob *) palloc(sizeof(Blob));
|
|
|
|
|
|
|
|
*result = oid;
|
|
|
|
|
|
|
|
return (result);
|
1998-06-16 15:07:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This simply outputs the Oid of the Blob as a string.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
lo_out(Blob * addr)
|
|
|
|
{
|
1999-05-26 00:15:34 +08:00
|
|
|
char *result;
|
|
|
|
|
|
|
|
if (addr == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
result = (char *) palloc(32);
|
2002-08-15 10:58:29 +08:00
|
|
|
snprintf(result, 32, "%u", *addr);
|
1999-05-26 00:15:34 +08:00
|
|
|
return (result);
|
1998-06-16 15:07:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function converts Blob to oid.
|
|
|
|
*
|
|
|
|
* eg: select lo_export(raster::oid,'/path/file') from table;
|
1999-05-26 00:15:34 +08:00
|
|
|
*
|
1998-06-16 15:07:11 +08:00
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
lo_oid(Blob * addr)
|
|
|
|
{
|
1999-05-26 00:15:34 +08:00
|
|
|
if (addr == NULL)
|
|
|
|
return InvalidOid;
|
|
|
|
return (Oid) (*addr);
|
1998-06-16 15:07:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is used so we can convert oid's to lo's
|
|
|
|
*
|
1999-05-26 00:15:34 +08:00
|
|
|
* ie: insert into table values(lo_import('/path/file')::lo);
|
1998-06-16 15:07:11 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
Blob *
|
|
|
|
lo(Oid oid)
|
|
|
|
{
|
1999-05-26 00:15:34 +08:00
|
|
|
Blob *result = (Blob *) palloc(sizeof(Blob));
|
|
|
|
|
|
|
|
*result = oid;
|
|
|
|
return (result);
|
1998-06-16 15:07:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This handles the trigger that protects us from orphaned large objects
|
|
|
|
*/
|
2000-11-21 04:36:57 +08:00
|
|
|
PG_FUNCTION_INFO_V1(lo_manage);
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
Datum
|
|
|
|
lo_manage(PG_FUNCTION_ARGS)
|
1998-06-16 15:07:11 +08:00
|
|
|
{
|
2000-05-29 09:59:17 +08:00
|
|
|
TriggerData *trigdata = (TriggerData *) fcinfo->context;
|
1999-05-26 00:15:34 +08:00
|
|
|
int attnum; /* attribute number to monitor */
|
|
|
|
char **args; /* Args containing attr name */
|
|
|
|
TupleDesc tupdesc; /* Tuple Descriptor */
|
|
|
|
HeapTuple rettuple; /* Tuple to be returned */
|
|
|
|
bool isdelete; /* are we deleting? */
|
2001-10-28 14:26:15 +08:00
|
|
|
HeapTuple newtuple = NULL; /* The new value for tuple */
|
1999-05-26 00:15:34 +08:00
|
|
|
HeapTuple trigtuple; /* The original value of tuple */
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "not fired by trigger manager");
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
/*
|
2000-05-29 09:59:17 +08:00
|
|
|
* Fetch some values from trigdata
|
1999-05-26 00:15:34 +08:00
|
|
|
*/
|
2000-05-29 09:59:17 +08:00
|
|
|
newtuple = trigdata->tg_newtuple;
|
|
|
|
trigtuple = trigdata->tg_trigtuple;
|
|
|
|
tupdesc = trigdata->tg_relation->rd_att;
|
|
|
|
args = trigdata->tg_trigger->tgargs;
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
/* tuple to return to Executor */
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
|
1999-05-26 00:15:34 +08:00
|
|
|
rettuple = newtuple;
|
|
|
|
else
|
|
|
|
rettuple = trigtuple;
|
|
|
|
|
|
|
|
/* Are we deleting the row? */
|
2000-05-29 09:59:17 +08:00
|
|
|
isdelete = TRIGGER_FIRED_BY_DELETE(trigdata->tg_event);
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
/* Get the column were interested in */
|
|
|
|
attnum = SPI_fnumber(tupdesc, args[0]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle updates
|
|
|
|
*
|
|
|
|
* Here, if the value of the monitored attribute changes, then the large
|
|
|
|
* object associated with the original value is unlinked.
|
|
|
|
*/
|
|
|
|
if (newtuple != NULL)
|
|
|
|
{
|
|
|
|
char *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
|
|
|
|
char *newv = SPI_getvalue(newtuple, tupdesc, attnum);
|
|
|
|
|
2001-12-07 12:18:31 +08:00
|
|
|
if (orig != NULL && (newv == NULL || strcmp(orig, newv)))
|
2000-06-09 09:11:16 +08:00
|
|
|
DirectFunctionCall1(lo_unlink,
|
2000-11-22 05:51:58 +08:00
|
|
|
ObjectIdGetDatum(atooid(orig)));
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
if (newv)
|
|
|
|
pfree(newv);
|
|
|
|
if (orig)
|
|
|
|
pfree(orig);
|
1998-06-16 15:07:11 +08:00
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle deleting of rows
|
|
|
|
*
|
|
|
|
* Here, we unlink the large object associated with the managed attribute
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
if (isdelete)
|
|
|
|
{
|
|
|
|
char *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
|
|
|
|
|
|
|
|
if (orig != NULL)
|
|
|
|
{
|
2000-06-09 09:11:16 +08:00
|
|
|
DirectFunctionCall1(lo_unlink,
|
2000-11-22 05:51:58 +08:00
|
|
|
ObjectIdGetDatum(atooid(orig)));
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
pfree(orig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(rettuple);
|
1998-06-16 15:07:11 +08:00
|
|
|
}
|