diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-07-10 15:17:12 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-07-10 15:17:12 +0200 |
commit | f3f9389365a03603c6f9bcc4d502ff4049c72fe2 (patch) | |
tree | ef7bfa4d1c2791e5bc3365649552b12c6820a1d8 | |
parent | 7ed3669ac5c5efb5cc05ee777ed0ca1f52f0e69d (diff) |
Add support for custom database type mapping
New pragma qualifier, map, and specifiers: as, to, from. New tests:
<database>/custom.
-rw-r--r-- | odb/mssql/details/conversion.hxx | 59 | ||||
-rw-r--r-- | odb/mssql/query.cxx | 18 | ||||
-rw-r--r-- | odb/mssql/query.hxx | 61 | ||||
-rw-r--r-- | odb/mssql/query.ixx | 10 | ||||
-rw-r--r-- | odb/mssql/query.txx | 33 |
5 files changed, 138 insertions, 43 deletions
diff --git a/odb/mssql/details/conversion.hxx b/odb/mssql/details/conversion.hxx new file mode 100644 index 0000000..4b419da --- /dev/null +++ b/odb/mssql/details/conversion.hxx @@ -0,0 +1,59 @@ +// file : odb/mssql/details/conversion.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_MSSQL_DETAILS_CONVERSION_HXX +#define ODB_MSSQL_DETAILS_CONVERSION_HXX + +#include <odb/mssql/traits.hxx> + +#include <odb/details/meta/answer.hxx> + +namespace odb +{ + // @@ Revise this. + // + namespace details {} + + namespace mssql + { + namespace details + { + using namespace odb::details; + + // Detect whether conversion is specified in type_traits. + // + template <typename T> + meta::yes + conversion_p_test (typename type_traits<T>::conversion*); + + template <typename T> + meta::no + conversion_p_test (...); + + template <typename T> + struct conversion_p + { + static const bool value = + sizeof (conversion_p_test<T> (0)) == sizeof (meta::yes); + }; + + template <typename T, bool = conversion_p<T>::value> + struct conversion; + + template <typename T> + struct conversion<T, true> + { + static const char* to () {return type_traits<T>::conversion::to ();} + }; + + template <typename T> + struct conversion<T, false> + { + static const char* to () {return 0;} + }; + } + } +} + +#endif // ODB_MSSQL_DETAILS_CONVERSION_HXX diff --git a/odb/mssql/query.cxx b/odb/mssql/query.cxx index 368cd82..07dba0e 100644 --- a/odb/mssql/query.cxx +++ b/odb/mssql/query.cxx @@ -118,10 +118,13 @@ namespace odb } void query:: - add (details::shared_ptr<query_param> p) + add (details::shared_ptr<query_param> p, const char* conv) { clause_.push_back (clause_part (clause_part::param)); + if (conv != 0) + clause_.back ().part = conv; + parameters_.push_back (p); bind_.push_back (bind ()); binding_.bind = &bind_[0]; @@ -258,7 +261,20 @@ namespace odb if (last != ' ' && last != '(') r += ' '; + // Add the conversion expression, if any. + // + string::size_type p; + if (!i->part.empty ()) + { + p = i->part.find ("(?)"); + r.append (i->part, 0, p); + } + r += '?'; + + if (!i->part.empty ()) + r.append (i->part, p + 3, string::npos); + break; } case clause_part::native: diff --git a/odb/mssql/query.hxx b/odb/mssql/query.hxx index 5b5ea57..4844463 100644 --- a/odb/mssql/query.hxx +++ b/odb/mssql/query.hxx @@ -24,6 +24,7 @@ #include <odb/details/shared-ptr.hxx> #include <odb/mssql/details/export.hxx> +#include <odb/mssql/details/conversion.hxx> namespace odb { @@ -121,7 +122,7 @@ namespace odb clause_part (bool p): kind (boolean), bool_part (p) {} kind_type kind; - std::string part; + std::string part; // If kind is param, then part is conversion expr. bool bool_part; }; @@ -164,7 +165,8 @@ namespace odb query (val_bind<T> v) : binding_ (0, 0) { - append<T, type_traits<T>::db_type_id> (v); + append<T, type_traits<T>::db_type_id> ( + v, details::conversion<T>::to ()); } template <typename T> @@ -172,7 +174,8 @@ namespace odb query (ref_bind<T> r) : binding_ (0, 0) { - append<T, type_traits<T>::db_type_id> (r); + append<T, type_traits<T>::db_type_id> ( + r, details::conversion<T>::to ()); } template <database_type_id ID> @@ -243,7 +246,8 @@ namespace odb query& operator+= (val_bind<T> v) { - append<T, type_traits<T>::db_type_id> (v); + append<T, type_traits<T>::db_type_id> ( + v, details::conversion<T>::to ()); return *this; } @@ -251,18 +255,19 @@ namespace odb query& operator+= (ref_bind<T> r) { - append<T, type_traits<T>::db_type_id> (r); + append<T, type_traits<T>::db_type_id> ( + r, details::conversion<T>::to ()); return *this; } public: template <typename T, database_type_id ID> void - append (val_bind<T>); + append (val_bind<T>, const char* conv); template <typename T, database_type_id ID> void - append (ref_bind<T>); + append (ref_bind<T>, const char* conv); void append (const std::string& native); @@ -272,7 +277,7 @@ namespace odb private: void - add (details::shared_ptr<query_param>); + add (details::shared_ptr<query_param>, const char* conv); private: typedef std::vector<clause_part> clause_type; @@ -413,13 +418,16 @@ namespace odb template <typename T, database_type_id ID> struct query_column { - // Note that we keep shalow copies of the table and column names. + // Note that we keep shalow copies of the table, column, and conversion + // expression. The latter can be NULL. // query_column (const char* table, const char* column, + const char* conv, unsigned short prec = 0, unsigned short scale = 0xFFFF) - : table_ (table), column_ (column), prec_ (prec), scale_ (scale) + : table_ (table), column_ (column), conversion_ (conv), + prec_ (prec), scale_ (scale) { } @@ -435,6 +443,14 @@ namespace odb return column_; } + // Can be NULL. + // + const char* + conversion () const + { + return conversion_; + } + unsigned short prec () const { @@ -502,7 +518,7 @@ namespace odb query q (table_, column_); q += "="; - q.append<T, ID> (v); + q.append<T, ID> (v, conversion_); return q; } @@ -522,7 +538,7 @@ namespace odb query q (table_, column_); q += "="; - q.append<T, ID> (r); + q.append<T, ID> (r, conversion_); return q; } @@ -593,7 +609,7 @@ namespace odb query q (table_, column_); q += "!="; - q.append<T, ID> (v); + q.append<T, ID> (v, conversion_); return q; } @@ -613,7 +629,7 @@ namespace odb query q (table_, column_); q += "!="; - q.append<T, ID> (r); + q.append<T, ID> (r, conversion_); return q; } @@ -684,7 +700,7 @@ namespace odb query q (table_, column_); q += "<"; - q.append<T, ID> (v); + q.append<T, ID> (v, conversion_); return q; } @@ -704,7 +720,7 @@ namespace odb query q (table_, column_); q += "<"; - q.append<T, ID> (r); + q.append<T, ID> (r, conversion_); return q; } @@ -775,7 +791,7 @@ namespace odb query q (table_, column_); q += ">"; - q.append<T, ID> (v); + q.append<T, ID> (v, conversion_); return q; } @@ -795,7 +811,7 @@ namespace odb query q (table_, column_); q += ">"; - q.append<T, ID> (r); + q.append<T, ID> (r, conversion_); return q; } @@ -866,7 +882,7 @@ namespace odb query q (table_, column_); q += "<="; - q.append<T, ID> (v); + q.append<T, ID> (v, conversion_); return q; } @@ -886,7 +902,7 @@ namespace odb query q (table_, column_); q += "<="; - q.append<T, ID> (r); + q.append<T, ID> (r, conversion_); return q; } @@ -957,7 +973,7 @@ namespace odb query q (table_, column_); q += ">="; - q.append<T, ID> (v); + q.append<T, ID> (v, conversion_); return q; } @@ -977,7 +993,7 @@ namespace odb query q (table_, column_); q += ">="; - q.append<T, ID> (r); + q.append<T, ID> (r, conversion_); return q; } @@ -1121,6 +1137,7 @@ namespace odb private: const char* table_; const char* column_; + const char* conversion_; unsigned short prec_; unsigned short scale_; diff --git a/odb/mssql/query.ixx b/odb/mssql/query.ixx index e7f2399..0476b26 100644 --- a/odb/mssql/query.ixx +++ b/odb/mssql/query.ixx @@ -8,20 +8,22 @@ namespace odb { template <typename T, database_type_id ID> inline void query:: - append (val_bind<T> v) + append (val_bind<T> v, const char* conv) { add ( details::shared_ptr<query_param> ( - new (details::shared) query_param_impl<T, ID> (v))); + new (details::shared) query_param_impl<T, ID> (v)), + conv); } template <typename T, database_type_id ID> inline void query:: - append (ref_bind<T> r) + append (ref_bind<T> r, const char* conv) { add ( details::shared_ptr<query_param> ( - new (details::shared) query_param_impl<T, ID> (r))); + new (details::shared) query_param_impl<T, ID> (r)), + conv); } } } diff --git a/odb/mssql/query.txx b/odb/mssql/query.txx index a86c039..522059a 100644 --- a/odb/mssql/query.txx +++ b/odb/mssql/query.txx @@ -19,7 +19,8 @@ namespace odb // append (c.table (), c.column ()); append ("="); - append<bool, ID> (val_bind<bool> (true, c.prec (), c.scale ())); + append<bool, ID> (val_bind<bool> (true, c.prec (), c.scale ()), + c.conversion ()); } // query_column @@ -30,9 +31,9 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append<T, ID> (val_bind<T> (v1, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v1, prec_, scale_), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v2, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v2, prec_, scale_), conversion_); q += ")"; return q; } @@ -43,11 +44,11 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append<T, ID> (val_bind<T> (v1, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v1, prec_, scale_), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v2, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v2, prec_, scale_), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v3, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v3, prec_, scale_), conversion_); q += ")"; return q; } @@ -58,13 +59,13 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append<T, ID> (val_bind<T> (v1, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v1, prec_, scale_), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v2, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v2, prec_, scale_), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v3, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v3, prec_, scale_), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v4, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v4, prec_, scale_), conversion_); q += ")"; return q; } @@ -75,15 +76,15 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append<T, ID> (val_bind<T> (v1, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v1, prec_, scale_), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v2, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v2, prec_, scale_), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v3, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v3, prec_, scale_), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v4, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v4, prec_, scale_), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v5, prec_, scale_)); + q.append<T, ID> (val_bind<T> (v5, prec_, scale_), conversion_); q += ")"; return q; } @@ -101,7 +102,7 @@ namespace odb if (i != begin) q += ","; - q.append<T, ID> (val_bind<T> (*i, prec_, scale_)); + q.append<T, ID> (val_bind<T> (*i, prec_, scale_), conversion_); } q += ")"; return q; |