@@ -33,15 +33,46 @@ quote_ident(PG_FUNCTION_ARGS)
3333}
3434
3535/*
36- * quote_literal -
37- * returns a properly quoted literal
36+ * quote_literal_internal -
37+ * helper function for quote_literal and quote_literal_cstr
3838 *
3939 * NOTE: think not to make this function's behavior change with
4040 * standard_conforming_strings. We don't know where the result
4141 * literal will be used, and so we must generate a result that
4242 * will work with either setting. Take a look at what dblink
4343 * uses this for before thinking you know better.
4444 */
45+ static size_t
46+ quote_literal_internal (char * dst , char * src , size_t len )
47+ {
48+ char * s ;
49+ char * savedst = dst ;
50+
51+ for (s = src ; s < src + len ; s ++ )
52+ {
53+ if (* s == '\\' )
54+ {
55+ * dst ++ = ESCAPE_STRING_SYNTAX ;
56+ break ;
57+ }
58+ }
59+
60+ * dst ++ = '\'' ;
61+ while (len -- > 0 )
62+ {
63+ if (SQL_STR_DOUBLE (* src , true))
64+ * dst ++ = * src ;
65+ * dst ++ = * src ++ ;
66+ }
67+ * dst ++ = '\'' ;
68+
69+ return dst - savedst ;
70+ }
71+
72+ /*
73+ * quote_literal -
74+ * returns a properly quoted literal
75+ */
4576Datum
4677quote_literal (PG_FUNCTION_ARGS )
4778{
@@ -58,30 +89,30 @@ quote_literal(PG_FUNCTION_ARGS)
5889 cp1 = VARDATA (t );
5990 cp2 = VARDATA (result );
6091
61- for (; len -- > 0 ; cp1 ++ )
62- {
63- if (* cp1 == '\\' )
64- {
65- * cp2 ++ = ESCAPE_STRING_SYNTAX ;
66- break ;
67- }
68- }
92+ SET_VARSIZE (result , VARHDRSZ + quote_literal_internal (cp2 , cp1 , len ));
6993
70- len = VARSIZE ( t ) - VARHDRSZ ;
71- cp1 = VARDATA ( t );
94+ PG_RETURN_TEXT_P ( result ) ;
95+ }
7296
73- * cp2 ++ = '\'' ;
74- while (len -- > 0 )
75- {
76- if (SQL_STR_DOUBLE (* cp1 , true))
77- * cp2 ++ = * cp1 ;
78- * cp2 ++ = * cp1 ++ ;
79- }
80- * cp2 ++ = '\'' ;
97+ /*
98+ * quote_literal_cstr -
99+ * returns a properly quoted literal
100+ */
101+ char *
102+ quote_literal_cstr (char * rawstr )
103+ {
104+ char * result ;
105+ int len ;
106+ int newlen ;
81107
82- SET_VARSIZE (result , cp2 - ((char * ) result ));
108+ len = strlen (rawstr );
109+ /* We make a worst-case result area; wasting a little space is OK */
110+ result = palloc (len * 2 + 3 );
83111
84- PG_RETURN_TEXT_P (result );
112+ newlen = quote_literal_internal (result , rawstr , len );
113+ result [newlen ] = '\0' ;
114+
115+ return result ;
85116}
86117
87118/*
0 commit comments