/*
* PostgreSQL type definitions for managed LargeObjects.
*
- * $Id: lo.c,v 1.5 2000/11/20 20:36:55 tgl Exp $
+ * $Header: /cvsroot/pgsql/contrib/lo/lo.c,v 1.6 2000/11/21 21:51:58 tgl Exp $
*
*/
+#include "postgres.h"
+
#include
-#include
+#include "utils/palloc.h"
/* Required for largeobjects */
-#include
-#include
+#include "libpq/libpq-fs.h"
+#include "libpq/be-fsstubs.h"
/* Required for SPI */
-#include
+#include "executor/spi.h"
/* Required for triggers */
-#include
+#include "commands/trigger.h"
+
+
+#define atooid(x) ((Oid) strtoul((x), NULL, 10))
-/* required for tolower() */
/*
* This is the internal storage format for managed large objects
Datum lo_manage(PG_FUNCTION_ARGS); /* Trigger handler */
/*
- * This creates a large object, and set's its OID to the value in the
+ * 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
if (strlen(str) > 0)
{
-
- count = sscanf(str, "%d", &oid);
+ count = sscanf(str, "%u", &oid);
if (count < 1)
- {
elog(ERROR, "lo_in: error in parsing \"%s\"", str);
- return (NULL);
- }
- if (oid < 0)
- {
+ if (oid == InvalidOid)
elog(ERROR, "lo_in: illegal oid \"%s\"", str);
- return (NULL);
- }
}
else
{
oid = DatumGetObjectId(DirectFunctionCall1(lo_creat,
Int32GetDatum(INV_READ | INV_WRITE)));
if (oid == InvalidOid)
- {
elog(ERROR, "lo_in: InvalidOid returned from lo_creat");
- return (NULL);
- }
}
result = (Blob *) palloc(sizeof(Blob));
return (NULL);
result = (char *) palloc(32);
- sprintf(result, "%d", *addr);
+ sprintf(result, "%u", *addr);
return (result);
}
if ((orig != newv && (orig == NULL || newv == NULL)) || (orig != NULL && newv != NULL && strcmp(orig, newv)))
DirectFunctionCall1(lo_unlink,
- ObjectIdGetDatum((Oid) atoi(orig)));
+ ObjectIdGetDatum(atooid(orig)));
if (newv)
pfree(newv);
if (orig != NULL)
{
DirectFunctionCall1(lo_unlink,
- ObjectIdGetDatum((Oid) atoi(orig)));
+ ObjectIdGetDatum(atooid(orig)));
pfree(orig);
}
--
-- PostgreSQL code for LargeObjects
--
--- $Id: lo.sql.in,v 1.5 2000/11/20 20:36:55 tgl Exp $
+-- $Id: lo.sql.in,v 1.6 2000/11/21 21:51:58 tgl Exp $
--
--
-- Create the data type
as 'MODULE_PATHNAME'
language 'c';
+-- same function, named to allow it to be used as a type coercion, eg:
+-- create table a (image lo);
+-- select image::oid from a;
+--
+create function oid(lo)
+ returns oid
+ as 'MODULE_PATHNAME', 'lo_oid'
+ language 'c';
+
-- this allows us to convert an oid to a managed lo object
-- ie: insert into test values (lo_import('/fullpath/file')::lo);
create function lo(oid)
create function lo_manage()
returns opaque
as 'MODULE_PATHNAME'
- language 'C';
-
--- This allows us to map lo to oid
---
--- eg:
--- create table a (image lo);
--- select image::oid from a;
---
-create function oid(lo) returns oid as 'select lo_oid($1)' language 'sql';
-
+ language 'c';