99 *
1010 *
1111 * 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 $
1313 *
1414 *-------------------------------------------------------------------------
1515*/
1616
1717#include " plpgsql.h"
1818
1919#include " catalog/pg_type.h"
20+ #include " lib/stringinfo.h"
2021#include " parser/parser.h"
2122
2223
@@ -1978,16 +1979,16 @@ read_sql_construct(int until,
19781979{
19791980int tok;
19801981int lno;
1981- PLpgSQL_dstring ds;
1982+ StringInfoData ds;
19821983int parenlevel =0 ;
19831984int nparams =0 ;
19841985int params[MAX_EXPR_PARAMS];
19851986char buf[32 ];
19861987PLpgSQL_expr*expr;
19871988
19881989lno =plpgsql_scanner_lineno ();
1989- plpgsql_dstring_init (&ds);
1990- plpgsql_dstring_append (&ds, sqlstart);
1990+ initStringInfo (&ds);
1991+ appendStringInfoString (&ds, sqlstart);
19911992
19921993for (;;)
19931994{
@@ -2029,33 +2030,33 @@ read_sql_construct(int until,
20292030}
20302031
20312032if (plpgsql_SpaceScanned)
2032- plpgsql_dstring_append (&ds," " );
2033+ appendStringInfoChar (&ds,' ' );
20332034
20342035switch (tok)
20352036{
20362037case T_SCALAR:
20372038snprintf (buf,sizeof (buf)," $%d" ,
20382039assign_expr_param (yylval.scalar ->dno ,
20392040 params, &nparams));
2040- plpgsql_dstring_append (&ds, buf);
2041+ appendStringInfoString (&ds, buf);
20412042break ;
20422043
20432044case T_ROW:
20442045snprintf (buf,sizeof (buf)," $%d" ,
20452046assign_expr_param (yylval.row ->dno ,
20462047 params, &nparams));
2047- plpgsql_dstring_append (&ds, buf);
2048+ appendStringInfoString (&ds, buf);
20482049break ;
20492050
20502051case T_RECORD:
20512052snprintf (buf,sizeof (buf)," $%d" ,
20522053assign_expr_param (yylval.rec ->dno ,
20532054 params, &nparams));
2054- plpgsql_dstring_append (&ds, buf);
2055+ appendStringInfoString (&ds, buf);
20552056break ;
20562057
20572058default :
2058- plpgsql_dstring_append (&ds, yytext);
2059+ appendStringInfoString (&ds, yytext);
20592060break ;
20602061}
20612062}
@@ -2065,12 +2066,12 @@ read_sql_construct(int until,
20652066
20662067expr =palloc (sizeof (PLpgSQL_expr) +sizeof (int ) * nparams -sizeof (int ));
20672068expr->dtype = PLPGSQL_DTYPE_EXPR;
2068- expr->query =pstrdup (plpgsql_dstring_get (&ds) );
2069+ expr->query =pstrdup (ds. data );
20692070expr->plan =NULL ;
20702071expr->nparams = nparams;
20712072while (nparams-- >0 )
20722073expr->params [nparams] = params[nparams];
2073- plpgsql_dstring_free (&ds );
2074+ pfree (ds. data );
20742075
20752076if (valid_sql)
20762077check_sql_expr (expr->query );
@@ -2082,7 +2083,7 @@ static PLpgSQL_type *
20822083read_datatype (int tok)
20832084{
20842085int lno;
2085- PLpgSQL_dstring ds;
2086+ StringInfoData ds;
20862087char *type_name;
20872088PLpgSQL_type*result;
20882089bool needspace =false ;
@@ -2100,7 +2101,7 @@ read_datatype(int tok)
21002101return yylval.dtype ;
21012102}
21022103
2103- plpgsql_dstring_init (&ds);
2104+ initStringInfo (&ds);
21042105
21052106while (tok !=' ;' )
21062107{
@@ -2122,16 +2123,16 @@ read_datatype(int tok)
21222123else if (tok ==' )' )
21232124parenlevel--;
21242125if (needspace)
2125- plpgsql_dstring_append (&ds," " );
2126+ appendStringInfoChar (&ds,' ' );
21262127needspace =true ;
2127- plpgsql_dstring_append (&ds, yytext);
2128+ appendStringInfoString (&ds, yytext);
21282129
21292130tok =yylex ();
21302131}
21312132
21322133plpgsql_push_back_token (tok);
21332134
2134- type_name =plpgsql_dstring_get (&ds) ;
2135+ type_name =ds. data ;
21352136
21362137if (type_name[0 ] ==' \0 ' )
21372138yyerror (" missing data type declaration" );
@@ -2140,15 +2141,15 @@ read_datatype(int tok)
21402141
21412142result =plpgsql_parse_datatype (type_name);
21422143
2143- plpgsql_dstring_free (&ds );
2144+ pfree (ds. data );
21442145
21452146return result;
21462147}
21472148
21482149static PLpgSQL_stmt *
21492150make_execsql_stmt (const char *sqlstart,int lineno)
21502151{
2151- PLpgSQL_dstring ds;
2152+ StringInfoData ds;
21522153int nparams =0 ;
21532154int params[MAX_EXPR_PARAMS];
21542155char buf[32 ];
@@ -2161,8 +2162,8 @@ make_execsql_stmt(const char *sqlstart, int lineno)
21612162bool have_into =false ;
21622163bool have_strict =false ;
21632164
2164- plpgsql_dstring_init (&ds);
2165- plpgsql_dstring_append (&ds, sqlstart);
2165+ initStringInfo (&ds);
2166+ appendStringInfoString (&ds, sqlstart);
21662167
21672168/*
21682169 * 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)
21962197}
21972198
21982199if (plpgsql_SpaceScanned)
2199- plpgsql_dstring_append (&ds," " );
2200+ appendStringInfoChar (&ds,' ' );
22002201
22012202switch (tok)
22022203{
22032204case T_SCALAR:
22042205snprintf (buf,sizeof (buf)," $%d" ,
22052206assign_expr_param (yylval.scalar ->dno ,
22062207 params, &nparams));
2207- plpgsql_dstring_append (&ds, buf);
2208+ appendStringInfoString (&ds, buf);
22082209break ;
22092210
22102211case T_ROW:
22112212snprintf (buf,sizeof (buf)," $%d" ,
22122213assign_expr_param (yylval.row ->dno ,
22132214 params, &nparams));
2214- plpgsql_dstring_append (&ds, buf);
2215+ appendStringInfoString (&ds, buf);
22152216break ;
22162217
22172218case T_RECORD:
22182219snprintf (buf,sizeof (buf)," $%d" ,
22192220assign_expr_param (yylval.rec ->dno ,
22202221 params, &nparams));
2221- plpgsql_dstring_append (&ds, buf);
2222+ appendStringInfoString (&ds, buf);
22222223break ;
22232224
22242225default :
2225- plpgsql_dstring_append (&ds, yytext);
2226+ appendStringInfoString (&ds, yytext);
22262227break ;
22272228}
22282229}
22292230
22302231expr =palloc (sizeof (PLpgSQL_expr) +sizeof (int ) * nparams -sizeof (int ));
22312232expr->dtype = PLPGSQL_DTYPE_EXPR;
2232- expr->query =pstrdup (plpgsql_dstring_get (&ds) );
2233+ expr->query =pstrdup (ds. data );
22332234expr->plan =NULL ;
22342235expr->nparams = nparams;
22352236while (nparams-- >0 )
22362237expr->params [nparams] = params[nparams];
2237- plpgsql_dstring_free (&ds );
2238+ pfree (ds. data );
22382239
22392240check_sql_expr (expr->query );
22402241
@@ -3023,8 +3024,7 @@ make_case(int lineno, PLpgSQL_expr *t_expr,
30233024PLpgSQL_expr *expr = cwt->expr ;
30243025int nparams = expr->nparams ;
30253026PLpgSQL_expr *new_expr;
3026- PLpgSQL_dstring ds;
3027- char buff[32 ];
3027+ StringInfoDatads;
30283028
30293029/* Must add the CASE variable as an extra param to expression*/
30303030if (nparams >= MAX_EXPR_PARAMS)
@@ -3041,22 +3041,19 @@ make_case(int lineno, PLpgSQL_expr *t_expr,
30413041new_expr->nparams = nparams +1 ;
30423042new_expr->params [nparams] = t_varno;
30433043
3044+ /* copy expression query without SELECT keyword (expr->query + 7)*/
3045+ Assert (strncmp (expr->query ," SELECT" ,7 ) ==0 );
3046+
30443047/* 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);
30513049
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 );
30563053
3057- new_expr->query =pstrdup (plpgsql_dstring_get (&ds) );
3054+ new_expr->query =pstrdup (ds. data );
30583055
3059- plpgsql_dstring_free (&ds );
3056+ pfree (ds. data );
30603057pfree (expr->query );
30613058pfree (expr);
30623059