15
15
*
16
16
*
17
17
* IDENTIFICATION
18
- *$Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.44 2002/04/24 14:03:22 momjian Exp $
18
+ *$Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.45 2002/05/06 17:34:45 tgl Exp $
19
19
*
20
20
* Modifications - 28-Jun-2000 - pjw@rhyme.com.au
21
21
*
@@ -2069,25 +2069,32 @@ _reconnectAsOwner(ArchiveHandle *AH, const char *dbname, TocEntry *te)
2069
2069
/*
2070
2070
* fmtId
2071
2071
*
2072
- *checks input stringfor non-lowercase characters
2073
- *returns pointer to input string or string surrounded by double quotes
2072
+ *Quotes input stringif it's not a legitimate SQL identifier as-is,
2073
+ *or all the time if force_quotes is true.
2074
2074
*
2075
- *Note that the returned string should be used immediately since it
2076
- *uses a static buffer to hold the string. Non-reentrant but faster?
2075
+ *Note that the returned string must be used before calling fmtId again,
2076
+ *since we re-use the same return buffer each time. Non-reentrant but
2077
+ *avoids memory leakage.
2077
2078
*/
2078
2079
const char *
2079
2080
fmtId (const char * rawid ,bool force_quotes )
2080
2081
{
2081
2082
static PQExpBuffer id_return = NULL ;
2082
2083
const char * cp ;
2083
2084
2085
+ if (id_return )/* first time through? */
2086
+ resetPQExpBuffer (id_return );
2087
+ else
2088
+ id_return = createPQExpBuffer ();
2089
+
2084
2090
if (!force_quotes )
2085
2091
{
2086
2092
/* do a quick check on the first character... */
2087
- if (!islower ((unsignedchar )* rawid ))
2093
+ if (!islower ((unsignedchar )* rawid )&& * rawid != '_' )
2088
2094
force_quotes = true;
2089
- /* otherwise check the entire string */
2090
2095
else
2096
+ {
2097
+ /* otherwise check the entire string */
2091
2098
for (cp = rawid ;* cp ;cp ++ )
2092
2099
{
2093
2100
if (!(islower ((unsignedchar )* cp )||
@@ -2098,32 +2105,30 @@ fmtId(const char *rawid, bool force_quotes)
2098
2105
break ;
2099
2106
}
2100
2107
}
2108
+ }
2101
2109
}
2102
2110
2103
2111
if (!force_quotes )
2104
- return rawid ; /* no quoting needed */
2105
-
2106
- if (id_return )
2107
- resetPQExpBuffer ( id_return );
2112
+ {
2113
+ /* no quoting needed */
2114
+ appendPQExpBufferStr (id_return , rawid );
2115
+ }
2108
2116
else
2109
- id_return = createPQExpBuffer ();
2110
-
2111
- appendPQExpBufferChar (id_return ,'\"' );
2112
- for (cp = rawid ;* cp ;cp ++ )
2113
2117
{
2114
- /*
2115
- * Did we find a double-quote in the string? Then make this a
2116
- * double double-quote per SQL99. Before, we put in a
2117
- * backslash/double-quote pair. - thomas 2000-08-05
2118
- */
2119
- if (* cp == '\"' )
2118
+ appendPQExpBufferChar (id_return ,'\"' );
2119
+ for (cp = rawid ;* cp ;cp ++ )
2120
2120
{
2121
- appendPQExpBufferChar (id_return ,'\"' );
2122
- appendPQExpBufferChar (id_return ,'\"' );
2121
+ /*
2122
+ * Did we find a double-quote in the string? Then make this a
2123
+ * double double-quote per SQL99. Before, we put in a
2124
+ * backslash/double-quote pair. - thomas 2000-08-05
2125
+ */
2126
+ if (* cp == '\"' )
2127
+ appendPQExpBufferChar (id_return ,'\"' );
2128
+ appendPQExpBufferChar (id_return ,* cp );
2123
2129
}
2124
- appendPQExpBufferChar (id_return ,* cp );
2130
+ appendPQExpBufferChar (id_return ,'\"' );
2125
2131
}
2126
- appendPQExpBufferChar (id_return ,'\"' );
2127
2132
2128
2133
return id_return -> data ;
2129
2134
}