From e04895dcc4008a8c0cf53f34ef787a9c686ebcde 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/mssql/makefile | 1 + odb/mssql/query-dynamic.cxx | 136 ++++++++++++++++++++++++++++++++++++++++++++ odb/mssql/query-dynamic.hxx | 33 +++++++++++ odb/mssql/query-dynamic.ixx | 31 ++++++++++ odb/mssql/query-dynamic.txx | 26 +++++++++ odb/mssql/query.cxx | 2 +- odb/mssql/query.hxx | 121 +++++++++++++++++++++++++++------------ odb/mssql/query.ixx | 4 +- odb/mssql/traits.hxx | 21 ++++--- 9 files changed, 328 insertions(+), 47 deletions(-) create mode 100644 odb/mssql/query-dynamic.cxx create mode 100644 odb/mssql/query-dynamic.hxx create mode 100644 odb/mssql/query-dynamic.ixx create mode 100644 odb/mssql/query-dynamic.txx diff --git a/odb/mssql/makefile b/odb/mssql/makefile index fb87738..1b48b44 100644 --- a/odb/mssql/makefile +++ b/odb/mssql/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/mssql/query-dynamic.cxx b/odb/mssql/query-dynamic.cxx new file mode 100644 index 0000000..0efe5f1 --- /dev/null +++ b/odb/mssql/query-dynamic.cxx @@ -0,0 +1,136 @@ +// file : odb/mssql/query-dynamic.cxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include // std::size_t + +#include + +using namespace std; + +namespace odb +{ + namespace mssql + { + 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_mssql].column)); + + q.append (c->table (), c->column ()); + break; + } + case part::kind_param_val: + case part::kind_param_ref: + { + const query_column_base& qc ( + *static_cast ( + x.native_info[id_mssql].column)); + + query_param_factory f ( + reinterpret_cast ( + x.native_info[id_mssql].param_factory)); + + const odb::query_param* p ( + reinterpret_cast (x.data)); + + q.append (f (p->value, qc, x.kind == part::kind_param_ref), + qc.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/mssql/query-dynamic.hxx b/odb/mssql/query-dynamic.hxx new file mode 100644 index 0000000..d771d02 --- /dev/null +++ b/odb/mssql/query-dynamic.hxx @@ -0,0 +1,33 @@ +// file : odb/mssql/query-dynamic.hxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#ifndef ODB_MSSQL_QUERY_DYNAMIC_HXX +#define ODB_MSSQL_QUERY_DYNAMIC_HXX + +#include + +#include +#include + +#include + +namespace odb +{ + namespace mssql + { + typedef details::shared_ptr (*query_param_factory) ( + const void* val, const query_column_base&, bool by_ref); + + template + details::shared_ptr + query_param_factory_impl (const void*, const query_column_base&, bool); + } +} + +#include +#include + +#include + +#endif // ODB_MSSQL_QUERY_DYNAMIC_HXX diff --git a/odb/mssql/query-dynamic.ixx b/odb/mssql/query-dynamic.ixx new file mode 100644 index 0000000..7e9decc --- /dev/null +++ b/odb/mssql/query-dynamic.ixx @@ -0,0 +1,31 @@ +// file : odb/mssql/query-dynamic.ixx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +namespace odb +{ + namespace mssql + { + // + // + template + inline query_column:: + query_column (odb::query_column& qc, + const char* table, + const char* column, + const char* conv, + unsigned short prec, + unsigned short scale) + : query_column_base (table, column, conv, prec, scale) + { + native_column_info& ci (qc.native_info[id_mssql]); + 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/mssql/query-dynamic.txx b/odb/mssql/query-dynamic.txx new file mode 100644 index 0000000..e685af6 --- /dev/null +++ b/odb/mssql/query-dynamic.txx @@ -0,0 +1,26 @@ +// file : odb/mssql/query-dynamic.txx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +namespace odb +{ + namespace mssql + { + template + details::shared_ptr + query_param_factory_impl (const void* val, + const query_column_base& qc, + bool by_ref) + { + const T& v (*static_cast (val)); + + unsigned short p (qc.prec ()); + unsigned short s (qc.scale ()); + + return details::shared_ptr ( + by_ref + ? new (details::shared) query_param_impl (ref_bind (v, p, s)) + : new (details::shared) query_param_impl (val_bind (v, p, s))); + } + } +} diff --git a/odb/mssql/query.cxx b/odb/mssql/query.cxx index bea3352..371df37 100644 --- a/odb/mssql/query.cxx +++ b/odb/mssql/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/mssql/query.hxx b/odb/mssql/query.hxx index c0788a6..356ed58 100644 --- a/odb/mssql/query.hxx +++ b/odb/mssql/query.hxx @@ -11,6 +11,7 @@ #include #include // std::size_t +#include // odb::query_column #include #include @@ -137,7 +138,7 @@ namespace odb query_base (bool v) : binding_ (0, 0) { - clause_.push_back (clause_part (v)); + append (v); } explicit @@ -181,6 +182,13 @@ namespace odb template query_base (const query_column&); + // Translate common query representation to SQL Server native. Defined + // in query-dynamic.cxx + // + query_base (const odb::query_base&); + + // Copy c-tor and assignment. + // query_base (const query_base&); query_base& @@ -265,6 +273,8 @@ namespace odb return *this; } + // Implementation details. + // public: template void @@ -275,14 +285,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; @@ -405,34 +426,18 @@ namespace odb // query_column // - - template - class copy_bind: public val_bind + struct LIBODB_MSSQL_EXPORT query_column_base { - public: - explicit - copy_bind (const T2& v): val_bind (val), val (v) {} - - const T val; - }; - - template - const T& - type_instance (); - - template - struct query_column - { - // Note that we keep shalow copies of the table, column, and conversion + // 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, - unsigned short prec = 0, - unsigned short scale = 0xFFFF) - : table_ (table), column_ (column), conversion_ (conv), - prec_ (prec), scale_ (scale) + query_column_base (const char* table, + const char* column, + const char* conv, + unsigned short prec, + unsigned short scale) + : table_ (table), column_ (column), conversion_ (conv), + prec_ (prec), scale_ (scale) { } @@ -468,6 +473,51 @@ namespace odb return scale_; } + protected: + const char* table_; + const char* column_; + const char* conversion_; + + unsigned short prec_; + unsigned short scale_; + }; + + 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 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 = 0, + unsigned short scale = 0xFFFF) + : query_column_base (table, column, conv, prec, scale) {} + + // Implementation is in query-dynamic.ixx. + // + query_column (odb::query_column&, + const char* table, + const char* column, + const char* conv, + unsigned short prec = 0, + unsigned short scale = 0xFFFF); + // is_null, is_not_null // public: @@ -1138,14 +1188,6 @@ namespace odb q.append (c.table (), c.column ()); return q; } - - private: - const char* table_; - const char* column_; - const char* conversion_; - - unsigned short prec_; - unsigned short scale_; }; // Provide operator+() for using columns to construct native @@ -2328,6 +2370,11 @@ namespace odb : query_base (qc) { } + + query (const odb::query_base& q) + : query_base (q) + { + } }; } diff --git a/odb/mssql/query.ixx b/odb/mssql/query.ixx index 8683aa0..04e6fde 100644 --- a/odb/mssql/query.ixx +++ b/odb/mssql/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/mssql/traits.hxx b/odb/mssql/traits.hxx index f31c38d..d1399aa 100644 --- a/odb/mssql/traits.hxx +++ b/odb/mssql/traits.hxx @@ -612,7 +612,6 @@ namespace odb { public: typedef const char* value_type; - typedef const char* query_type; typedef char* image_type; static void @@ -637,17 +636,20 @@ namespace odb struct LIBODB_MSSQL_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]; }; // std::string specialization for long_string. @@ -706,7 +708,6 @@ namespace odb { public: typedef const char* value_type; - typedef const char* query_type; typedef long_callback image_type; static void @@ -735,18 +736,21 @@ namespace odb id_long_string>: c_string_long_value_traits { + typedef const char* query_type; }; template struct default_value_traits: c_string_long_value_traits { + typedef char query_type[N]; }; template struct default_value_traits: c_string_long_value_traits { + typedef const char query_type[N]; }; // std::wstring specialization for nstring. @@ -829,7 +833,6 @@ namespace odb { public: typedef const wchar_t* value_type; - typedef const wchar_t* query_type; typedef ucs2_char* image_type; typedef wstring_functions<> functions; @@ -856,18 +859,21 @@ namespace odb id_nstring>: c_wstring_value_traits { + typedef const wchar_t* query_type; }; template struct default_value_traits: c_wstring_value_traits { + typedef wchar_t query_type[N]; }; template struct default_value_traits: c_wstring_value_traits { + typedef const wchar_t query_type[N]; }; // std::wstring specialization for long_nstring. @@ -961,7 +967,6 @@ namespace odb struct LIBODB_MSSQL_EXPORT c_wstring_long_value_traits<2> { typedef const wchar_t* value_type; - typedef const wchar_t* query_type; typedef long_callback image_type; static void @@ -990,7 +995,6 @@ namespace odb struct LIBODB_MSSQL_EXPORT c_wstring_long_value_traits<4> { typedef const wchar_t* value_type; - typedef const wchar_t* query_type; typedef long_callback image_type; static void @@ -1020,18 +1024,21 @@ namespace odb id_long_nstring>: c_wstring_long_value_traits<> { + typedef const wchar_t* query_type; }; template struct default_value_traits: c_wstring_long_value_traits<> { + typedef wchar_t query_type[N]; }; template struct default_value_traits: c_wstring_long_value_traits<> { + typedef const wchar_t query_type[N]; }; // std::vector (buffer) specialization for binary. @@ -1095,7 +1102,7 @@ namespace odb struct c_array_binary_value_traits { typedef C* value_type; - typedef const C* query_type; + typedef C query_type[N]; typedef char* image_type; static void @@ -1283,7 +1290,7 @@ namespace odb struct c_array_long_binary_value_traits { typedef C* value_type; - typedef const C* query_type; + typedef C query_type[N]; typedef long_callback image_type; static void -- cgit v1.1