Handle turning standard_conforming_strings to off in mid-session.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Tue, 20 Aug 2013 20:26:30 +0000 (23:26 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Tue, 20 Aug 2013 20:28:18 +0000 (23:28 +0300)
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
test/Makefile
test/expected/quotes.out [new file with mode: 0644]
test/src/quotes-test.c [new file with mode: 0644]

index e452bc90f597555dbd89ed342d395535018733bc..cc545240ce7940fe539312f36e2caa393840c0d9 100644 (file)
@@ -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)
    {
index 30dd5df10ff09d95a12b520e53757a8367951daf..c368937e64c351470e260c33d076d5d3312f6a48 100644 (file)
@@ -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 (file)
index 0000000..e8560b0
--- /dev/null
@@ -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 (file)
index 0000000..17fcf7b
--- /dev/null
@@ -0,0 +1,78 @@
+/* Test parameter quoting, with standard_conforming_strings on/off */
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#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();
+}
+