From 0051807a6c28dd0ebb425d05511fbf9127483ee1 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 12 Oct 2012 17:24:44 +0200 Subject: Completion of prepared query support --- odb/oracle/connection.cxx | 1 + odb/oracle/connection.hxx | 18 ++++++++++++++++++ odb/oracle/connection.ixx | 30 ++++++++++++++++++++++++++++++ odb/oracle/database.hxx | 16 ++++++++++++++++ odb/oracle/database.ixx | 26 ++++++++++++++++++++++++++ odb/oracle/makefile | 1 + odb/oracle/prepared-query.cxx | 16 ++++++++++++++++ odb/oracle/prepared-query.hxx | 33 +++++++++++++++++++++++++++++++++ odb/oracle/query.cxx | 19 +++++-------------- odb/oracle/query.hxx | 5 +++++ odb/oracle/query.ixx | 6 ++++++ odb/oracle/statement.cxx | 36 ++++++++++++++++++++++-------------- odb/oracle/statement.hxx | 43 ++++++++++++++++++++++++++++--------------- 13 files changed, 207 insertions(+), 43 deletions(-) create mode 100644 odb/oracle/connection.ixx create mode 100644 odb/oracle/prepared-query.cxx create mode 100644 odb/oracle/prepared-query.hxx (limited to 'odb/oracle') diff --git a/odb/oracle/connection.cxx b/odb/oracle/connection.cxx index 1f87443..1194de9 100644 --- a/odb/oracle/connection.cxx +++ b/odb/oracle/connection.cxx @@ -139,6 +139,7 @@ namespace odb { // Deallocate prepared statements before we close the connection. // + prepared_map_.clear (); statement_cache_.reset (); } diff --git a/odb/oracle/connection.hxx b/odb/oracle/connection.hxx index f494a80..38be958 100644 --- a/odb/oracle/connection.hxx +++ b/odb/oracle/connection.hxx @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -60,6 +61,21 @@ namespace odb virtual unsigned long long execute (const char* statement, std::size_t length); + // Query preparation. + // + public: + template + prepared_query + prepare_query (const char* name, const char*); + + template + prepared_query + prepare_query (const char* name, const std::string&); + + template + prepared_query + prepare_query (const char* name, const query&); + // SQL statement tracing. // public: @@ -138,6 +154,8 @@ namespace odb } } +#include + #include #endif // ODB_ORACLE_CONNECTION_HXX diff --git a/odb/oracle/connection.ixx b/odb/oracle/connection.ixx new file mode 100644 index 0000000..9dcc818 --- /dev/null +++ b/odb/oracle/connection.ixx @@ -0,0 +1,30 @@ +// file : odb/oracle/connection.ixx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +namespace odb +{ + namespace oracle + { + 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)); + } + + template + inline prepared_query connection:: + prepare_query (const char* n, const query& q) + { + return query_::call (*this, n, q); + } + } +} diff --git a/odb/oracle/database.hxx b/odb/oracle/database.hxx index da98228..c03ce49 100644 --- a/odb/oracle/database.hxx +++ b/odb/oracle/database.hxx @@ -268,6 +268,22 @@ namespace odb result query (const oracle::query&); + // Query preparation. + // + template + prepared_query + prepare_query (const char* name, const char*); + + template + prepared_query + prepare_query (const char* name, const std::string&); + + template + prepared_query + prepare_query (const char* name, const oracle::query&); + + // Transactions. + // public: virtual transaction_impl* begin (); diff --git a/odb/oracle/database.ixx b/odb/oracle/database.ixx index 6d60d1e..38594a1 100644 --- a/odb/oracle/database.ixx +++ b/odb/oracle/database.ixx @@ -2,6 +2,8 @@ // copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC // license : ODB NCUEL; see accompanying LICENSE file +#include + namespace odb { namespace oracle @@ -393,5 +395,29 @@ namespace odb // return query_::call (*this, q); } + + template + inline prepared_query database:: + prepare_query (const char* n, const char* q) + { + return prepare_query (n, oracle::query (q)); + } + + template + inline prepared_query database:: + prepare_query (const char* n, const std::string& q) + { + return prepare_query (n, oracle::query (q)); + } + + template + inline prepared_query database:: + prepare_query (const char* n, const oracle::query& q) + { + // Throws if not in transaction. + // + oracle::connection& c (transaction::current ().connection ()); + return c.prepare_query (n, q); + } } } diff --git a/odb/oracle/makefile b/odb/oracle/makefile index f270e17..34fda85 100644 --- a/odb/oracle/makefile +++ b/odb/oracle/makefile @@ -13,6 +13,7 @@ database.cxx \ error.cxx \ exceptions.cxx \ oracle-types.cxx \ +prepared-query.cxx \ query.cxx \ query-const-expr.cxx \ simple-object-statements.cxx \ diff --git a/odb/oracle/prepared-query.cxx b/odb/oracle/prepared-query.cxx new file mode 100644 index 0000000..6237f4a --- /dev/null +++ b/odb/oracle/prepared-query.cxx @@ -0,0 +1,16 @@ +// file : odb/oracle/prepared-query.cxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include + +namespace odb +{ + namespace oracle + { + prepared_query_impl:: + ~prepared_query_impl () + { + } + } +} diff --git a/odb/oracle/prepared-query.hxx b/odb/oracle/prepared-query.hxx new file mode 100644 index 0000000..784509c --- /dev/null +++ b/odb/oracle/prepared-query.hxx @@ -0,0 +1,33 @@ +// file : odb/oracle/prepared-query.hxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#ifndef ODB_ORACLE_PREPARED_QUERY_HXX +#define ODB_ORACLE_PREPARED_QUERY_HXX + +#include + +#include + +#include +#include + +#include + +namespace odb +{ + namespace oracle + { + struct LIBODB_ORACLE_EXPORT prepared_query_impl: odb::prepared_query_impl + { + virtual + ~prepared_query_impl (); + + oracle::query_base query; + }; + } +} + +#include + +#endif // ODB_ORACLE_PREPARED_QUERY_HXX diff --git a/odb/oracle/query.cxx b/odb/oracle/query.cxx index df6a629..59d8027 100644 --- a/odb/oracle/query.cxx +++ b/odb/oracle/query.cxx @@ -137,19 +137,12 @@ namespace odb p->bind (b); } - binding& query_base:: - parameters_binding () const + void query_base:: + init_parameters () const { - size_t n (parameters_.size ()); - binding& r (binding_); - - if (n == 0) - return r; - bool inc_ver (false); - bind* b (&bind_[0]); - for (size_t i (0); i < n; ++i) + for (size_t i (0); i < parameters_.size (); ++i) { query_param& p (*parameters_[i]); @@ -157,16 +150,14 @@ namespace odb { if (p.init ()) { - p.bind (b + i); + p.bind (&bind_[i]); inc_ver = true; } } } if (inc_ver) - r.version++; - - return r; + binding_.version++; } static bool diff --git a/odb/oracle/query.hxx b/odb/oracle/query.hxx index aaba740..0e753f3 100644 --- a/odb/oracle/query.hxx +++ b/odb/oracle/query.hxx @@ -192,6 +192,11 @@ namespace odb const char* clause_prefix () const; + // Initialize the by-reference parameters from bound variables. + // + void + init_parameters () const; + binding& parameters_binding () const; diff --git a/odb/oracle/query.ixx b/odb/oracle/query.ixx index 75e7bfe..611128c 100644 --- a/odb/oracle/query.ixx +++ b/odb/oracle/query.ixx @@ -6,6 +6,12 @@ namespace odb { namespace oracle { + inline binding& query_base:: + parameters_binding () const + { + return binding_; + } + template inline void query_base:: append (val_bind v, const char* conv) diff --git a/odb/oracle/statement.cxx b/odb/oracle/statement.cxx index 7eb65c1..ded8d29 100644 --- a/odb/oracle/statement.cxx +++ b/odb/oracle/statement.cxx @@ -192,14 +192,14 @@ namespace odb } statement:: - statement (connection& conn, const string& text) + statement (connection_type& conn, const string& text) : conn_ (conn), udata_ (0), usize_ (0) { init (text.c_str (), text.size ()); } statement:: - statement (connection& conn, const char* text) + statement (connection_type& conn, const char* text) : conn_ (conn), udata_ (0), usize_ (0) { init (text, strlen (text)); @@ -1090,14 +1090,14 @@ namespace odb // generic_statement:: - generic_statement (connection& conn, const string& text) + generic_statement (connection_type& conn, const string& text) : statement (conn, text), bound_ (false) { init (); } generic_statement:: - generic_statement (connection& conn, const char* text) + generic_statement (connection_type& conn, const char* text) : statement (conn, text), bound_ (false) { init (); @@ -1268,7 +1268,7 @@ namespace odb } select_statement:: - select_statement (connection& conn, + select_statement (connection_type& conn, const string& text, binding& param, binding& result, @@ -1285,7 +1285,7 @@ namespace odb } select_statement:: - select_statement (connection& conn, + select_statement (connection_type& conn, const char* text, binding& param, binding& result, @@ -1302,7 +1302,7 @@ namespace odb } select_statement:: - select_statement (connection& conn, + select_statement (connection_type& conn, const string& text, binding& result, size_t lob_prefetch_size) @@ -1317,7 +1317,7 @@ namespace odb } select_statement:: - select_statement (connection& conn, + select_statement (connection_type& conn, const char* text, binding& result, size_t lob_prefetch_size) @@ -1493,7 +1493,7 @@ namespace odb } insert_statement:: - insert_statement (connection& conn, + insert_statement (connection_type& conn, const string& text, binding& param, bool returning) @@ -1503,7 +1503,7 @@ namespace odb } insert_statement:: - insert_statement (connection& conn, + insert_statement (connection_type& conn, const char* text, binding& param, bool returning) @@ -1634,14 +1634,18 @@ namespace odb } update_statement:: - update_statement (connection& conn, const string& text, binding& param) + update_statement (connection_type& conn, + const string& text, + binding& param) : statement (conn, text) { bind_param (param.bind, param.count); } update_statement:: - update_statement (connection& conn, const char* text, binding& param) + update_statement (connection_type& conn, + const char* text, + binding& param) : statement (conn, text) { bind_param (param.bind, param.count); @@ -1700,14 +1704,18 @@ namespace odb } delete_statement:: - delete_statement (connection& conn, const string& text, binding& param) + delete_statement (connection_type& conn, + const string& text, + binding& param) : statement (conn, text) { bind_param (param.bind, param.count); } delete_statement:: - delete_statement (connection& conn, const char* text, binding& param) + delete_statement (connection_type& conn, + const char* text, + binding& param) : statement (conn, text) { bind_param (param.bind, param.count); diff --git a/odb/oracle/statement.hxx b/odb/oracle/statement.hxx index bedddbc..ea08ac0 100644 --- a/odb/oracle/statement.hxx +++ b/odb/oracle/statement.hxx @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -28,6 +29,8 @@ namespace odb class LIBODB_ORACLE_EXPORT statement: public odb::statement { public: + typedef oracle::connection connection_type; + virtual ~statement () = 0; @@ -40,9 +43,15 @@ namespace odb virtual const char* text () const; + virtual connection_type& + connection () + { + return conn_; + } + protected: - statement (connection&, const std::string& text); - statement (connection&, const char* text); + statement (connection_type&, const std::string& text); + statement (connection_type&, const char* text); private: void @@ -98,7 +107,7 @@ namespace odb void* new_base = 0); protected: - connection& conn_; + connection_type& conn_; auto_handle stmt_; unbind* udata_; @@ -111,8 +120,8 @@ namespace odb virtual ~generic_statement (); - generic_statement (connection&, const std::string& text); - generic_statement (connection&, const char* text); + generic_statement (connection_type&, const std::string& text); + generic_statement (connection_type&, const char* text); unsigned long long execute (); @@ -136,24 +145,24 @@ namespace odb virtual ~select_statement (); - select_statement (connection& conn, + select_statement (connection_type& conn, const std::string& text, binding& param, binding& result, std::size_t lob_prefetch_size = 0); - select_statement (connection& conn, + select_statement (connection_type& conn, const char* text, binding& param, binding& result, std::size_t lob_prefetch_size = 0); - select_statement (connection& conn, + select_statement (connection_type& conn, const std::string& text, binding& result, std::size_t lob_prefetch_size = 0); - select_statement (connection& conn, + select_statement (connection_type& conn, const char* text, binding& result, std::size_t lob_prefetch_size = 0); @@ -212,12 +221,12 @@ namespace odb virtual ~insert_statement (); - insert_statement (connection& conn, + insert_statement (connection_type& conn, const std::string& text, binding& param, bool returning); - insert_statement (connection& conn, + insert_statement (connection_type& conn, const char* text, binding& param, bool returning); @@ -272,11 +281,13 @@ namespace odb virtual ~update_statement (); - update_statement (connection& conn, + update_statement (connection_type& conn, const std::string& text, binding& param); - update_statement (connection& conn, const char* text, binding& param); + update_statement (connection_type& conn, + const char* text, + binding& param); unsigned long long execute (); @@ -292,11 +303,13 @@ namespace odb virtual ~delete_statement (); - delete_statement (connection& conn, + delete_statement (connection_type& conn, const std::string& text, binding& param); - delete_statement (connection& conn, const char* text, binding& param); + delete_statement (connection_type& conn, + const char* text, + binding& param); unsigned long long execute (); -- cgit v1.1