From c970a38889d5774f7d0f73215dfd70934524d76a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 10 Jul 2012 15:17:13 +0200 Subject: Add support for custom database type mapping New pragma qualifier, map, and specifiers: as, to, from. New tests: /custom. --- odb/mysql/details/conversion.hxx | 59 ++++++++++++++++++++++++++++++++++++++ odb/mysql/query.cxx | 18 +++++++++++- odb/mysql/query.hxx | 61 +++++++++++++++++++++++++--------------- odb/mysql/query.ixx | 10 ++++--- odb/mysql/query.txx | 32 ++++++++++----------- 5 files changed, 136 insertions(+), 44 deletions(-) create mode 100644 odb/mysql/details/conversion.hxx diff --git a/odb/mysql/details/conversion.hxx b/odb/mysql/details/conversion.hxx new file mode 100644 index 0000000..4d473f8 --- /dev/null +++ b/odb/mysql/details/conversion.hxx @@ -0,0 +1,59 @@ +// file : odb/mysql/details/conversion.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_MYSQL_DETAILS_CONVERSION_HXX +#define ODB_MYSQL_DETAILS_CONVERSION_HXX + +#include + +#include + +namespace odb +{ + // @@ Revise this. + // + namespace details {} + + namespace mysql + { + namespace details + { + using namespace odb::details; + + // Detect whether conversion is specified in type_traits. + // + template + meta::yes + conversion_p_test (typename type_traits::conversion*); + + template + meta::no + conversion_p_test (...); + + template + struct conversion_p + { + static const bool value = + sizeof (conversion_p_test (0)) == sizeof (meta::yes); + }; + + template ::value> + struct conversion; + + template + struct conversion + { + static const char* to () {return type_traits::conversion::to ();} + }; + + template + struct conversion + { + static const char* to () {return 0;} + }; + } + } +} + +#endif // ODB_MYSQL_DETAILS_CONVERSION_HXX diff --git a/odb/mysql/query.cxx b/odb/mysql/query.cxx index 2573ac1..70d55c2 100644 --- a/odb/mysql/query.cxx +++ b/odb/mysql/query.cxx @@ -118,10 +118,13 @@ namespace odb } void query:: - add (details::shared_ptr p) + add (details::shared_ptr 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 (MYSQL_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/mysql/query.hxx b/odb/mysql/query.hxx index c0f812a..732244c 100644 --- a/odb/mysql/query.hxx +++ b/odb/mysql/query.hxx @@ -24,6 +24,7 @@ #include #include +#include namespace odb { @@ -99,7 +100,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; }; @@ -142,7 +143,8 @@ namespace odb query (val_bind v) : binding_ (0, 0) { - append::db_type_id> (v); + append::db_type_id> ( + v, details::conversion::to ()); } template @@ -150,7 +152,8 @@ namespace odb query (ref_bind r) : binding_ (0, 0) { - append::db_type_id> (r); + append::db_type_id> ( + r, details::conversion::to ()); } template @@ -221,7 +224,8 @@ namespace odb query& operator+= (val_bind v) { - append::db_type_id> (v); + append::db_type_id> ( + v, details::conversion::to ()); return *this; } @@ -229,18 +233,19 @@ namespace odb query& operator+= (ref_bind r) { - append::db_type_id> (r); + append::db_type_id> ( + r, details::conversion::to ()); return *this; } public: template void - append (val_bind); + append (val_bind, const char* conv); template void - append (ref_bind); + append (ref_bind, const char* conv); void append (const std::string& native); @@ -250,7 +255,7 @@ namespace odb private: void - add (details::shared_ptr); + add (details::shared_ptr, const char* conv); private: typedef std::vector clause_type; @@ -391,10 +396,11 @@ namespace odb template struct query_column { - // Note that we keep shalow copies of the table and column names. + // Note that we keep shallow copies of the table, column, and conversion + // expression. The latter can be NULL. // - query_column (const char* table, const char* column) - : table_ (table), column_ (column) + query_column (const char* table, const char* column, const char* conv) + : table_ (table), column_ (column), conversion_ (conv) { } @@ -410,6 +416,14 @@ namespace odb return column_; } + // Can be NULL. + // + const char* + conversion () const + { + return conversion_; + } + // is_null, is_not_null // public: @@ -462,7 +476,7 @@ namespace odb { query q (table_, column_); q += "="; - q.append (v); + q.append (v, conversion_); return q; } @@ -479,7 +493,7 @@ namespace odb { query q (table_, column_); q += "="; - q.append (r); + q.append (r, conversion_); return q; } @@ -547,7 +561,7 @@ namespace odb { query q (table_, column_); q += "!="; - q.append (v); + q.append (v, conversion_); return q; } @@ -564,7 +578,7 @@ namespace odb { query q (table_, column_); q += "!="; - q.append (r); + q.append (r, conversion_); return q; } @@ -632,7 +646,7 @@ namespace odb { query q (table_, column_); q += "<"; - q.append (v); + q.append (v, conversion_); return q; } @@ -649,7 +663,7 @@ namespace odb { query q (table_, column_); q += "<"; - q.append (r); + q.append (r, conversion_); return q; } @@ -717,7 +731,7 @@ namespace odb { query q (table_, column_); q += ">"; - q.append (v); + q.append (v, conversion_); return q; } @@ -734,7 +748,7 @@ namespace odb { query q (table_, column_); q += ">"; - q.append (r); + q.append (r, conversion_); return q; } @@ -802,7 +816,7 @@ namespace odb { query q (table_, column_); q += "<="; - q.append (v); + q.append (v, conversion_); return q; } @@ -819,7 +833,7 @@ namespace odb { query q (table_, column_); q += "<="; - q.append (r); + q.append (r, conversion_); return q; } @@ -887,7 +901,7 @@ namespace odb { query q (table_, column_); q += ">="; - q.append (v); + q.append (v, conversion_); return q; } @@ -904,7 +918,7 @@ namespace odb { query q (table_, column_); q += ">="; - q.append (r); + q.append (r, conversion_); return q; } @@ -1048,6 +1062,7 @@ namespace odb private: const char* table_; const char* column_; + const char* conversion_; }; // Provide operator+() for using columns to construct native diff --git a/odb/mysql/query.ixx b/odb/mysql/query.ixx index 674c6cd..2508ae3 100644 --- a/odb/mysql/query.ixx +++ b/odb/mysql/query.ixx @@ -8,20 +8,22 @@ namespace odb { template inline void query:: - append (val_bind v) + append (val_bind v, const char* conv) { add ( details::shared_ptr ( - new (details::shared) query_param_impl (v))); + new (details::shared) query_param_impl (v)), + conv); } template inline void query:: - append (ref_bind r) + append (ref_bind r, const char* conv) { add ( details::shared_ptr ( - new (details::shared) query_param_impl (r))); + new (details::shared) query_param_impl (r)), + conv); } } } diff --git a/odb/mysql/query.txx b/odb/mysql/query.txx index 0377c33..80b6358 100644 --- a/odb/mysql/query.txx +++ b/odb/mysql/query.txx @@ -19,7 +19,7 @@ namespace odb // append (c.table (), c.column ()); append ("="); - append (val_bind (true)); + append (val_bind (true), c.conversion ()); } // query_column @@ -30,9 +30,9 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append (val_bind (v1)); + q.append (val_bind (v1), conversion_); q += ","; - q.append (val_bind (v2)); + q.append (val_bind (v2), conversion_); q += ")"; return q; } @@ -43,11 +43,11 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append (val_bind (v1)); + q.append (val_bind (v1), conversion_); q += ","; - q.append (val_bind (v2)); + q.append (val_bind (v2), conversion_); q += ","; - q.append (val_bind (v3)); + q.append (val_bind (v3), conversion_); q += ")"; return q; } @@ -58,13 +58,13 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append (val_bind (v1)); + q.append (val_bind (v1), conversion_); q += ","; - q.append (val_bind (v2)); + q.append (val_bind (v2), conversion_); q += ","; - q.append (val_bind (v3)); + q.append (val_bind (v3), conversion_); q += ","; - q.append (val_bind (v4)); + q.append (val_bind (v4), conversion_); q += ")"; return q; } @@ -75,15 +75,15 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append (val_bind (v1)); + q.append (val_bind (v1), conversion_); q += ","; - q.append (val_bind (v2)); + q.append (val_bind (v2), conversion_); q += ","; - q.append (val_bind (v3)); + q.append (val_bind (v3), conversion_); q += ","; - q.append (val_bind (v4)); + q.append (val_bind (v4), conversion_); q += ","; - q.append (val_bind (v5)); + q.append (val_bind (v5), conversion_); q += ")"; return q; } @@ -101,7 +101,7 @@ namespace odb if (i != begin) q += ","; - q.append (val_bind (*i)); + q.append (val_bind (*i), conversion_); } q += ")"; return q; -- cgit v1.1