diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-11-21 13:11:43 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-11-21 13:11:43 +0200 |
commit | 4d51a8a248bcbe50849b6c7682aaca1aa9ebff98 (patch) | |
tree | 968edd20009ec455ed54c072820c9a338eaa1988 | |
parent | 2eac10b0aae159bdfc249c67f11b32a65036352d (diff) |
Add dynamic multi-database query support
-rw-r--r-- | odb/pgsql/makefile | 1 | ||||
-rw-r--r-- | odb/pgsql/query-dynamic.cxx | 136 | ||||
-rw-r--r-- | odb/pgsql/query-dynamic.hxx | 33 | ||||
-rw-r--r-- | odb/pgsql/query-dynamic.ixx | 27 | ||||
-rw-r--r-- | odb/pgsql/query-dynamic.txx | 21 | ||||
-rw-r--r-- | odb/pgsql/query.cxx | 2 | ||||
-rw-r--r-- | odb/pgsql/query.hxx | 95 | ||||
-rw-r--r-- | odb/pgsql/query.ixx | 4 | ||||
-rw-r--r-- | odb/pgsql/traits.hxx | 11 |
9 files changed, 297 insertions, 33 deletions
diff --git a/odb/pgsql/makefile b/odb/pgsql/makefile index c18a8de..4217d4c 100644 --- a/odb/pgsql/makefile +++ b/odb/pgsql/makefile @@ -13,6 +13,7 @@ error.cxx \ exceptions.cxx \ prepared-query.cxx \ query.cxx \ +query-dynamic.cxx \ query-const-expr.cxx \ simple-object-statements.cxx \ statement.cxx \ diff --git a/odb/pgsql/query-dynamic.cxx b/odb/pgsql/query-dynamic.cxx new file mode 100644 index 0000000..8e3ae71 --- /dev/null +++ b/odb/pgsql/query-dynamic.cxx @@ -0,0 +1,136 @@ +// file : odb/pgsql/query-dynamic.cxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <cstddef> // std::size_t + +#include <odb/pgsql/query-dynamic.hxx> + +using namespace std; + +namespace odb +{ + namespace pgsql + { + static const char* logic_operators[] = {") AND (", ") OR ("}; + static const char* comp_operators[] = {"=", "!=", "<", ">", "<=", ">="}; + + static void + translate (query_base& q, const odb::query_base& s, size_t p) + { + typedef odb::query_base::clause_part part; + + const part& x (s.clause ()[p]); + + switch (x.kind) + { + case part::kind_column: + { + const query_column_base* c ( + static_cast<const query_column_base*> ( + x.native_info[id_pgsql].column)); + + q.append (c->table (), c->column ()); + break; + } + case part::kind_param_val: + case part::kind_param_ref: + { + const query_column_base* c ( + static_cast<const query_column_base*> ( + x.native_info[id_pgsql].column)); + + query_param_factory f ( + reinterpret_cast<query_param_factory> ( + x.native_info[id_pgsql].param_factory)); + + const odb::query_param* p ( + reinterpret_cast<const odb::query_param*> (x.data)); + + q.append (f (p->value, x.kind == part::kind_param_ref), + c->conversion ()); + break; + } + case part::kind_native: + { + q.append (s.strings ()[x.data]); + break; + } + case part::kind_true: + case part::kind_false: + { + q.append (x.kind == part::kind_true); + break; + } + case part::op_add: + { + translate (q, s, x.data); + translate (q, s, p - 1); + break; + } + case part::op_and: + case part::op_or: + { + q += "("; + translate (q, s, x.data); + q += logic_operators[x.kind - part::op_and]; + translate (q, s, p - 1); + q += ")"; + break; + } + case part::op_not: + { + q += "NOT ("; + translate (q, s, p - 1); + q += ")"; + break; + } + case part::op_null: + case part::op_not_null: + { + translate (q, s, p - 1); + q += (x.kind == part::op_null ? "IS NULL" : "IS NOT NULL"); + break; + } + case part::op_in: + { + size_t b (p - x.data); + + translate (q, s, b - 1); // column + q += "IN ("; + + for (size_t i (b); i != p; ++i) + { + if (i != b) + q += ","; + + translate (q, s, i); + } + + q += ")"; + break; + } + case part::op_eq: + case part::op_ne: + case part::op_lt: + case part::op_gt: + case part::op_le: + case part::op_ge: + { + translate (q, s, x.data); + q += comp_operators[x.kind - part::op_eq]; + translate (q, s, p - 1); + break; + } + } + } + + query_base:: + query_base (const odb::query_base& q) + : binding_ (0, 0), native_binding_ (0, 0, 0, 0) + { + if (!q.empty ()) + translate (*this, q, q.clause ().size () - 1); + } + } +} diff --git a/odb/pgsql/query-dynamic.hxx b/odb/pgsql/query-dynamic.hxx new file mode 100644 index 0000000..6cc9a43 --- /dev/null +++ b/odb/pgsql/query-dynamic.hxx @@ -0,0 +1,33 @@ +// file : odb/pgsql/query-dynamic.hxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_PGSQL_QUERY_DYNAMIC_HXX +#define ODB_PGSQL_QUERY_DYNAMIC_HXX + +#include <odb/pre.hxx> + +#include <odb/query.hxx> +#include <odb/query-dynamic.hxx> + +#include <odb/pgsql/query.hxx> + +namespace odb +{ + namespace pgsql + { + typedef details::shared_ptr<query_param> (*query_param_factory) ( + const void* val, bool by_ref); + + template <typename T, database_type_id ID> + details::shared_ptr<query_param> + query_param_factory_impl (const void*, bool); + } +} + +#include <odb/pgsql/query-dynamic.ixx> +#include <odb/pgsql/query-dynamic.txx> + +#include <odb/post.hxx> + +#endif // ODB_PGSQL_QUERY_DYNAMIC_HXX diff --git a/odb/pgsql/query-dynamic.ixx b/odb/pgsql/query-dynamic.ixx new file mode 100644 index 0000000..5ea89a1 --- /dev/null +++ b/odb/pgsql/query-dynamic.ixx @@ -0,0 +1,27 @@ +// file : odb/pgsql/query-dynamic.ixx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace pgsql + { + // + // + template <typename T, database_type_id ID> + inline query_column<T, ID>:: + query_column (odb::query_column<T>& qc, + const char* table, const char* column, const char* conv) + : query_column_base (table, column, conv) + { + native_column_info& ci (qc.native_info[id_pgsql]); + ci.column = static_cast<query_column_base*> (this); + + // For some reason GCC needs this statically-typed pointer in + // order to instantiate the functions. + // + query_param_factory f (&query_param_factory_impl<T, ID>); + ci.param_factory = reinterpret_cast<void*> (f); + } + } +} diff --git a/odb/pgsql/query-dynamic.txx b/odb/pgsql/query-dynamic.txx new file mode 100644 index 0000000..d08338c --- /dev/null +++ b/odb/pgsql/query-dynamic.txx @@ -0,0 +1,21 @@ +// file : odb/pgsql/query-dynamic.txx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace pgsql + { + template <typename T, database_type_id ID> + details::shared_ptr<query_param> + query_param_factory_impl (const void* val, bool by_ref) + { + const T& v (*static_cast<const T*> (val)); + + return details::shared_ptr<query_param> ( + by_ref + ? new (details::shared) query_param_impl<T, ID> (ref_bind<T> (v)) + : new (details::shared) query_param_impl<T, ID> (val_bind<T> (v))); + } + } +} diff --git a/odb/pgsql/query.cxx b/odb/pgsql/query.cxx index d02a195..725ed6a 100644 --- a/odb/pgsql/query.cxx +++ b/odb/pgsql/query.cxx @@ -188,7 +188,7 @@ namespace odb } void query_base:: - add (details::shared_ptr<query_param> p, const char* conv) + append (details::shared_ptr<query_param> p, const char* conv) { clause_.push_back (clause_part (clause_part::kind_param)); diff --git a/odb/pgsql/query.hxx b/odb/pgsql/query.hxx index 401f29a..997f206 100644 --- a/odb/pgsql/query.hxx +++ b/odb/pgsql/query.hxx @@ -11,6 +11,7 @@ #include <vector> #include <cstddef> // std::size_t +#include <odb/forward.hxx> // odb::query_column #include <odb/query.hxx> #include <odb/pgsql/version.hxx> @@ -119,7 +120,7 @@ namespace odb query_base (bool v) : binding_ (0, 0), native_binding_ (0, 0, 0, 0) { - clause_.push_back (clause_part (v)); + append (v); } explicit @@ -163,6 +164,13 @@ namespace odb template <database_type_id ID> query_base (const query_column<bool, ID>&); + // Translate common query representation to PostgreSQL native. Defined + // in query-dynamic.cxx + // + query_base (const odb::query_base&); + + // Copy c-tor and assignment. + // query_base (const query_base&); query_base& @@ -259,6 +267,8 @@ namespace odb return *this; } + // Implementation details. + // public: template <typename T, database_type_id ID> void @@ -269,14 +279,25 @@ namespace odb append (ref_bind<T>, const char* conv); void + append (details::shared_ptr<query_param>, const char* conv); + + void + append (bool v) + { + clause_.push_back (clause_part (v)); + } + + void append (const std::string& native); void - append (const char* table, const char* column); + append (const char* native) // Clashes with append(bool). + { + append (std::string (native)); + } - private: void - add (details::shared_ptr<query_param>, const char* conv); + append (const char* table, const char* column); private: typedef std::vector<clause_part> clause_type; @@ -406,28 +427,14 @@ namespace odb // query_column // - - template <typename T, typename T2> - class copy_bind: public val_bind<T> - { - public: - explicit - copy_bind (const T2& v): val_bind<T> (val), val (v) {} - - const T val; - }; - - template <typename T> - const T& - type_instance (); - - template <typename T, database_type_id ID> - struct query_column + struct LIBODB_PGSQL_EXPORT query_column_base { // 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, const char* conv) + query_column_base (const char* table, + const char* column, + const char* conv) : table_ (table), column_ (column), conversion_ (conv) { } @@ -452,6 +459,40 @@ namespace odb return conversion_; } + protected: + const char* table_; + const char* column_; + const char* conversion_; + }; + + template <typename T, typename T2> + class copy_bind: public val_bind<T> + { + public: + explicit + copy_bind (const T2& v): val_bind<T> (val), val (v) {} + + const T val; + }; + + template <typename T> + const T& + type_instance (); + + template <typename T, database_type_id ID> + struct query_column: query_column_base + { + // 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, const char* conv) + : query_column_base (table, column, conv) {} + + // Implementation is in query-dynamic.ixx. + // + query_column (odb::query_column<T>&, + const char* table, const char* column, const char* conv); + // is_null, is_not_null // public: @@ -1086,11 +1127,6 @@ namespace odb q.append (c.table (), c.column ()); return q; } - - private: - const char* table_; - const char* column_; - const char* conversion_; }; // Provide operator+() for using columns to construct native @@ -1824,6 +1860,11 @@ namespace odb : query_base (qc) { } + + query (const odb::query_base& q) + : query_base (q) + { + } }; } diff --git a/odb/pgsql/query.ixx b/odb/pgsql/query.ixx index 178a6eb..0000d22 100644 --- a/odb/pgsql/query.ixx +++ b/odb/pgsql/query.ixx @@ -16,7 +16,7 @@ namespace odb inline void query_base:: append (val_bind<T> v, const char* conv) { - add ( + append ( details::shared_ptr<query_param> ( new (details::shared) query_param_impl<T, ID> (v)), conv); @@ -26,7 +26,7 @@ namespace odb inline void query_base:: append (ref_bind<T> r, const char* conv) { - add ( + append ( details::shared_ptr<query_param> ( new (details::shared) query_param_impl<T, ID> (r)), conv); diff --git a/odb/pgsql/traits.hxx b/odb/pgsql/traits.hxx index 8b330e5..f5c6f8d 100644 --- a/odb/pgsql/traits.hxx +++ b/odb/pgsql/traits.hxx @@ -425,7 +425,6 @@ namespace odb { public: typedef const char* value_type; - typedef const char* query_type; typedef details::buffer image_type; static void @@ -439,34 +438,40 @@ namespace odb struct LIBODB_PGSQL_EXPORT default_value_traits<const char*, id_numeric>: c_string_value_traits { + typedef const char* query_type; }; template <> struct LIBODB_PGSQL_EXPORT default_value_traits<const char*, id_string>: c_string_value_traits { + typedef const char* query_type; }; template <std::size_t N> struct default_value_traits<char[N], id_numeric>: c_string_value_traits { + typedef char query_type[N]; }; template <std::size_t N> struct default_value_traits<const char[N], id_numeric>: c_string_value_traits { + typedef const char query_type[N]; }; template <std::size_t N> struct default_value_traits<char[N], id_string>: c_string_value_traits { + typedef char query_type[N]; }; template <std::size_t N> struct default_value_traits<const char[N], id_string>: c_string_value_traits { + typedef const char query_type[N]; }; // std::vector<char> (buffer) specialization. @@ -540,7 +545,7 @@ namespace odb { public: typedef char* value_type; - typedef const char* query_type; + typedef char query_type[N]; typedef details::buffer image_type; static void @@ -578,7 +583,7 @@ namespace odb { public: typedef unsigned char* value_type; - typedef const unsigned char* query_type; + typedef unsigned char query_type[N]; typedef details::buffer image_type; static void |