diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-07-10 15:17:14 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-07-10 15:17:14 +0200 |
commit | 1568e64c2135bf245de960ae614bd3533c6d157a (patch) | |
tree | fe9d13549fe51ec95a22a3e0770e2a488fa57007 | |
parent | 47d783b340df91a43abc36470c78f998db0b33fe (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/sqlite/details/conversion.hxx | 59 | ||||
-rw-r--r-- | odb/sqlite/query.cxx | 19 | ||||
-rw-r--r-- | odb/sqlite/query.hxx | 62 | ||||
-rw-r--r-- | odb/sqlite/query.ixx | 10 | ||||
-rw-r--r-- | odb/sqlite/query.txx | 32 |
5 files changed, 138 insertions, 44 deletions
diff --git a/odb/sqlite/details/conversion.hxx b/odb/sqlite/details/conversion.hxx new file mode 100644 index 0000000..9daf35c --- /dev/null +++ b/odb/sqlite/details/conversion.hxx @@ -0,0 +1,59 @@ +// file : odb/sqlite/details/conversion.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SQLITE_DETAILS_CONVERSION_HXX +#define ODB_SQLITE_DETAILS_CONVERSION_HXX + +#include <odb/sqlite/traits.hxx> + +#include <odb/details/meta/answer.hxx> + +namespace odb +{ + // @@ Revise this. + // + namespace details {} + + namespace sqlite + { + 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_SQLITE_DETAILS_CONVERSION_HXX diff --git a/odb/sqlite/query.cxx b/odb/sqlite/query.cxx index 0adf8d9..2c0706a 100644 --- a/odb/sqlite/query.cxx +++ b/odb/sqlite/query.cxx @@ -188,9 +188,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_->add (p); } @@ -287,7 +291,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/sqlite/query.hxx b/odb/sqlite/query.hxx index 54e0b2b..ee05feb 100644 --- a/odb/sqlite/query.hxx +++ b/odb/sqlite/query.hxx @@ -20,7 +20,9 @@ #include <odb/sqlite/traits.hxx> #include <odb/sqlite/sqlite-types.hxx> #include <odb/sqlite/binding.hxx> + #include <odb/sqlite/details/export.hxx> +#include <odb/sqlite/details/conversion.hxx> namespace odb { @@ -129,7 +131,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; }; @@ -172,7 +174,8 @@ namespace odb query (val_bind<T> v) : parameters_ (new (details::shared) query_params) { - 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> @@ -180,7 +183,8 @@ namespace odb query (ref_bind<T> r) : parameters_ (new (details::shared) query_params) { - 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> @@ -254,7 +258,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; } @@ -262,18 +267,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); @@ -283,7 +289,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; @@ -421,10 +427,11 @@ 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 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) { } @@ -440,6 +447,14 @@ namespace odb return column_; } + // Can be NULL. + // + const char* + conversion () const + { + return conversion_; + } + // is_null, is_not_null // public: @@ -492,7 +507,7 @@ namespace odb { query q (table_, column_); q += "="; - q.append<T, ID> (v); + q.append<T, ID> (v, conversion_); return q; } @@ -509,7 +524,7 @@ namespace odb { query q (table_, column_); q += "="; - q.append<T, ID> (r); + q.append<T, ID> (r, conversion_); return q; } @@ -577,7 +592,7 @@ namespace odb { query q (table_, column_); q += "!="; - q.append<T, ID> (v); + q.append<T, ID> (v, conversion_); return q; } @@ -594,7 +609,7 @@ namespace odb { query q (table_, column_); q += "!="; - q.append<T, ID> (r); + q.append<T, ID> (r, conversion_); return q; } @@ -662,7 +677,7 @@ namespace odb { query q (table_, column_); q += "<"; - q.append<T, ID> (v); + q.append<T, ID> (v, conversion_); return q; } @@ -679,7 +694,7 @@ namespace odb { query q (table_, column_); q += "<"; - q.append<T, ID> (r); + q.append<T, ID> (r, conversion_); return q; } @@ -747,7 +762,7 @@ namespace odb { query q (table_, column_); q += ">"; - q.append<T, ID> (v); + q.append<T, ID> (v, conversion_); return q; } @@ -764,7 +779,7 @@ namespace odb { query q (table_, column_); q += ">"; - q.append<T, ID> (r); + q.append<T, ID> (r, conversion_); return q; } @@ -832,7 +847,7 @@ namespace odb { query q (table_, column_); q += "<="; - q.append<T, ID> (v); + q.append<T, ID> (v, conversion_); return q; } @@ -849,7 +864,7 @@ namespace odb { query q (table_, column_); q += "<="; - q.append<T, ID> (r); + q.append<T, ID> (r, conversion_); return q; } @@ -917,7 +932,7 @@ namespace odb { query q (table_, column_); q += ">="; - q.append<T, ID> (v); + q.append<T, ID> (v, conversion_); return q; } @@ -934,7 +949,7 @@ namespace odb { query q (table_, column_); q += ">="; - q.append<T, ID> (r); + q.append<T, ID> (r, conversion_); return q; } @@ -1078,6 +1093,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/sqlite/query.ixx b/odb/sqlite/query.ixx index dcd1ebe..113b376 100644 --- a/odb/sqlite/query.ixx +++ b/odb/sqlite/query.ixx @@ -20,20 +20,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/sqlite/query.txx b/odb/sqlite/query.txx index b095d52..ba3fac1 100644 --- a/odb/sqlite/query.txx +++ b/odb/sqlite/query.txx @@ -19,7 +19,7 @@ namespace odb // append (c.table (), c.column ()); append ("="); - append<bool, ID> (val_bind<bool> (true)); + append<bool, ID> (val_bind<bool> (true), c.conversion ()); } // query_column @@ -30,9 +30,9 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append<T, ID> (val_bind<T> (v1)); + q.append<T, ID> (val_bind<T> (v1), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v2)); + q.append<T, ID> (val_bind<T> (v2), conversion_); q += ")"; return q; } @@ -43,11 +43,11 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append<T, ID> (val_bind<T> (v1)); + q.append<T, ID> (val_bind<T> (v1), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v2)); + q.append<T, ID> (val_bind<T> (v2), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v3)); + q.append<T, ID> (val_bind<T> (v3), conversion_); q += ")"; return q; } @@ -58,13 +58,13 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append<T, ID> (val_bind<T> (v1)); + q.append<T, ID> (val_bind<T> (v1), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v2)); + q.append<T, ID> (val_bind<T> (v2), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v3)); + q.append<T, ID> (val_bind<T> (v3), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v4)); + q.append<T, ID> (val_bind<T> (v4), conversion_); q += ")"; return q; } @@ -75,15 +75,15 @@ namespace odb { query q (table_, column_); q += "IN ("; - q.append<T, ID> (val_bind<T> (v1)); + q.append<T, ID> (val_bind<T> (v1), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v2)); + q.append<T, ID> (val_bind<T> (v2), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v3)); + q.append<T, ID> (val_bind<T> (v3), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v4)); + q.append<T, ID> (val_bind<T> (v4), conversion_); q += ","; - q.append<T, ID> (val_bind<T> (v5)); + q.append<T, ID> (val_bind<T> (v5), conversion_); q += ")"; return q; } @@ -101,7 +101,7 @@ namespace odb if (i != begin) q += ","; - q.append<T, ID> (val_bind<T> (*i)); + q.append<T, ID> (val_bind<T> (*i), conversion_); } q += ")"; return q; |