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
commit9ccd928f7b3d36b7be4a8dc8d180a441c9bf55e1 (patch)
treee6f398593fdc511d260ca9464344ff4bf3e2f127
parent945e65b2f5db606740e46053a4539ccc08403996 (diff)
Add dynamic multi-database query support
-rw-r--r--odb/mysql/makefile1
-rw-r--r--odb/mysql/query-dynamic.cxx136
-rw-r--r--odb/mysql/query-dynamic.hxx33
-rw-r--r--odb/mysql/query-dynamic.ixx27
-rw-r--r--odb/mysql/query-dynamic.txx21
-rw-r--r--odb/mysql/query.cxx2
-rw-r--r--odb/mysql/query.hxx95
-rw-r--r--odb/mysql/query.ixx4
-rw-r--r--odb/mysql/traits.hxx17
9 files changed, 303 insertions, 33 deletions
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 <cstddef> // std::size_t
+
+#include <odb/mysql/query-dynamic.hxx>
+
+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<const query_column_base*> (
+ 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<const query_column_base*> (
+ x.native_info[id_mysql].column));
+
+ query_param_factory f (
+ reinterpret_cast<query_param_factory> (
+ x.native_info[id_mysql].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)
+ {
+ 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 <odb/pre.hxx>
+
+#include <odb/query.hxx>
+#include <odb/query-dynamic.hxx>
+
+#include <odb/mysql/query.hxx>
+
+namespace odb
+{
+ namespace mysql
+ {
+ 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/mysql/query-dynamic.ixx>
+#include <odb/mysql/query-dynamic.txx>
+
+#include <odb/post.hxx>
+
+#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 <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_mysql]);
+ 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/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 <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/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<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/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 <vector>
#include <cstddef> // std::size_t
+#include <odb/forward.hxx> // odb::query_column
#include <odb/query.hxx>
#include <odb/mysql/mysql.hxx>
@@ -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 <database_type_id ID>
query_base (const query_column<bool, ID>&);
+ // 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 <typename T, database_type_id ID>
void
@@ -253,14 +263,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;
@@ -383,28 +404,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_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 <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:
@@ -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<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/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<const char*, id_string>:
c_string_value_traits
{
+ typedef const char* query_type;
};
template <>
struct LIBODB_MYSQL_EXPORT default_value_traits<const char*, id_decimal>:
c_string_value_traits
{
+ typedef const char* query_type;
};
template <>
struct LIBODB_MYSQL_EXPORT default_value_traits<const char*, id_enum>:
c_string_value_traits
{
+ typedef const char* query_type;
};
template <>
struct LIBODB_MYSQL_EXPORT default_value_traits<const char*, id_set>:
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];
};
template <std::size_t N>
struct default_value_traits<char[N], id_decimal>: c_string_value_traits
{
+ typedef char query_type[N];
};
template <std::size_t N>
struct default_value_traits<const char[N], id_decimal>:
c_string_value_traits
{
+ typedef const char query_type[N];
};
template <std::size_t N>
struct default_value_traits<char[N], id_enum>: c_string_value_traits
{
+ typedef char query_type[N];
};
template <std::size_t N>
struct default_value_traits<const char[N], id_enum>: c_string_value_traits
{
+ typedef const char query_type[N];
};
template <std::size_t N>
struct default_value_traits<char[N], id_set>: c_string_value_traits
{
+ typedef char query_type[N];
};
template <std::size_t N>
struct default_value_traits<const char[N], id_set>: c_string_value_traits
{
+ typedef const char query_type[N];
};
// std::vector<char> (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