7
7
*
8
8
*
9
9
* 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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
16
16
17
17
#include "access/heapam.h"
18
18
#include "catalog/pg_rewrite.h"
19
+ #include "lib/stringinfo.h"
19
20
#include "parser/parse_relation.h"
20
21
#include "rewrite/rewriteDefine.h"
21
22
#include "rewrite/rewriteSupport.h"
24
25
25
26
Oid LastOidProcessed = InvalidOid ;
26
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
+ */
27
34
static void
28
- strcpyq ( char * dest , char * source )
35
+ quoteString ( StringInfo buf , char * source )
29
36
{
30
- char * current = source ,
31
- * destp = dest ;
37
+ char * current ;
32
38
39
+ appendStringInfoChar (buf , '\'' );
33
40
for (current = source ; * current ; current ++ )
34
41
{
35
- if (* current == '\"' )
42
+ char ch = * current ;
43
+ if (ch == '\'' || ch == '\\' )
36
44
{
37
- * destp = '\\' ;
38
- destp ++ ;
45
+ appendStringInfoChar ( buf , '\\' ) ;
46
+ appendStringInfoChar ( buf , ch ) ;
39
47
}
40
- * destp = * current ;
41
- destp ++ ;
48
+ else if (ch >= 0 && ch < ' ' )
49
+ appendStringInfo (buf , "\\%03o" , (int ) ch );
50
+ else
51
+ appendStringInfoChar (buf , ch );
42
52
}
43
- * destp = '\0' ;
53
+ appendStringInfoChar ( buf , '\'' ) ;
44
54
}
45
55
46
56
/*
@@ -68,15 +78,11 @@ InsertRule(char *rulname,
68
78
bool evinstead ,
69
79
char * actiontree )
70
80
{
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 ;
77
85
char * is_instead = "f" ;
78
- extern void eval_as_new_xact ();
79
- char * template ;
80
86
81
87
eventrel = heap_openr (evobj , AccessShareLock );
82
88
eventrel_oid = RelationGetRelid (eventrel );
@@ -87,7 +93,7 @@ InsertRule(char *rulname,
87
93
if (evslot == NULL )
88
94
evslot_index = -1 ;
89
95
else
90
- evslot_index = attnameAttNum (eventrel , ( char * ) evslot );
96
+ evslot_index = attnameAttNum (eventrel , evslot );
91
97
heap_close (eventrel , AccessShareLock );
92
98
93
99
if (evinstead )
@@ -99,22 +105,21 @@ InsertRule(char *rulname,
99
105
if (IsDefinedRewriteRule (rulname ))
100
106
elog (ERROR , "Attempt to insert rule '%s' failed: already exists" ,
101
107
rulname );
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 );
118
123
119
124
return LastOidProcessed ;
120
125
}
0 commit comments