9
9
*
10
10
*
11
11
* IDENTIFICATION
12
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.126 2009/07/11 21:15:32 petere Exp $
12
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.127 2009/07/22 02:31:38 joe Exp $
13
13
*
14
14
*-------------------------------------------------------------------------
15
15
*/
16
16
17
17
#include " plpgsql.h"
18
18
19
19
#include " catalog/pg_type.h"
20
+ #include " lib/stringinfo.h"
20
21
#include " parser/parser.h"
21
22
22
23
@@ -1978,16 +1979,16 @@ read_sql_construct(int until,
1978
1979
{
1979
1980
int tok;
1980
1981
int lno;
1981
- PLpgSQL_dstring ds;
1982
+ StringInfoData ds;
1982
1983
int parenlevel = 0 ;
1983
1984
int nparams = 0 ;
1984
1985
int params[MAX_EXPR_PARAMS];
1985
1986
char buf[32 ];
1986
1987
PLpgSQL_expr *expr;
1987
1988
1988
1989
lno = plpgsql_scanner_lineno ();
1989
- plpgsql_dstring_init (&ds);
1990
- plpgsql_dstring_append (&ds, sqlstart);
1990
+ initStringInfo (&ds);
1991
+ appendStringInfoString (&ds, sqlstart);
1991
1992
1992
1993
for (;;)
1993
1994
{
@@ -2029,33 +2030,33 @@ read_sql_construct(int until,
2029
2030
}
2030
2031
2031
2032
if (plpgsql_SpaceScanned)
2032
- plpgsql_dstring_append (&ds, " " );
2033
+ appendStringInfoChar (&ds, ' ' );
2033
2034
2034
2035
switch (tok)
2035
2036
{
2036
2037
case T_SCALAR:
2037
2038
snprintf (buf, sizeof (buf), " $%d " ,
2038
2039
assign_expr_param (yylval.scalar ->dno ,
2039
2040
params, &nparams));
2040
- plpgsql_dstring_append (&ds, buf);
2041
+ appendStringInfoString (&ds, buf);
2041
2042
break ;
2042
2043
2043
2044
case T_ROW:
2044
2045
snprintf (buf, sizeof (buf), " $%d " ,
2045
2046
assign_expr_param (yylval.row ->dno ,
2046
2047
params, &nparams));
2047
- plpgsql_dstring_append (&ds, buf);
2048
+ appendStringInfoString (&ds, buf);
2048
2049
break ;
2049
2050
2050
2051
case T_RECORD:
2051
2052
snprintf (buf, sizeof (buf), " $%d " ,
2052
2053
assign_expr_param (yylval.rec ->dno ,
2053
2054
params, &nparams));
2054
- plpgsql_dstring_append (&ds, buf);
2055
+ appendStringInfoString (&ds, buf);
2055
2056
break ;
2056
2057
2057
2058
default :
2058
- plpgsql_dstring_append (&ds, yytext);
2059
+ appendStringInfoString (&ds, yytext);
2059
2060
break ;
2060
2061
}
2061
2062
}
@@ -2065,12 +2066,12 @@ read_sql_construct(int until,
2065
2066
2066
2067
expr = palloc (sizeof (PLpgSQL_expr) + sizeof (int ) * nparams - sizeof (int ));
2067
2068
expr->dtype = PLPGSQL_DTYPE_EXPR;
2068
- expr->query = pstrdup (plpgsql_dstring_get (&ds) );
2069
+ expr->query = pstrdup (ds. data );
2069
2070
expr->plan = NULL ;
2070
2071
expr->nparams = nparams;
2071
2072
while (nparams-- > 0 )
2072
2073
expr->params [nparams] = params[nparams];
2073
- plpgsql_dstring_free (&ds );
2074
+ pfree (ds. data );
2074
2075
2075
2076
if (valid_sql)
2076
2077
check_sql_expr (expr->query );
@@ -2082,7 +2083,7 @@ static PLpgSQL_type *
2082
2083
read_datatype (int tok)
2083
2084
{
2084
2085
int lno;
2085
- PLpgSQL_dstring ds;
2086
+ StringInfoData ds;
2086
2087
char *type_name;
2087
2088
PLpgSQL_type *result;
2088
2089
bool needspace = false ;
@@ -2100,7 +2101,7 @@ read_datatype(int tok)
2100
2101
return yylval.dtype ;
2101
2102
}
2102
2103
2103
- plpgsql_dstring_init (&ds);
2104
+ initStringInfo (&ds);
2104
2105
2105
2106
while (tok != ' ;' )
2106
2107
{
@@ -2122,16 +2123,16 @@ read_datatype(int tok)
2122
2123
else if (tok == ' )' )
2123
2124
parenlevel--;
2124
2125
if (needspace)
2125
- plpgsql_dstring_append (&ds, " " );
2126
+ appendStringInfoChar (&ds, ' ' );
2126
2127
needspace = true ;
2127
- plpgsql_dstring_append (&ds, yytext);
2128
+ appendStringInfoString (&ds, yytext);
2128
2129
2129
2130
tok = yylex ();
2130
2131
}
2131
2132
2132
2133
plpgsql_push_back_token (tok);
2133
2134
2134
- type_name = plpgsql_dstring_get (&ds) ;
2135
+ type_name = ds. data ;
2135
2136
2136
2137
if (type_name[0 ] == ' \0 ' )
2137
2138
yyerror (" missing data type declaration" );
@@ -2140,15 +2141,15 @@ read_datatype(int tok)
2140
2141
2141
2142
result = plpgsql_parse_datatype (type_name);
2142
2143
2143
- plpgsql_dstring_free (&ds );
2144
+ pfree (ds. data );
2144
2145
2145
2146
return result;
2146
2147
}
2147
2148
2148
2149
static PLpgSQL_stmt *
2149
2150
make_execsql_stmt (const char *sqlstart, int lineno)
2150
2151
{
2151
- PLpgSQL_dstring ds;
2152
+ StringInfoData ds;
2152
2153
int nparams = 0 ;
2153
2154
int params[MAX_EXPR_PARAMS];
2154
2155
char buf[32 ];
@@ -2161,8 +2162,8 @@ make_execsql_stmt(const char *sqlstart, int lineno)
2161
2162
bool have_into = false ;
2162
2163
bool have_strict = false ;
2163
2164
2164
- plpgsql_dstring_init (&ds);
2165
- plpgsql_dstring_append (&ds, sqlstart);
2165
+ initStringInfo (&ds);
2166
+ appendStringInfoString (&ds, sqlstart);
2166
2167
2167
2168
/*
2168
2169
* We have to special-case the sequence INSERT INTO, because we don't want
@@ -2196,45 +2197,45 @@ make_execsql_stmt(const char *sqlstart, int lineno)
2196
2197
}
2197
2198
2198
2199
if (plpgsql_SpaceScanned)
2199
- plpgsql_dstring_append (&ds, " " );
2200
+ appendStringInfoChar (&ds, ' ' );
2200
2201
2201
2202
switch (tok)
2202
2203
{
2203
2204
case T_SCALAR:
2204
2205
snprintf (buf, sizeof (buf), " $%d " ,
2205
2206
assign_expr_param (yylval.scalar ->dno ,
2206
2207
params, &nparams));
2207
- plpgsql_dstring_append (&ds, buf);
2208
+ appendStringInfoString (&ds, buf);
2208
2209
break ;
2209
2210
2210
2211
case T_ROW:
2211
2212
snprintf (buf, sizeof (buf), " $%d " ,
2212
2213
assign_expr_param (yylval.row ->dno ,
2213
2214
params, &nparams));
2214
- plpgsql_dstring_append (&ds, buf);
2215
+ appendStringInfoString (&ds, buf);
2215
2216
break ;
2216
2217
2217
2218
case T_RECORD:
2218
2219
snprintf (buf, sizeof (buf), " $%d " ,
2219
2220
assign_expr_param (yylval.rec ->dno ,
2220
2221
params, &nparams));
2221
- plpgsql_dstring_append (&ds, buf);
2222
+ appendStringInfoString (&ds, buf);
2222
2223
break ;
2223
2224
2224
2225
default :
2225
- plpgsql_dstring_append (&ds, yytext);
2226
+ appendStringInfoString (&ds, yytext);
2226
2227
break ;
2227
2228
}
2228
2229
}
2229
2230
2230
2231
expr = palloc (sizeof (PLpgSQL_expr) + sizeof (int ) * nparams - sizeof (int ));
2231
2232
expr->dtype = PLPGSQL_DTYPE_EXPR;
2232
- expr->query = pstrdup (plpgsql_dstring_get (&ds) );
2233
+ expr->query = pstrdup (ds. data );
2233
2234
expr->plan = NULL ;
2234
2235
expr->nparams = nparams;
2235
2236
while (nparams-- > 0 )
2236
2237
expr->params [nparams] = params[nparams];
2237
- plpgsql_dstring_free (&ds );
2238
+ pfree (ds. data );
2238
2239
2239
2240
check_sql_expr (expr->query );
2240
2241
@@ -3023,8 +3024,7 @@ make_case(int lineno, PLpgSQL_expr *t_expr,
3023
3024
PLpgSQL_expr *expr = cwt->expr ;
3024
3025
int nparams = expr->nparams ;
3025
3026
PLpgSQL_expr *new_expr;
3026
- PLpgSQL_dstring ds;
3027
- char buff[32 ];
3027
+ StringInfoData ds;
3028
3028
3029
3029
/* Must add the CASE variable as an extra param to expression */
3030
3030
if (nparams >= MAX_EXPR_PARAMS)
@@ -3041,22 +3041,19 @@ make_case(int lineno, PLpgSQL_expr *t_expr,
3041
3041
new_expr->nparams = nparams + 1 ;
3042
3042
new_expr->params [nparams] = t_varno;
3043
3043
3044
+ /* copy expression query without SELECT keyword (expr->query + 7) */
3045
+ Assert (strncmp (expr->query , " SELECT " , 7 ) == 0 );
3046
+
3044
3047
/* And do the string hacking */
3045
- plpgsql_dstring_init (&ds);
3046
-
3047
- plpgsql_dstring_append (&ds, " SELECT $" );
3048
- snprintf (buff, sizeof (buff), " %d" , nparams + 1 );
3049
- plpgsql_dstring_append (&ds, buff);
3050
- plpgsql_dstring_append (&ds, " IN (" );
3048
+ initStringInfo (&ds);
3051
3049
3052
- /* copy expression query without SELECT keyword */
3053
- Assert (strncmp (expr->query , " SELECT " , 7 ) == 0 );
3054
- plpgsql_dstring_append (&ds, expr->query + 7 );
3055
- plpgsql_dstring_append_char (&ds, ' )' );
3050
+ appendStringInfo (&ds, " SELECT $%d IN(%s)" ,
3051
+ nparams + 1 ,
3052
+ expr->query + 7 );
3056
3053
3057
- new_expr->query = pstrdup (plpgsql_dstring_get (&ds) );
3054
+ new_expr->query = pstrdup (ds. data );
3058
3055
3059
- plpgsql_dstring_free (&ds );
3056
+ pfree (ds. data );
3060
3057
pfree (expr->query );
3061
3058
pfree (expr);
3062
3059
0 commit comments