Commit 6cf9f319 authored by Joe Conway's avatar Joe Conway

Replace PLpgSQL_dstring by StringInfo.

Replace redundant PLpgSQL_dstring functionality with StringInfo.
Patch by Pavel Stehule. Review by Joe Conway.
parent ca7c8168
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.126 2009/07/11 21:15:32 petere Exp $ * $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.127 2009/07/22 02:31:38 joe Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "plpgsql.h" #include "plpgsql.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "lib/stringinfo.h"
#include "parser/parser.h" #include "parser/parser.h"
...@@ -1978,7 +1979,7 @@ read_sql_construct(int until, ...@@ -1978,7 +1979,7 @@ read_sql_construct(int until,
{ {
int tok; int tok;
int lno; int lno;
PLpgSQL_dstring ds; StringInfoData ds;
int parenlevel = 0; int parenlevel = 0;
int nparams = 0; int nparams = 0;
int params[MAX_EXPR_PARAMS]; int params[MAX_EXPR_PARAMS];
...@@ -1986,8 +1987,8 @@ read_sql_construct(int until, ...@@ -1986,8 +1987,8 @@ read_sql_construct(int until,
PLpgSQL_expr *expr; PLpgSQL_expr *expr;
lno = plpgsql_scanner_lineno(); lno = plpgsql_scanner_lineno();
plpgsql_dstring_init(&ds); initStringInfo(&ds);
plpgsql_dstring_append(&ds, sqlstart); appendStringInfoString(&ds, sqlstart);
for (;;) for (;;)
{ {
...@@ -2029,7 +2030,7 @@ read_sql_construct(int until, ...@@ -2029,7 +2030,7 @@ read_sql_construct(int until,
} }
if (plpgsql_SpaceScanned) if (plpgsql_SpaceScanned)
plpgsql_dstring_append(&ds, " "); appendStringInfoChar(&ds, ' ');
switch (tok) switch (tok)
{ {
...@@ -2037,25 +2038,25 @@ read_sql_construct(int until, ...@@ -2037,25 +2038,25 @@ read_sql_construct(int until,
snprintf(buf, sizeof(buf), " $%d ", snprintf(buf, sizeof(buf), " $%d ",
assign_expr_param(yylval.scalar->dno, assign_expr_param(yylval.scalar->dno,
params, &nparams)); params, &nparams));
plpgsql_dstring_append(&ds, buf); appendStringInfoString(&ds, buf);
break; break;
case T_ROW: case T_ROW:
snprintf(buf, sizeof(buf), " $%d ", snprintf(buf, sizeof(buf), " $%d ",
assign_expr_param(yylval.row->dno, assign_expr_param(yylval.row->dno,
params, &nparams)); params, &nparams));
plpgsql_dstring_append(&ds, buf); appendStringInfoString(&ds, buf);
break; break;
case T_RECORD: case T_RECORD:
snprintf(buf, sizeof(buf), " $%d ", snprintf(buf, sizeof(buf), " $%d ",
assign_expr_param(yylval.rec->dno, assign_expr_param(yylval.rec->dno,
params, &nparams)); params, &nparams));
plpgsql_dstring_append(&ds, buf); appendStringInfoString(&ds, buf);
break; break;
default: default:
plpgsql_dstring_append(&ds, yytext); appendStringInfoString(&ds, yytext);
break; break;
} }
} }
...@@ -2065,12 +2066,12 @@ read_sql_construct(int until, ...@@ -2065,12 +2066,12 @@ read_sql_construct(int until,
expr = palloc(sizeof(PLpgSQL_expr) + sizeof(int) * nparams - sizeof(int)); expr = palloc(sizeof(PLpgSQL_expr) + sizeof(int) * nparams - sizeof(int));
expr->dtype = PLPGSQL_DTYPE_EXPR; expr->dtype = PLPGSQL_DTYPE_EXPR;
expr->query = pstrdup(plpgsql_dstring_get(&ds)); expr->query = pstrdup(ds.data);
expr->plan = NULL; expr->plan = NULL;
expr->nparams = nparams; expr->nparams = nparams;
while(nparams-- > 0) while(nparams-- > 0)
expr->params[nparams] = params[nparams]; expr->params[nparams] = params[nparams];
plpgsql_dstring_free(&ds); pfree(ds.data);
if (valid_sql) if (valid_sql)
check_sql_expr(expr->query); check_sql_expr(expr->query);
...@@ -2082,7 +2083,7 @@ static PLpgSQL_type * ...@@ -2082,7 +2083,7 @@ static PLpgSQL_type *
read_datatype(int tok) read_datatype(int tok)
{ {
int lno; int lno;
PLpgSQL_dstring ds; StringInfoData ds;
char *type_name; char *type_name;
PLpgSQL_type *result; PLpgSQL_type *result;
bool needspace = false; bool needspace = false;
...@@ -2100,7 +2101,7 @@ read_datatype(int tok) ...@@ -2100,7 +2101,7 @@ read_datatype(int tok)
return yylval.dtype; return yylval.dtype;
} }
plpgsql_dstring_init(&ds); initStringInfo(&ds);
while (tok != ';') while (tok != ';')
{ {
...@@ -2122,16 +2123,16 @@ read_datatype(int tok) ...@@ -2122,16 +2123,16 @@ read_datatype(int tok)
else if (tok == ')') else if (tok == ')')
parenlevel--; parenlevel--;
if (needspace) if (needspace)
plpgsql_dstring_append(&ds, " "); appendStringInfoChar(&ds, ' ');
needspace = true; needspace = true;
plpgsql_dstring_append(&ds, yytext); appendStringInfoString(&ds, yytext);
tok = yylex(); tok = yylex();
} }
plpgsql_push_back_token(tok); plpgsql_push_back_token(tok);
type_name = plpgsql_dstring_get(&ds); type_name = ds.data;
if (type_name[0] == '\0') if (type_name[0] == '\0')
yyerror("missing data type declaration"); yyerror("missing data type declaration");
...@@ -2140,7 +2141,7 @@ read_datatype(int tok) ...@@ -2140,7 +2141,7 @@ read_datatype(int tok)
result = plpgsql_parse_datatype(type_name); result = plpgsql_parse_datatype(type_name);
plpgsql_dstring_free(&ds); pfree(ds.data);
return result; return result;
} }
...@@ -2148,7 +2149,7 @@ read_datatype(int tok) ...@@ -2148,7 +2149,7 @@ read_datatype(int tok)
static PLpgSQL_stmt * static PLpgSQL_stmt *
make_execsql_stmt(const char *sqlstart, int lineno) make_execsql_stmt(const char *sqlstart, int lineno)
{ {
PLpgSQL_dstring ds; StringInfoData ds;
int nparams = 0; int nparams = 0;
int params[MAX_EXPR_PARAMS]; int params[MAX_EXPR_PARAMS];
char buf[32]; char buf[32];
...@@ -2161,8 +2162,8 @@ make_execsql_stmt(const char *sqlstart, int lineno) ...@@ -2161,8 +2162,8 @@ make_execsql_stmt(const char *sqlstart, int lineno)
bool have_into = false; bool have_into = false;
bool have_strict = false; bool have_strict = false;
plpgsql_dstring_init(&ds); initStringInfo(&ds);
plpgsql_dstring_append(&ds, sqlstart); appendStringInfoString(&ds, sqlstart);
/* /*
* We have to special-case the sequence INSERT INTO, because we don't want * We have to special-case the sequence INSERT INTO, because we don't want
...@@ -2196,7 +2197,7 @@ make_execsql_stmt(const char *sqlstart, int lineno) ...@@ -2196,7 +2197,7 @@ make_execsql_stmt(const char *sqlstart, int lineno)
} }
if (plpgsql_SpaceScanned) if (plpgsql_SpaceScanned)
plpgsql_dstring_append(&ds, " "); appendStringInfoChar(&ds, ' ');
switch (tok) switch (tok)
{ {
...@@ -2204,37 +2205,37 @@ make_execsql_stmt(const char *sqlstart, int lineno) ...@@ -2204,37 +2205,37 @@ make_execsql_stmt(const char *sqlstart, int lineno)
snprintf(buf, sizeof(buf), " $%d ", snprintf(buf, sizeof(buf), " $%d ",
assign_expr_param(yylval.scalar->dno, assign_expr_param(yylval.scalar->dno,
params, &nparams)); params, &nparams));
plpgsql_dstring_append(&ds, buf); appendStringInfoString(&ds, buf);
break; break;
case T_ROW: case T_ROW:
snprintf(buf, sizeof(buf), " $%d ", snprintf(buf, sizeof(buf), " $%d ",
assign_expr_param(yylval.row->dno, assign_expr_param(yylval.row->dno,
params, &nparams)); params, &nparams));
plpgsql_dstring_append(&ds, buf); appendStringInfoString(&ds, buf);
break; break;
case T_RECORD: case T_RECORD:
snprintf(buf, sizeof(buf), " $%d ", snprintf(buf, sizeof(buf), " $%d ",
assign_expr_param(yylval.rec->dno, assign_expr_param(yylval.rec->dno,
params, &nparams)); params, &nparams));
plpgsql_dstring_append(&ds, buf); appendStringInfoString(&ds, buf);
break; break;
default: default:
plpgsql_dstring_append(&ds, yytext); appendStringInfoString(&ds, yytext);
break; break;
} }
} }
expr = palloc(sizeof(PLpgSQL_expr) + sizeof(int) * nparams - sizeof(int)); expr = palloc(sizeof(PLpgSQL_expr) + sizeof(int) * nparams - sizeof(int));
expr->dtype = PLPGSQL_DTYPE_EXPR; expr->dtype = PLPGSQL_DTYPE_EXPR;
expr->query = pstrdup(plpgsql_dstring_get(&ds)); expr->query = pstrdup(ds.data);
expr->plan = NULL; expr->plan = NULL;
expr->nparams = nparams; expr->nparams = nparams;
while(nparams-- > 0) while(nparams-- > 0)
expr->params[nparams] = params[nparams]; expr->params[nparams] = params[nparams];
plpgsql_dstring_free(&ds); pfree(ds.data);
check_sql_expr(expr->query); check_sql_expr(expr->query);
...@@ -3023,8 +3024,7 @@ make_case(int lineno, PLpgSQL_expr *t_expr, ...@@ -3023,8 +3024,7 @@ make_case(int lineno, PLpgSQL_expr *t_expr,
PLpgSQL_expr *expr = cwt->expr; PLpgSQL_expr *expr = cwt->expr;
int nparams = expr->nparams; int nparams = expr->nparams;
PLpgSQL_expr *new_expr; PLpgSQL_expr *new_expr;
PLpgSQL_dstring ds; StringInfoData ds;
char buff[32];
/* Must add the CASE variable as an extra param to expression */ /* Must add the CASE variable as an extra param to expression */
if (nparams >= MAX_EXPR_PARAMS) if (nparams >= MAX_EXPR_PARAMS)
...@@ -3041,22 +3041,19 @@ make_case(int lineno, PLpgSQL_expr *t_expr, ...@@ -3041,22 +3041,19 @@ make_case(int lineno, PLpgSQL_expr *t_expr,
new_expr->nparams = nparams + 1; new_expr->nparams = nparams + 1;
new_expr->params[nparams] = t_varno; new_expr->params[nparams] = t_varno;
/* copy expression query without SELECT keyword (expr->query + 7) */
Assert(strncmp(expr->query, "SELECT ", 7) == 0);
/* And do the string hacking */ /* And do the string hacking */
plpgsql_dstring_init(&ds); initStringInfo(&ds);
plpgsql_dstring_append(&ds, "SELECT $");
snprintf(buff, sizeof(buff), "%d", nparams + 1);
plpgsql_dstring_append(&ds, buff);
plpgsql_dstring_append(&ds, " IN (");
/* copy expression query without SELECT keyword */ appendStringInfo(&ds, "SELECT $%d IN(%s)",
Assert(strncmp(expr->query, "SELECT ", 7) == 0); nparams + 1,
plpgsql_dstring_append(&ds, expr->query + 7); expr->query + 7);
plpgsql_dstring_append_char(&ds, ')');
new_expr->query = pstrdup(plpgsql_dstring_get(&ds)); new_expr->query = pstrdup(ds.data);
plpgsql_dstring_free(&ds); pfree(ds.data);
pfree(expr->query); pfree(expr->query);
pfree(expr); pfree(expr);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.245 2009/07/18 19:15:42 tgl Exp $ * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.246 2009/07/22 02:31:38 joe Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "executor/spi_priv.h" #include "executor/spi_priv.h"
#include "funcapi.h" #include "funcapi.h"
#include "lib/stringinfo.h"
#include "nodes/nodeFuncs.h" #include "nodes/nodeFuncs.h"
#include "parser/scansup.h" #include "parser/scansup.h"
#include "storage/proc.h" #include "storage/proc.h"
...@@ -2394,11 +2395,11 @@ exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt) ...@@ -2394,11 +2395,11 @@ exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
if (stmt->message) if (stmt->message)
{ {
PLpgSQL_dstring ds; StringInfoData ds;
ListCell *current_param; ListCell *current_param;
char *cp; char *cp;
plpgsql_dstring_init(&ds); initStringInfo(&ds);
current_param = list_head(stmt->params); current_param = list_head(stmt->params);
for (cp = stmt->message; *cp; cp++) for (cp = stmt->message; *cp; cp++)
...@@ -2416,7 +2417,7 @@ exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt) ...@@ -2416,7 +2417,7 @@ exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
if (cp[1] == '%') if (cp[1] == '%')
{ {
plpgsql_dstring_append_char(&ds, cp[1]); appendStringInfoChar(&ds, '%');
cp++; cp++;
continue; continue;
} }
...@@ -2435,12 +2436,12 @@ exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt) ...@@ -2435,12 +2436,12 @@ exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
extval = "<NULL>"; extval = "<NULL>";
else else
extval = convert_value_to_string(paramvalue, paramtypeid); extval = convert_value_to_string(paramvalue, paramtypeid);
plpgsql_dstring_append(&ds, extval); appendStringInfoString(&ds, extval);
current_param = lnext(current_param); current_param = lnext(current_param);
exec_eval_cleanup(estate); exec_eval_cleanup(estate);
} }
else else
plpgsql_dstring_append_char(&ds, cp[0]); appendStringInfoChar(&ds, cp[0]);
} }
/* /*
...@@ -2452,8 +2453,8 @@ exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt) ...@@ -2452,8 +2453,8 @@ exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
(errcode(ERRCODE_SYNTAX_ERROR), (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("too many parameters specified for RAISE"))); errmsg("too many parameters specified for RAISE")));
err_message = plpgsql_dstring_get(&ds); err_message = ds.data;
/* No dstring_free here, the pfree(err_message) does it */ /* No pfree(ds.data), the pfree(err_message) does it */
} }
foreach(lc, stmt->options) foreach(lc, stmt->options)
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.79 2009/06/11 14:49:14 momjian Exp $ * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.80 2009/07/22 02:31:38 joe Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -28,90 +28,6 @@ static PLpgSQL_ns *ns_current = NULL; ...@@ -28,90 +28,6 @@ static PLpgSQL_ns *ns_current = NULL;
static bool ns_localmode = false; static bool ns_localmode = false;
/* ----------
* plpgsql_dstring_init Dynamic string initialization
* ----------
*/
void
plpgsql_dstring_init(PLpgSQL_dstring *ds)
{
ds->value = palloc(ds->alloc = 512);
ds->used = 1;
ds->value[0] = '\0';
}
/* ----------
* plpgsql_dstring_free Dynamic string destruction
* ----------
*/
void
plpgsql_dstring_free(PLpgSQL_dstring *ds)
{
pfree(ds->value);
}
static void
plpgsql_dstring_expand(PLpgSQL_dstring *ds, int needed)
{
/* Don't allow truncating the string */
Assert(needed > ds->alloc);
Assert(ds->used <= ds->alloc);
/* Might have to double more than once, if needed is large */
do
{
ds->alloc *= 2;
} while (needed > ds->alloc);
ds->value = repalloc(ds->value, ds->alloc);
}
/* ----------
* plpgsql_dstring_append Dynamic string extending
* ----------
*/
void
plpgsql_dstring_append(PLpgSQL_dstring *ds, const char *str)
{
int len = strlen(str);
int needed = ds->used + len;
if (needed > ds->alloc)
plpgsql_dstring_expand(ds, needed);
memcpy(&(ds->value[ds->used - 1]), str, len);
ds->used += len;
ds->value[ds->used - 1] = '\0';
}
/* ----------
* plpgsql_dstring_append_char Append a single character
* to a dynamic string
* ----------
*/
void
plpgsql_dstring_append_char(PLpgSQL_dstring *ds, char c)
{
if (ds->used == ds->alloc)
plpgsql_dstring_expand(ds, ds->used + 1);
ds->value[ds->used - 1] = c;
ds->value[ds->used] = '\0';
ds->used++;
}
/* ----------
* plpgsql_dstring_get Dynamic string get value
* ----------
*/
char *
plpgsql_dstring_get(PLpgSQL_dstring *ds)
{
return ds->value;
}
/* ---------- /* ----------
* plpgsql_ns_init Initialize the namestack * plpgsql_ns_init Initialize the namestack
* ---------- * ----------
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.113 2009/06/11 14:49:14 momjian Exp $ * $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.114 2009/07/22 02:31:38 joe Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -147,14 +147,6 @@ enum ...@@ -147,14 +147,6 @@ enum
**********************************************************************/ **********************************************************************/
typedef struct
{ /* Dynamic string control structure */
int alloc;
int used; /* Including NUL terminator */
char *value;
} PLpgSQL_dstring;
typedef struct typedef struct
{ /* Postgres data type */ { /* Postgres data type */
char *typname; /* (simple) name of the type */ char *typname; /* (simple) name of the type */
...@@ -851,16 +843,6 @@ extern void plpgsql_xact_cb(XactEvent event, void *arg); ...@@ -851,16 +843,6 @@ extern void plpgsql_xact_cb(XactEvent event, void *arg);
extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid, extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
SubTransactionId parentSubid, void *arg); SubTransactionId parentSubid, void *arg);
/* ----------
* Functions for the dynamic string handling in pl_funcs.c
* ----------
*/
extern void plpgsql_dstring_init(PLpgSQL_dstring *ds);
extern void plpgsql_dstring_free(PLpgSQL_dstring *ds);
extern void plpgsql_dstring_append(PLpgSQL_dstring *ds, const char *str);
extern void plpgsql_dstring_append_char(PLpgSQL_dstring *ds, char c);
extern char *plpgsql_dstring_get(PLpgSQL_dstring *ds);
/* ---------- /* ----------
* Functions for namestack handling in pl_funcs.c * Functions for namestack handling in pl_funcs.c
* ---------- * ----------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment