From ef92d12421e039f21f3c26e8f00d8e9c2d23c843 Mon Sep 17 00:00:00 2001 From: Ishjaap Singh Date: Thu, 19 Nov 2026 16:55:07 +0000 Subject: [PATCH v1 4/4] ecpg: Quote host variable values in EXECUTE name(...) For EXECUTE name(:var, ...), ecpglib inserts the host variable values into the text of the EXECUTE command and sends it to the server as plain SQL. The values were inserted without quoting. So a character host variable had to contain a complete, quoted SQL literal, otherwise its contents were parsed as part of the command, and boolean, date, timestamp and interval host variables, as well as NaN and infinite numbers, didn't work in this list at all. Write every value as a quoted literal with PQescapeLiteral(), so that the value of the host variable is passed as it is. NULL is still inserted as the keyword NULL, and bytea keeps its own literal. Numbers are quoted too, which makes NaN and Infinity work and keeps the code free of a list of types. Values are now converted to the parameter's type the same way as with EXECUTE ... USING. This is for master only, since it changes behavior: - Programs that put a quoted literal, or other SQL text, into a character host variable in this list must be changed. - Some numbers convert differently: a double 1.5 for an integer parameter used to be rounded to 2 and is now rejected as invalid input for type integer, and an int 1 for a boolean parameter, which used to fail, is now accepted. The new behavior applies to any program using the new libecpg, whatever the server version. Replace the documentation of the old behavior with a description of the new one and a note about the change. --- doc/src/sgml/ecpg.sgml | 72 +++--- src/interfaces/ecpg/ecpglib/execute.c | 35 +++ .../ecpg/test/expected/sql-prepareas.c | 205 ++++++++++++++---- .../ecpg/test/expected/sql-prepareas.stdout | 3 + src/interfaces/ecpg/test/sql/prepareas.pgc | 48 +++- 5 files changed, 266 insertions(+), 97 deletions(-) diff --git a/doc/src/sgml/ecpg.sgml b/doc/src/sgml/ecpg.sgml index 0558a447e30..61580e8ac68 100644 --- a/doc/src/sgml/ecpg.sgml +++ b/doc/src/sgml/ecpg.sgml @@ -2009,53 +2009,31 @@ EXEC SQL EXECUTE mystmt2(:id, :name); In this form, host variables in the parenthesized list are not sent to the server as separate parameter values. Instead, the value of - each host variable is inserted, without quoting, into the text of - the EXECUTE command, and the server parses the - resulting command as SQL. This is the case regardless of how the - statement was prepared. As a consequence: + each host variable is written into the text of + the EXECUTE command as a quoted SQL literal, + escaped as needed, so the value is passed exactly as it is. This is + the case regardless of how the statement was prepared. To pass host + variable values as separate parameter values, use + USING as shown above. - - - - Integer, floating-point and numeric host variables work - as expected for ordinary finite values. NaN and - infinite values of floating-point and numeric host - variables do not work. - - - - - - A character host variable must contain a complete SQL literal for - the parameter, including the enclosing single quotes and escaped as - required by the connection's client encoding and settings. For - example, to pass the string O'Brien, the variable - must contain 'O''Brien'. A value that is not - written this way is parsed as part of the SQL command, so a value - taken from untrusted input can change the command that is executed. - The libpq function - , called with the connection - handle returned by ECPGget_PGconn() - (see ), produces such a literal. - - - - - - Boolean, date, timestamp and interval host variables do not work in - this list, because their values are not inserted as valid literals - of their types. - - - - - - To pass host variable values to a prepared statement as separate - parameter values, without these restrictions, use - USING as shown above. This works regardless of how - the statement was prepared. - + + + Before PostgreSQL 20, + ecpglib inserted the values in this list + into the command text without quoting, so a character host variable + had to contain a complete, quoted SQL literal, and boolean and + date/time host variables did not work. A program that puts a quoted + literal or other SQL text into a character host variable in this list + must be changed to pass the plain value; otherwise the quotes or SQL + text become part of the value. Numeric values are now also passed + as quoted literals and converted the same way as with + USING; for example, a double host + variable containing 1.5 for an integer parameter is now + an error instead of being rounded. What counts is the version of + ecpglib the program uses, not the server version. + + When you don't need the prepared statement anymore, you should @@ -7723,8 +7701,8 @@ PREPARE prepared_name FROM A prepared statement can be executed with USING, or with its parameter values in parentheses after the statement name. - In the second form, host variable values are inserted into the - command text without quoting; see . + In the second form, host variable values are written into the + command text as quoted SQL literals; see . diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c index 66ef0bdcc9d..463a55744e9 100644 --- a/src/interfaces/ecpg/ecpglib/execute.c +++ b/src/interfaces/ecpg/ecpglib/execute.c @@ -1505,6 +1505,41 @@ ecpg_build_params(struct statement *stmt) } tobeinserted = p; } + else + { + /* + * Write the value as a quoted literal, so that quotes or + * other SQL in it are read as part of the value. + * PQescapeLiteral() takes the connection's client encoding + * and standard_conforming_strings into account. Its result + * is allocated by libpq, so copy it into memory that + * insert_tobeinserted() can free. + */ + char *escaped; + + escaped = PQescapeLiteral(stmt->connection->connection, + tobeinserted, strlen(tobeinserted)); + ecpg_free(tobeinserted); + if (escaped == NULL) + { + /* + * This fails, for example, on a string that is not valid + * in the client encoding. Report libpq's message. + */ + ecpg_raise_backend(stmt->lineno, NULL, + stmt->connection->connection, + stmt->compat); + ecpg_free_params(stmt, false); + return false; + } + tobeinserted = ecpg_strdup(escaped, stmt->lineno, NULL); + PQfreemem(escaped); + if (tobeinserted == NULL) + { + ecpg_free_params(stmt, false); + return false; + } + } /* * Replace the whole placeholder: "?", or "$" followed by all of diff --git a/src/interfaces/ecpg/test/expected/sql-prepareas.c b/src/interfaces/ecpg/test/expected/sql-prepareas.c index 2467d67f198..6b5886a11ef 100644 --- a/src/interfaces/ecpg/test/expected/sql-prepareas.c +++ b/src/interfaces/ecpg/test/expected/sql-prepareas.c @@ -10,7 +10,7 @@ #include #include #include - +#include #line 1 "regression.h" @@ -701,39 +701,38 @@ if (sqlca.sqlcode < 0) sqlprint();} /* * A value that contains a quote or $n (or ?, with -r questionmarks) must - * not hide the placeholder after it or be taken for one. A bytea value - * is inserted as a quoted literal, and so is a character value that - * already holds one. Placeholders from $10 up must be replaced whole. - * All rows must be stored as given. + * not hide the placeholder after it or be taken for one. Every value is + * inserted as a quoted literal. Placeholders from $10 up must be + * replaced whole. All rows must be stored as given. */ { /* exec sql begin declare section */ -#line 217 "prepareas.pgc" +#line 216 "prepareas.pgc" struct bytea_1 { int len; char arr[ 4 ]; } v_bytea ; -#line 218 "prepareas.pgc" +#line 217 "prepareas.pgc" char v_literal [ 16 ] ; /* exec sql end declare section */ -#line 219 "prepareas.pgc" +#line 218 "prepareas.pgc" printf("+++++ Test for a quoted value before another value in EXECUTE(exprlist) +++++\n"); { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table test_pos ( a text , b int )", ECPGt_EOIT, ECPGt_EORT); -#line 222 "prepareas.pgc" +#line 221 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 222 "prepareas.pgc" +#line 221 "prepareas.pgc" { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepare, "prepare $0 ( bytea , int ) as insert into test_pos values ( encode ( $1 , 'escape' ) , $2 )", ECPGt_const,"stmt_pos_b",(long)10,(long)1,strlen("stmt_pos_b"), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); -#line 223 "prepareas.pgc" +#line 222 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 223 "prepareas.pgc" +#line 222 "prepareas.pgc" v_bytea.len = 2; memcpy(v_bytea.arr, "ab", 2); @@ -744,20 +743,20 @@ if (sqlca.sqlcode < 0) sqlprint();} ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); -#line 226 "prepareas.pgc" +#line 225 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 226 "prepareas.pgc" +#line 225 "prepareas.pgc" { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepare, "prepare $0 ( text , int ) as insert into test_pos values ( $1 , $2 )", ECPGt_const,"stmt_pos_t",(long)10,(long)1,strlen("stmt_pos_t"), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); -#line 227 "prepareas.pgc" +#line 226 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 227 "prepareas.pgc" +#line 226 "prepareas.pgc" - strcpy(v_literal, "'cd'"); + strcpy(v_literal, "cd"); { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_exec_with_exprlist, "execute $0 ( $1 , $2 )", ECPGt_const,"stmt_pos_t",(long)10,(long)1,strlen("stmt_pos_t"), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, @@ -765,12 +764,12 @@ if (sqlca.sqlcode < 0) sqlprint();} ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); -#line 229 "prepareas.pgc" +#line 228 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 229 "prepareas.pgc" +#line 228 "prepareas.pgc" - strcpy(v_literal, "'x$2'"); + strcpy(v_literal, "x$2"); { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_exec_with_exprlist, "execute $0 ( $1 , $2 )", ECPGt_const,"stmt_pos_t",(long)10,(long)1,strlen("stmt_pos_t"), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, @@ -778,12 +777,12 @@ if (sqlca.sqlcode < 0) sqlprint();} ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); -#line 231 "prepareas.pgc" +#line 230 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 231 "prepareas.pgc" +#line 230 "prepareas.pgc" - strcpy(v_literal, "'a?b'"); + strcpy(v_literal, "a?b"); { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_exec_with_exprlist, "execute $0 ( $1 , $2 )", ECPGt_const,"stmt_pos_t",(long)10,(long)1,strlen("stmt_pos_t"), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, @@ -791,27 +790,27 @@ if (sqlca.sqlcode < 0) sqlprint();} ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); -#line 233 "prepareas.pgc" +#line 232 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 233 "prepareas.pgc" +#line 232 "prepareas.pgc" { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from test_pos where a in ( 'ab' , 'cd' , 'x$2' , 'a?b' ) and b = 2", ECPGt_EOIT, ECPGt_int,&(ivar1),(long)1,(long)1,sizeof(int), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT); -#line 234 "prepareas.pgc" +#line 233 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 234 "prepareas.pgc" +#line 233 "prepareas.pgc" printf("rows stored (must be 4) = %d\n", ivar1); { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepare, "prepare $0 ( int , int , int , int , int , int , int , int , int , int , int ) as insert into test_pos values ( concat_ws ( ',' , $1 , $2 , $3 , $4 , $5 , $6 , $7 , $8 , $9 , $10 ) , $11 )", ECPGt_const,"stmt_pos_11",(long)11,(long)1,strlen("stmt_pos_11"), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); -#line 237 "prepareas.pgc" +#line 236 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 237 "prepareas.pgc" +#line 236 "prepareas.pgc" { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_exec_with_exprlist, "execute $0 ( $1 , $2 , $3 , $4 , $5 , $6 , $7 , $8 , $9 , $10 , $11 )", ECPGt_const,"stmt_pos_11",(long)11,(long)1,strlen("stmt_pos_11"), @@ -838,57 +837,179 @@ if (sqlca.sqlcode < 0) sqlprint();} ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_int,&(ivar2),(long)1,(long)1,sizeof(int), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); -#line 239 "prepareas.pgc" +#line 238 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 239 "prepareas.pgc" +#line 238 "prepareas.pgc" { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from test_pos where a = '2,2,2,2,2,2,2,2,2,2' and b = 2", ECPGt_EOIT, ECPGt_int,&(ivar1),(long)1,(long)1,sizeof(int), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT); -#line 240 "prepareas.pgc" +#line 239 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 240 "prepareas.pgc" +#line 239 "prepareas.pgc" printf("eleven values stored (must be 1) = %d\n", ivar1); { ECPGdeallocate(__LINE__, 0, NULL, "stmt_pos_b"); +#line 241 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 241 "prepareas.pgc" + + { ECPGdeallocate(__LINE__, 0, NULL, "stmt_pos_t"); #line 242 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} #line 242 "prepareas.pgc" - { ECPGdeallocate(__LINE__, 0, NULL, "stmt_pos_t"); + { ECPGdeallocate(__LINE__, 0, NULL, "stmt_pos_11"); #line 243 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} #line 243 "prepareas.pgc" - { ECPGdeallocate(__LINE__, 0, NULL, "stmt_pos_11"); + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test_pos", ECPGt_EOIT, ECPGt_EORT); #line 244 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} #line 244 "prepareas.pgc" - { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test_pos", ECPGt_EOIT, ECPGt_EORT); -#line 245 "prepareas.pgc" + } + + /* + * A character value containing SQL text must be stored as is, and the + * INSERT into test_marker in it must not run. Boolean, date and NaN + * values, which can't be written as bare words, must be stored too. + */ + { + /* exec sql begin declare section */ + + + + + +#line 254 "prepareas.pgc" + char v_text [ 64 ] ; + +#line 255 "prepareas.pgc" + bool v_bool = true ; + +#line 256 "prepareas.pgc" + date v_date ; + +#line 257 "prepareas.pgc" + double v_double ; +/* exec sql end declare section */ +#line 258 "prepareas.pgc" + + + printf("+++++ Test for quoting values in EXECUTE(exprlist) +++++\n"); + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table test_quote ( t text , b bool , d date , f float8 )", ECPGt_EOIT, ECPGt_EORT); +#line 261 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 261 "prepareas.pgc" + + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table test_marker ( c int )", ECPGt_EOIT, ECPGt_EORT); +#line 262 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 262 "prepareas.pgc" + + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepare, "prepare $0 ( text , bool , date , float8 ) as insert into test_quote values ( $1 , $2 , $3 , $4 )", + ECPGt_const,"stmt_q",(long)6,(long)1,strlen("stmt_q"), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); +#line 263 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 263 "prepareas.pgc" + + v_date = PGTYPESdate_from_asc("2026-11-18", NULL); + v_double = strtod("NaN", NULL); + strcpy(v_text, "x'); insert into test_marker values(1); --"); + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_exec_with_exprlist, "execute $0 ( $1 , $2 , $3 , $4 )", + ECPGt_const,"stmt_q",(long)6,(long)1,strlen("stmt_q"), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_char,(v_text),(long)64,(long)1,(64)*sizeof(char), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_bool,&(v_bool),(long)1,(long)1,sizeof(bool), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_date,&(v_date),(long)1,(long)1,sizeof(date), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_double,&(v_double),(long)1,(long)1,sizeof(double), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); +#line 267 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 267 "prepareas.pgc" + + strcpy(v_text, "O'Brien"); + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_exec_with_exprlist, "execute $0 ( $1 , $2 , $3 , $4 )", + ECPGt_const,"stmt_q",(long)6,(long)1,strlen("stmt_q"), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_char,(v_text),(long)64,(long)1,(64)*sizeof(char), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_bool,&(v_bool),(long)1,(long)1,sizeof(bool), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_date,&(v_date),(long)1,(long)1,sizeof(date), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, + ECPGt_double,&(v_double),(long)1,(long)1,sizeof(double), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT); +#line 269 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 269 "prepareas.pgc" + + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from test_marker", ECPGt_EOIT, + ECPGt_int,&(ivar1),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT); +#line 270 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 270 "prepareas.pgc" + + printf("rows in test_marker (must be 0) = %d\n", ivar1); + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from test_quote where ( t = 'O''Brien' or t like 'x''); insert%' ) and b and d = '2026-11-18' and f = 'NaN'", ECPGt_EOIT, + ECPGt_int,&(ivar1),(long)1,(long)1,sizeof(int), + ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT); +#line 273 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 273 "prepareas.pgc" + + printf("rows stored (must be 2) = %d\n", ivar1); + { ECPGdeallocate(__LINE__, 0, NULL, "stmt_q"); +#line 275 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 275 "prepareas.pgc" + + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test_quote", ECPGt_EOIT, ECPGt_EORT); +#line 276 "prepareas.pgc" + +if (sqlca.sqlcode < 0) sqlprint();} +#line 276 "prepareas.pgc" + + { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test_marker", ECPGt_EOIT, ECPGt_EORT); +#line 277 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 245 "prepareas.pgc" +#line 277 "prepareas.pgc" } { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test", ECPGt_EOIT, ECPGt_EORT); -#line 248 "prepareas.pgc" +#line 280 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 248 "prepareas.pgc" +#line 280 "prepareas.pgc" { ECPGtrans(__LINE__, NULL, "commit work"); -#line 249 "prepareas.pgc" +#line 281 "prepareas.pgc" if (sqlca.sqlcode < 0) sqlprint();} -#line 249 "prepareas.pgc" +#line 281 "prepareas.pgc" return 0; diff --git a/src/interfaces/ecpg/test/expected/sql-prepareas.stdout b/src/interfaces/ecpg/test/expected/sql-prepareas.stdout index ac912227a7c..bc81e4983cc 100644 --- a/src/interfaces/ecpg/test/expected/sql-prepareas.stdout +++ b/src/interfaces/ecpg/test/expected/sql-prepareas.stdout @@ -69,3 +69,6 @@ rows with NULL c1 (must be 1) = 1 +++++ Test for a quoted value before another value in EXECUTE(exprlist) +++++ rows stored (must be 4) = 4 eleven values stored (must be 1) = 1 ++++++ Test for quoting values in EXECUTE(exprlist) +++++ +rows in test_marker (must be 0) = 0 +rows stored (must be 2) = 2 diff --git a/src/interfaces/ecpg/test/sql/prepareas.pgc b/src/interfaces/ecpg/test/sql/prepareas.pgc index 2cf4526a65d..4933030580e 100644 --- a/src/interfaces/ecpg/test/sql/prepareas.pgc +++ b/src/interfaces/ecpg/test/sql/prepareas.pgc @@ -1,7 +1,7 @@ #include #include #include - +#include exec sql include ../regression; exec sql whenever sqlerror sqlprint; @@ -207,10 +207,9 @@ int main(void) /* * A value that contains a quote or $n (or ?, with -r questionmarks) must - * not hide the placeholder after it or be taken for one. A bytea value - * is inserted as a quoted literal, and so is a character value that - * already holds one. Placeholders from $10 up must be replaced whole. - * All rows must be stored as given. + * not hide the placeholder after it or be taken for one. Every value is + * inserted as a quoted literal. Placeholders from $10 up must be + * replaced whole. All rows must be stored as given. */ { exec sql begin declare section; @@ -225,11 +224,11 @@ int main(void) memcpy(v_bytea.arr, "ab", 2); exec sql execute stmt_pos_b(:v_bytea, :ivar2); exec sql prepare stmt_pos_t(text,int) as insert into test_pos values($1,$2); - strcpy(v_literal, "'cd'"); + strcpy(v_literal, "cd"); exec sql execute stmt_pos_t(:v_literal, :ivar2); - strcpy(v_literal, "'x$2'"); + strcpy(v_literal, "x$2"); exec sql execute stmt_pos_t(:v_literal, :ivar2); - strcpy(v_literal, "'a?b'"); + strcpy(v_literal, "a?b"); exec sql execute stmt_pos_t(:v_literal, :ivar2); exec sql select count(*) into :ivar1 from test_pos where a in ('ab', 'cd', 'x$2', 'a?b') and b = 2; printf("rows stored (must be 4) = %d\n", ivar1); @@ -245,6 +244,39 @@ int main(void) exec sql drop table test_pos; } + /* + * A character value containing SQL text must be stored as is, and the + * INSERT into test_marker in it must not run. Boolean, date and NaN + * values, which can't be written as bare words, must be stored too. + */ + { + exec sql begin declare section; + char v_text[64]; + bool v_bool = true; + date v_date; + double v_double; + exec sql end declare section; + + printf("+++++ Test for quoting values in EXECUTE(exprlist) +++++\n"); + exec sql create table test_quote (t text, b bool, d date, f float8); + exec sql create table test_marker (c int); + exec sql prepare stmt_q(text,bool,date,float8) as insert into test_quote values($1,$2,$3,$4); + v_date = PGTYPESdate_from_asc("2026-11-18", NULL); + v_double = strtod("NaN", NULL); + strcpy(v_text, "x'); insert into test_marker values(1); --"); + exec sql execute stmt_q(:v_text, :v_bool, :v_date, :v_double); + strcpy(v_text, "O'Brien"); + exec sql execute stmt_q(:v_text, :v_bool, :v_date, :v_double); + exec sql select count(*) into :ivar1 from test_marker; + printf("rows in test_marker (must be 0) = %d\n", ivar1); + exec sql select count(*) into :ivar1 from test_quote + where (t = 'O''Brien' or t like 'x''); insert%') and b and d = '2026-11-18' and f = 'NaN'; + printf("rows stored (must be 2) = %d\n", ivar1); + exec sql deallocate stmt_q; + exec sql drop table test_quote; + exec sql drop table test_marker; + } + exec sql drop table test; exec sql commit work; -- 2.43.0