*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.36 1999/09/18 19:07:18 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.37 1999/10/21 01:46:24 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "access/heapam.h"
#include "catalog/pg_rewrite.h"
+#include "lib/stringinfo.h"
#include "parser/parse_relation.h"
#include "rewrite/rewriteDefine.h"
#include "rewrite/rewriteSupport.h"
Oid LastOidProcessed = InvalidOid;
+
+/*
+ * Convert given string to a suitably quoted string constant,
+ * and append it to the StringInfo buffer.
+ * XXX Any MULTIBYTE considerations here?
+ */
static void
-strcpyq(char *dest, char *source)
+quoteString(StringInfo buf, char *source)
{
- char *current = source,
- *destp = dest;
+ char *current;
+ appendStringInfoChar(buf, '\'');
for (current = source; *current; current++)
{
- if (*current == '\"')
+ char ch = *current;
+ if (ch == '\'' || ch == '\\')
{
- *destp = '\\';
- destp++;
+ appendStringInfoChar(buf, '\\');
+ appendStringInfoChar(buf, ch);
}
- *destp = *current;
- destp++;
+ else if (ch >= 0 && ch < ' ')
+ appendStringInfo(buf, "\\%03o", (int) ch);
+ else
+ appendStringInfoChar(buf, ch);
}
- *destp = '\0';
+ appendStringInfoChar(buf, '\'');
}
/*
bool evinstead,
char *actiontree)
{
- static char rulebuf[MaxAttrSize];
- static char actionbuf[MaxAttrSize];
- static char qualbuf[MaxAttrSize];
- Oid eventrel_oid = InvalidOid;
- AttrNumber evslot_index = InvalidAttrNumber;
- Relation eventrel = NULL;
+ StringInfoData rulebuf;
+ Relation eventrel;
+ Oid eventrel_oid;
+ AttrNumber evslot_index;
char *is_instead = "f";
- extern void eval_as_new_xact();
- char *template;
eventrel = heap_openr(evobj, AccessShareLock);
eventrel_oid = RelationGetRelid(eventrel);
if (evslot == NULL)
evslot_index = -1;
else
- evslot_index = attnameAttNum(eventrel, (char *) evslot);
+ evslot_index = attnameAttNum(eventrel, evslot);
heap_close(eventrel, AccessShareLock);
if (evinstead)
if (IsDefinedRewriteRule(rulname))
elog(ERROR, "Attempt to insert rule '%s' failed: already exists",
rulname);
- strcpyq(actionbuf, actiontree);
- strcpyq(qualbuf, evqual);
-
- template = "INSERT INTO pg_rewrite \
-(rulename, ev_type, ev_class, ev_attr, ev_action, ev_qual, is_instead) VALUES \
-('%s', %d::char, %u::oid, %d::int2, '%s'::text, '%s'::text, \
- '%s'::bool);";
- if (MAXALIGN(sizeof(FormData_pg_rewrite)) +
- MAXALIGN(strlen(actionbuf)) +
- MAXALIGN(strlen(qualbuf)) > MaxAttrSize)
- elog(ERROR, "DefineQueryRewrite: rule plan string too big.");
- sprintf(rulebuf, template,
- rulname, evtype, eventrel_oid, evslot_index, actionbuf,
- qualbuf, is_instead);
-
- pg_exec_query_acl_override(rulebuf);
+
+ initStringInfo(&rulebuf);
+ appendStringInfo(&rulebuf,
+ "INSERT INTO pg_rewrite (rulename, ev_type, ev_class, ev_attr, ev_action, ev_qual, is_instead) VALUES (");
+ quoteString(&rulebuf, rulname);
+ appendStringInfo(&rulebuf, ", %d::char, %u::oid, %d::int2, ",
+ evtype, eventrel_oid, evslot_index);
+ quoteString(&rulebuf, actiontree);
+ appendStringInfo(&rulebuf, "::text, ");
+ quoteString(&rulebuf, evqual);
+ appendStringInfo(&rulebuf, "::text, '%s'::bool);",
+ is_instead);
+
+ pg_exec_query_acl_override(rulebuf.data);
+ pfree(rulebuf.data);
return LastOidProcessed;
}