Commit 7a846ecc authored by Bruce Momjian's avatar Bruce Momjian

Use E'' strings internally only when standard_conforming_strings =

'off'. This allows pg_dump output with standard_conforming_strings =
'on' to generate proper strings that can be loaded into other databases
without the backslash doubling we typically do.  I have added the
dumping of the standard_conforming_strings value to pg_dump.

I also added standard backslash handling for plpgsql.
parent 4d63e267
......@@ -7,13 +7,14 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.18 2006/03/05 15:58:43 momjian Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.19 2006/05/26 23:48:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "utils/builtins.h"
#include "parser/gramparse.h"
/*
......@@ -65,19 +66,20 @@ quote_literal(PG_FUNCTION_ARGS)
cp1 = VARDATA(t);
cp2 = VARDATA(result);
for (; len-- > 0; cp1++)
if (*cp1 == '\\')
{
*cp2++ = ESCAPE_STRING_SYNTAX;
break;
}
if (!standard_conforming_strings)
for (; len-- > 0; cp1++)
if (*cp1 == '\\')
{
*cp2++ = ESCAPE_STRING_SYNTAX;
break;
}
len = VARSIZE(t) - VARHDRSZ;
cp1 = VARDATA(t);
*cp2++ = '\'';
while (len-- > 0)
{
if (SQL_STR_DOUBLE(*cp1))
if (SQL_STR_DOUBLE(*cp1, !standard_conforming_strings))
*cp2++ = *cp1;
*cp2++ = *cp1++;
}
......
......@@ -2,7 +2,7 @@
* ruleutils.c - Functions to convert stored expressions/querytrees
* back to source text
*
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.221 2006/04/30 18:30:40 tgl Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.222 2006/05/26 23:48:54 momjian Exp $
**********************************************************************/
#include "postgres.h"
......@@ -31,6 +31,7 @@
#include "nodes/makefuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/tlist.h"
#include "parser/gramparse.h"
#include "parser/keywords.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h"
......@@ -533,13 +534,13 @@ pg_get_triggerdef(PG_FUNCTION_ARGS)
{
if (i > 0)
appendStringInfo(&buf, ", ");
if (strchr(p, '\\') != NULL)
if (!standard_conforming_strings && strchr(p, '\\') != NULL)
appendStringInfoChar(&buf, ESCAPE_STRING_SYNTAX);
appendStringInfoChar(&buf, '\'');
while (*p)
{
if (SQL_STR_DOUBLE(*p))
if (SQL_STR_DOUBLE(*p, !standard_conforming_strings))
appendStringInfoChar(&buf, *p);
appendStringInfoChar(&buf, *p++);
}
......@@ -3882,7 +3883,8 @@ get_const_expr(Const *constval, deparse_context *context)
char *valptr;
bool isfloat = false;
bool needlabel;
bool is_e_string = false;
if (constval->constisnull)
{
/*
......@@ -3948,10 +3950,11 @@ get_const_expr(Const *constval, deparse_context *context)
* representation. XXX Any MULTIBYTE considerations here?
*/
for (valptr = extval; *valptr; valptr++)
if (*valptr == '\\' ||
if ((!standard_conforming_strings && *valptr == '\\') ||
(unsigned char) *valptr < (unsigned char) ' ')
{
appendStringInfoChar(buf, ESCAPE_STRING_SYNTAX);
is_e_string = true;
break;
}
......@@ -3960,7 +3963,7 @@ get_const_expr(Const *constval, deparse_context *context)
{
char ch = *valptr;
if (SQL_STR_DOUBLE(ch))
if (SQL_STR_DOUBLE(ch, is_e_string))
{
appendStringInfoChar(buf, ch);
appendStringInfoChar(buf, ch);
......
......@@ -42,7 +42,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
* Portions taken from FreeBSD.
*
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.114 2006/03/21 17:54:28 alvherre Exp $
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.115 2006/05/26 23:48:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -2103,7 +2103,8 @@ check_ok(void)
}
/*
* Escape any single quotes or backslashes in given string
* Escape any single quotes or backslashes in given string;
* postgresql.conf always enables backslash escapes
*/
static char *
escape_quotes(const char *src)
......@@ -2115,7 +2116,7 @@ escape_quotes(const char *src)
for (i = 0, j = 0; i < len; i++)
{
if (SQL_STR_DOUBLE(src[i]))
if (SQL_STR_DOUBLE(src[i], true))
result[j++] = src[i];
result[j++] = src[i];
}
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.27 2006/04/30 21:15:33 tgl Exp $
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.28 2006/05/26 23:48:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -105,24 +105,29 @@ fmtId(const char *rawid)
* Special characters are escaped. Quote mark ' goes to '' per SQL
* standard, other stuff goes to \ sequences. If escapeAll is false,
* whitespace characters are not escaped (tabs, newlines, etc.). This
* is appropriate for dump file output.
* is appropriate for dump file output. Using E'' strings for
* backslashes is always safe for standard_conforming_strings on or off.
*/
void
appendStringLiteral(PQExpBuffer buf, const char *str, bool escapeAll)
appendStringLiteral(PQExpBuffer buf, const char *str, bool escapeAll,
bool e_string_for_backslash)
{
char ch;
const char *p;
bool is_e_string = false;
for (p = str; *p; p++)
{
ch = *p;
if (ch == '\\' ||
if ((e_string_for_backslash && ch == '\\') ||
((unsigned char) ch < (unsigned char) ' ' &&
(escapeAll ||
(ch != '\t' && ch != '\n' && ch != '\v' &&
ch != '\f' && ch != '\r'))))
{
appendPQExpBufferChar(buf, ESCAPE_STRING_SYNTAX);
is_e_string = true;
break;
}
}
......@@ -131,7 +136,7 @@ appendStringLiteral(PQExpBuffer buf, const char *str, bool escapeAll)
for (p = str; *p; p++)
{
ch = *p;
if (SQL_STR_DOUBLE(ch))
if (SQL_STR_DOUBLE(ch, is_e_string))
{
appendPQExpBufferChar(buf, ch);
appendPQExpBufferChar(buf, ch);
......@@ -208,7 +213,7 @@ appendStringLiteralDQOpt(PQExpBuffer buf, const char *str,
bool escapeAll, const char *dqprefix)
{
if (strchr(str, '\'') == NULL && strchr(str, '\\') == NULL)
appendStringLiteral(buf, str, escapeAll);
appendStringLiteral(buf, str, escapeAll, true);
else
appendStringLiteralDQ(buf, str, dqprefix);
}
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.h,v 1.15 2006/03/05 15:58:50 momjian Exp $
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.h,v 1.16 2006/05/26 23:48:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -17,10 +17,9 @@
#include "pqexpbuffer.h"
extern const char *fmtId(const char *identifier);
extern void appendStringLiteral(PQExpBuffer buf, const char *str,
bool escapeAll);
bool escapeAll, bool e_string_for_backslash);
extern void appendStringLiteralDQ(PQExpBuffer buf, const char *str,
const char *dqprefix);
extern void appendStringLiteralDQOpt(PQExpBuffer buf, const char *str,
......
......@@ -15,7 +15,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.129 2006/05/24 21:20:11 tgl Exp $
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.130 2006/05/26 23:48:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -1900,9 +1900,11 @@ _tocEntryRequired(TocEntry *te, RestoreOptions *ropt, bool include_acls)
{
teReqs res = REQ_ALL;
/* ENCODING objects are dumped specially, so always reject here */
/* ENCODING and STDSTRINGS objects are dumped specially, so always reject */
if (strcmp(te->desc, "ENCODING") == 0)
return 0;
if (strcmp(te->desc, "STDSTRINGS") == 0)
return 0;
/* If it's an ACL, maybe ignore it */
if ((!include_acls || ropt->aclsSkip) && strcmp(te->desc, "ACL") == 0)
......@@ -2005,15 +2007,14 @@ _doSetFixedOutputState(ArchiveHandle *AH)
{
TocEntry *te;
/* If we have an encoding setting, emit that */
/* If we have an encoding or std_strings setting, emit that */
te = AH->toc->next;
while (te != AH->toc)
{
if (strcmp(te->desc, "ENCODING") == 0)
{
ahprintf(AH, "%s", te->defn);
break;
}
if (strcmp(te->desc, "STDSTRINGS") == 0)
ahprintf(AH, "%s", te->defn);
te = te->next;
}
......@@ -2042,7 +2043,7 @@ _doSetSessionAuth(ArchiveHandle *AH, const char *user)
* SQL requires a string literal here. Might as well be correct.
*/
if (user && *user)
appendStringLiteral(cmd, user, false);
appendStringLiteral(cmd, user, false, true);
else
appendPQExpBuffer(cmd, "DEFAULT");
appendPQExpBuffer(cmd, ";");
......
This diff is collapsed.
......@@ -6,7 +6,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.75 2006/05/22 11:21:54 petere Exp $
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.76 2006/05/26 23:48:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -535,7 +535,7 @@ dumpRoles(PGconn *conn)
if (!PQgetisnull(res, i, i_rolpassword))
{
appendPQExpBuffer(buf, " PASSWORD ");
appendStringLiteral(buf, PQgetvalue(res, i, i_rolpassword), true);
appendStringLiteral(buf, PQgetvalue(res, i, i_rolpassword), true, true);
}
if (!PQgetisnull(res, i, i_rolvaliduntil))
......@@ -546,7 +546,7 @@ dumpRoles(PGconn *conn)
if (!PQgetisnull(res, i, i_rolcomment)) {
appendPQExpBuffer(buf, "COMMENT ON ROLE %s IS ", fmtId(rolename));
appendStringLiteral(buf, PQgetvalue(res, i, i_rolcomment), true);
appendStringLiteral(buf, PQgetvalue(res, i, i_rolcomment), true, true);
appendPQExpBuffer(buf, ";\n");
}
......@@ -730,7 +730,7 @@ dumpTablespaces(PGconn *conn)
appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
appendPQExpBuffer(buf, " LOCATION ");
appendStringLiteral(buf, spclocation, true);
appendStringLiteral(buf, spclocation, true, true);
appendPQExpBuffer(buf, ";\n");
if (!skip_acls &&
......@@ -745,7 +745,7 @@ dumpTablespaces(PGconn *conn)
if (spccomment && strlen(spccomment)) {
appendPQExpBuffer(buf, "COMMENT ON TABLESPACE %s IS ", fspcname);
appendStringLiteral(buf, spccomment, true);
appendStringLiteral(buf, spccomment, true, true);
appendPQExpBuffer(buf, ";\n");
}
......@@ -868,7 +868,7 @@ dumpCreateDB(PGconn *conn)
appendPQExpBuffer(buf, " OWNER = %s", fmtId(dbowner));
appendPQExpBuffer(buf, " ENCODING = ");
appendStringLiteral(buf, dbencoding, true);
appendStringLiteral(buf, dbencoding, true, true);
/* Output tablespace if it isn't default */
if (strcmp(dbtablespace, "pg_default") != 0)
......@@ -884,7 +884,7 @@ dumpCreateDB(PGconn *conn)
if (strcmp(dbistemplate, "t") == 0)
{
appendPQExpBuffer(buf, "UPDATE pg_database SET datistemplate = 't' WHERE datname = ");
appendStringLiteral(buf, dbname, true);
appendStringLiteral(buf, dbname, true, true);
appendPQExpBuffer(buf, ";\n");
}
}
......@@ -929,7 +929,7 @@ dumpDatabaseConfig(PGconn *conn, const char *dbname)
PGresult *res;
printfPQExpBuffer(buf, "SELECT datconfig[%d] FROM pg_database WHERE datname = ", count);
appendStringLiteral(buf, dbname, true);
appendStringLiteral(buf, dbname, true, true);
appendPQExpBuffer(buf, ";");
res = executeQuery(conn, buf->data);
......@@ -968,7 +968,7 @@ dumpUserConfig(PGconn *conn, const char *username)
printfPQExpBuffer(buf, "SELECT rolconfig[%d] FROM pg_authid WHERE rolname = ", count);
else
printfPQExpBuffer(buf, "SELECT useconfig[%d] FROM pg_shadow WHERE usename = ", count);
appendStringLiteral(buf, username, true);
appendStringLiteral(buf, username, true, true);
res = executeQuery(conn, buf->data);
if (PQntuples(res) == 1 &&
......@@ -1016,7 +1016,7 @@ makeAlterConfigCommand(const char *arrayitem, const char *type, const char *name
|| pg_strcasecmp(mine, "search_path") == 0)
appendPQExpBuffer(buf, "%s", pos + 1);
else
appendStringLiteral(buf, pos + 1, false);
appendStringLiteral(buf, pos + 1, false, true);
appendPQExpBuffer(buf, ";\n");
printf("%s", buf->data);
......
......@@ -3,7 +3,7 @@
*
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
*
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.134 2006/04/26 23:15:45 momjian Exp $
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.135 2006/05/26 23:48:54 momjian Exp $
*/
#include "postgres_fe.h"
#include "describe.h"
......@@ -1867,7 +1867,7 @@ processNamePattern(PQExpBuffer buf, const char *pattern,
appendPQExpBuffer(&namebuf, "\\\\");
/* Ensure chars special to string literals are passed properly */
if (SQL_STR_DOUBLE(*cp))
if (SQL_STR_DOUBLE(*cp, true))
appendPQExpBufferChar(&namebuf, *cp);
i = PQmblen(cp, pset.encoding);
......
......@@ -3,7 +3,7 @@
*
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
*
* $PostgreSQL: pgsql/src/bin/psql/large_obj.c,v 1.41 2006/03/05 15:58:51 momjian Exp $
* $PostgreSQL: pgsql/src/bin/psql/large_obj.c,v 1.42 2006/05/26 23:48:54 momjian Exp $
*/
#include "postgres_fe.h"
#include "large_obj.h"
......@@ -178,10 +178,11 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
if (strchr(comment_arg, '\\') != NULL)
*bufptr++ = ESCAPE_STRING_SYNTAX;
*bufptr++ = '\'';
for (i = 0; i < slen; i++)
{
if (SQL_STR_DOUBLE(comment_arg[i]))
if (SQL_STR_DOUBLE(comment_arg[i], true))
*bufptr++ = comment_arg[i];
*bufptr++ = comment_arg[i];
}
......
......@@ -5,7 +5,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/bin/scripts/createdb.c,v 1.16 2006/03/05 15:58:52 momjian Exp $
* $PostgreSQL: pgsql/src/bin/scripts/createdb.c,v 1.17 2006/05/26 23:48:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -184,7 +184,7 @@ main(int argc, char *argv[])
if (comment)
{
printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
appendStringLiteral(&sql, comment, false);
appendStringLiteral(&sql, comment, false, true);
appendPQExpBuffer(&sql, ";\n");
conn = connectDatabase(dbname, host, port, username, password, progname);
......
......@@ -5,7 +5,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.27 2006/03/05 15:58:52 momjian Exp $
* $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.28 2006/05/26 23:48:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -258,11 +258,11 @@ main(int argc, char *argv[])
fprintf(stderr, _("Password encryption failed.\n"));
exit(1);
}
appendStringLiteral(&sql, encrypted_password, false);
appendStringLiteral(&sql, encrypted_password, false, true);
PQfreemem(encrypted_password);
}
else
appendStringLiteral(&sql, newpassword, false);
appendStringLiteral(&sql, newpassword, false, true);
}
if (superuser == TRI_YES)
appendPQExpBuffer(&sql, " SUPERUSER");
......
......@@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/c.h,v 1.200 2006/04/14 03:38:56 tgl Exp $
* $PostgreSQL: pgsql/src/include/c.h,v 1.201 2006/05/26 23:48:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -472,7 +472,14 @@ typedef NameData *Name;
#define NameStr(name) ((name).data)
#define SQL_STR_DOUBLE(ch) ((ch) == '\'' || (ch) == '\\')
/*
* In 8.2, we are warning for \ in a non-E string if std_strings are off.
* For this reason, we use E for \ strings, unless standard_conforming_strings
* is on.
*/
#define SQL_STR_DOUBLE(ch, escape_backslash) \
((ch) == '\'' || ((escape_backslash) && (ch) == '\\'))
#define ESCAPE_STRING_SYNTAX 'E'
/* ----------------------------------------------------------------
......
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.45 2006/04/24 09:45:22 meskes Exp $ */
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.46 2006/05/26 23:48:54 momjian Exp $ */
/*
* The aim is to get a simpler inteface to the database routines.
......@@ -32,8 +32,9 @@
#include "pgtypes_timestamp.h"
#include "pgtypes_interval.h"
/* This function returns a newly malloced string that has the \
in the argument quoted with \ and the ' quoted with ' as SQL92 says.
/*
* This function returns a newly malloced string that has ' and \
* escaped.
*/
static char *
quote_postgres(char *arg, int lineno)
......@@ -45,13 +46,17 @@ quote_postgres(char *arg, int lineno)
if (!res)
return (res);
/*
* We don't know if the target database is using
* standard_conforming_strings, so we always use E'' strings.
*/
if (strchr(arg, '\\') != NULL)
res[ri++] = ESCAPE_STRING_SYNTAX;
res[ri++] = '\'';
for (i = 0; arg[i]; i++, ri++)
{
if (SQL_STR_DOUBLE(arg[i]))
if (SQL_STR_DOUBLE(arg[i], true))
res[ri++] = arg[i];
res[ri] = arg[i];
}
......
......@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.88 2006/03/23 04:22:36 tgl Exp $
* $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.89 2006/05/26 23:48:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -17,6 +17,7 @@
#include "plpgsql.h"
#include "parser/parser.h"
#include "parser/gramparse.h"
static PLpgSQL_expr *read_sql_construct(int until,
int until2,
......@@ -376,12 +377,12 @@ decl_statement : decl_varname decl_const decl_datatype decl_notnull decl_defval
strcpy(buf, "SELECT ");
cp1 = new->refname;
cp2 = buf + strlen(buf);
if (strchr(cp1, '\\') != NULL)
if (!standard_conforming_strings && strchr(cp1, '\\') != NULL)
*cp2++ = ESCAPE_STRING_SYNTAX;
*cp2++ = '\'';
while (*cp1)
{
if (SQL_STR_DOUBLE(*cp1))
if (SQL_STR_DOUBLE(*cp1, !standard_conforming_strings))
*cp2++ = *cp1;
*cp2++ = *cp1++;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment