1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* rewriteDefine.c--
|
1997-09-07 07:04:48 +02:00
|
|
|
* routines for defining a rewrite rule
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1998-01-13 05:05:12 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.14 1998/01/13 04:04:12 scrappy Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1998-01-13 05:05:12 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/heapam.h" /* access methods like amopenr */
|
|
|
|
#include "nodes/parsenodes.h"
|
1998-01-13 05:05:12 +01:00
|
|
|
#include "nodes/pg_list.h" /* for Lisp support */
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "parser/parse_relation.h"
|
1997-09-07 07:04:48 +02:00
|
|
|
#include "rewrite/locks.h"
|
1996-11-10 04:06:38 +01:00
|
|
|
#include "rewrite/rewriteDefine.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "rewrite/rewriteRemove.h"
|
|
|
|
#include "rewrite/rewriteSupport.h"
|
|
|
|
#include "tcop/tcopprot.h"
|
1998-01-13 05:05:12 +01:00
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/lsyscache.h" /* for get_typlen */
|
|
|
|
#include "utils/rel.h" /* for Relation stuff */
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
Oid LastOidProcessed = InvalidOid;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is too small for many rule plans, but it'll have to do for now.
|
|
|
|
* Rule plans, etc will eventually have to be large objects.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* should this be smaller?
|
|
|
|
*/
|
1998-01-13 05:05:12 +01:00
|
|
|
#define RULE_PLAN_SIZE BLCKSZ
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
strcpyq(char *dest, char *source)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *current = source,
|
|
|
|
*destp = dest;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (current = source; *current; current++)
|
|
|
|
{
|
|
|
|
if (*current == '\"')
|
|
|
|
{
|
|
|
|
*destp = '\\';
|
|
|
|
destp++;
|
|
|
|
}
|
|
|
|
*destp = *current;
|
|
|
|
destp++;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
*destp = '\0';
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* InsertRule -
|
1997-09-07 07:04:48 +02:00
|
|
|
* takes the arguments and inserts them as attributes into the system
|
|
|
|
* relation "pg_rewrite"
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* MODS : changes the value of LastOidProcessed as a side
|
|
|
|
* effect of inserting the rule tuple
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* ARGS : rulname - name of the rule
|
|
|
|
* evtype - one of RETRIEVE,REPLACE,DELETE,APPEND
|
|
|
|
* evobj - name of relation
|
|
|
|
* evslot - comma delimited list of slots
|
|
|
|
* if null => multi-attr rule
|
|
|
|
* evinstead - is an instead rule
|
|
|
|
* actiontree - parsetree(s) of rule action
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static Oid
|
1996-07-09 08:22:35 +02:00
|
|
|
InsertRule(char *rulname,
|
1997-09-07 07:04:48 +02:00
|
|
|
int evtype,
|
|
|
|
char *evobj,
|
|
|
|
char *evslot,
|
|
|
|
char *evqual,
|
|
|
|
bool evinstead,
|
|
|
|
char *actiontree)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
static char rulebuf[RULE_PLAN_SIZE];
|
|
|
|
static char actionbuf[RULE_PLAN_SIZE];
|
|
|
|
static char qualbuf[RULE_PLAN_SIZE];
|
|
|
|
Oid eventrel_oid = InvalidOid;
|
|
|
|
AttrNumber evslot_index = InvalidAttrNumber;
|
|
|
|
Relation eventrel = NULL;
|
|
|
|
char *is_instead = "f";
|
|
|
|
extern void eval_as_new_xact();
|
|
|
|
char *template;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
eventrel = heap_openr(evobj);
|
|
|
|
if (eventrel == NULL)
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "rules cannot be defined on relations not in schema");
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
eventrel_oid = RelationGetRelationId(eventrel);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if the slotname is null, we know that this is a multi-attr rule
|
|
|
|
*/
|
|
|
|
if (evslot == NULL)
|
|
|
|
evslot_index = -1;
|
|
|
|
else
|
1997-11-25 23:07:18 +01:00
|
|
|
evslot_index = attnameAttNum(eventrel, (char *) evslot);
|
1997-09-07 07:04:48 +02:00
|
|
|
heap_close(eventrel);
|
|
|
|
|
|
|
|
if (evinstead)
|
|
|
|
is_instead = "t";
|
|
|
|
|
|
|
|
if (evqual == NULL)
|
1998-01-07 16:32:47 +01:00
|
|
|
evqual = "<>";
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (IsDefinedRewriteRule(rulname))
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "Attempt to insert rule '%s' failed: already exists",
|
1997-09-07 07:04:48 +02:00
|
|
|
rulname);
|
|
|
|
strcpyq(actionbuf, actiontree);
|
|
|
|
strcpyq(qualbuf, evqual);
|
|
|
|
|
|
|
|
template = "INSERT INTO pg_rewrite \
|
1997-10-25 07:37:07 +02:00
|
|
|
(rulename, ev_type, ev_class, ev_attr, ev_action, ev_qual, is_instead) VALUES \
|
1996-07-09 08:22:35 +02:00
|
|
|
('%s', %d::char, %d::oid, %d::int2, '%s'::text, '%s'::text, \
|
|
|
|
'%s'::bool);";
|
1997-09-07 07:04:48 +02:00
|
|
|
if (strlen(template) + strlen(rulname) + strlen(actionbuf) +
|
|
|
|
strlen(qualbuf) + 20 /* fudge fac */ > RULE_PLAN_SIZE)
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "DefineQueryRewrite: rule plan string too big.");
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
sprintf(rulebuf, template,
|
|
|
|
rulname, evtype, eventrel_oid, evslot_index, actionbuf,
|
|
|
|
qualbuf, is_instead);
|
|
|
|
|
1997-12-11 18:36:58 +01:00
|
|
|
pg_exec_query(rulebuf, (char **) NULL, (Oid *) NULL, 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
return (LastOidProcessed);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* for now, event_object must be a single attribute
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ValidateRule(int event_type,
|
1997-09-07 07:04:48 +02:00
|
|
|
char *eobj_string,
|
|
|
|
char *eslot_string,
|
1997-09-08 23:56:23 +02:00
|
|
|
Node *event_qual,
|
|
|
|
List **action,
|
1997-09-07 07:04:48 +02:00
|
|
|
int is_instead,
|
|
|
|
Oid event_attype)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (((event_type == CMD_INSERT) || (event_type == CMD_DELETE)) &&
|
|
|
|
eslot_string)
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR,
|
1997-09-07 07:04:48 +02:00
|
|
|
"rules not allowed for insert or delete events to an attribute");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event_qual && !*action && is_instead)
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR,
|
1997-09-07 07:04:48 +02:00
|
|
|
"event_quals on 'instead nothing' rules not currently supported");
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
/*
|
|
|
|
* on retrieve to class.attribute do instead nothing is converted to
|
|
|
|
* 'on retrieve to class.attribute do instead retrieve (attribute =
|
|
|
|
* NULL)' --- this is also a terrible hack that works well -- glass
|
|
|
|
*/
|
|
|
|
if (is_instead && !*action && eslot_string && event_type == CMD_SELECT)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *temp_buffer = (char *) palloc(strlen(template) + 80);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
sprintf(temp_buffer, template, event_attype,
|
|
|
|
get_typlen(event_attype), eslot_string,
|
|
|
|
event_attype);
|
|
|
|
|
|
|
|
*action = (List *) stringToNode(temp_buffer);
|
|
|
|
|
|
|
|
pfree(temp_buffer);
|
|
|
|
}
|
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-09-08 23:56:23 +02:00
|
|
|
DefineQueryRewrite(RuleStmt *stmt)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
CmdType event_type = stmt->event;
|
|
|
|
Attr *event_obj = stmt->object;
|
|
|
|
Node *event_qual = stmt->whereClause;
|
|
|
|
bool is_instead = stmt->instead;
|
|
|
|
List *action = stmt->actions;
|
|
|
|
Relation event_relation = NULL;
|
|
|
|
Oid ruleId;
|
|
|
|
Oid ev_relid = 0;
|
|
|
|
char *eslot_string = NULL;
|
|
|
|
int event_attno = 0;
|
|
|
|
Oid event_attype = 0;
|
|
|
|
char *actionP,
|
|
|
|
*event_qualP;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (event_obj->attrs)
|
|
|
|
eslot_string = strVal(lfirst(event_obj->attrs));
|
|
|
|
else
|
|
|
|
eslot_string = NULL;
|
|
|
|
|
|
|
|
event_relation = heap_openr(event_obj->relname);
|
|
|
|
if (event_relation == NULL)
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "virtual relations not supported yet");
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
ev_relid = RelationGetRelationId(event_relation);
|
|
|
|
|
|
|
|
if (eslot_string == NULL)
|
|
|
|
{
|
|
|
|
event_attno = -1;
|
|
|
|
event_attype = -1; /* XXX - don't care */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1997-11-25 23:07:18 +01:00
|
|
|
event_attno = attnameAttNum(event_relation, eslot_string);
|
|
|
|
event_attype = attnumTypeId(event_relation, event_attno);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
heap_close(event_relation);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* fix bug about instead nothing */
|
|
|
|
ValidateRule(event_type, event_obj->relname,
|
|
|
|
eslot_string, event_qual, &action,
|
|
|
|
is_instead, event_attype);
|
|
|
|
|
|
|
|
if (action == NULL)
|
|
|
|
{
|
|
|
|
if (!is_instead)
|
|
|
|
return; /* doesn't do anything */
|
|
|
|
|
|
|
|
event_qualP = nodeToString(event_qual);
|
|
|
|
|
|
|
|
ruleId = InsertRule(stmt->rulename,
|
|
|
|
event_type,
|
|
|
|
event_obj->relname,
|
|
|
|
eslot_string,
|
|
|
|
event_qualP,
|
|
|
|
true,
|
1998-01-07 16:32:47 +01:00
|
|
|
"<>");
|
1997-09-07 07:04:48 +02:00
|
|
|
prs2_addToRelation(ev_relid, ruleId, event_type, event_attno, TRUE,
|
|
|
|
event_qual, NIL);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
event_qualP = nodeToString(event_qual);
|
|
|
|
actionP = nodeToString(action);
|
|
|
|
|
|
|
|
ruleId = InsertRule(stmt->rulename,
|
|
|
|
event_type,
|
|
|
|
event_obj->relname,
|
|
|
|
eslot_string,
|
|
|
|
event_qualP,
|
|
|
|
is_instead,
|
|
|
|
actionP);
|
|
|
|
|
|
|
|
/* what is the max size of type text? XXX -- glass */
|
|
|
|
if (length(action) > 15)
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "max # of actions exceeded");
|
1997-09-07 07:04:48 +02:00
|
|
|
prs2_addToRelation(ev_relid, ruleId, event_type, event_attno,
|
|
|
|
is_instead, event_qual, action);
|
|
|
|
}
|
|
|
|
}
|