aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2012-11-21 13:11:43 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2012-11-21 13:11:43 +0200
commite04895dcc4008a8c0cf53f34ef787a9c686ebcde (patch)
treea3085ab592d9f037e08d6469bcb9ba4a554704dc
parent94607b822a6dcb4be1ab54dafaafde4810cb1f72 (diff)
Add dynamic multi-database query support
-rw-r--r--odb/mssql/makefile1
-rw-r--r--odb/mssql/query-dynamic.cxx136
-rw-r--r--odb/mssql/query-dynamic.hxx33
-rw-r--r--odb/mssql/query-dynamic.ixx31
-rw-r--r--odb/mssql/query-dynamic.txx26
-rw-r--r--odb/mssql/query.cxx2
-rw-r--r--odb/mssql/query.hxx121
-rw-r--r--odb/mssql/query.ixx4
-rw-r--r--odb/mssql/traits.hxx21
9 files changed, 328 insertions, 47 deletions
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 <cstddef> // std::size_t
+
+#include <odb/mssql/query-dynamic.hxx>
+
+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<const query_column_base*> (
+ 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<const query_column_base*> (
+ x.native_info[id_mssql].column));
+
+ query_param_factory f (
+ reinterpret_cast<query_param_factory> (
+ x.native_info[id_mssql].param_factory));
+
+ const odb::query_param* p (
+ reinterpret_cast<const odb::query_param*> (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 <odb/pre.hxx>
+
+#include <odb/query.hxx>
+#include <odb/query-dynamic.hxx>
+
+#include <odb/mssql/query.hxx>
+
+namespace odb
+{
+ namespace mssql
+ {
+ typedef details::shared_ptr<query_param> (*query_param_factory) (
+ const void* val, const query_column_base&, bool by_ref);
+
+ template <typename T, database_type_id ID>
+ details::shared_ptr<query_param>
+ query_param_factory_impl (const void*, const query_column_base&, bool);
+ }
+}
+
+#include <odb/mssql/query-dynamic.ixx>
+#include <odb/mssql/query-dynamic.txx>
+
+#include <odb/post.hxx>
+
+#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 <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,
+ 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<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/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 <typename T, database_type_id ID>
+ details::shared_ptr<query_param>
+ query_param_factory_impl (const void* val,
+ const query_column_base& qc,
+ bool by_ref)
+ {
+ const T& v (*static_cast<const T*> (val));
+
+ unsigned short p (qc.prec ());
+ unsigned short s (qc.scale ());
+
+ return details::shared_ptr<query_param> (
+ by_ref
+ ? new (details::shared) query_param_impl<T, ID> (ref_bind<T> (v, p, s))
+ : new (details::shared) query_param_impl<T, ID> (val_bind<T> (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<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/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 <vector>
#include <cstddef> // std::size_t
+#include <odb/forward.hxx> // odb::query_column
#include <odb/query.hxx>
#include <odb/mssql/version.hxx>
@@ -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 <database_type_id ID>
query_base (const query_column<bool, ID>&);
+ // 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 <typename T, database_type_id ID>
void
@@ -275,14 +285,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;
@@ -405,34 +426,18 @@ namespace odb
// query_column
//
-
- template <typename T, typename T2>
- class copy_bind: public val_bind<T>
+ struct LIBODB_MSSQL_EXPORT query_column_base
{
- 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
- {
- // 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 <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 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<T>&,
+ 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<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/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<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_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::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 <std::size_t N>
struct default_value_traits<char[N], id_long_string>:
c_string_long_value_traits
{
+ typedef char query_type[N];
};
template <std::size_t N>
struct default_value_traits<const char[N], id_long_string>:
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 <std::size_t N>
struct default_value_traits<wchar_t[N], id_nstring>:
c_wstring_value_traits
{
+ typedef wchar_t query_type[N];
};
template <std::size_t N>
struct default_value_traits<const wchar_t[N], id_nstring>:
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 <std::size_t N>
struct default_value_traits<wchar_t[N], id_long_nstring>:
c_wstring_long_value_traits<>
{
+ typedef wchar_t query_type[N];
};
template <std::size_t N>
struct default_value_traits<const wchar_t[N], id_long_nstring>:
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