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

Commit 6cf9f31

Browse files
committed
Replace PLpgSQL_dstring by StringInfo.
Replace redundant PLpgSQL_dstring functionality with StringInfo. Patch by Pavel Stehule. Review by Joe Conway.
1 parent ca7c816 commit 6cf9f31

File tree

4 files changed

+49
-153
lines changed

4 files changed

+49
-153
lines changed

src/pl/plpgsql/src/gram.y

+38-41
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
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
{
19791980
int tok;
19801981
int lno;
1981-
PLpgSQL_dstring ds;
1982+
StringInfoData ds;
19821983
int parenlevel = 0;
19831984
int nparams = 0;
19841985
int params[MAX_EXPR_PARAMS];
19851986
char buf[32];
19861987
PLpgSQL_expr *expr;
19871988

19881989
lno = plpgsql_scanner_lineno();
1989-
plpgsql_dstring_init(&ds);
1990-
plpgsql_dstring_append(&ds, sqlstart);
1990+
initStringInfo(&ds);
1991+
appendStringInfoString(&ds, sqlstart);
19911992

19921993
for (;;)
19931994
{
@@ -2029,33 +2030,33 @@ read_sql_construct(int until,
20292030
}
20302031

20312032
if (plpgsql_SpaceScanned)
2032-
plpgsql_dstring_append(&ds, " ");
2033+
appendStringInfoChar(&ds, ' ');
20332034

20342035
switch (tok)
20352036
{
20362037
case T_SCALAR:
20372038
snprintf(buf, sizeof(buf), " $%d ",
20382039
assign_expr_param(yylval.scalar->dno,
20392040
params, &nparams));
2040-
plpgsql_dstring_append(&ds, buf);
2041+
appendStringInfoString(&ds, buf);
20412042
break;
20422043

20432044
case T_ROW:
20442045
snprintf(buf, sizeof(buf), " $%d ",
20452046
assign_expr_param(yylval.row->dno,
20462047
params, &nparams));
2047-
plpgsql_dstring_append(&ds, buf);
2048+
appendStringInfoString(&ds, buf);
20482049
break;
20492050

20502051
case T_RECORD:
20512052
snprintf(buf, sizeof(buf), " $%d ",
20522053
assign_expr_param(yylval.rec->dno,
20532054
params, &nparams));
2054-
plpgsql_dstring_append(&ds, buf);
2055+
appendStringInfoString(&ds, buf);
20552056
break;
20562057

20572058
default:
2058-
plpgsql_dstring_append(&ds, yytext);
2059+
appendStringInfoString(&ds, yytext);
20592060
break;
20602061
}
20612062
}
@@ -2065,12 +2066,12 @@ read_sql_construct(int until,
20652066

20662067
expr = palloc(sizeof(PLpgSQL_expr) + sizeof(int) * nparams - sizeof(int));
20672068
expr->dtype = PLPGSQL_DTYPE_EXPR;
2068-
expr->query = pstrdup(plpgsql_dstring_get(&ds));
2069+
expr->query = pstrdup(ds.data);
20692070
expr->plan = NULL;
20702071
expr->nparams = nparams;
20712072
while(nparams-- > 0)
20722073
expr->params[nparams] = params[nparams];
2073-
plpgsql_dstring_free(&ds);
2074+
pfree(ds.data);
20742075

20752076
if (valid_sql)
20762077
check_sql_expr(expr->query);
@@ -2082,7 +2083,7 @@ static PLpgSQL_type *
20822083
read_datatype(int tok)
20832084
{
20842085
int lno;
2085-
PLpgSQL_dstring ds;
2086+
StringInfoData ds;
20862087
char *type_name;
20872088
PLpgSQL_type *result;
20882089
bool needspace = false;
@@ -2100,7 +2101,7 @@ read_datatype(int tok)
21002101
return yylval.dtype;
21012102
}
21022103

2103-
plpgsql_dstring_init(&ds);
2104+
initStringInfo(&ds);
21042105

21052106
while (tok != ';')
21062107
{
@@ -2122,16 +2123,16 @@ read_datatype(int tok)
21222123
else if (tok == ')')
21232124
parenlevel--;
21242125
if (needspace)
2125-
plpgsql_dstring_append(&ds, " ");
2126+
appendStringInfoChar(&ds, ' ');
21262127
needspace = true;
2127-
plpgsql_dstring_append(&ds, yytext);
2128+
appendStringInfoString(&ds, yytext);
21282129

21292130
tok = yylex();
21302131
}
21312132

21322133
plpgsql_push_back_token(tok);
21332134

2134-
type_name = plpgsql_dstring_get(&ds);
2135+
type_name = ds.data;
21352136

21362137
if (type_name[0] == '\0')
21372138
yyerror("missing data type declaration");
@@ -2140,15 +2141,15 @@ read_datatype(int tok)
21402141

21412142
result = plpgsql_parse_datatype(type_name);
21422143

2143-
plpgsql_dstring_free(&ds);
2144+
pfree(ds.data);
21442145

21452146
return result;
21462147
}
21472148

21482149
static PLpgSQL_stmt *
21492150
make_execsql_stmt(const char *sqlstart, int lineno)
21502151
{
2151-
PLpgSQL_dstring ds;
2152+
StringInfoData ds;
21522153
int nparams = 0;
21532154
int params[MAX_EXPR_PARAMS];
21542155
char buf[32];
@@ -2161,8 +2162,8 @@ make_execsql_stmt(const char *sqlstart, int lineno)
21612162
bool have_into = false;
21622163
bool 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

21982199
if (plpgsql_SpaceScanned)
2199-
plpgsql_dstring_append(&ds, " ");
2200+
appendStringInfoChar(&ds, ' ');
22002201

22012202
switch (tok)
22022203
{
22032204
case T_SCALAR:
22042205
snprintf(buf, sizeof(buf), " $%d ",
22052206
assign_expr_param(yylval.scalar->dno,
22062207
params, &nparams));
2207-
plpgsql_dstring_append(&ds, buf);
2208+
appendStringInfoString(&ds, buf);
22082209
break;
22092210

22102211
case T_ROW:
22112212
snprintf(buf, sizeof(buf), " $%d ",
22122213
assign_expr_param(yylval.row->dno,
22132214
params, &nparams));
2214-
plpgsql_dstring_append(&ds, buf);
2215+
appendStringInfoString(&ds, buf);
22152216
break;
22162217

22172218
case T_RECORD:
22182219
snprintf(buf, sizeof(buf), " $%d ",
22192220
assign_expr_param(yylval.rec->dno,
22202221
params, &nparams));
2221-
plpgsql_dstring_append(&ds, buf);
2222+
appendStringInfoString(&ds, buf);
22222223
break;
22232224

22242225
default:
2225-
plpgsql_dstring_append(&ds, yytext);
2226+
appendStringInfoString(&ds, yytext);
22262227
break;
22272228
}
22282229
}
22292230

22302231
expr = palloc(sizeof(PLpgSQL_expr) + sizeof(int) * nparams - sizeof(int));
22312232
expr->dtype = PLPGSQL_DTYPE_EXPR;
2232-
expr->query = pstrdup(plpgsql_dstring_get(&ds));
2233+
expr->query = pstrdup(ds.data);
22332234
expr->plan = NULL;
22342235
expr->nparams = nparams;
22352236
while(nparams-- > 0)
22362237
expr->params[nparams] = params[nparams];
2237-
plpgsql_dstring_free(&ds);
2238+
pfree(ds.data);
22382239

22392240
check_sql_expr(expr->query);
22402241

@@ -3023,8 +3024,7 @@ make_case(int lineno, PLpgSQL_expr *t_expr,
30233024
PLpgSQL_expr *expr = cwt->expr;
30243025
int nparams = expr->nparams;
30253026
PLpgSQL_expr *new_expr;
3026-
PLpgSQL_dstring ds;
3027-
char buff[32];
3027+
StringInfoData ds;
30283028

30293029
/* Must add the CASE variable as an extra param to expression */
30303030
if (nparams >= MAX_EXPR_PARAMS)
@@ -3041,22 +3041,19 @@ make_case(int lineno, PLpgSQL_expr *t_expr,
30413041
new_expr->nparams = nparams + 1;
30423042
new_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);
30603057
pfree(expr->query);
30613058
pfree(expr);
30623059

src/pl/plpgsql/src/pl_exec.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.245 2009/07/18 19:15:42 tgl Exp $
11+
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.246 2009/07/22 02:31:38 joe Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -22,6 +22,7 @@
2222
#include "catalog/pg_type.h"
2323
#include "executor/spi_priv.h"
2424
#include "funcapi.h"
25+
#include "lib/stringinfo.h"
2526
#include "nodes/nodeFuncs.h"
2627
#include "parser/scansup.h"
2728
#include "storage/proc.h"
@@ -2394,11 +2395,11 @@ exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
23942395

23952396
if (stmt->message)
23962397
{
2397-
PLpgSQL_dstring ds;
2398+
StringInfoData ds;
23982399
ListCell *current_param;
23992400
char *cp;
24002401

2401-
plpgsql_dstring_init(&ds);
2402+
initStringInfo(&ds);
24022403
current_param = list_head(stmt->params);
24032404

24042405
for (cp = stmt->message; *cp; cp++)
@@ -2416,7 +2417,7 @@ exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
24162417

24172418
if (cp[1] == '%')
24182419
{
2419-
plpgsql_dstring_append_char(&ds, cp[1]);
2420+
appendStringInfoChar(&ds, '%');
24202421
cp++;
24212422
continue;
24222423
}
@@ -2435,12 +2436,12 @@ exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
24352436
extval = "<NULL>";
24362437
else
24372438
extval = convert_value_to_string(paramvalue, paramtypeid);
2438-
plpgsql_dstring_append(&ds, extval);
2439+
appendStringInfoString(&ds, extval);
24392440
current_param = lnext(current_param);
24402441
exec_eval_cleanup(estate);
24412442
}
24422443
else
2443-
plpgsql_dstring_append_char(&ds, cp[0]);
2444+
appendStringInfoChar(&ds, cp[0]);
24442445
}
24452446

24462447
/*
@@ -2452,8 +2453,8 @@ exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
24522453
(errcode(ERRCODE_SYNTAX_ERROR),
24532454
errmsg("too many parameters specified for RAISE")));
24542455

2455-
err_message = plpgsql_dstring_get(&ds);
2456-
/* No dstring_free here, the pfree(err_message) does it */
2456+
err_message = ds.data;
2457+
/* No pfree(ds.data), the pfree(err_message) does it */
24572458
}
24582459

24592460
foreach(lc, stmt->options)

0 commit comments

Comments
 (0)