77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.36 1999/09/18 19:07:18 tgl Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.37 1999/10/21 01:46:24 tgl Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
1616
1717#include "access/heapam.h"
1818#include "catalog/pg_rewrite.h"
19+ #include "lib/stringinfo.h"
1920#include "parser/parse_relation.h"
2021#include "rewrite/rewriteDefine.h"
2122#include "rewrite/rewriteSupport.h"
2425
2526Oid LastOidProcessed = InvalidOid ;
2627
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+ */
2734static void
28- strcpyq ( char * dest ,char * source )
35+ quoteString ( StringInfo buf ,char * source )
2936{
30- char * current = source ,
31- * destp = dest ;
37+ char * current ;
3238
39+ appendStringInfoChar (buf ,'\'' );
3340for (current = source ;* current ;current ++ )
3441{
35- if (* current == '\"' )
42+ char ch = * current ;
43+ if (ch == '\'' || ch == '\\' )
3644{
37- * destp = '\\' ;
38- destp ++ ;
45+ appendStringInfoChar ( buf , '\\' ) ;
46+ appendStringInfoChar ( buf , ch ) ;
3947}
40- * destp = * current ;
41- destp ++ ;
48+ else if (ch >=0 && ch < ' ' )
49+ appendStringInfo (buf ,"\\%03o" , (int )ch );
50+ else
51+ appendStringInfoChar (buf ,ch );
4252}
43- * destp = '\0' ;
53+ appendStringInfoChar ( buf , '\'' ) ;
4454}
4555
4656/*
@@ -68,15 +78,11 @@ InsertRule(char *rulname,
6878bool evinstead ,
6979char * actiontree )
7080{
71- static char rulebuf [MaxAttrSize ];
72- static char actionbuf [MaxAttrSize ];
73- static char qualbuf [MaxAttrSize ];
74- Oid eventrel_oid = InvalidOid ;
75- AttrNumber evslot_index = InvalidAttrNumber ;
76- Relation eventrel = NULL ;
81+ StringInfoData rulebuf ;
82+ Relation eventrel ;
83+ Oid eventrel_oid ;
84+ AttrNumber evslot_index ;
7785char * is_instead = "f" ;
78- extern void eval_as_new_xact ();
79- char * template ;
8086
8187eventrel = heap_openr (evobj ,AccessShareLock );
8288eventrel_oid = RelationGetRelid (eventrel );
@@ -87,7 +93,7 @@ InsertRule(char *rulname,
8793if (evslot == NULL )
8894evslot_index = -1 ;
8995else
90- evslot_index = attnameAttNum (eventrel ,( char * ) evslot );
96+ evslot_index = attnameAttNum (eventrel ,evslot );
9197heap_close (eventrel ,AccessShareLock );
9298
9399if (evinstead )
@@ -99,22 +105,21 @@ InsertRule(char *rulname,
99105if (IsDefinedRewriteRule (rulname ))
100106elog (ERROR ,"Attempt to insert rule '%s' failed: already exists" ,
101107rulname );
102- strcpyq (actionbuf ,actiontree );
103- strcpyq (qualbuf ,evqual );
104-
105- template = "INSERT INTO pg_rewrite \
106- (rulename, ev_type, ev_class, ev_attr, ev_action, ev_qual, is_instead) VALUES \
107- ('%s', %d::char, %u::oid, %d::int2, '%s'::text, '%s'::text, \
108- '%s'::bool);" ;
109- if (MAXALIGN (sizeof (FormData_pg_rewrite ))+
110- MAXALIGN (strlen (actionbuf ))+
111- MAXALIGN (strlen (qualbuf ))> MaxAttrSize )
112- elog (ERROR ,"DefineQueryRewrite: rule plan string too big." );
113- sprintf (rulebuf ,template ,
114- rulname ,evtype ,eventrel_oid ,evslot_index ,actionbuf ,
115- qualbuf ,is_instead );
116-
117- pg_exec_query_acl_override (rulebuf );
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_acl_override (rulebuf .data );
122+ pfree (rulebuf .data );
118123
119124return LastOidProcessed ;
120125}