From 969ea2c42712036d473fab5d4db715452580c6d3 Mon Sep 17 00:00:00 2001 From: v-makouz Date: Thu, 10 Oct 2019 14:02:31 -0700 Subject: [PATCH] Fix for issue 617 (#623) * Merging updates. (#1) Merging updates. * fix for smalldatetime issue * Fixed a bad merge * Fix for inserting high unicode chars * merge with main branch * Fix for function sequence error * reverted unnecessary file changes * removed obsolete include * fix for 540 * fix for TVP type mismatch issue * Combined the IFs * Fix for high unicode insertion, WIP * Fix python2 high unicode insertion * Renamed a table to t1 --- src/params.cpp | 4 +--- tests2/sqlservertests.py | 7 +++++++ tests3/sqlservertests.py | 7 +++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/params.cpp b/src/params.cpp index ee665fcd..d07df8fb 100644 --- a/src/params.cpp +++ b/src/params.cpp @@ -1910,12 +1910,11 @@ bool ExecuteMulti(Cursor* cur, PyObject* pSql, PyObject* paramArrayObj) { PyObject* objCell = pInfo->cell; -#if PY_MAJOR_VERSION >= 3 // If the object is Unicode it needs to be converted into bytes before it can be used by SQLPutData if (PyUnicode_Check(objCell)) { const TextEnc& enc = cur->cnxn->sqlwchar_enc; - int cb = PyUnicode_GET_LENGTH(objCell); + int cb = PyUnicode_GET_DATA_SIZE(objCell) / 2; PyObject* bytes = NULL; const Py_UNICODE* source = PyUnicode_AS_UNICODE(objCell); @@ -1941,7 +1940,6 @@ bool ExecuteMulti(Cursor* cur, PyObject* pSql, PyObject* paramArrayObj) objCell = bytes; } } -#endif szLastFunction = "SQLPutData"; if (PyBytes_Check(objCell) diff --git a/tests2/sqlservertests.py b/tests2/sqlservertests.py index 62f44905..48a52d99 100755 --- a/tests2/sqlservertests.py +++ b/tests2/sqlservertests.py @@ -470,6 +470,13 @@ def test_fast_executemany_to_datetime2(self): self.cursor.executemany(sql, params) self.assertEqual(self.cursor.execute("SELECT CAST(dt2 AS VARCHAR) FROM ##issue540").fetchval(), '2019-03-12 10:00:00.12') + def test_high_unicode(self): + v = u"🎥" + self.cursor.fast_executemany = True + self.cursor.execute("CREATE TABLE t1 (col1 nvarchar(max) null)") + self.cursor.executemany("INSERT INTO t1 (col1) VALUES (?)", [[v,]]) + self.assertEqual(self.cursor.execute("SELECT * FROM t1").fetchone()[0], v) + # # binary # diff --git a/tests3/sqlservertests.py b/tests3/sqlservertests.py index e4f28b41..f4f1c99f 100644 --- a/tests3/sqlservertests.py +++ b/tests3/sqlservertests.py @@ -462,6 +462,13 @@ def test_fast_executemany_to_datetime2(self): self.cursor.executemany(sql, params) self.assertEqual(self.cursor.execute("SELECT CAST(dt2 AS VARCHAR) FROM ##issue540").fetchval(), '2019-03-12 10:00:00.12') + def test_high_unicode(self): + v = "🎥" + self.cursor.fast_executemany = True + self.cursor.execute("CREATE TABLE t1 (col1 nvarchar(max) null)") + self.cursor.executemany("INSERT INTO t1 (col1) VALUES (?)", [[v,]]) + self.assertEqual(self.cursor.execute("SELECT * FROM t1").fetchone()[0], v) + # # binary #