From 4d51a8a248bcbe50849b6c7682aaca1aa9ebff98 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 21 Nov 2012 13:11:43 +0200 Subject: Add dynamic multi-database query support --- odb/pgsql/makefile | 1 + odb/pgsql/query-dynamic.cxx | 136 ++++++++++++++++++++++++++++++++++++++++++++ odb/pgsql/query-dynamic.hxx | 33 +++++++++++ odb/pgsql/query-dynamic.ixx | 27 +++++++++ odb/pgsql/query-dynamic.txx | 21 +++++++ odb/pgsql/query.cxx | 2 +- odb/pgsql/query.hxx | 95 ++++++++++++++++++++++--------- odb/pgsql/query.ixx | 4 +- odb/pgsql/traits.hxx | 11 +++- 9 files changed, 297 insertions(+), 33 deletions(-) create mode 100644 odb/pgsql/query-dynamic.cxx create mode 100644 odb/pgsql/query-dynamic.hxx create mode 100644 odb/pgsql/query-dynamic.ixx create mode 100644 odb/pgsql/query-dynamic.txx 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 // std::size_t + +#include + +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 ( + 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 ( + x.native_info[id_pgsql].column)); + + query_param_factory f ( + reinterpret_cast ( + x.native_info[id_pgsql].param_factory)); + + const odb::query_param* p ( + reinterpret_cast (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 + +#include +#include + +#include + +namespace odb +{ + namespace pgsql + { + typedef details::shared_ptr (*query_param_factory) ( + const void* val, bool by_ref); + + template + details::shared_ptr + query_param_factory_impl (const void*, bool); + } +} + +#include +#include + +#include + +#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 + inline query_column:: + query_column (odb::query_column& 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 (this); + + // For some reason GCC needs this statically-typed pointer in + // order to instantiate the functions. + // + query_param_factory f (&query_param_factory_impl); + ci.param_factory = reinterpret_cast (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 + details::shared_ptr + query_param_factory_impl (const void* val, bool by_ref) + { + const T& v (*static_cast (val)); + + return details::shared_ptr ( + by_ref + ? new (details::shared) query_param_impl (ref_bind (v)) + : new (details::shared) query_param_impl (val_bind (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 p, const char* conv) + append (details::shared_ptr 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 #include // std::size_t +#include // odb::query_column #include #include @@ -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 query_base (const query_column&); + // 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 void @@ -269,14 +279,25 @@ namespace odb append (ref_bind, const char* conv); void + append (details::shared_ptr, 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, const char* conv); + append (const char* table, const char* column); private: typedef std::vector clause_type; @@ -406,28 +427,14 @@ namespace odb // query_column // - - template - class copy_bind: public val_bind - { - public: - explicit - copy_bind (const T2& v): val_bind (val), val (v) {} - - const T val; - }; - - template - const T& - type_instance (); - - template - 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 + class copy_bind: public val_bind + { + public: + explicit + copy_bind (const T2& v): val_bind (val), val (v) {} + + const T val; + }; + + template + const T& + type_instance (); + + template + 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&, + 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 v, const char* conv) { - add ( + append ( details::shared_ptr ( new (details::shared) query_param_impl (v)), conv); @@ -26,7 +26,7 @@ namespace odb inline void query_base:: append (ref_bind r, const char* conv) { - add ( + append ( details::shared_ptr ( new (details::shared) query_param_impl (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: c_string_value_traits { + typedef const char* query_type; }; template <> struct LIBODB_PGSQL_EXPORT default_value_traits: c_string_value_traits { + typedef const char* query_type; }; template struct default_value_traits: c_string_value_traits { + typedef char query_type[N]; }; template struct default_value_traits: c_string_value_traits { + typedef const char query_type[N]; }; template struct default_value_traits: c_string_value_traits { + typedef char query_type[N]; }; template struct default_value_traits: c_string_value_traits { + typedef const char query_type[N]; }; // std::vector (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 -- cgit v1.1