Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 775d283

Browse files
committed
Fix quote_ident to use quote_identifier rather than its own, not quite
up-to-speed logic; in particular this will cause it to quote names that match keywords. Remove unnecessary multibyte cruft from quote_literal (all backend-internal encodings are 8-bit-safe).
1 parent 6a8eb1a commit 775d283

File tree

1 file changed

+23
-131
lines changed

1 file changed

+23
-131
lines changed

src/backend/utils/adt/quote.c

Lines changed: 23 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,15 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.14 2005/01/01 05:43:07 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.15 2005/03/21 16:29:20 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#include "postgres.h"
1515

16-
#include <ctype.h>
17-
18-
#include "mb/pg_wchar.h"
1916
#include "utils/builtins.h"
2017

2118

22-
static bool quote_ident_required(text *iptr);
23-
static text *do_quote_ident(text *iptr);
24-
static text *do_quote_literal(text *iptr);
25-
26-
2719
/*
2820
* quote_ident -
2921
* returns a properly quoted identifier
@@ -33,16 +25,22 @@ quote_ident(PG_FUNCTION_ARGS)
3325
{
3426
text *t = PG_GETARG_TEXT_P(0);
3527
text *result;
28+
const char *qstr;
29+
char *str;
30+
int len;
3631

37-
if (quote_ident_required(t))
38-
result = do_quote_ident(t);
39-
else
40-
{
41-
result = (text *) palloc(VARSIZE(t));
42-
memcpy(result, t, VARSIZE(t));
43-
}
32+
/* We have to convert to a C string to use quote_identifier */
33+
len = VARSIZE(t) - VARHDRSZ;
34+
str = (char *) palloc(len + 1);
35+
memcpy(str, VARDATA(t), len);
36+
str[len] = '\0';
4437

45-
PG_FREE_IF_COPY(t, 0);
38+
qstr = quote_identifier(str);
39+
40+
len = strlen(qstr);
41+
result = (text *) palloc(len + VARHDRSZ);
42+
VARATT_SIZEP(result) = len + VARHDRSZ;
43+
memcpy(VARDATA(result), qstr, len);
4644

4745
PG_RETURN_TEXT_P(result);
4846
}
@@ -56,136 +54,30 @@ quote_literal(PG_FUNCTION_ARGS)
5654
{
5755
text *t = PG_GETARG_TEXT_P(0);
5856
text *result;
59-
60-
result = do_quote_literal(t);
61-
62-
PG_FREE_IF_COPY(t, 0);
63-
64-
PG_RETURN_TEXT_P(result);
65-
}
66-
67-
/*
68-
* Check if a given identifier needs quoting
69-
*/
70-
static bool
71-
quote_ident_required(text *iptr)
72-
{
73-
char *cp;
74-
char *ep;
75-
76-
cp = VARDATA(iptr);
77-
ep = VARDATA(iptr) + VARSIZE(iptr) - VARHDRSZ;
78-
79-
if (cp >= ep)
80-
return true;
81-
82-
if (pg_mblen(cp) != 1)
83-
return true;
84-
if (!(*cp == '_' || (*cp >= 'a' && *cp <= 'z')))
85-
return true;
86-
87-
while ((++cp) < ep)
88-
{
89-
if (pg_mblen(cp) != 1)
90-
return true;
91-
92-
if (*cp >= 'a' && *cp <= 'z')
93-
continue;
94-
if (*cp >= '0' && *cp <= '9')
95-
continue;
96-
if (*cp == '_')
97-
continue;
98-
99-
return true;
100-
}
101-
102-
return false;
103-
}
104-
105-
/*
106-
* Return a properly quoted identifier
107-
*/
108-
static text *
109-
do_quote_ident(text *iptr)
110-
{
111-
text *result;
11257
char *cp1;
11358
char *cp2;
11459
int len;
115-
int wl;
11660

117-
len = VARSIZE(iptr) - VARHDRSZ;
118-
result = (text *) palloc(len * 2 + VARHDRSZ + 2);
61+
len = VARSIZE(t) - VARHDRSZ;
62+
/* We make a worst-case result area; wasting a little space is OK */
63+
result = (text *) palloc(len * 2 + 2 + VARHDRSZ);
11964

120-
cp1 = VARDATA(iptr);
121-
cp2 = VARDATA(result);
122-
123-
*cp2++ = '"';
124-
while (len > 0)
125-
{
126-
if ((wl = pg_mblen(cp1)) != 1)
127-
{
128-
len -= wl;
129-
130-
while (wl-- > 0)
131-
*cp2++ = *cp1++;
132-
continue;
133-
}
134-
135-
if (*cp1 == '"')
136-
*cp2++ = '"';
137-
*cp2++ = *cp1++;
138-
139-
len--;
140-
}
141-
*cp2++ = '"';
142-
143-
VARATT_SIZEP(result) = cp2 - ((char *) result);
144-
145-
return result;
146-
}
147-
148-
/*
149-
* Return a properly quoted literal value
150-
*/
151-
static text *
152-
do_quote_literal(text *lptr)
153-
{
154-
text *result;
155-
char *cp1;
156-
char *cp2;
157-
int len;
158-
int wl;
159-
160-
len = VARSIZE(lptr) - VARHDRSZ;
161-
result = (text *) palloc(len * 2 + VARHDRSZ + 2);
162-
163-
cp1 = VARDATA(lptr);
65+
cp1 = VARDATA(t);
16466
cp2 = VARDATA(result);
16567

16668
*cp2++ = '\'';
167-
while (len > 0)
69+
while (len-- > 0)
16870
{
169-
if ((wl = pg_mblen(cp1)) != 1)
170-
{
171-
len -= wl;
172-
173-
while (wl-- > 0)
174-
*cp2++ = *cp1++;
175-
continue;
176-
}
177-
17871
if (*cp1 == '\'')
17972
*cp2++ = '\'';
180-
if (*cp1 == '\\')
73+
else if (*cp1 == '\\')
18174
*cp2++ = '\\';
182-
*cp2++ = *cp1++;
18375

184-
len--;
76+
*cp2++ = *cp1++;
18577
}
18678
*cp2++ = '\'';
18779

18880
VARATT_SIZEP(result) = cp2 - ((char *) result);
18981

190-
return result;
82+
PG_RETURN_TEXT_P(result);
19183
}

0 commit comments

Comments
 (0)