aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2012-10-04 11:33:49 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2012-10-19 11:38:24 +0200
commitf101a400442692f349822ab1d9119bca5d2b7240 (patch)
treee046f3b727226810d08813cc4c7a55244937f275
parentbe97326d67365e16175cc599e23348feaf80e0fe (diff)
Initial support for prepared queries
-rw-r--r--odb/connection.hxx18
-rw-r--r--odb/connection.ixx14
-rw-r--r--odb/connection.txx15
-rw-r--r--odb/makefile2
-rw-r--r--odb/no-id-object-result.hxx7
-rw-r--r--odb/no-id-object-result.txx6
-rw-r--r--odb/polymorphic-object-result.hxx7
-rw-r--r--odb/polymorphic-object-result.txx6
-rw-r--r--odb/prepared-query.cxx13
-rw-r--r--odb/prepared-query.hxx65
-rw-r--r--odb/result.cxx13
-rw-r--r--odb/result.hxx8
-rw-r--r--odb/simple-object-result.hxx7
-rw-r--r--odb/simple-object-result.txx6
-rw-r--r--odb/statement.hxx15
-rw-r--r--odb/view-result.hxx7
-rw-r--r--odb/view-result.txx6
17 files changed, 162 insertions, 53 deletions
diff --git a/odb/connection.hxx b/odb/connection.hxx
index 9f42f33..f274b39 100644
--- a/odb/connection.hxx
+++ b/odb/connection.hxx
@@ -11,6 +11,8 @@
#include <cstddef> // std::size_t
#include <odb/forward.hxx>
+#include <odb/query.hxx>
+#include <odb/prepared-query.hxx>
#include <odb/details/export.hxx>
#include <odb/details/shared-ptr.hxx>
@@ -50,6 +52,21 @@ namespace odb
virtual unsigned long long
execute (const char* statement, std::size_t length) = 0;
+ // Query preparation.
+ //
+ public:
+ template <typename T>
+ prepared_query<T>
+ prepare_query (const char* name, const char* q);
+
+ template <typename T>
+ prepared_query<T>
+ prepare_query (const char* name, const std::string& q);
+
+ template <typename T>
+ prepared_query<T>
+ prepare_query (const char* name, const query<T>& q);
+
// SQL statement tracing.
//
public:
@@ -96,6 +113,7 @@ namespace odb
}
#include <odb/connection.ixx>
+#include <odb/connection.txx>
#include <odb/post.hxx>
diff --git a/odb/connection.ixx b/odb/connection.ixx
index fa2d510..fd1a624 100644
--- a/odb/connection.ixx
+++ b/odb/connection.ixx
@@ -30,6 +30,20 @@ namespace odb
return execute (st.c_str (), st.size ());
}
+ template <typename T>
+ inline prepared_query<T> connection::
+ prepare_query (const char* n, const char* q)
+ {
+ return prepare_query<T> (n, query<T> (q));
+ }
+
+ template <typename T>
+ inline prepared_query<T> connection::
+ prepare_query (const char* n, const std::string& q)
+ {
+ return prepare_query<T> (n, query<T> (q));
+ }
+
inline void connection::
tracer (tracer_type& t)
{
diff --git a/odb/connection.txx b/odb/connection.txx
new file mode 100644
index 0000000..f44cdad
--- /dev/null
+++ b/odb/connection.txx
@@ -0,0 +1,15 @@
+// file : odb/connection.txx
+// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+namespace odb
+{
+ template <typename T>
+ prepared_query<T> connection::
+ prepare_query (const char* n, const query<T>& q)
+ {
+ //@@ Views. Inline?
+ //
+ return prepared_query<T> (object_traits<T>::prepare_query (*this, n, q));
+ }
+}
diff --git a/odb/makefile b/odb/makefile
index 8b62d46..36100f6 100644
--- a/odb/makefile
+++ b/odb/makefile
@@ -10,6 +10,8 @@ exceptions.cxx \
database.cxx \
connection.cxx \
lazy-ptr-impl.cxx \
+prepared-query.cxx \
+result.cxx \
schema-catalog.cxx \
session.cxx \
statement.cxx \
diff --git a/odb/no-id-object-result.hxx b/odb/no-id-object-result.hxx
index 8893e7a..21be2dc 100644
--- a/odb/no-id-object-result.hxx
+++ b/odb/no-id-object-result.hxx
@@ -17,19 +17,14 @@
#include <odb/pointer-traits.hxx>
#include <odb/details/config.hxx> // ODB_CXX11
-#include <odb/details/shared-ptr.hxx>
namespace odb
{
// Implementation for objects without object id (always non-polymorphic).
//
template <typename T>
- class no_id_object_result_impl: public details::shared_base
+ class no_id_object_result_impl: public result_impl
{
- public:
- virtual
- ~no_id_object_result_impl ();
-
protected:
typedef odb::database database_type;
diff --git a/odb/no-id-object-result.txx b/odb/no-id-object-result.txx
index 99bd80e..b37b36c 100644
--- a/odb/no-id-object-result.txx
+++ b/odb/no-id-object-result.txx
@@ -9,12 +9,6 @@ namespace odb
//
template <typename T>
- no_id_object_result_impl<T>::
- ~no_id_object_result_impl ()
- {
- }
-
- template <typename T>
void no_id_object_result_impl<T>::
load ()
{
diff --git a/odb/polymorphic-object-result.hxx b/odb/polymorphic-object-result.hxx
index 805fe96..5804db5 100644
--- a/odb/polymorphic-object-result.hxx
+++ b/odb/polymorphic-object-result.hxx
@@ -17,19 +17,14 @@
#include <odb/pointer-traits.hxx>
#include <odb/details/config.hxx> // ODB_CXX11
-#include <odb/details/shared-ptr.hxx>
namespace odb
{
// Implementation for polymorphic objects with object id.
//
template <typename T>
- class polymorphic_object_result_impl: public details::shared_base
+ class polymorphic_object_result_impl: public result_impl
{
- public:
- virtual
- ~polymorphic_object_result_impl ();
-
protected:
typedef odb::database database_type;
diff --git a/odb/polymorphic-object-result.txx b/odb/polymorphic-object-result.txx
index e673483..033b1ad 100644
--- a/odb/polymorphic-object-result.txx
+++ b/odb/polymorphic-object-result.txx
@@ -11,12 +11,6 @@ namespace odb
//
template <typename T>
- polymorphic_object_result_impl<T>::
- ~polymorphic_object_result_impl ()
- {
- }
-
- template <typename T>
void polymorphic_object_result_impl<T>::
load ()
{
diff --git a/odb/prepared-query.cxx b/odb/prepared-query.cxx
new file mode 100644
index 0000000..d192541
--- /dev/null
+++ b/odb/prepared-query.cxx
@@ -0,0 +1,13 @@
+// file : odb/prepared-query.cxx
+// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <odb/prepared-query.hxx>
+
+namespace odb
+{
+ prepared_query_impl::
+ ~prepared_query_impl ()
+ {
+ }
+}
diff --git a/odb/prepared-query.hxx b/odb/prepared-query.hxx
new file mode 100644
index 0000000..60ae48d
--- /dev/null
+++ b/odb/prepared-query.hxx
@@ -0,0 +1,65 @@
+// file : odb/prepared-query.hxx
+// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef ODB_PREPARED_QUERY_HXX
+#define ODB_PREPARED_QUERY_HXX
+
+#include <odb/pre.hxx>
+
+#include <odb/forward.hxx>
+#include <odb/traits.hxx>
+#include <odb/result.hxx>
+
+#include <odb/details/export.hxx>
+#include <odb/details/shared-ptr.hxx>
+
+namespace odb
+{
+ struct LIBODB_EXPORT prepared_query_impl: details::shared_base
+ {
+ virtual
+ ~prepared_query_impl ();
+
+ const char* name;
+ details::shared_ptr<result_impl> (*execute) (prepared_query_impl&);
+ };
+
+ template <typename T>
+ struct prepared_query
+ {
+ result<T>
+ execute (bool cache = true)
+ {
+ typedef
+ typename result_base<T, class_traits<T>::kind>::result_impl_type
+ derived_type;
+
+ details::shared_ptr<result_impl> ri (impl_->execute (*impl_));
+ result<T> r (
+ details::shared_ptr<derived_type> (
+ static_cast<derived_type*> (ri.release ())));
+
+ if (cache)
+ r.cache ();
+
+ return r;
+ }
+
+ explicit
+ prepared_query (details::shared_ptr<prepared_query_impl> impl)
+ : impl_ (impl) {}
+
+ private:
+ details::shared_ptr<prepared_query_impl> impl_;
+ };
+
+ namespace core
+ {
+ using odb::prepared_query;
+ }
+}
+
+#include <odb/post.hxx>
+
+#endif // ODB_PREPARED_QUERY_HXX
diff --git a/odb/result.cxx b/odb/result.cxx
new file mode 100644
index 0000000..e5b72d0
--- /dev/null
+++ b/odb/result.cxx
@@ -0,0 +1,13 @@
+// file : odb/result.cxx
+// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <odb/result.hxx>
+
+namespace odb
+{
+ result_impl::
+ ~result_impl ()
+ {
+ }
+}
diff --git a/odb/result.hxx b/odb/result.hxx
index 29c03df..b85242f 100644
--- a/odb/result.hxx
+++ b/odb/result.hxx
@@ -12,8 +12,16 @@
#include <odb/forward.hxx>
#include <odb/traits.hxx>
+#include <odb/details/shared-ptr.hxx>
+
namespace odb
{
+ struct result_impl: details::shared_base
+ {
+ virtual
+ ~result_impl ();
+ };
+
template <typename T, class_kind kind>
class result_base;
diff --git a/odb/simple-object-result.hxx b/odb/simple-object-result.hxx
index 58d8958..6f0868a 100644
--- a/odb/simple-object-result.hxx
+++ b/odb/simple-object-result.hxx
@@ -17,19 +17,14 @@
#include <odb/pointer-traits.hxx>
#include <odb/details/config.hxx> // ODB_CXX11
-#include <odb/details/shared-ptr.hxx>
namespace odb
{
// Implementation for non-polymorphic objects with object id.
//
template <typename T>
- class object_result_impl: public details::shared_base
+ class object_result_impl: public result_impl
{
- public:
- virtual
- ~object_result_impl ();
-
protected:
typedef odb::database database_type;
diff --git a/odb/simple-object-result.txx b/odb/simple-object-result.txx
index b440f13..5ee9686 100644
--- a/odb/simple-object-result.txx
+++ b/odb/simple-object-result.txx
@@ -9,12 +9,6 @@ namespace odb
//
template <typename T>
- object_result_impl<T>::
- ~object_result_impl ()
- {
- }
-
- template <typename T>
void object_result_impl<T>::
load ()
{
diff --git a/odb/statement.hxx b/odb/statement.hxx
index 7015e44..b7295ad 100644
--- a/odb/statement.hxx
+++ b/odb/statement.hxx
@@ -7,7 +7,7 @@
#include <odb/pre.hxx>
-#include <odb/forward.hxx>
+#include <odb/forward.hxx> // connection
#include <odb/details/export.hxx>
#include <odb/details/shared-ptr.hxx>
@@ -17,18 +17,23 @@ namespace odb
class LIBODB_EXPORT statement: public details::shared_base
{
public:
+ typedef odb::connection connection_type;
+
+ virtual const char*
+ text () const = 0;
+
+ virtual connection_type&
+ connection () = 0;
+
virtual
~statement () = 0;
+ protected:
statement () {}
private:
statement (const statement&);
statement& operator= (const statement&);
-
- public:
- virtual const char*
- text () const = 0;
};
}
diff --git a/odb/view-result.hxx b/odb/view-result.hxx
index 1fd30e0..d111e36 100644
--- a/odb/view-result.hxx
+++ b/odb/view-result.hxx
@@ -17,17 +17,12 @@
#include <odb/pointer-traits.hxx>
#include <odb/details/config.hxx> // ODB_CXX11
-#include <odb/details/shared-ptr.hxx>
namespace odb
{
template <typename T>
- class view_result_impl: public details::shared_base
+ class view_result_impl: public result_impl
{
- public:
- virtual
- ~view_result_impl ();
-
protected:
friend class result<T>;
friend class result<const T>;
diff --git a/odb/view-result.txx b/odb/view-result.txx
index 9584d80..fb05764 100644
--- a/odb/view-result.txx
+++ b/odb/view-result.txx
@@ -9,12 +9,6 @@ namespace odb
//
template <typename T>
- view_result_impl<T>::
- ~view_result_impl ()
- {
- }
-
- template <typename T>
typename view_result_impl<T>::pointer_type&
view_result_impl<T>::
current ()