From 9ccd928f7b3d36b7be4a8dc8d180a441c9bf55e1 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/mysql/makefile | 1 + odb/mysql/query-dynamic.cxx | 136 ++++++++++++++++++++++++++++++++++++++++++++ odb/mysql/query-dynamic.hxx | 33 +++++++++++ odb/mysql/query-dynamic.ixx | 27 +++++++++ odb/mysql/query-dynamic.txx | 21 +++++++ odb/mysql/query.cxx | 2 +- odb/mysql/query.hxx | 95 ++++++++++++++++++++++--------- odb/mysql/query.ixx | 4 +- odb/mysql/traits.hxx | 17 +++++- 9 files changed, 303 insertions(+), 33 deletions(-) create mode 100644 odb/mysql/query-dynamic.cxx create mode 100644 odb/mysql/query-dynamic.hxx create mode 100644 odb/mysql/query-dynamic.ixx create mode 100644 odb/mysql/query-dynamic.txx diff --git a/odb/mysql/makefile b/odb/mysql/makefile index 936d17c..f098795 100644 --- a/odb/mysql/makefile +++ b/odb/mysql/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/mysql/query-dynamic.cxx b/odb/mysql/query-dynamic.cxx new file mode 100644 index 0000000..dd78b41 --- /dev/null +++ b/odb/mysql/query-dynamic.cxx @@ -0,0 +1,136 @@ +// file : odb/mysql/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 mysql + { + 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_mysql].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_mysql].column)); + + query_param_factory f ( + reinterpret_cast ( + x.native_info[id_mysql].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) + { + if (!q.empty ()) + translate (*this, q, q.clause ().size () - 1); + } + } +} diff --git a/odb/mysql/query-dynamic.hxx b/odb/mysql/query-dynamic.hxx new file mode 100644 index 0000000..f724174 --- /dev/null +++ b/odb/mysql/query-dynamic.hxx @@ -0,0 +1,33 @@ +// file : odb/mysql/query-dynamic.hxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_MYSQL_QUERY_DYNAMIC_HXX +#define ODB_MYSQL_QUERY_DYNAMIC_HXX + +#include + +#include +#include + +#include + +namespace odb +{ + namespace mysql + { + 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_MYSQL_QUERY_DYNAMIC_HXX diff --git a/odb/mysql/query-dynamic.ixx b/odb/mysql/query-dynamic.ixx new file mode 100644 index 0000000..697f557 --- /dev/null +++ b/odb/mysql/query-dynamic.ixx @@ -0,0 +1,27 @@ +// file : odb/mysql/query-dynamic.ixx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace mysql + { + // + // + 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_mysql]); + 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/mysql/query-dynamic.txx b/odb/mysql/query-dynamic.txx new file mode 100644 index 0000000..42790e0 --- /dev/null +++ b/odb/mysql/query-dynamic.txx @@ -0,0 +1,21 @@ +// file : odb/mysql/query-dynamic.txx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace mysql + { + 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/mysql/query.cxx b/odb/mysql/query.cxx index bd984f6..3ae113c 100644 --- a/odb/mysql/query.cxx +++ b/odb/mysql/query.cxx @@ -119,7 +119,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/mysql/query.hxx b/odb/mysql/query.hxx index 8327ed4..2a02b95 100644 --- a/odb/mysql/query.hxx +++ b/odb/mysql/query.hxx @@ -11,6 +11,7 @@ #include #include // std::size_t +#include // odb::query_column #include #include @@ -115,7 +116,7 @@ namespace odb query_base (bool v) : binding_ (0, 0) { - clause_.push_back (clause_part (v)); + append (v); } explicit @@ -159,6 +160,13 @@ namespace odb template query_base (const query_column&); + // Translate common query representation to MySQL native. Defined + // in query-dynamic.cxx + // + query_base (const odb::query_base&); + + // Copy c-tor and assignment. + // query_base (const query_base&); query_base& @@ -243,6 +251,8 @@ namespace odb return *this; } + // Implementation details. + // public: template void @@ -253,14 +263,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; @@ -383,28 +404,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_MYSQL_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) { } @@ -429,6 +436,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: @@ -1063,11 +1104,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 @@ -1923,6 +1959,11 @@ namespace odb : query_base (qc) { } + + query (const odb::query_base& q) + : query_base (q) + { + } }; } diff --git a/odb/mysql/query.ixx b/odb/mysql/query.ixx index 60affe4..1fedf1a 100644 --- a/odb/mysql/query.ixx +++ b/odb/mysql/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/mysql/traits.hxx b/odb/mysql/traits.hxx index 68ef2dd..4175829 100644 --- a/odb/mysql/traits.hxx +++ b/odb/mysql/traits.hxx @@ -420,7 +420,6 @@ namespace odb { public: typedef const char* value_type; - typedef const char* query_type; typedef details::buffer image_type; static void @@ -434,66 +433,78 @@ namespace odb struct LIBODB_MYSQL_EXPORT default_value_traits: c_string_value_traits { + typedef const char* query_type; }; template <> struct LIBODB_MYSQL_EXPORT default_value_traits: c_string_value_traits { + typedef const char* query_type; }; template <> struct LIBODB_MYSQL_EXPORT default_value_traits: c_string_value_traits { + typedef const char* query_type; }; template <> struct LIBODB_MYSQL_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]; }; 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. @@ -566,7 +577,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 @@ -604,7 +615,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