diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2013-01-24 15:10:22 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2013-01-24 15:10:22 +0200 |
commit | 89de275db2b77d0abf9fa1ec066ef11e262c88af (patch) | |
tree | 06cb2272c5e791946dc58421ed3f2d03b7c8122e | |
parent | 539c92147e8d58c49f350c4070051a6ddf6b2354 (diff) |
Add support for mapping char[N] to CHAR/VARCHAR database types
Also improve query support for arrays (decaying).
-rw-r--r-- | common/makefile | 3 | ||||
-rw-r--r-- | common/query/array/driver.cxx | 170 | ||||
-rw-r--r-- | common/query/array/makefile | 116 | ||||
-rw-r--r-- | common/query/array/test.hxx | 77 | ||||
-rw-r--r-- | common/query/array/test.std | 0 | ||||
-rw-r--r-- | common/query/basics/driver.cxx (renamed from common/query/driver.cxx) | 4 | ||||
-rw-r--r-- | common/query/basics/makefile (renamed from common/query/makefile) | 14 | ||||
-rw-r--r-- | common/query/basics/test.hxx (renamed from common/query/test.hxx) | 2 | ||||
-rw-r--r-- | common/query/basics/test.std (renamed from common/query/test.std) | 0 | ||||
-rw-r--r-- | libcommon/common/common.hxx | 4 | ||||
-rw-r--r-- | libcommon/common/common.txx | 2 | ||||
-rw-r--r-- | mssql/types/driver.cxx | 47 | ||||
-rw-r--r-- | mssql/types/test.hxx | 103 | ||||
-rw-r--r-- | mysql/types/driver.cxx | 28 | ||||
-rw-r--r-- | mysql/types/test.hxx | 47 | ||||
-rw-r--r-- | oracle/types/driver.cxx | 33 | ||||
-rw-r--r-- | oracle/types/test.hxx | 54 | ||||
-rw-r--r-- | pgsql/types/driver.cxx | 35 | ||||
-rw-r--r-- | pgsql/types/test.hxx | 48 | ||||
-rw-r--r-- | sqlite/types/driver.cxx | 34 | ||||
-rw-r--r-- | sqlite/types/test.hxx | 70 |
21 files changed, 839 insertions, 52 deletions
diff --git a/common/makefile b/common/makefile index 9e6e319..eae7f7e 100644 --- a/common/makefile +++ b/common/makefile @@ -33,7 +33,8 @@ no-id \ optimistic \ pragma \ prepared \ -query \ +query/basics \ +query/array \ readonly \ relationship \ relationship-query \ diff --git a/common/query/array/driver.cxx b/common/query/array/driver.cxx new file mode 100644 index 0000000..2bda0a9 --- /dev/null +++ b/common/query/array/driver.cxx @@ -0,0 +1,170 @@ +// file : common/query/array/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test query support for C arrays. +// + +#include <string> +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include <common/config.hxx> // DATABASE_* +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +#if defined(DATABASE_MYSQL) +const odb::mysql::database_type_id bt = odb::mysql::id_blob; +const odb::mysql::database_type_id st = odb::mysql::id_string; +#elif defined(DATABASE_SQLITE) +const odb::sqlite::database_type_id bt = odb::sqlite::id_blob; +const odb::sqlite::database_type_id st = odb::sqlite::id_text; +#elif defined(DATABASE_PGSQL) +const odb::pgsql::database_type_id bt = odb::pgsql::id_bytea; +const odb::pgsql::database_type_id st = odb::pgsql::id_string; +#elif defined(DATABASE_ORACLE) +const odb::oracle::database_type_id bt = odb::oracle::id_raw; +const odb::oracle::database_type_id st = odb::oracle::id_string; +#elif defined(DATABASE_MSSQL) +const odb::mssql::database_type_id bt = odb::mssql::id_binary; +const odb::mssql::database_type_id st = odb::mssql::id_string; +#else +# error unknown database +#endif + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + typedef odb::query<object> query; + + const char buf[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}; + + // + // + { + object o1 (1, "abc", buf); + object o2 (2, "bcd", buf); + object o3 (3, "cde", buf); + + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + { + transaction t (db->begin ()); + + // string + // + assert (size (db->query<object> (query::s == "abc")) == 1); + assert (size (db->query<object> (query::s == query::_val ("bcd"))) == 1); + assert (size (db->query<object> ("s = " + query::_val ("bcd"))) == 1); + assert (size (db->query<object> ("s = " + query::_ref ("bcd"))) == 1); + + { + char a[] = "bcd"; + char* ra = a; + assert (size (db->query<object> (query::s == a)) == 1); + assert (size (db->query<object> (query::s == query::_val (a))) == 1); + assert (size (db->query<object> (query::s == query::_ref (ra))) == 1); + assert (size (db->query<object> ("s = " + query::_val (a))) == 1); + assert (size (db->query<object> ("s = " + query::_ref (a))) == 1); + } + + { + const char a[] = "bcd"; + const char* ra = a; + assert (size (db->query<object> (query::s == a)) == 1); + assert (size (db->query<object> (query::s == query::_val (a))) == 1); + assert (size (db->query<object> (query::s == query::_ref (ra))) == 1); + assert (size (db->query<object> ("s = " + query::_val (a))) == 1); + assert (size (db->query<object> ("s = " + query::_ref (a))) == 1); + } + + { + const char* p = "cde"; + assert (size (db->query<object> (query::s == p)) == 1); + assert (size (db->query<object> (query::s == query::_val (p))) == 1); + assert (size (db->query<object> (query::s == query::_ref (p))) == 1); + assert (size (db->query<object> ("s = " + query::_val (p))) == 1); + assert (size (db->query<object> ("s = " + query::_ref (p))) == 1); + } + + { + char a[] = "cde"; + char* p = a; + assert (size (db->query<object> (query::s == p)) == 1); + assert (size (db->query<object> (query::s == query::_val (p))) == 1); + assert (size (db->query<object> (query::s == query::_ref (p))) == 1); + assert (size (db->query<object> ("s = " + query::_val (p))) == 1); + assert (size (db->query<object> ("s = " + query::_ref (p))) == 1); + } + + string s ("abc"); + //assert (size (db->query<object> (query::s == s)) == 1); + assert (size (db->query<object> (query::s == s.c_str ())) == 1); + //assert (size (db->query<object> (query::s == query::_val (s))) == 1); + assert (size (db->query<object> (query::s == query::_val (s.c_str ()))) == 1); + assert (size (db->query<object> ("s = " + query::_val (s))) == 1); + assert (size (db->query<object> ("s = " + query::_ref (s))) == 1); + + assert (size (db->query<object> (query::s == query::s1)) == 3); + + // std::array + // +#ifdef ODB_CXX11 + array<char, 17> a; + strcpy (a.data (), "abc"); + + assert (size (db->query<object> (query::a == a)) == 1); + assert (size (db->query<object> (query::a == query::_val (a))) == 1); + assert (size (db->query<object> (query::a == query::_ref (a))) == 1); + assert (size (db->query<object> ("a = " + query::_val<st> (a))) == 1); + assert (size (db->query<object> ("a = " + query::_ref<st> (a))) == 1); +#endif + + // char + // + assert (size (db->query<object> (query::c == 'a')) == 1); + + char c ('b'); + assert (size (db->query<object> (query::c == query::_val (c))) == 1); + assert (size (db->query<object> (query::c == query::_ref (c))) == 1); + + assert (size (db->query<object> ("c = " + query::_val ('c'))) == 1); + assert (size (db->query<object> ("c = " + query::_ref (c))) == 1); + + assert (size (db->query<object> (query::c == query::c1)) == 3); + + // buffer + // + assert (size (db->query<object> (query::b == buf)) == 3); + assert (size (db->query<object> (query::b == query::_val (buf))) == 3); + assert (size (db->query<object> (query::b == query::_ref (buf))) == 3); + assert (size (db->query<object> ("b = " + query::_val<bt> (buf))) == 3); + assert (size (db->query<object> ("b = " + query::_ref<bt> (buf))) == 3); + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/query/array/makefile b/common/query/array/makefile new file mode 100644 index 0000000..817828f --- /dev/null +++ b/common/query/array/makefile @@ -0,0 +1,116 @@ +# file : common/query/array/makefile +# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ +--generate-query --generate-prepared --sql-name-case oracle:upper \ +--table-prefix t_query_array_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(subst /,-,$(subst $(src_root)/common/,,$(src_base))) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) $(call vc11projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../../template/Makefile.am) + $(call meta-vc9projs,../../template/template,$(name)) + $(call meta-vc10projs,../../template/template,$(name)) + $(call meta-vc11projs,../../template/template,$(name)) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/vc11proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/common/query/array/test.hxx b/common/query/array/test.hxx new file mode 100644 index 0000000..32ee413 --- /dev/null +++ b/common/query/array/test.hxx @@ -0,0 +1,77 @@ +// file : common/query/array/test.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <common/config.hxx> // HAVE_CXX11 + +#include <cstring> // std::strncpy + +#ifdef HAVE_CXX11 +# include <array> +#endif + +#include <odb/core.hxx> + +#pragma db object +struct object +{ + object () {} + object (unsigned long id, const char* s, const char* b) + : id_ (id) + { + std::strncpy (s_, s, sizeof (s_)); + std::strncpy (s1_, s, sizeof (s1_)); +#ifdef HAVE_CXX11 + std::strncpy (a_.data (), s, a_.size ()); +#endif + c_ = c1_ = *s; + std::memcpy (b_, b, sizeof (b_)); + } + + #pragma db id + unsigned long id_; + + char s_[17]; + char s1_[17]; + +#ifdef HAVE_CXX11 +#ifdef ODB_COMPILER +# if defined(ODB_DATABASE_MYSQL) || \ + defined(ODB_DATABASE_PGSQL) || \ + defined(ODB_DATABASE_ORACLE) || \ + defined(ODB_DATABASE_MSSQL) +# pragma db type("VARCHAR(16)") +# elif defined(ODB_DATABASE_SQLITE) +# pragma db type("TEXT") +# else +# error unknown database +# endif +#endif + std::array<char, 17> a_; +#endif + + char c_; + char c1_; + +#ifdef ODB_COMPILER +# if defined(ODB_DATABASE_MYSQL) +# pragma db type("BINARY(16)") +# elif defined(ODB_DATABASE_SQLITE) +# pragma db type("BLOB") +# elif defined(ODB_DATABASE_PGSQL) +# pragma db type("BYTEA") +# elif defined(ODB_DATABASE_ORACLE) +# pragma db type("RAW(16)") +# elif defined(ODB_DATABASE_MSSQL) +# pragma db type("BINARY(16)") +# else +# error unknown database +# endif +#endif + char b_[16]; +}; + +#endif // TEST_HXX diff --git a/common/query/array/test.std b/common/query/array/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/common/query/array/test.std diff --git a/common/query/driver.cxx b/common/query/basics/driver.cxx index bec61fb..686b4b6 100644 --- a/common/query/driver.cxx +++ b/common/query/basics/driver.cxx @@ -1,8 +1,8 @@ -// file : common/query/driver.cxx +// file : common/query/basics/driver.cxx // copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file -// Test query support. +// Test basic query support. // #include <memory> // std::auto_ptr diff --git a/common/query/makefile b/common/query/basics/makefile index 6f25ba4..0be5352 100644 --- a/common/query/makefile +++ b/common/query/basics/makefile @@ -1,8 +1,8 @@ -# file : common/query/makefile +# file : common/query/basics/makefile # copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC # license : GNU GPL v2; see accompanying LICENSE file -include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make +include $(dir $(lastword $(MAKEFILE_LIST)))../../../build/bootstrap.make cxx_tun := driver.cxx odb_hdr := test.hxx @@ -35,7 +35,7 @@ gen := $(addprefix $(out_base)/,$(genf)) $(gen): $(odb) $(gen): odb := $(odb) $(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ ---generate-query --generate-prepared --table-prefix t_query_ +--generate-query --generate-prepared --table-prefix t_query_basics_ $(gen): cpp_options := -I$(src_base) $(gen): $(common.l.cpp-options) @@ -58,10 +58,10 @@ $(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \ $(call vc10projs,$(name)) $(call vc11projs,$(name)) $(dist): $(call dist-data,$(sources) $(headers) $(data_dist)) - $(call meta-automake,../template/Makefile.am) - $(call meta-vc9projs,../template/template,$(name)) - $(call meta-vc10projs,../template/template,$(name)) - $(call meta-vc11projs,../template/template,$(name)) + $(call meta-automake,../../template/Makefile.am) + $(call meta-vc9projs,../../template/template,$(name)) + $(call meta-vc10projs,../../template/template,$(name)) + $(call meta-vc11projs,../../template/template,$(name)) # Test. # diff --git a/common/query/test.hxx b/common/query/basics/test.hxx index 948ba30..f572d75 100644 --- a/common/query/test.hxx +++ b/common/query/basics/test.hxx @@ -1,4 +1,4 @@ -// file : common/query/test.hxx +// file : common/query/basics/test.hxx // copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file diff --git a/common/query/test.std b/common/query/basics/test.std index 0548968..0548968 100644 --- a/common/query/test.std +++ b/common/query/basics/test.std diff --git a/libcommon/common/common.hxx b/libcommon/common/common.hxx index 2b79a5a..0d56d7c 100644 --- a/libcommon/common/common.hxx +++ b/libcommon/common/common.hxx @@ -38,11 +38,11 @@ create_specific_database (int& argc, } // This function returns an accurate result only if the result iterator -// hasn't been advanced. +// hasn't been advanced and after the call the result is no longer valid. // template <typename T> std::size_t -size (odb::result<T>&); +size (odb::result<T>); #include <common/common.txx> diff --git a/libcommon/common/common.txx b/libcommon/common/common.txx index 66ccac9..5c40520 100644 --- a/libcommon/common/common.txx +++ b/libcommon/common/common.txx @@ -11,7 +11,7 @@ size_available (); template <typename T> std::size_t -size (odb::result<T>& r) +size (odb::result<T> r) { if (size_available ()) return r.size (); 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<char_array> p1 (db->load<char_array> (1)); + auto_ptr<char_array> p2 (db->load<char_array> (2)); + auto_ptr<char_array> p3 (db->load<char_array> (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 <string> #include <vector> #include <memory> // std::auto_ptr -#include <cstring> // std::memcmp +#include <cstring> // std::memcmp, std::strncpy, std::str[n]cmp +#include <cwchar> // std::wcsncpy, std::wcs[n]cmp #include <odb/core.hxx> @@ -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 diff --git a/mysql/types/driver.cxx b/mysql/types/driver.cxx index d2f159c..0a7155e 100644 --- a/mysql/types/driver.cxx +++ b/mysql/types/driver.cxx @@ -101,6 +101,34 @@ main (int argc, char* argv[]) assert (o == *o1); } + + // Test char array. + // + { + char_array o1 (1, ""); + char_array o2 (2, "1234567890"); + char_array o3 (3, "1234567890123456"); + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<char_array> p1 (db->load<char_array> (1)); + auto_ptr<char_array> p2 (db->load<char_array> (2)); + auto_ptr<char_array> p3 (db->load<char_array> (3)); + t.commit (); + + assert (o1 == *p1); + assert (o2 == *p2); + assert (o3 == *p3); + } + } } catch (const odb::exception& e) { diff --git a/mysql/types/test.hxx b/mysql/types/test.hxx index bf1ac9f..bc4c773 100644 --- a/mysql/types/test.hxx +++ b/mysql/types/test.hxx @@ -9,6 +9,7 @@ #include <string> #include <vector> #include <memory> // std::auto_ptr +#include <cstring> // std::strncpy, std::str[n]cmp #include <odb/core.hxx> @@ -87,14 +88,8 @@ enum color {red, green, blue}; #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_; @@ -274,4 +269,40 @@ struct object } }; +// Test char array. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id, const char* s) + : id_ (id) + { + std::strncpy (s1, s, sizeof (s1)); + std::strncpy (s2, s, sizeof (s2)); + s3[0] = c1 = *s; + } + + #pragma db id + unsigned long id_; + + char s1[17]; + + #pragma db type("CHAR(16)") + char s2[16]; + + char s3[1]; + char c1; + + 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; + } +}; + #endif // TEST_HXX diff --git a/oracle/types/driver.cxx b/oracle/types/driver.cxx index be6251c..6370e44 100644 --- a/oracle/types/driver.cxx +++ b/oracle/types/driver.cxx @@ -272,6 +272,39 @@ main (int argc, char* argv[]) t.commit (); } + + // Test char array. + // + { + char_array o1 (1, ""); + char_array o2 (2, "1234567890"); + char_array o3 (3, "1234567890123456"); + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + // Oracle returns padded values for CHAR(N) unless they are + // empty (represented as NULL). + // + memcpy (o2.s2, "1234567890 ", 16); + + { + transaction t (db->begin ()); + auto_ptr<char_array> p1 (db->load<char_array> (1)); + auto_ptr<char_array> p2 (db->load<char_array> (2)); + auto_ptr<char_array> p3 (db->load<char_array> (3)); + t.commit (); + + assert (o1 == *p1); + assert (o2 == *p2); + assert (o3 == *p3); + } + } } catch (const odb::exception& e) { diff --git a/oracle/types/test.hxx b/oracle/types/test.hxx index 0543598..51665c9 100644 --- a/oracle/types/test.hxx +++ b/oracle/types/test.hxx @@ -8,6 +8,7 @@ #include <string> #include <vector> #include <memory> // std::auto_ptr +#include <cstring> // std::strncpy, std::str[n]cmp #include <odb/core.hxx> @@ -101,14 +102,8 @@ typedef std::vector<std::string> strings; #pragma db object struct object { - object (unsigned int id) - : id_ (id) - { - } - - object () - { - } + object () {} + object (unsigned long id): id_ (id) {} #pragma db id unsigned int id_; @@ -166,13 +161,13 @@ struct object #pragma db type ("CHAR(13)") std::string char_; - #pragma db type ("VARCHAR2(512)") + #pragma db type ("VARCHAR2(512)") null std::string varchar2_; #pragma db type ("NCHAR(8)") std::string nchar_; - #pragma db type ("NVARCHAR2(512)") + #pragma db type ("NVARCHAR2(512)") null std::string nvarchar2_; // Oracle treats empty and NULL VARCHAR2 the same. Test that we @@ -302,4 +297,43 @@ struct blob } }; +// Test char array. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id, const char* s) + : id_ (id) + { + std::strncpy (s1, s, sizeof (s1)); + std::strncpy (s2, s, sizeof (s2)); + s3[0] = c1 = *s; + } + + #pragma db id + unsigned long id_; + + char s1[17]; + + #pragma db type("CHAR(16)") null + char s2[16]; + + #pragma db null + char s3[1]; + + #pragma db null + char c1; + + 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; + } +}; + #endif // TEST_HXX diff --git a/pgsql/types/driver.cxx b/pgsql/types/driver.cxx index 8b969aa..d2e5e80 100644 --- a/pgsql/types/driver.cxx +++ b/pgsql/types/driver.cxx @@ -85,6 +85,41 @@ main (int argc, char* argv[]) assert (o == *o1); } + + // Test char array. + // + { + char_array o1 (1, ""); + char_array o2 (2, "1234567890"); + char_array o3 (3, "1234567890123456"); + + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + db->persist (o3); + t.commit (); + } + + // PostgreSQL returns padded values for CHAR(N). + // + memcpy (o1.s2, " ", 16); + o1.s3[0] = o1.c1 = ' '; + memcpy (o2.s2, "1234567890 ", 16); + + { + transaction t (db->begin ()); + auto_ptr<char_array> p1 (db->load<char_array> (1)); + auto_ptr<char_array> p2 (db->load<char_array> (2)); + auto_ptr<char_array> p3 (db->load<char_array> (3)); + t.commit (); + + assert (o1 == *p1); + assert (o2 == *p2); + assert (o3 == *p3); + } + } } catch (const odb::exception& e) { diff --git a/pgsql/types/test.hxx b/pgsql/types/test.hxx index 34f913f..3078576 100644 --- a/pgsql/types/test.hxx +++ b/pgsql/types/test.hxx @@ -9,7 +9,7 @@ #include <string> #include <vector> #include <memory> // std::auto_ptr -#include <cstring> // std::memcmp +#include <cstring> // std::memcmp, std::strncpy, std::str[n]cmp #include <cstddef> // std::size_t #include <odb/core.hxx> @@ -79,14 +79,8 @@ enum color {red, green, blue}; #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_; @@ -188,4 +182,40 @@ struct object } }; +// Test char array. +// +#pragma db object +struct char_array +{ + char_array () {} + char_array (unsigned long id, const char* s) + : id_ (id) + { + std::strncpy (s1, s, sizeof (s1)); + std::strncpy (s2, s, sizeof (s2)); + s3[0] = c1 = *s; + } + + #pragma db id + unsigned long id_; + + char s1[17]; + + #pragma db type("CHAR(16)") + char s2[16]; + + char s3[1]; + char c1; + + 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; + } +}; + #endif // TEST_HXX 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<char_array> p1 (db->load<char_array> (1)); + auto_ptr<char_array> p2 (db->load<char_array> (2)); + auto_ptr<char_array> p3 (db->load<char_array> (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 <string> #include <vector> #include <memory> // std::auto_ptr +#include <cstring> // std::strncpy, std::str[n]cmp + +#ifdef _WIN32 +# include <cwchar> // std::wcsncpy, std::wcs[n]cmp +#endif #include <odb/core.hxx> @@ -17,14 +22,8 @@ typedef std::auto_ptr<std::string> 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 |