From a049724c258a42af57d1ff572c3d15a3678e3875 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/mssql/connection.cxx | 1 + odb/mssql/connection.hxx | 18 ++++++++++++++++++ odb/mssql/connection.ixx | 30 ++++++++++++++++++++++++++++++ odb/mssql/database.hxx | 16 ++++++++++++++++ odb/mssql/database.ixx | 26 ++++++++++++++++++++++++++ odb/mssql/makefile | 1 + odb/mssql/prepared-query.cxx | 16 ++++++++++++++++ odb/mssql/prepared-query.hxx | 33 +++++++++++++++++++++++++++++++++ odb/mssql/query.cxx | 19 +++++-------------- odb/mssql/query.hxx | 5 +++++ odb/mssql/query.ixx | 6 ++++++ odb/mssql/statement.cxx | 24 ++++++++++++------------ odb/mssql/statement.hxx | 35 ++++++++++++++++++++++------------- 13 files changed, 191 insertions(+), 39 deletions(-) create mode 100644 odb/mssql/connection.ixx create mode 100644 odb/mssql/prepared-query.cxx create mode 100644 odb/mssql/prepared-query.hxx diff --git a/odb/mssql/connection.cxx b/odb/mssql/connection.cxx index 3e750d5..808d560 100644 --- a/odb/mssql/connection.cxx +++ b/odb/mssql/connection.cxx @@ -103,6 +103,7 @@ namespace odb { // Deallocate prepared statements before we close the connection. // + prepared_map_.clear (); statement_cache_.reset (); direct_stmt_.reset (); diff --git a/odb/mssql/connection.hxx b/odb/mssql/connection.hxx index 10cc4fc..2744f4f 100644 --- a/odb/mssql/connection.hxx +++ b/odb/mssql/connection.hxx @@ -17,6 +17,7 @@ #include #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: @@ -135,6 +151,8 @@ namespace odb } } +#include + #include #endif // ODB_MSSQL_CONNECTION_HXX diff --git a/odb/mssql/connection.ixx b/odb/mssql/connection.ixx new file mode 100644 index 0000000..c4c13a8 --- /dev/null +++ b/odb/mssql/connection.ixx @@ -0,0 +1,30 @@ +// file : odb/mssql/connection.ixx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +namespace odb +{ + namespace mssql + { + 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/mssql/database.hxx b/odb/mssql/database.hxx index 1bd419b..b64eccc 100644 --- a/odb/mssql/database.hxx +++ b/odb/mssql/database.hxx @@ -311,6 +311,22 @@ namespace odb result query (const mssql::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 mssql::query&); + + // Transactions. + // public: virtual transaction_impl* begin (); diff --git a/odb/mssql/database.ixx b/odb/mssql/database.ixx index d73332e..44ffe00 100644 --- a/odb/mssql/database.ixx +++ b/odb/mssql/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 mssql @@ -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, mssql::query (q)); + } + + template + inline prepared_query database:: + prepare_query (const char* n, const std::string& q) + { + return prepare_query (n, mssql::query (q)); + } + + template + inline prepared_query database:: + prepare_query (const char* n, const mssql::query& q) + { + // Throws if not in transaction. + // + mssql::connection& c (transaction::current ().connection ()); + return c.prepare_query (n, q); + } } } diff --git a/odb/mssql/makefile b/odb/mssql/makefile index 51a9a62..fb87738 100644 --- a/odb/mssql/makefile +++ b/odb/mssql/makefile @@ -11,6 +11,7 @@ connection-factory.cxx \ database.cxx \ error.cxx \ exceptions.cxx \ +prepared-query.cxx \ query.cxx \ query-const-expr.cxx \ simple-object-statements.cxx \ diff --git a/odb/mssql/prepared-query.cxx b/odb/mssql/prepared-query.cxx new file mode 100644 index 0000000..b54ea5b --- /dev/null +++ b/odb/mssql/prepared-query.cxx @@ -0,0 +1,16 @@ +// file : odb/mssql/prepared-query.cxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include + +namespace odb +{ + namespace mssql + { + prepared_query_impl:: + ~prepared_query_impl () + { + } + } +} diff --git a/odb/mssql/prepared-query.hxx b/odb/mssql/prepared-query.hxx new file mode 100644 index 0000000..35a4774 --- /dev/null +++ b/odb/mssql/prepared-query.hxx @@ -0,0 +1,33 @@ +// file : odb/mssql/prepared-query.hxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#ifndef ODB_MSSQL_PREPARED_QUERY_HXX +#define ODB_MSSQL_PREPARED_QUERY_HXX + +#include + +#include + +#include +#include + +#include + +namespace odb +{ + namespace mssql + { + struct LIBODB_MSSQL_EXPORT prepared_query_impl: odb::prepared_query_impl + { + virtual + ~prepared_query_impl (); + + mssql::query_base query; + }; + } +} + +#include + +#endif // ODB_MSSQL_PREPARED_QUERY_HXX diff --git a/odb/mssql/query.cxx b/odb/mssql/query.cxx index a0371c7..a40bb23 100644 --- a/odb/mssql/query.cxx +++ b/odb/mssql/query.cxx @@ -136,19 +136,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]); @@ -156,16 +149,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/mssql/query.hxx b/odb/mssql/query.hxx index 08e3319..8b470b4 100644 --- a/odb/mssql/query.hxx +++ b/odb/mssql/query.hxx @@ -193,6 +193,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/mssql/query.ixx b/odb/mssql/query.ixx index 18b856c..8683aa0 100644 --- a/odb/mssql/query.ixx +++ b/odb/mssql/query.ixx @@ -6,6 +6,12 @@ namespace odb { namespace mssql { + 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/mssql/statement.cxx b/odb/mssql/statement.cxx index 079d7f8..524fc06 100644 --- a/odb/mssql/statement.cxx +++ b/odb/mssql/statement.cxx @@ -95,14 +95,14 @@ namespace odb // statement:: - statement (connection& conn, const string& text) + statement (connection_type& conn, const string& text) : conn_ (conn), text_copy_ (text), text_ (text_copy_.c_str ()) { init (text_copy_.size ()); } statement:: - statement (connection& conn, const char* text, bool copy) + statement (connection_type& conn, const char* text, bool copy) : conn_ (conn) { size_t n; @@ -700,7 +700,7 @@ namespace odb } select_statement:: - select_statement (connection& conn, + select_statement (connection_type& conn, const string& t, binding& param, binding& result) @@ -711,7 +711,7 @@ namespace odb } select_statement:: - select_statement (connection& conn, + select_statement (connection_type& conn, const char* t, binding& param, binding& result, @@ -723,14 +723,14 @@ namespace odb } select_statement:: - select_statement (connection& conn, const string& t, binding& result) + select_statement (connection_type& conn, const string& t, binding& result) : statement (conn, t), result_ (result) { first_long_ = bind_result (result.bind, result.count); } select_statement:: - select_statement (connection& conn, + select_statement (connection_type& conn, const char* t, binding& result, bool ct) @@ -792,7 +792,7 @@ namespace odb } insert_statement:: - insert_statement (connection& conn, + insert_statement (connection_type& conn, const string& t, binding& param, bool returning) @@ -805,7 +805,7 @@ namespace odb } insert_statement:: - insert_statement (connection& conn, + insert_statement (connection_type& conn, const char* t, binding& param, bool returning, @@ -953,14 +953,14 @@ namespace odb } update_statement:: - update_statement (connection& conn, const string& t, binding& param) + update_statement (connection_type& conn, const string& t, binding& param) : statement (conn, t) { bind_param (param.bind, param.count); } update_statement:: - update_statement (connection& conn, + update_statement (connection_type& conn, const char* t, binding& param, bool ct) @@ -1003,14 +1003,14 @@ namespace odb } delete_statement:: - delete_statement (connection& conn, const string& t, binding& param) + delete_statement (connection_type& conn, const string& t, binding& param) : statement (conn, t) { bind_param (param.bind, param.count); } delete_statement:: - delete_statement (connection& conn, + delete_statement (connection_type& conn, const char* t, binding& param, bool ct) diff --git a/odb/mssql/statement.hxx b/odb/mssql/statement.hxx index 86178e6..53e7c97 100644 --- a/odb/mssql/statement.hxx +++ b/odb/mssql/statement.hxx @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -29,6 +30,8 @@ namespace odb class LIBODB_MSSQL_EXPORT statement: public odb::statement { public: + typedef mssql::connection connection_type; + virtual ~statement () = 0; @@ -41,9 +44,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, bool copy_text); + statement (connection_type&, const std::string& text); + statement (connection_type&, const char* text, bool copy_text); private: void @@ -73,7 +82,7 @@ namespace odb void* new_base = 0); protected: - connection& conn_; + connection_type& conn_; std::string text_copy_; const char* text_; auto_handle stmt_; @@ -89,22 +98,22 @@ namespace odb // result binding, they should appear last in the statement // text. // - select_statement (connection& conn, + select_statement (connection_type& conn, const std::string& text, binding& param, binding& result); - select_statement (connection& conn, + select_statement (connection_type& conn, const char* text, binding& param, binding& result, bool copy_text = true); - select_statement (connection& conn, + select_statement (connection_type& conn, const std::string& text, binding& result); - select_statement (connection& conn, + select_statement (connection_type& conn, const char* text, binding& result, bool copy_text = true); @@ -188,12 +197,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, @@ -232,11 +241,11 @@ namespace odb virtual ~update_statement (); - update_statement (connection& conn, + update_statement (connection_type& conn, const std::string& text, binding& param); - update_statement (connection& conn, + update_statement (connection_type& conn, const char* text, binding& param, bool copy_text = true); @@ -255,11 +264,11 @@ namespace odb virtual ~delete_statement (); - delete_statement (connection& conn, + delete_statement (connection_type& conn, const std::string& text, binding& param); - delete_statement (connection& conn, + delete_statement (connection_type& conn, const char* text, binding& param, bool copy_text = true); -- cgit v1.1