mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Cleanup the contrib/lo module: there is no need anymore to implement
a physically separate type. Defining 'lo' as a domain over OID works just fine and is more efficient. Improve documentation and fix up the test script. (Would like to turn test script into a proper regression test, but right now its output is not constant because of numeric OIDs; plus it makes Unix-specific assumptions about files it can import.)
This commit is contained in:
128
contrib/lo/lo.c
128
contrib/lo/lo.c
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* PostgreSQL type definitions for managed LargeObjects.
|
||||
* PostgreSQL definitions for managed Large Objects.
|
||||
*
|
||||
* $PostgreSQL: pgsql/contrib/lo/lo.c,v 1.14 2003/11/29 19:51:35 pgsql Exp $
|
||||
* $PostgreSQL: pgsql/contrib/lo/lo.c,v 1.15 2005/06/23 00:06:37 tgl Exp $
|
||||
*
|
||||
*/
|
||||
|
||||
@ -21,117 +21,12 @@
|
||||
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
|
||||
|
||||
|
||||
/*
|
||||
* This is the internal storage format for managed large objects
|
||||
*
|
||||
*/
|
||||
/* forward declarations */
|
||||
Datum lo_manage(PG_FUNCTION_ARGS);
|
||||
|
||||
typedef Oid Blob;
|
||||
|
||||
/*
|
||||
* Various forward declarations:
|
||||
*/
|
||||
|
||||
Blob *lo_in(char *str); /* Create from String */
|
||||
char *lo_out(Blob * addr); /* Output oid as String */
|
||||
Oid lo_oid(Blob * addr); /* Return oid as an oid */
|
||||
Blob *lo(Oid oid); /* Return Blob based on oid */
|
||||
Datum lo_manage(PG_FUNCTION_ARGS); /* Trigger handler */
|
||||
|
||||
/*
|
||||
* This creates a large object, and sets its OID to the value in the
|
||||
* 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)
|
||||
{
|
||||
Blob *result;
|
||||
Oid oid;
|
||||
int count;
|
||||
|
||||
if (strlen(str) > 0)
|
||||
{
|
||||
count = sscanf(str, "%u", &oid);
|
||||
|
||||
if (count < 1)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("error in parsing \"%s\"", str)));
|
||||
|
||||
if (oid == InvalidOid)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("illegal oid: \"%s\"", str)));
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* There is no Oid passed, so create a new one
|
||||
*/
|
||||
oid = DatumGetObjectId(DirectFunctionCall1(lo_creat,
|
||||
Int32GetDatum(INV_READ | INV_WRITE)));
|
||||
if (oid == InvalidOid)
|
||||
/* internal error */
|
||||
elog(ERROR, "InvalidOid returned from lo_creat");
|
||||
}
|
||||
|
||||
result = (Blob *) palloc(sizeof(Blob));
|
||||
|
||||
*result = oid;
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* This simply outputs the Oid of the Blob as a string.
|
||||
*/
|
||||
char *
|
||||
lo_out(Blob * addr)
|
||||
{
|
||||
char *result;
|
||||
|
||||
if (addr == NULL)
|
||||
return (NULL);
|
||||
|
||||
result = (char *) palloc(32);
|
||||
snprintf(result, 32, "%u", *addr);
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function converts Blob to oid.
|
||||
*
|
||||
* eg: select lo_export(raster::oid,'/path/file') from table;
|
||||
*
|
||||
*/
|
||||
Oid
|
||||
lo_oid(Blob * addr)
|
||||
{
|
||||
if (addr == NULL)
|
||||
return InvalidOid;
|
||||
return (Oid) (*addr);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function is used so we can convert oid's to lo's
|
||||
*
|
||||
* ie: insert into table values(lo_import('/path/file')::lo);
|
||||
*
|
||||
*/
|
||||
Blob *
|
||||
lo(Oid oid)
|
||||
{
|
||||
Blob *result = (Blob *) palloc(sizeof(Blob));
|
||||
|
||||
*result = oid;
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* This handles the trigger that protects us from orphaned large objects
|
||||
* This is the trigger that protects us from orphaned large objects
|
||||
*/
|
||||
PG_FUNCTION_INFO_V1(lo_manage);
|
||||
|
||||
@ -144,11 +39,10 @@ lo_manage(PG_FUNCTION_ARGS)
|
||||
TupleDesc tupdesc; /* Tuple Descriptor */
|
||||
HeapTuple rettuple; /* Tuple to be returned */
|
||||
bool isdelete; /* are we deleting? */
|
||||
HeapTuple newtuple = NULL; /* The new value for tuple */
|
||||
HeapTuple newtuple; /* The new value for tuple */
|
||||
HeapTuple trigtuple; /* The original value of tuple */
|
||||
|
||||
if (!CALLED_AS_TRIGGER(fcinfo))
|
||||
/* internal error */
|
||||
if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
|
||||
elog(ERROR, "not fired by trigger manager");
|
||||
|
||||
/*
|
||||
@ -168,9 +62,12 @@ lo_manage(PG_FUNCTION_ARGS)
|
||||
/* Are we deleting the row? */
|
||||
isdelete = TRIGGER_FIRED_BY_DELETE(trigdata->tg_event);
|
||||
|
||||
/* Get the column were interested in */
|
||||
/* Get the column we're interested in */
|
||||
attnum = SPI_fnumber(tupdesc, args[0]);
|
||||
|
||||
if (attnum <= 0)
|
||||
elog(ERROR, "column \"%s\" does not exist", args[0]);
|
||||
|
||||
/*
|
||||
* Handle updates
|
||||
*
|
||||
@ -182,7 +79,7 @@ lo_manage(PG_FUNCTION_ARGS)
|
||||
char *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
|
||||
char *newv = SPI_getvalue(newtuple, tupdesc, attnum);
|
||||
|
||||
if (orig != NULL && (newv == NULL || strcmp(orig, newv)))
|
||||
if (orig != NULL && (newv == NULL || strcmp(orig, newv) != 0))
|
||||
DirectFunctionCall1(lo_unlink,
|
||||
ObjectIdGetDatum(atooid(orig)));
|
||||
|
||||
@ -196,7 +93,6 @@ lo_manage(PG_FUNCTION_ARGS)
|
||||
* Handle deleting of rows
|
||||
*
|
||||
* Here, we unlink the large object associated with the managed attribute
|
||||
*
|
||||
*/
|
||||
if (isdelete)
|
||||
{
|
||||
|
Reference in New Issue
Block a user