From 6d36a3b005962e253c03f5cc0b9586aa52c505fa Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Tue, 20 Aug 2013 23:26:30 +0300 Subject: [PATCH] Handle turning standard_conforming_strings to off in mid-session. We already watched for a ParameterStatus response indicating that standard_conforming_strings was turned on, and acted accordingly, but if it was turned off, we did nothing. Fix that. Also add a test case for that. --- connection.c | 4 +++ test/Makefile | 2 +- test/expected/quotes.out | 23 ++++++++++++ test/src/quotes-test.c | 78 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 test/expected/quotes.out create mode 100644 test/src/quotes-test.c diff --git a/connection.c b/connection.c index e452bc9..cc54524 100644 --- a/connection.c +++ b/connection.c @@ -1098,6 +1098,10 @@ inolog("parameter name=%s\n", msgbuffer); { conn->escape_in_literal = '\0'; } + if (stricmp(msgbuffer, "off") == 0) + { + conn->escape_in_literal = ESCAPE_IN_LITERAL; + } } else if (stricmp(msgbuffer, "server_version") == 0) { diff --git a/test/Makefile b/test/Makefile index 30dd5df..c368937 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,6 @@ TESTS = connect stmthandles select getresult prepare params notice \ arraybinding insertreturning dataatexecution boolsaschar cvtnulldate \ - alter + alter quotes TESTBINS = $(patsubst %,src/%-test, $(TESTS)) TESTSQLS = $(patsubst %,sql/%.sql, $(TESTS)) diff --git a/test/expected/quotes.out b/test/expected/quotes.out new file mode 100644 index 0000000..e8560b0 --- /dev/null +++ b/test/expected/quotes.out @@ -0,0 +1,23 @@ +\! ./src/quotes-test +connected +Result set: +Result set: +foo +param'quote +Result set: +foo +param\backslash +Result set: +foo +ends with backslash\ +Result set: +Result set: +foo +param'quote +Result set: +foo +param\backslash +Result set: +foo +ends with backslash\ +disconnecting diff --git a/test/src/quotes-test.c b/test/src/quotes-test.c new file mode 100644 index 0000000..17fcf7b --- /dev/null +++ b/test/src/quotes-test.c @@ -0,0 +1,78 @@ +/* Test parameter quoting, with standard_conforming_strings on/off */ + +#include +#include +#include + +#include "common.h" + +static void +execWithParam(HSTMT hstmt, char *param) +{ + SQLLEN cbParam1; + int rc; + + /* bind param */ + cbParam1 = SQL_NTS; + rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, + SQL_C_CHAR, /* value type */ + SQL_CHAR, /* param type */ + 20, /* column size */ + 0, /* dec digits */ + param, /* param value ptr */ + 0, /* buffer len */ + &cbParam1 /* StrLen_or_IndPtr */); + CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt); + + rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT 'foo' UNION ALL SELECT ?", SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); + print_result(hstmt); + + rc = SQLFreeStmt(hstmt, SQL_CLOSE); + CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); +} + +int main(int argc, char **argv) +{ + int rc; + HSTMT hstmt = SQL_NULL_HSTMT; + char sql[100000]; + char *sqlend; + int i; + char *strings[] = { + "param'quote", + "param\\backslash", + "ends with backslash\\", + NULL }; + + test_connect(); + + rc = SQLAllocStmt(conn, &hstmt); + if (!SQL_SUCCEEDED(rc)) + { + print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); + exit(1); + } + + rc = SQLExecDirect(hstmt, (SQLCHAR *) "SET standard_conforming_strings=on", SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); + print_result(hstmt); + rc = SQLFreeStmt(hstmt, SQL_CLOSE); + CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); + + for (i = 0; strings[i] != NULL; i++) + execWithParam(hstmt, strings[i]); + + rc = SQLExecDirect(hstmt, (SQLCHAR *) "SET standard_conforming_strings=off", SQL_NTS); + CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt); + print_result(hstmt); + rc = SQLFreeStmt(hstmt, SQL_CLOSE); + CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); + + for (i = 0; strings[i] != NULL; i++) + execWithParam(hstmt, strings[i]); + + /* Clean up */ + test_disconnect(); +} + -- 2.39.5