|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * aggregatecmds.c |
| 4 | + * |
| 5 | + * Routines for aggregate-manipulation commands |
| 6 | + * |
| 7 | + * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group |
| 8 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 9 | + * |
| 10 | + * |
| 11 | + * IDENTIFICATION |
| 12 | + * $Header: /cvsroot/pgsql/src/backend/commands/aggregatecmds.c,v 1.1 2002/04/15 05:22:03 tgl Exp $ |
| 13 | + * |
| 14 | + * DESCRIPTION |
| 15 | + * The "DefineFoo" routines take the parse tree and pick out the |
| 16 | + * appropriate arguments/flags, passing the results to the |
| 17 | + * corresponding "FooDefine" routines (in src/catalog) that do |
| 18 | + * the actual catalog-munging. These routines also verify permission |
| 19 | + * of the user to execute the command. |
| 20 | + * |
| 21 | + *------------------------------------------------------------------------- |
| 22 | + */ |
| 23 | +#include"postgres.h" |
| 24 | + |
| 25 | +#include"access/heapam.h" |
| 26 | +#include"catalog/catname.h" |
| 27 | +#include"catalog/namespace.h" |
| 28 | +#include"catalog/pg_aggregate.h" |
| 29 | +#include"commands/comment.h" |
| 30 | +#include"commands/defrem.h" |
| 31 | +#include"miscadmin.h" |
| 32 | +#include"parser/parse_func.h" |
| 33 | +#include"parser/parse_type.h" |
| 34 | +#include"utils/acl.h" |
| 35 | +#include"utils/builtins.h" |
| 36 | +#include"utils/lsyscache.h" |
| 37 | +#include"utils/syscache.h" |
| 38 | + |
| 39 | + |
| 40 | +/* |
| 41 | + *DefineAggregate |
| 42 | + */ |
| 43 | +void |
| 44 | +DefineAggregate(List*names,List*parameters) |
| 45 | +{ |
| 46 | +char*aggName; |
| 47 | +OidaggNamespace; |
| 48 | +List*transfuncName=NIL; |
| 49 | +List*finalfuncName=NIL; |
| 50 | +TypeName*baseType=NULL; |
| 51 | +TypeName*transType=NULL; |
| 52 | +char*initval=NULL; |
| 53 | +OidbaseTypeId; |
| 54 | +OidtransTypeId; |
| 55 | +List*pl; |
| 56 | + |
| 57 | +/* Convert list of names to a name and namespace */ |
| 58 | +aggNamespace=QualifiedNameGetCreationNamespace(names,&aggName); |
| 59 | + |
| 60 | +foreach(pl,parameters) |
| 61 | +{ |
| 62 | +DefElem*defel= (DefElem*)lfirst(pl); |
| 63 | + |
| 64 | +/* |
| 65 | + * sfunc1, stype1, and initcond1 are accepted as obsolete |
| 66 | + * spellings for sfunc, stype, initcond. |
| 67 | + */ |
| 68 | +if (strcasecmp(defel->defname,"sfunc")==0) |
| 69 | +transfuncName=defGetQualifiedName(defel); |
| 70 | +elseif (strcasecmp(defel->defname,"sfunc1")==0) |
| 71 | +transfuncName=defGetQualifiedName(defel); |
| 72 | +elseif (strcasecmp(defel->defname,"finalfunc")==0) |
| 73 | +finalfuncName=defGetQualifiedName(defel); |
| 74 | +elseif (strcasecmp(defel->defname,"basetype")==0) |
| 75 | +baseType=defGetTypeName(defel); |
| 76 | +elseif (strcasecmp(defel->defname,"stype")==0) |
| 77 | +transType=defGetTypeName(defel); |
| 78 | +elseif (strcasecmp(defel->defname,"stype1")==0) |
| 79 | +transType=defGetTypeName(defel); |
| 80 | +elseif (strcasecmp(defel->defname,"initcond")==0) |
| 81 | +initval=defGetString(defel); |
| 82 | +elseif (strcasecmp(defel->defname,"initcond1")==0) |
| 83 | +initval=defGetString(defel); |
| 84 | +else |
| 85 | +elog(WARNING,"DefineAggregate: attribute \"%s\" not recognized", |
| 86 | +defel->defname); |
| 87 | +} |
| 88 | + |
| 89 | +/* |
| 90 | + * make sure we have our required definitions |
| 91 | + */ |
| 92 | +if (baseType==NULL) |
| 93 | +elog(ERROR,"Define: \"basetype\" unspecified"); |
| 94 | +if (transType==NULL) |
| 95 | +elog(ERROR,"Define: \"stype\" unspecified"); |
| 96 | +if (transfuncName==NIL) |
| 97 | +elog(ERROR,"Define: \"sfunc\" unspecified"); |
| 98 | + |
| 99 | +/* |
| 100 | + * Handle the aggregate's base type (input data type). This can be |
| 101 | + * specified as 'ANY' for a data-independent transition function, such |
| 102 | + * as COUNT(*). |
| 103 | + */ |
| 104 | +baseTypeId=LookupTypeName(baseType); |
| 105 | +if (OidIsValid(baseTypeId)) |
| 106 | +{ |
| 107 | +/* no need to allow aggregates on as-yet-undefined types */ |
| 108 | +if (!get_typisdefined(baseTypeId)) |
| 109 | +elog(ERROR,"Type \"%s\" is only a shell", |
| 110 | +TypeNameToString(baseType)); |
| 111 | +} |
| 112 | +else |
| 113 | +{ |
| 114 | +char*typnam=TypeNameToString(baseType); |
| 115 | + |
| 116 | +if (strcasecmp(typnam,"ANY")!=0) |
| 117 | +elog(ERROR,"Type \"%s\" does not exist",typnam); |
| 118 | +baseTypeId=InvalidOid; |
| 119 | +} |
| 120 | + |
| 121 | +/* handle transtype --- no special cases here */ |
| 122 | +transTypeId=typenameTypeId(transType); |
| 123 | + |
| 124 | +/* |
| 125 | + * Most of the argument-checking is done inside of AggregateCreate |
| 126 | + */ |
| 127 | +AggregateCreate(aggName,/* aggregate name */ |
| 128 | +aggNamespace,/* namespace */ |
| 129 | +transfuncName,/* step function name */ |
| 130 | +finalfuncName,/* final function name */ |
| 131 | +baseTypeId,/* type of data being aggregated */ |
| 132 | +transTypeId,/* transition data type */ |
| 133 | +initval);/* initial condition */ |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +void |
| 138 | +RemoveAggregate(List*aggName,TypeName*aggType) |
| 139 | +{ |
| 140 | +Relationrelation; |
| 141 | +HeapTupletup; |
| 142 | +OidbasetypeID; |
| 143 | +OidprocOid; |
| 144 | + |
| 145 | +/* |
| 146 | + * if a basetype is passed in, then attempt to find an aggregate for |
| 147 | + * that specific type. |
| 148 | + * |
| 149 | + * else if the basetype is blank, then attempt to find an aggregate with |
| 150 | + * a basetype of zero.This is valid. It means that the aggregate is |
| 151 | + * to apply to all basetypes (eg, COUNT). |
| 152 | + */ |
| 153 | +if (aggType) |
| 154 | +basetypeID=typenameTypeId(aggType); |
| 155 | +else |
| 156 | +basetypeID=InvalidOid; |
| 157 | + |
| 158 | +procOid=find_aggregate_func("RemoveAggregate",aggName,basetypeID); |
| 159 | + |
| 160 | +/* Permission check */ |
| 161 | + |
| 162 | +if (!pg_proc_ownercheck(procOid,GetUserId())) |
| 163 | +{ |
| 164 | +if (basetypeID==InvalidOid) |
| 165 | +elog(ERROR,"RemoveAggregate: aggregate %s for all types: permission denied", |
| 166 | +NameListToString(aggName)); |
| 167 | +else |
| 168 | +elog(ERROR,"RemoveAggregate: aggregate %s for type %s: permission denied", |
| 169 | +NameListToString(aggName),format_type_be(basetypeID)); |
| 170 | +} |
| 171 | + |
| 172 | +/* Remove the pg_proc tuple */ |
| 173 | + |
| 174 | +relation=heap_openr(ProcedureRelationName,RowExclusiveLock); |
| 175 | + |
| 176 | +tup=SearchSysCache(PROCOID, |
| 177 | +ObjectIdGetDatum(procOid), |
| 178 | +0,0,0); |
| 179 | +if (!HeapTupleIsValid(tup))/* should not happen */ |
| 180 | +elog(ERROR,"RemoveAggregate: couldn't find pg_proc tuple for %s", |
| 181 | +NameListToString(aggName)); |
| 182 | + |
| 183 | +/* Delete any comments associated with this function */ |
| 184 | +DeleteComments(procOid,RelationGetRelid(relation)); |
| 185 | + |
| 186 | +simple_heap_delete(relation,&tup->t_self); |
| 187 | + |
| 188 | +ReleaseSysCache(tup); |
| 189 | + |
| 190 | +heap_close(relation,RowExclusiveLock); |
| 191 | + |
| 192 | +/* Remove the pg_aggregate tuple */ |
| 193 | + |
| 194 | +relation=heap_openr(AggregateRelationName,RowExclusiveLock); |
| 195 | + |
| 196 | +tup=SearchSysCache(AGGFNOID, |
| 197 | +ObjectIdGetDatum(procOid), |
| 198 | +0,0,0); |
| 199 | +if (!HeapTupleIsValid(tup))/* should not happen */ |
| 200 | +elog(ERROR,"RemoveAggregate: couldn't find pg_aggregate tuple for %s", |
| 201 | +NameListToString(aggName)); |
| 202 | + |
| 203 | +simple_heap_delete(relation,&tup->t_self); |
| 204 | + |
| 205 | +ReleaseSysCache(tup); |
| 206 | + |
| 207 | +heap_close(relation,RowExclusiveLock); |
| 208 | +} |