]> granicus.if.org Git - postgresql/blob - src/backend/rewrite/rewriteDefine.c
49ca4526c3c3a0254b37f4f52020993a60d76cec
[postgresql] / src / backend / rewrite / rewriteDefine.c
1 /*-------------------------------------------------------------------------
2  *
3  * rewriteDefine.c
4  *        routines for defining a rewrite rule
5  *
6  * Copyright (c) 1994, Regents of the University of California
7  *
8  *
9  * IDENTIFICATION
10  *        $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.40 1999/11/18 13:56:27 wieck Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14
15 #include "postgres.h"
16
17 #include "access/heapam.h"
18 #include "utils/builtins.h"
19 #include "catalog/pg_rewrite.h"
20 #include "lib/stringinfo.h"
21 #include "parser/parse_relation.h"
22 #include "rewrite/rewriteDefine.h"
23 #include "rewrite/rewriteSupport.h"
24 #include "tcop/tcopprot.h"
25
26 Oid                     LastOidProcessed = InvalidOid;
27
28
29 /*
30  * Convert given string to a suitably quoted string constant,
31  * and append it to the StringInfo buffer.
32  * XXX Any MULTIBYTE considerations here?
33  */
34 static void
35 quoteString(StringInfo buf, char *source)
36 {
37         char       *current;
38
39         appendStringInfoChar(buf, '\'');
40         for (current = source; *current; current++)
41         {
42                 char    ch = *current;
43                 if (ch == '\'' || ch == '\\')
44                 {
45                         appendStringInfoChar(buf, '\\');
46                         appendStringInfoChar(buf, ch);
47                 }
48                 else if (ch >= 0 && ch < ' ')
49                         appendStringInfo(buf, "\\%03o", (int) ch);
50                 else
51                         appendStringInfoChar(buf, ch);
52         }
53         appendStringInfoChar(buf, '\'');
54 }
55
56 /*
57  * InsertRule -
58  *        takes the arguments and inserts them as attributes into the system
59  *        relation "pg_rewrite"
60  *
61  *              MODS :  changes the value of LastOidProcessed as a side
62  *                              effect of inserting the rule tuple
63  *
64  *              ARGS :  rulname                 -               name of the rule
65  *                              evtype                  -               one of RETRIEVE,REPLACE,DELETE,APPEND
66  *                              evobj                   -               name of relation
67  *                              evslot                  -               comma delimited list of slots
68  *                                                                              if null => multi-attr rule
69  *                              evinstead               -               is an instead rule
70  *                              actiontree              -               parsetree(s) of rule action
71  */
72 static Oid
73 InsertRule(char *rulname,
74                    int evtype,
75                    char *evobj,
76                    char *evslot,
77                    char *evqual,
78                    bool evinstead,
79                    char *actiontree)
80 {
81         StringInfoData rulebuf;
82         Relation        eventrel;
83         Oid                     eventrel_oid;
84         AttrNumber      evslot_index;
85         char       *is_instead = "f";
86
87         eventrel = heap_openr(evobj, AccessShareLock);
88         eventrel_oid = RelationGetRelid(eventrel);
89
90         /*
91          * if the slotname is null, we know that this is a multi-attr rule
92          */
93         if (evslot == NULL)
94                 evslot_index = -1;
95         else
96                 evslot_index = attnameAttNum(eventrel, evslot);
97         heap_close(eventrel, AccessShareLock);
98
99         if (evinstead)
100                 is_instead = "t";
101
102         if (evqual == NULL)
103                 evqual = "<>";
104
105         if (IsDefinedRewriteRule(rulname))
106                 elog(ERROR, "Attempt to insert rule '%s' failed: already exists",
107                          rulname);
108
109         initStringInfo(&rulebuf);
110         appendStringInfo(&rulebuf,
111                                          "INSERT INTO pg_rewrite (rulename, ev_type, ev_class, ev_attr, ev_action, ev_qual, is_instead) VALUES (");
112         quoteString(&rulebuf, rulname);
113         appendStringInfo(&rulebuf, ", %d::char, %u::oid, %d::int2, ",
114                                          evtype, eventrel_oid, evslot_index);
115         quoteString(&rulebuf, actiontree);
116         appendStringInfo(&rulebuf, "::text, ");
117         quoteString(&rulebuf, evqual);
118         appendStringInfo(&rulebuf, "::text, '%s'::bool);",
119                                          is_instead);
120
121         pg_exec_query_dest(NameStr(rulebuf), None, true);
122
123         pfree(NameStr(rulebuf));
124
125         return LastOidProcessed;
126 }
127
128 /*
129  *              for now, event_object must be a single attribute
130  */
131 static void
132 ValidateRule(int event_type,
133                          char *eobj_string,
134                          char *eslot_string,
135                          Node *event_qual,
136                          List **action,
137                          int is_instead,
138                          Oid event_attype)
139 {
140         if (((event_type == CMD_INSERT) || (event_type == CMD_DELETE)) &&
141                 eslot_string)
142         {
143                 elog(ERROR,
144                 "rules not allowed for insert or delete events to an attribute");
145         }
146
147 #ifdef NOT_USED
148
149         /*
150          * on retrieve to class.attribute do instead nothing is converted to
151          * 'on retrieve to class.attribute do instead retrieve (attribute =
152          * NULL)' --- this is also a terrible hack that works well -- glass
153          */
154         if (is_instead && !*action && eslot_string && event_type == CMD_SELECT)
155         {
156                 char       *temp_buffer = (char *) palloc(strlen(template) + 80);
157
158                 sprintf(temp_buffer, template, event_attype,
159                                 get_typlen(event_attype), eslot_string,
160                                 event_attype);
161
162                 *action = (List *) stringToNode(temp_buffer);
163
164                 pfree(temp_buffer);
165         }
166 #endif
167 }
168
169 void
170 DefineQueryRewrite(RuleStmt *stmt)
171 {
172         CmdType         event_type = stmt->event;
173         Attr       *event_obj = stmt->object;
174         Node       *event_qual = stmt->whereClause;
175         bool            is_instead = stmt->instead;
176         List       *action = stmt->actions;
177         Relation        event_relation = NULL;
178         Oid                     ruleId;
179         Oid                     ev_relid = 0;
180         char       *eslot_string = NULL;
181         int                     event_attno = 0;
182         Oid                     event_attype = 0;
183         char       *actionP,
184                            *event_qualP;
185         List       *l;
186         Query      *query;
187
188         /* ----------
189          * The current rewrite handler is known to work on relation level
190          * rules only. And for SELECT events, it expects one non-nothing
191          * action that is instead and returns exactly a tuple of the
192          * rewritten relation. This restricts SELECT rules to views.
193          *
194          *         Jan
195          * ----------
196          */
197         if (event_obj->attrs)
198                 elog(ERROR, "attribute level rules currently not supported");
199
200         /*
201          * eslot_string = strVal(lfirst(event_obj->attrs));
202          */
203         else
204                 eslot_string = NULL;
205
206         /*
207          * No rule actions that modify OLD or NEW
208          */
209         if (action != NIL)
210                 foreach(l, action)
211         {
212                 query = (Query *) lfirst(l);
213                 if (query->resultRelation == 1)
214                 {
215                         elog(NOTICE, "rule actions on OLD currently not supported");
216                         elog(ERROR, " use views or triggers instead");
217                 }
218                 if (query->resultRelation == 2)
219                 {
220                         elog(NOTICE, "rule actions on NEW currently not supported");
221                         elog(ERROR, " use triggers instead");
222                 }
223         }
224
225         /*
226          * Rules ON SELECT are restricted to view definitions
227          */
228         if (event_type == CMD_SELECT)
229         {
230                 TargetEntry *tle;
231                 Resdom     *resdom;
232                 Form_pg_attribute attr;
233                 char       *attname;
234                 int                     i;
235                 char            expected_name[NAMEDATALEN + 5];
236
237                 /*
238                  * So there cannot be INSTEAD NOTHING, ...
239                  */
240                 if (length(action) == 0)
241                 {
242                         elog(NOTICE, "instead nothing rules on select currently not supported");
243                         elog(ERROR, " use views instead");
244                 }
245
246                 /*
247                  * ... there cannot be multiple actions, ...
248                  */
249                 if (length(action) > 1)
250                         elog(ERROR, "multiple action rules on select currently not supported");
251
252                 /*
253                  * ... the one action must be a SELECT, ...
254                  */
255                 query = (Query *) lfirst(action);
256                 if (!is_instead || query->commandType != CMD_SELECT)
257                         elog(ERROR, "only instead-select rules currently supported on select");
258                 if (event_qual != NULL)
259                         elog(ERROR, "event qualifications not supported for rules on select");
260
261                 /*
262                  * ... the targetlist of the SELECT action must exactly match the
263                  * event relation, ...
264                  */
265                 event_relation = heap_openr(event_obj->relname, AccessShareLock);
266
267                 if (event_relation->rd_att->natts != length(query->targetList))
268                         elog(ERROR, "select rules target list must match event relations structure");
269
270                 for (i = 1; i <= event_relation->rd_att->natts; i++)
271                 {
272                         tle = (TargetEntry *) nth(i - 1, query->targetList);
273                         resdom = tle->resdom;
274                         attr = event_relation->rd_att->attrs[i - 1];
275                         attname = pstrdup(NameStr(attr->attname));
276
277                         if (strcmp(resdom->resname, attname) != 0)
278                                 elog(ERROR, "select rules target entry %d has different column name from %s", i, attname);
279
280                         if (attr->atttypid != resdom->restype)
281                                 elog(ERROR, "select rules target entry %d has different type from attribute %s", i, attname);
282
283                         if (attr->atttypmod != resdom->restypmod)
284                                 elog(ERROR, "select rules target entry %d has different size from attribute %s", i, attname);
285                 }
286
287                 /*
288                  * ... there must not be another ON SELECT rule already ...
289                  */
290                 if (event_relation->rd_rules != NULL)
291                 {
292                         for (i = 0; i < event_relation->rd_rules->numLocks; i++)
293                         {
294                                 RewriteRule *rule;
295
296                                 rule = event_relation->rd_rules->rules[i];
297                                 if (rule->event == CMD_SELECT)
298                                         elog(ERROR, "%s is already a view",
299                                                 RelationGetRelationName(event_relation));
300                         }
301                 }
302
303                 heap_close(event_relation, AccessShareLock);
304
305                 /*
306                  * LIMIT in view is not supported
307                  */
308                 if (query->limitOffset != NULL || query->limitCount != NULL)
309                         elog(ERROR, "LIMIT clause not supported in views");
310
311                 /*
312                  * DISTINCT on view is not supported
313                  */
314                 if (query->uniqueFlag != NULL)
315                         elog(ERROR, "DISTINCT not supported in views");
316
317                 /*
318                  * ORDER BY in view is not supported
319                  */
320                 if (query->sortClause != NIL)
321                         elog(ERROR, "ORDER BY not supported in views");
322
323                 /*
324                  * ... and finally the rule must be named _RETviewname.
325                  */
326                 sprintf(expected_name, "_RET%s", event_obj->relname);
327                 if (strcmp(expected_name, stmt->rulename) != 0)
328                 {
329                         elog(ERROR, "view rule for %s must be named %s",
330                                  event_obj->relname, expected_name);
331                 }
332         }
333
334         /*
335          * This rule is allowed - install it.
336          */
337
338         event_relation = heap_openr(event_obj->relname, AccessShareLock);
339         ev_relid = RelationGetRelid(event_relation);
340
341         if (eslot_string == NULL)
342         {
343                 event_attno = -1;
344                 event_attype = -1;              /* XXX - don't care */
345         }
346         else
347         {
348                 event_attno = attnameAttNum(event_relation, eslot_string);
349                 event_attype = attnumTypeId(event_relation, event_attno);
350         }
351         heap_close(event_relation, AccessShareLock);
352
353         /* fix bug about instead nothing */
354         ValidateRule(event_type, event_obj->relname,
355                                  eslot_string, event_qual, &action,
356                                  is_instead, event_attype);
357
358         if (action == NULL)
359         {
360                 if (!is_instead)
361                         return;                         /* doesn't do anything */
362
363                 event_qualP = nodeToString(event_qual);
364
365                 ruleId = InsertRule(stmt->rulename,
366                                                         event_type,
367                                                         event_obj->relname,
368                                                         eslot_string,
369                                                         event_qualP,
370                                                         true,
371                                                         "<>");
372                 prs2_addToRelation(ev_relid, ruleId, event_type, event_attno, TRUE,
373                                                    event_qual, NIL);
374
375         }
376         else
377         {
378                 event_qualP = nodeToString(event_qual);
379                 actionP = nodeToString(action);
380
381                 ruleId = InsertRule(stmt->rulename,
382                                                         event_type,
383                                                         event_obj->relname,
384                                                         eslot_string,
385                                                         event_qualP,
386                                                         is_instead,
387                                                         actionP);
388
389                 /* what is the max size of type text? XXX -- glass */
390                 if (length(action) > 15)
391                         elog(ERROR, "max # of actions exceeded");
392                 prs2_addToRelation(ev_relid, ruleId, event_type, event_attno,
393                                                    is_instead, event_qual, action);
394         }
395 }