|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * pg_largeobject.c |
| 4 | + * routines to support manipulation of the pg_largeobject relation |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2000, PostgreSQL, Inc |
| 7 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 8 | + * |
| 9 | + * |
| 10 | + * IDENTIFICATION |
| 11 | + * $Header: /cvsroot/pgsql/src/backend/catalog/pg_largeobject.c,v 1.3 2000/10/21 15:55:21 momjian Exp $ |
| 12 | + * |
| 13 | + *------------------------------------------------------------------------- |
| 14 | + */ |
| 15 | +#include"postgres.h" |
| 16 | + |
| 17 | +#include"access/genam.h" |
| 18 | +#include"access/heapam.h" |
| 19 | +#include"catalog/catname.h" |
| 20 | +#include"catalog/indexing.h" |
| 21 | +#include"catalog/pg_largeobject.h" |
| 22 | +#include"miscadmin.h" |
| 23 | +#include"utils/fmgroids.h" |
| 24 | + |
| 25 | +bytea*_byteain(constchar*data,int32size); |
| 26 | + |
| 27 | +bytea*_byteain(constchar*data,int32size) { |
| 28 | +bytea*result; |
| 29 | + |
| 30 | +result= (bytea*)palloc(size+VARHDRSZ); |
| 31 | +result->vl_len=size+VARHDRSZ; |
| 32 | +if (size>0) |
| 33 | +memcpy(result->vl_dat,data,size); |
| 34 | + |
| 35 | +returnresult; |
| 36 | +} |
| 37 | + |
| 38 | +OidLargeobjectCreate(Oidloid) { |
| 39 | +Oidretval; |
| 40 | +Relationpg_largeobject; |
| 41 | +HeapTuplentup= (HeapTuple)palloc(sizeof(HeapTupleData)); |
| 42 | +Relationidescs[Num_pg_largeobject_indices]; |
| 43 | +Datumvalues[Natts_pg_largeobject]; |
| 44 | +charnulls[Natts_pg_largeobject]; |
| 45 | +inti; |
| 46 | + |
| 47 | +for (i=0;i<Natts_pg_largeobject;i++) { |
| 48 | +nulls[i]=' '; |
| 49 | +values[i]= (Datum)NULL; |
| 50 | +} |
| 51 | + |
| 52 | +i=0; |
| 53 | +values[i++]=ObjectIdGetDatum(loid); |
| 54 | +values[i++]=Int32GetDatum(0); |
| 55 | +values[i++]= (Datum)_byteain(NULL,0); |
| 56 | + |
| 57 | +pg_largeobject=heap_openr(LargeobjectRelationName,RowExclusiveLock); |
| 58 | +ntup=heap_formtuple(pg_largeobject->rd_att,values,nulls); |
| 59 | +retval=heap_insert(pg_largeobject,ntup); |
| 60 | + |
| 61 | +if (!IsIgnoringSystemIndexes()) { |
| 62 | +CatalogOpenIndices(Num_pg_largeobject_indices,Name_pg_largeobject_indices,idescs); |
| 63 | +CatalogIndexInsert(idescs,Num_pg_largeobject_indices,pg_largeobject,ntup); |
| 64 | +CatalogCloseIndices(Num_pg_largeobject_indices,idescs); |
| 65 | +} |
| 66 | + |
| 67 | +heap_close(pg_largeobject,RowExclusiveLock); |
| 68 | +heap_freetuple(ntup); |
| 69 | + |
| 70 | +CommandCounterIncrement(); |
| 71 | + |
| 72 | +returnretval; |
| 73 | +} |
| 74 | + |
| 75 | +voidLargeobjectDrop(Oidloid) { |
| 76 | +Relationpg_largeobject; |
| 77 | +Relationpg_lo_id; |
| 78 | +ScanKeyDataskey; |
| 79 | +IndexScanDescsd= (IndexScanDesc)NULL; |
| 80 | +RetrieveIndexResultindexRes; |
| 81 | +intfound=0; |
| 82 | + |
| 83 | +ScanKeyEntryInitialize(&skey, |
| 84 | + (bits16)0x0, |
| 85 | + (AttrNumber)1, |
| 86 | + (RegProcedure)F_OIDEQ, |
| 87 | +ObjectIdGetDatum(loid)); |
| 88 | + |
| 89 | +pg_largeobject=heap_openr(LargeobjectRelationName,RowShareLock); |
| 90 | +pg_lo_id=index_openr(LargeobjectLOIdIndex); |
| 91 | + |
| 92 | +sd=index_beginscan(pg_lo_id, false,1,&skey); |
| 93 | + |
| 94 | +while((indexRes=index_getnext(sd,ForwardScanDirection))) { |
| 95 | +found++; |
| 96 | +heap_delete(pg_largeobject,&indexRes->heap_iptr,NULL); |
| 97 | +pfree(indexRes); |
| 98 | +} |
| 99 | + |
| 100 | +index_endscan(sd); |
| 101 | + |
| 102 | +index_close(pg_lo_id); |
| 103 | +heap_close(pg_largeobject,RowShareLock); |
| 104 | +if (found==0) |
| 105 | +elog(ERROR,"LargeobjectDrop: large object %d not found",loid); |
| 106 | +} |
| 107 | + |
| 108 | +intLargeobjectFind(Oidloid) { |
| 109 | +intretval=0; |
| 110 | +Relationpg_lo_id; |
| 111 | +ScanKeyDataskey; |
| 112 | +IndexScanDescsd= (IndexScanDesc)NULL; |
| 113 | +RetrieveIndexResultindexRes; |
| 114 | + |
| 115 | +ScanKeyEntryInitialize(&skey, |
| 116 | + (bits16)0x0, |
| 117 | + (AttrNumber)1, |
| 118 | + (RegProcedure)F_OIDEQ, |
| 119 | +ObjectIdGetDatum(loid)); |
| 120 | + |
| 121 | +pg_lo_id=index_openr(LargeobjectLOIdIndex); |
| 122 | + |
| 123 | +sd=index_beginscan(pg_lo_id, false,1,&skey); |
| 124 | + |
| 125 | +if ((indexRes=index_getnext(sd,ForwardScanDirection))) { |
| 126 | +retval=1; |
| 127 | +pfree(indexRes); |
| 128 | +} |
| 129 | + |
| 130 | +index_endscan(sd); |
| 131 | + |
| 132 | +index_close(pg_lo_id); |
| 133 | +returnretval; |
| 134 | +} |
| 135 | + |