view.c 7.6 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * view.c
4
 *	  use rewrite rules to construct views
5
 *
B
Add:  
Bruce Momjian 已提交
6 7
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
8
 *
9
 *	$Id: view.c,v 1.45 2000/07/04 06:11:30 tgl Exp $
10 11 12
 *
 *-------------------------------------------------------------------------
 */
M
Marc G. Fournier 已提交
13

14
#include "postgres.h"
M
Marc G. Fournier 已提交
15

16
#include "access/xact.h"
B
Bruce Momjian 已提交
17 18 19
#include "catalog/heap.h"
#include "commands/creatinh.h"
#include "commands/view.h"
20
#include "miscadmin.h"
21
#include "nodes/makefuncs.h"
22 23 24 25 26
#include "parser/parse_relation.h"
#include "parser/parse_type.h"
#include "rewrite/rewriteDefine.h"
#include "rewrite/rewriteManip.h"
#include "rewrite/rewriteRemove.h"
27 28 29 30 31 32 33 34 35 36 37 38 39

/*---------------------------------------------------------------------
 * DefineVirtualRelation
 *
 * Create the "view" relation.
 * `DefineRelation' does all the work, we just provide the correct
 * arguments!
 *
 * If the relation already exists, then 'DefineRelation' will abort
 * the xact...
 *---------------------------------------------------------------------
 */
static void
40
DefineVirtualRelation(char *relname, List *tlist)
41
{
42 43 44 45 46 47 48
	CreateStmt	createStmt;
	List	   *attrList,
			   *t;
	TargetEntry *entry;
	Resdom	   *res;
	char	   *resname;
	char	   *restypename;
49 50 51 52 53 54 55 56 57 58 59 60

	/*
	 * create a list with one entry per attribute of this relation. Each
	 * entry is a two element list. The first element is the name of the
	 * attribute (a string) and the second the name of the type (NOTE: a
	 * string, not a type id!).
	 */
	attrList = NIL;
	if (tlist != NIL)
	{
		foreach(t, tlist)
		{
61 62
			ColumnDef  *def = makeNode(ColumnDef);
			TypeName   *typename;
63 64 65 66 67 68 69

			/*
			 * find the names of the attribute & its type
			 */
			entry = lfirst(t);
			res = entry->resdom;
			resname = res->resname;
70
			restypename = typeidTypeName(res->restype);
71 72 73 74

			typename = makeNode(TypeName);

			typename->name = pstrdup(restypename);
75 76
			typename->typmod = res->restypmod;

77 78 79 80 81
			def->colname = pstrdup(resname);

			def->typename = typename;

			def->is_not_null = false;
82 83
			def->raw_default = NULL;
			def->cooked_default = NULL;
84 85 86

			attrList = lappend(attrList, def);
		}
87
	}
88
	else
89
		elog(ERROR, "attempted to define virtual relation with no attrs");
90 91 92 93 94 95

	/*
	 * now create the parametesr for keys/inheritance etc. All of them are
	 * nil...
	 */
	createStmt.relname = relname;
96
	createStmt.istemp = false;
97 98 99 100 101 102 103 104
	createStmt.tableElts = attrList;
/*	  createStmt.tableType = NULL;*/
	createStmt.inhRelnames = NIL;
	createStmt.constraints = NIL;

	/*
	 * finally create the relation...
	 */
105
	DefineRelation(&createStmt, RELKIND_RELATION);
106
}
107 108 109 110 111 112 113 114

/*------------------------------------------------------------------
 * makeViewRetrieveRuleName
 *
 * Given a view name, returns the name for the 'on retrieve to "view"'
 * rule.
 *------------------------------------------------------------------
 */
115
char *
116 117
MakeRetrieveViewRuleName(char *viewName)
{
118
	char	   *buf;
119 120

	buf = palloc(strlen(viewName) + 5);
M
 
Marc G. Fournier 已提交
121
	snprintf(buf, strlen(viewName) + 5, "_RET%s", viewName);
122

123
	return buf;
124 125 126
}

static RuleStmt *
127
FormViewRetrieveRule(char *viewName, Query *viewParse)
128
{
129 130 131
	RuleStmt   *rule;
	char	   *rname;
	Attr	   *attr;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

	/*
	 * Create a RuleStmt that corresponds to the suitable rewrite rule
	 * args for DefineQueryRewrite();
	 */
	rule = makeNode(RuleStmt);
	rname = MakeRetrieveViewRuleName(viewName);

	attr = makeNode(Attr);
	attr->relname = pstrdup(viewName);
/*	  attr->refname = pstrdup(viewName);*/
	rule->rulename = pstrdup(rname);
	rule->whereClause = NULL;
	rule->event = CMD_SELECT;
	rule->object = attr;
	rule->instead = true;
	rule->actions = lcons(viewParse, NIL);

	return rule;
151 152 153
}

static void
154
DefineViewRules(char *viewName, Query *viewParse)
155
{
156
	RuleStmt   *retrieve_rule = NULL;
157

158
#ifdef NOTYET
159 160 161
	RuleStmt   *replace_rule = NULL;
	RuleStmt   *append_rule = NULL;
	RuleStmt   *delete_rule = NULL;
162

163
#endif
164

165
	retrieve_rule = FormViewRetrieveRule(viewName, viewParse);
166

167
#ifdef NOTYET
168

169 170 171
	replace_rule = FormViewReplaceRule(viewName, viewParse);
	append_rule = FormViewAppendRule(viewName, viewParse);
	delete_rule = FormViewDeleteRule(viewName, viewParse);
172

173
#endif
174 175

	DefineQueryRewrite(retrieve_rule);
176 177

#ifdef NOTYET
178 179 180
	DefineQueryRewrite(replace_rule);
	DefineQueryRewrite(append_rule);
	DefineQueryRewrite(delete_rule);
181
#endif
182 183

}
184 185 186 187 188 189 190 191

/*---------------------------------------------------------------
 * UpdateRangeTableOfViewParse
 *
 * Update the range table of the given parsetree.
 * This update consists of adding two new entries IN THE BEGINNING
 * of the range table (otherwise the rule system will die a slow,
 * horrible and painful death, and we do not want that now, do we?)
192
 * one for the OLD relation and one for the NEW one (both of
193 194 195 196 197 198 199 200 201 202 203
 * them refer in fact to the "view" relation).
 *
 * Of course we must also increase the 'varnos' of all the Var nodes
 * by 2...
 *
 * NOTE: these are destructive changes. It would be difficult to
 * make a complete copy of the parse tree and make the changes
 * in the copy.
 *---------------------------------------------------------------
 */
static void
204
UpdateRangeTableOfViewParse(char *viewName, Query *viewParse)
205
{
206 207 208 209
	List	   *old_rt;
	List	   *new_rt;
	RangeTblEntry *rt_entry1,
			   *rt_entry2;
210 211 212 213

	/*
	 * first offset all var nodes by 2
	 */
B
Bruce Momjian 已提交
214 215
	OffsetVarNodes((Node *) viewParse->targetList, 2, 0);
	OffsetVarNodes(viewParse->qual, 2, 0);
216

B
Bruce Momjian 已提交
217
	OffsetVarNodes(viewParse->havingQual, 2, 0);
218

219

220 221 222 223 224 225 226
	/*
	 * find the old range table...
	 */
	old_rt = viewParse->rtable;

	/*
	 * create the 2 new range table entries and form the new range
227
	 * table... OLD first, then NEW....
228
	 */
229
	rt_entry1 = addRangeTableEntry(NULL, (char *) viewName,
230
								   makeAttr("*OLD*", NULL),
231
								   FALSE, FALSE, FALSE);
232 233
	rt_entry2 = addRangeTableEntry(NULL, (char *) viewName,
								   makeAttr("*NEW*", NULL),
234
								   FALSE, FALSE, FALSE);
235 236 237 238 239 240 241 242
	new_rt = lcons(rt_entry2, old_rt);
	new_rt = lcons(rt_entry1, new_rt);

	/*
	 * Now the tricky part.... Update the range table in place... Be
	 * careful here, or hell breaks loooooooooooooOOOOOOOOOOOOOOOOOOSE!
	 */
	viewParse->rtable = new_rt;
243 244 245 246 247
}

/*-------------------------------------------------------------------
 * DefineView
 *
248 249 250 251 252 253 254
 *		- takes a "viewname", "parsetree" pair and then
 *		1)		construct the "virtual" relation
 *		2)		commit the command but NOT the transaction,
 *				so that the relation exists
 *				before the rules are defined.
 *		2)		define the "n" rules specified in the PRS2 paper
 *				over the "virtual" relation
255 256 257
 *-------------------------------------------------------------------
 */
void
258
DefineView(char *viewName, Query *viewParse)
259
{
260
	List	   *viewTlist;
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

	viewTlist = viewParse->targetList;

	/*
	 * Create the "view" relation NOTE: if it already exists, the xaxt
	 * will be aborted.
	 */
	DefineVirtualRelation(viewName, viewTlist);

	/*
	 * The relation we have just created is not visible to any other
	 * commands running with the same transaction & command id. So,
	 * increment the command id counter (but do NOT pfree any memory!!!!)
	 */
	CommandCounterIncrement();

	/*
	 * The range table of 'viewParse' does not contain entries for the
279
	 * "OLD" and "NEW" relations. So... add them! NOTE: we make the
280 281 282 283 284
	 * update in place! After this call 'viewParse' will never be what it
	 * used to be...
	 */
	UpdateRangeTableOfViewParse(viewName, viewParse);
	DefineViewRules(viewName, viewParse);
285 286 287 288 289 290 291 292 293 294 295
}

/*------------------------------------------------------------------
 * RemoveView
 *
 * Remove a view given its name
 *------------------------------------------------------------------
 */
void
RemoveView(char *viewName)
{
296
	/*
297 298
	 * We just have to drop the relation; the associated rules will
	 * be cleaned up automatically.
299
	 */
300
	heap_drop_with_catalog(viewName, allowSystemTableMods);
301
}