From 89de275db2b77d0abf9fa1ec066ef11e262c88af Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 24 Jan 2013 15:10:22 +0200 Subject: Add support for mapping char[N] to CHAR/VARCHAR database types Also improve query support for arrays (decaying). --- mssql/types/driver.cxx | 47 ++++++++++++++++++++++ mssql/types/test.hxx | 103 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 149 insertions(+), 1 deletion(-) (limited to 'mssql/types') diff --git a/mssql/types/driver.cxx b/mssql/types/driver.cxx index a779079..6276bab 100644 --- a/mssql/types/driver.cxx +++ b/mssql/types/driver.cxx @@ -218,6 +218,53 @@ main (int argc, char* argv[]) } } + // Test char/wchar_t arrays. + // + { + char_array o1 (1, "", L""); + char_array o2 (2, "1234567890", L"12345678\x1FFF\xD7FF"); + char_array o3 (3, "1234567890123456", L"12345678901234\x1FFF\xD7FF"); + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + // SQL Server returns padded values for CHAR(N)/NCHAR(N). + // + memcpy (o1.s2, " ", 16); + o1.s3[0] = o1.c1 = ' '; + memcpy (o2.s2, "1234567890 ", 16); + + memset (o1.ls2, ' ', 1025); + memset (o2.ls2 + 10, ' ', 1025 - 10); + + memcpy (o1.ws2, L" ", 16 * sizeof (wchar_t)); + o1.ws3[0] = o1.wc1 = L' '; + memcpy (o2.ws2, L"12345678\x1FFF\xD7FF ", 16 * sizeof (wchar_t)); + + for (size_t i (0); i < 257; ++i) + o1.lws2[i] = L' '; + + for (size_t i (10); i < 257; ++i) + o2.lws2[i] = L' '; + + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (1)); + auto_ptr p2 (db->load (2)); + auto_ptr p3 (db->load (3)); + t.commit (); + + assert (o1 == *p1); + assert (o2 == *p2); + assert (o3 == *p3); + } + } + // Test optimistic concurrency using ROWVERSION. // { diff --git a/mssql/types/test.hxx b/mssql/types/test.hxx index 3e20e73..884db35 100644 --- a/mssql/types/test.hxx +++ b/mssql/types/test.hxx @@ -23,7 +23,8 @@ typedef struct _GUID #include #include #include // std::auto_ptr -#include // std::memcmp +#include // std::memcmp, std::strncpy, std::str[n]cmp +#include // std::wcsncpy, std::wcs[n]cmp #include @@ -369,6 +370,106 @@ struct long_cont } }; +// Test char/wchar_t arrays. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id, const char* s, const wchar_t* ws) + : id_ (id) + { + std::strncpy (s1, s, sizeof (s1)); + std::strncpy (s2, s, sizeof (s2)); + s3[0] = c1 = *s; + + std::wcsncpy (ws1, ws, sizeof (ws1) / sizeof(wchar_t)); + std::wcsncpy (ws2, ws, sizeof (ws2) / sizeof(wchar_t)); + ws3[0] = wc1 = *ws; + + if (std::strlen (s) == sizeof (s2)) + { + std::memset (ls1, '1', 1025); + ls1[1025] = '\0'; + std::memset (ls2, '2', 1025); + + for (std::size_t i (0); i < 257; ++i) + { + lws1[i] = L'1'; + lws2[i] = L'2'; + } + lws1[257] = L'\0'; + } + else + { + std::strcpy (ls1, s); + std::strcpy (ls2, s); + + std::wcscpy (lws1, ws); + std::wcscpy (lws2, ws); + } + } + + #pragma db id + unsigned long id_; + + // + // + char s1[17]; + + #pragma db type("CHAR(16)") + char s2[16]; + + char s3[1]; + char c1; + + // Long data. + // + char ls1[1026]; + + #pragma db type("CHAR(1025)") + char ls2[1025]; + + // + // + wchar_t ws1[17]; + + #pragma db type("NCHAR(16)") + wchar_t ws2[16]; + + wchar_t ws3[1]; + wchar_t wc1; + + // Long data. + // + wchar_t lws1[258]; + + #pragma db type("NCHAR(257)") + wchar_t lws2[257]; + + bool + operator== (const char_array& y) const + { + return id_ == y.id_ && + + std::strcmp (s1, y.s1) == 0 && + std::strncmp (s2, y.s2, sizeof (s2)) == 0 && + s3[0] == y.s3[0] && + c1 == y.c1 && + + std::strcmp (ls1, y.ls1) == 0 && + std::strncmp (ls2, y.ls2, sizeof (ls2)) == 0 && + + std::wcscmp (ws1, y.ws1) == 0 && + std::wcsncmp (ws2, y.ws2, sizeof (ws2) / sizeof (wchar_t)) == 0 && + ws3[0] == y.ws3[0] && + wc1 == y.wc1 && + + std::wcscmp (lws1, y.lws1) == 0 && + std::wcsncmp (lws2, y.lws2, sizeof (lws2) / sizeof (wchar_t)) == 0; + } +}; + // Test optimistic concurrency using ROWVERSION. // #pragma db object optimistic -- cgit v1.1