From 304dbce8fa2a5d033870988d17709e3eae050258 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 10 Jul 2012 15:17:15 +0200 Subject: Add support for custom database type mapping New pragma qualifier, map, and specifiers: as, to, from. New tests: /custom. --- odb/oracle/details/conversion.hxx | 59 +++++++++++++++++++++++++++++++ odb/oracle/query.cxx | 19 +++++++++- odb/oracle/query.hxx | 73 ++++++++++++++++++++++++--------------- odb/oracle/query.ixx | 10 +++--- odb/oracle/query.txx | 33 +++++++++--------- 5 files changed, 146 insertions(+), 48 deletions(-) create mode 100644 odb/oracle/details/conversion.hxx diff --git a/odb/oracle/details/conversion.hxx b/odb/oracle/details/conversion.hxx new file mode 100644 index 0000000..d9ea814 --- /dev/null +++ b/odb/oracle/details/conversion.hxx @@ -0,0 +1,59 @@ +// file : odb/oracle/details/conversion.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_ORACLE_DETAILS_CONVERSION_HXX +#define ODB_ORACLE_DETAILS_CONVERSION_HXX + +#include + +#include + +namespace odb +{ + // @@ Revise this. + // + namespace details {} + + namespace oracle + { + 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_ORACLE_DETAILS_CONVERSION_HXX diff --git a/odb/oracle/query.cxx b/odb/oracle/query.cxx index 53a7991..f2cdac1 100644 --- a/odb/oracle/query.cxx +++ b/odb/oracle/query.cxx @@ -119,10 +119,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 (bind ()); binding_.bind = &bind_[0]; @@ -262,8 +265,22 @@ namespace odb ostringstream os; os << param++; + + // 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 += ':'; r += os.str (); + + if (!i->part.empty ()) + r.append (i->part, p + 3, string::npos); + break; } case clause_part::native: diff --git a/odb/oracle/query.hxx b/odb/oracle/query.hxx index 5d3d7ac..50c9fb3 100644 --- a/odb/oracle/query.hxx +++ b/odb/oracle/query.hxx @@ -22,6 +22,7 @@ #include #include +#include namespace odb { @@ -120,7 +121,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; }; @@ -163,7 +164,8 @@ namespace odb query (val_bind v) : binding_ (0, 0) { - append::db_type_id> (v); + append::db_type_id> ( + v, details::conversion::to ()); } template @@ -171,7 +173,8 @@ namespace odb query (ref_bind r) : binding_ (0, 0) { - append::db_type_id> (r); + append::db_type_id> ( + r, details::conversion::to ()); } template @@ -242,7 +245,8 @@ namespace odb query& operator+= (val_bind v) { - append::db_type_id> (v); + append::db_type_id> ( + v, details::conversion::to ()); return *this; } @@ -250,18 +254,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); @@ -271,7 +276,7 @@ namespace odb private: void - add (details::shared_ptr); + add (details::shared_ptr, const char* conv); private: typedef std::vector clause_type; @@ -412,13 +417,16 @@ namespace odb template 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 = 0xFFF, short scale = 0xFFF) - : table_ (table), column_ (column), prec_ (prec), scale_ (scale) + : table_ (table), column_ (column), conversion_ (conv), + prec_ (prec), scale_ (scale) { } @@ -434,6 +442,14 @@ namespace odb return column_; } + // Can be NULL. + // + const char* + conversion () const + { + return conversion_; + } + unsigned short prec () const { @@ -501,7 +517,7 @@ namespace odb query q (table_, column_); q += "="; - q.append (v); + q.append (v, conversion_); return q; } @@ -521,7 +537,7 @@ namespace odb query q (table_, column_); q += "="; - q.append (r); + q.append (r, conversion_); return q; } @@ -592,7 +608,7 @@ namespace odb query q (table_, column_); q += "!="; - q.append (v); + q.append (v, conversion_); return q; } @@ -612,7 +628,7 @@ namespace odb query q (table_, column_); q += "!="; - q.append (r); + q.append (r, conversion_); return q; } @@ -683,7 +699,7 @@ namespace odb query q (table_, column_); q += "<"; - q.append (v); + q.append (v, conversion_); return q; } @@ -703,7 +719,7 @@ namespace odb query q (table_, column_); q += "<"; - q.append (r); + q.append (r, conversion_); return q; } @@ -774,7 +790,7 @@ namespace odb query q (table_, column_); q += ">"; - q.append (v); + q.append (v, conversion_); return q; } @@ -794,7 +810,7 @@ namespace odb query q (table_, column_); q += ">"; - q.append (r); + q.append (r, conversion_); return q; } @@ -865,7 +881,7 @@ namespace odb query q (table_, column_); q += "<="; - q.append (v); + q.append (v, conversion_); return q; } @@ -885,7 +901,7 @@ namespace odb query q (table_, column_); q += "<="; - q.append (r); + q.append (r, conversion_); return q; } @@ -956,7 +972,7 @@ namespace odb query q (table_, column_); q += ">="; - q.append (v); + q.append (v, conversion_); return q; } @@ -976,7 +992,7 @@ namespace odb query q (table_, column_); q += ">="; - q.append (r); + q.append (r, conversion_); return q; } @@ -1120,6 +1136,7 @@ namespace odb private: const char* table_; const char* column_; + const char* conversion_; unsigned short prec_; short scale_; @@ -1133,10 +1150,12 @@ namespace odb struct LIBODB_ORACLE_EXPORT lob_query_column { - // Note that we keep shalow copies of the table and column names. + // Note that we keep shallow copies of the table and column names. + // There is also no need for conversion expression since the only + // valid tests are is IS NULL/IS NOT NULL. // lob_query_column (const char* table, const char* column) - : table_ (table), column_ (column) + : table_ (table), column_ (column) { } @@ -1179,7 +1198,7 @@ namespace odb template struct query_column: lob_query_column { - query_column (const char* table, const char* column) + query_column (const char* table, const char* column, const char*) : lob_query_column (table, column) { } @@ -1188,7 +1207,7 @@ namespace odb template struct query_column: lob_query_column { - query_column (const char* table, const char* column) + query_column (const char* table, const char* column, const char*) : lob_query_column (table, column) { } @@ -1197,7 +1216,7 @@ namespace odb template struct query_column: lob_query_column { - query_column (const char* table, const char* column) + query_column (const char* table, const char* column, const char*) : lob_query_column (table, column) { } diff --git a/odb/oracle/query.ixx b/odb/oracle/query.ixx index 7f13d41..2a41ae4 100644 --- a/odb/oracle/query.ixx +++ b/odb/oracle/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/oracle/query.txx b/odb/oracle/query.txx index dc89db1..47bf513 100644 --- a/odb/oracle/query.txx +++ b/odb/oracle/query.txx @@ -19,7 +19,8 @@ namespace odb // append (c.table (), c.column ()); append ("="); - append (val_bind (true, c.prec (), c.scale ())); + append (val_bind (true, c.prec (), c.scale ()), + c.conversion ()); } // query_column @@ -30,9 +31,9 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append (val_bind (v1, prec_, scale_)); + q.append (val_bind (v1, prec_, scale_), conversion_); q += ","; - q.append (val_bind (v2, prec_, scale_)); + q.append (val_bind (v2, prec_, scale_), conversion_); q += ")"; return q; } @@ -43,11 +44,11 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append (val_bind (v1, prec_, scale_)); + q.append (val_bind (v1, prec_, scale_), conversion_); q += ","; - q.append (val_bind (v2, prec_, scale_)); + q.append (val_bind (v2, prec_, scale_), conversion_); q += ","; - q.append (val_bind (v3, prec_, scale_)); + q.append (val_bind (v3, prec_, scale_), conversion_); q += ")"; return q; } @@ -58,13 +59,13 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append (val_bind (v1, prec_, scale_)); + q.append (val_bind (v1, prec_, scale_), conversion_); q += ","; - q.append (val_bind (v2, prec_, scale_)); + q.append (val_bind (v2, prec_, scale_), conversion_); q += ","; - q.append (val_bind (v3, prec_, scale_)); + q.append (val_bind (v3, prec_, scale_), conversion_); q += ","; - q.append (val_bind (v4, prec_, scale_)); + q.append (val_bind (v4, prec_, scale_), conversion_); q += ")"; return q; } @@ -75,15 +76,15 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append (val_bind (v1, prec_, scale_)); + q.append (val_bind (v1, prec_, scale_), conversion_); q += ","; - q.append (val_bind (v2, prec_, scale_)); + q.append (val_bind (v2, prec_, scale_), conversion_); q += ","; - q.append (val_bind (v3, prec_, scale_)); + q.append (val_bind (v3, prec_, scale_), conversion_); q += ","; - q.append (val_bind (v4, prec_, scale_)); + q.append (val_bind (v4, prec_, scale_), conversion_); q += ","; - q.append (val_bind (v5, prec_, scale_)); + q.append (val_bind (v5, prec_, scale_), conversion_); q += ")"; return q; } @@ -101,7 +102,7 @@ namespace odb if (i != begin) q += ","; - q.append (val_bind (*i, prec_, scale_)); + q.append (val_bind (*i, prec_, scale_), conversion_); } q += ")"; return q; -- cgit v1.1