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). --- sqlite/types/driver.cxx | 34 ++++++++++++++++++++++++ sqlite/types/test.hxx | 70 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 94 insertions(+), 10 deletions(-) (limited to 'sqlite/types') diff --git a/sqlite/types/driver.cxx b/sqlite/types/driver.cxx index 69e8473..4e619fb 100644 --- a/sqlite/types/driver.cxx +++ b/sqlite/types/driver.cxx @@ -69,6 +69,40 @@ main (int argc, char* argv[]) t.commit (); } #endif + + // Test char/wchar_t arrays + // + { +#ifndef _WIN32 + char_array o1 (1, ""); + char_array o2 (2, "1234567890"); + char_array o3 (3, "12345678901234567"); +#else + char_array o1 (1, "", L""); + char_array o2 (2, "1234567890", L"123456789\x00C8"); + char_array o3 (3, "12345678901234567", L"1234567890123456\x00C8"); +#endif + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + { + 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); + } + } } catch (const odb::exception& e) { diff --git a/sqlite/types/test.hxx b/sqlite/types/test.hxx index fdaa541..a19cd20 100644 --- a/sqlite/types/test.hxx +++ b/sqlite/types/test.hxx @@ -9,6 +9,11 @@ #include #include #include // std::auto_ptr +#include // std::strncpy, std::str[n]cmp + +#ifdef _WIN32 +# include // std::wcsncpy, std::wcs[n]cmp +#endif #include @@ -17,14 +22,8 @@ typedef std::auto_ptr string_ptr; #pragma db object struct object { - object (unsigned long id) - : id_ (id) - { - } - - object () - { - } + object () {} + object (unsigned long id): id_ (id) {} #pragma db id unsigned long id_; @@ -38,8 +37,7 @@ struct object #pragma db type("REAL") double real_; - #pragma db type("REAL") - double nan_; + double nan_; // Represented in SQLite as NULL. #pragma db type("TEXT") std::string text_; @@ -73,4 +71,56 @@ struct object } }; +// Test char/wchar_t arrays. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id + , const char* s +#ifdef _WIN32 + , const wchar_t* ws +#endif + ) + : id_ (id) + { + std::strncpy (s1, s, sizeof (s1)); + s2[0] = c1 = *s; + +#ifdef _WIN32 + std::wcsncpy (ws1, ws, sizeof (ws1) / 2); + ws2[0] = wc1 = *ws; +#endif + } + + #pragma db id + unsigned long id_; + + char s1[17]; + char s2[1]; + char c1; + +#ifdef _WIN32 + wchar_t ws1[17]; + wchar_t ws2[1]; + wchar_t wc1; +#endif + + bool + operator== (const char_array& y) const + { + return id_ == y.id_ + && std::strncmp (s1, y.s1, sizeof (s1)) == 0 + && s2[0] == y.s2[0] + && c1 == y.c1 +#ifdef _WIN32 + && std::wcsncmp (ws1, y.ws1, sizeof (ws1) / 2) == 0 + && ws2[0] == y.ws2[0] + && wc1 == y.wc1 +#endif + ; + } +}; + #endif // TEST_HXX -- cgit v1.1