char *ev_qual;
char *ev_action;
List *actions = NIL;
+ Relation ev_relation;
+ TupleDesc viewResultDesc = NULL;
int fno;
Datum dat;
bool isnull;
if (ev_action != NULL)
actions = (List *) stringToNode(ev_action);
+ ev_relation = heap_open(ev_class, AccessShareLock);
+
/*
* Build the rules definition text
*/
{
case '1':
appendStringInfoString(buf, "SELECT");
+ viewResultDesc = RelationGetDescr(ev_relation);
break;
case '2':
foreach(action, actions)
{
query = (Query *) lfirst(action);
- get_query_def(query, buf, NIL, NULL,
+ get_query_def(query, buf, NIL, viewResultDesc,
prettyFlags, WRAP_COLUMN_DEFAULT, 0);
if (prettyFlags)
appendStringInfoString(buf, ";\n");
Query *query;
query = (Query *) linitial(actions);
- get_query_def(query, buf, NIL, NULL,
+ get_query_def(query, buf, NIL, viewResultDesc,
prettyFlags, WRAP_COLUMN_DEFAULT, 0);
appendStringInfoChar(buf, ';');
}
+
+ heap_close(ev_relation, AccessShareLock);
}
List *actions = NIL;
Relation ev_relation;
int fno;
+ Datum dat;
bool isnull;
/*
* Get the attribute values from the rules tuple
*/
fno = SPI_fnumber(rulettc, "ev_type");
- ev_type = (char) SPI_getbinval(ruletup, rulettc, fno, &isnull);
+ dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
+ Assert(!isnull);
+ ev_type = DatumGetChar(dat);
fno = SPI_fnumber(rulettc, "ev_class");
- ev_class = (Oid) SPI_getbinval(ruletup, rulettc, fno, &isnull);
+ dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
+ Assert(!isnull);
+ ev_class = DatumGetObjectId(dat);
fno = SPI_fnumber(rulettc, "is_instead");
- is_instead = (bool) SPI_getbinval(ruletup, rulettc, fno, &isnull);
+ dat = SPI_getbinval(ruletup, rulettc, fno, &isnull);
+ Assert(!isnull);
+ is_instead = DatumGetBool(dat);
+ /* these could be nulls */
fno = SPI_fnumber(rulettc, "ev_qual");
ev_qual = SPI_getvalue(ruletup, rulettc, fno);
LEFT JOIN tt6 ON TRUE;
(1 row)
+-- check handling of views with immediately-renamed columns
+create view tt23v (col_a, col_b) as
+select q1 as other_name1, q2 as other_name2 from int8_tbl
+union
+select 42, 43;
+select pg_get_viewdef('tt23v', true);
+ pg_get_viewdef
+-------------------------------
+ SELECT int8_tbl.q1 AS col_a,+
+ int8_tbl.q2 AS col_b +
+ FROM int8_tbl +
+ UNION +
+ SELECT 42 AS col_a, +
+ 43 AS col_b;
+(1 row)
+
+select pg_get_ruledef(oid, true) from pg_rewrite
+ where ev_class = 'tt23v'::regclass and ev_type = '1';
+ pg_get_ruledef
+-----------------------------------------------------------------
+ CREATE RULE "_RETURN" AS +
+ ON SELECT TO tt23v DO INSTEAD SELECT int8_tbl.q1 AS col_a,+
+ int8_tbl.q2 AS col_b +
+ FROM int8_tbl +
+ UNION +
+ SELECT 42 AS col_a, +
+ 43 AS col_b;
+(1 row)
+
-- clean up all the random objects we made above
set client_min_messages = warning;
DROP SCHEMA temp_view_test CASCADE;
select * from tt5 natural left join tt6;
select pg_get_viewdef('tt22v', true);
+-- check handling of views with immediately-renamed columns
+
+create view tt23v (col_a, col_b) as
+select q1 as other_name1, q2 as other_name2 from int8_tbl
+union
+select 42, 43;
+
+select pg_get_viewdef('tt23v', true);
+select pg_get_ruledef(oid, true) from pg_rewrite
+ where ev_class = 'tt23v'::regclass and ev_type = '1';
+
-- clean up all the random objects we made above
set client_min_messages = warning;
DROP SCHEMA temp_view_test CASCADE;