diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2013-03-29 18:51:07 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2013-03-29 18:51:07 +0200 |
commit | e76ab3a1ac2487e0dcb16ecdfe0c6e53c3f1e86c (patch) | |
tree | 981c0f5581bdada9969d5b692dfacbc2c82f123c /mssql/custom/test.hxx | |
parent | 34d177d03f5020ca0ec4bf9b77e20951ed17ff29 (diff) |
Add test for custom-mapping SQL Server SQL_VARIANT type
Diffstat (limited to 'mssql/custom/test.hxx')
-rw-r--r-- | mssql/custom/test.hxx | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/mssql/custom/test.hxx b/mssql/custom/test.hxx index 6f44a77..7e6e81c 100644 --- a/mssql/custom/test.hxx +++ b/mssql/custom/test.hxx @@ -10,6 +10,46 @@ #include <odb/core.hxx> +// Map SQL Server SQL_VARIANT type to our variant C++ class that is capable +// of storing either an integer or a string (QVariant and boost::variant +// would be natural alternatives to our own type). The SQL Server functions +// that are used in the 'to' and 'from' expressions below are defined in +// the custom.sql file. The other half of this mapping is in traits.hxx +// (value_traits<variant, id_long_string>). +// +#pragma db map type("SQL_VARIANT") \ + as("VARCHAR(max)") \ + to("dbo.string_to_variant((?))") \ + from("dbo.variant_to_string((?))") + +#pragma db value type("SQL_VARIANT") +struct variant +{ + variant (unsigned long v = 0): val_type (type_int), int_val (v) {} + variant (const std::string& v): val_type (type_str), str_val (v) {} + + enum {type_int, type_str} val_type; + unsigned long int_val; + std::string str_val; +}; + +inline bool +operator== (const variant& a, const variant& b) +{ + if (a.val_type != b.val_type) + return false; + + switch (a.val_type) + { + case variant::type_int: + return a.int_val == b.int_val; + case variant::type_str: + return a.str_val == b.str_val; + } + + return false; +} + #if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 // Map GEOMETRY SQL Server type to the point C++ struct. The other half // of this mapping is in traits.hxx (value_traits<point, id_string>). @@ -55,6 +95,9 @@ struct object #pragma db id unsigned long id; + variant v; + std::vector<variant> vv; + #if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 point p; std::vector<point> pv; @@ -67,6 +110,7 @@ struct object operator== (const object& y) const { return id == y.id + && vv == y.vv #if !defined(MSSQL_SERVER_VERSION) || MSSQL_SERVER_VERSION >= 1000 && p == y.p && pv == y.pv |