From f101a400442692f349822ab1d9119bca5d2b7240 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 4 Oct 2012 11:33:49 +0200 Subject: Initial support for prepared queries --- odb/connection.hxx | 18 +++++++++++ odb/connection.ixx | 14 +++++++++ odb/connection.txx | 15 +++++++++ odb/makefile | 2 ++ odb/no-id-object-result.hxx | 7 +---- odb/no-id-object-result.txx | 6 ---- odb/polymorphic-object-result.hxx | 7 +---- odb/polymorphic-object-result.txx | 6 ---- odb/prepared-query.cxx | 13 ++++++++ odb/prepared-query.hxx | 65 +++++++++++++++++++++++++++++++++++++++ odb/result.cxx | 13 ++++++++ odb/result.hxx | 8 +++++ odb/simple-object-result.hxx | 7 +---- odb/simple-object-result.txx | 6 ---- odb/statement.hxx | 15 ++++++--- odb/view-result.hxx | 7 +---- odb/view-result.txx | 6 ---- 17 files changed, 162 insertions(+), 53 deletions(-) create mode 100644 odb/connection.txx create mode 100644 odb/prepared-query.cxx create mode 100644 odb/prepared-query.hxx create mode 100644 odb/result.cxx 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 // std::size_t #include +#include +#include #include #include @@ -50,6 +52,21 @@ namespace odb virtual unsigned long long execute (const char* statement, std::size_t length) = 0; + // Query preparation. + // + public: + template + prepared_query + prepare_query (const char* name, const char* q); + + template + prepared_query + prepare_query (const char* name, const std::string& q); + + template + prepared_query + prepare_query (const char* name, const query& q); + // SQL statement tracing. // public: @@ -96,6 +113,7 @@ namespace odb } #include +#include #include 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 + inline prepared_query connection:: + prepare_query (const char* n, const char* q) + { + return prepare_query (n, query (q)); + } + + template + inline prepared_query connection:: + prepare_query (const char* n, const std::string& q) + { + return prepare_query (n, query (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 + prepared_query connection:: + prepare_query (const char* n, const query& q) + { + //@@ Views. Inline? + // + return prepared_query (object_traits::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 #include // ODB_CXX11 -#include namespace odb { // Implementation for objects without object id (always non-polymorphic). // template - 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 - no_id_object_result_impl:: - ~no_id_object_result_impl () - { - } - - template void no_id_object_result_impl:: 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 #include // ODB_CXX11 -#include namespace odb { // Implementation for polymorphic objects with object id. // template - 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 - polymorphic_object_result_impl:: - ~polymorphic_object_result_impl () - { - } - - template void polymorphic_object_result_impl:: 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 + +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 + +#include +#include +#include + +#include +#include + +namespace odb +{ + struct LIBODB_EXPORT prepared_query_impl: details::shared_base + { + virtual + ~prepared_query_impl (); + + const char* name; + details::shared_ptr (*execute) (prepared_query_impl&); + }; + + template + struct prepared_query + { + result + execute (bool cache = true) + { + typedef + typename result_base::kind>::result_impl_type + derived_type; + + details::shared_ptr ri (impl_->execute (*impl_)); + result r ( + details::shared_ptr ( + static_cast (ri.release ()))); + + if (cache) + r.cache (); + + return r; + } + + explicit + prepared_query (details::shared_ptr impl) + : impl_ (impl) {} + + private: + details::shared_ptr impl_; + }; + + namespace core + { + using odb::prepared_query; + } +} + +#include + +#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 + +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 #include +#include + namespace odb { + struct result_impl: details::shared_base + { + virtual + ~result_impl (); + }; + template 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 #include // ODB_CXX11 -#include namespace odb { // Implementation for non-polymorphic objects with object id. // template - 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 - object_result_impl:: - ~object_result_impl () - { - } - - template void object_result_impl:: 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 -#include +#include // connection #include #include @@ -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 #include // ODB_CXX11 -#include namespace odb { template - class view_result_impl: public details::shared_base + class view_result_impl: public result_impl { - public: - virtual - ~view_result_impl (); - protected: friend class result; friend class result; 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 - view_result_impl:: - ~view_result_impl () - { - } - - template typename view_result_impl::pointer_type& view_result_impl:: current () -- cgit v1.1