From 9126281b53722115b2e8624632f2dd616f0c26a0 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/mysql/connection.cxx | 4 ++++ odb/mysql/connection.hxx | 18 ++++++++++++++++++ odb/mysql/connection.ixx | 30 ++++++++++++++++++++++++++++++ odb/mysql/database.hxx | 16 ++++++++++++++++ odb/mysql/database.ixx | 26 ++++++++++++++++++++++++++ odb/mysql/makefile | 1 + odb/mysql/prepared-query.cxx | 16 ++++++++++++++++ odb/mysql/prepared-query.hxx | 33 +++++++++++++++++++++++++++++++++ odb/mysql/query.cxx | 19 +++++-------------- odb/mysql/query.hxx | 5 +++++ odb/mysql/query.ixx | 6 ++++++ odb/mysql/statement.cxx | 24 ++++++++++++------------ odb/mysql/statement.hxx | 35 ++++++++++++++++++++++------------- 13 files changed, 194 insertions(+), 39 deletions(-) create mode 100644 odb/mysql/connection.ixx create mode 100644 odb/mysql/prepared-query.cxx create mode 100644 odb/mysql/prepared-query.hxx diff --git a/odb/mysql/connection.cxx b/odb/mysql/connection.cxx index 0e475d2..ef854c6 100644 --- a/odb/mysql/connection.cxx +++ b/odb/mysql/connection.cxx @@ -83,6 +83,10 @@ namespace odb { active_ = 0; + // Destroy prepared query statements before freeing the connections. + // + prepared_map_.clear (); + if (stmt_handles_.size () > 0) free_stmt_handles (); } diff --git a/odb/mysql/connection.hxx b/odb/mysql/connection.hxx index 0d4cf75..88fd04d 100644 --- a/odb/mysql/connection.hxx +++ b/odb/mysql/connection.hxx @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -61,6 +62,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: @@ -183,6 +199,8 @@ namespace odb } } +#include + #include #endif // ODB_MYSQL_CONNECTION_HXX diff --git a/odb/mysql/connection.ixx b/odb/mysql/connection.ixx new file mode 100644 index 0000000..3a3c975 --- /dev/null +++ b/odb/mysql/connection.ixx @@ -0,0 +1,30 @@ +// file : odb/mysql/connection.ixx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace mysql + { + 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/mysql/database.hxx b/odb/mysql/database.hxx index 897134d..cc26a6e 100644 --- a/odb/mysql/database.hxx +++ b/odb/mysql/database.hxx @@ -353,6 +353,22 @@ namespace odb result query (const mysql::query&, bool cache = true); + // 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 mysql::query&); + + // Transactions. + // public: virtual transaction_impl* begin (); diff --git a/odb/mysql/database.ixx b/odb/mysql/database.ixx index b69a301..5f3854a 100644 --- a/odb/mysql/database.ixx +++ b/odb/mysql/database.ixx @@ -2,6 +2,8 @@ // copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file +#include + namespace odb { namespace mysql @@ -398,5 +400,29 @@ namespace odb return r; } + + template + inline prepared_query database:: + prepare_query (const char* n, const char* q) + { + return prepare_query (n, mysql::query (q)); + } + + template + inline prepared_query database:: + prepare_query (const char* n, const std::string& q) + { + return prepare_query (n, mysql::query (q)); + } + + template + inline prepared_query database:: + prepare_query (const char* n, const mysql::query& q) + { + // Throws if not in transaction. + // + mysql::connection& c (transaction::current ().connection ()); + return c.prepare_query (n, q); + } } } diff --git a/odb/mysql/makefile b/odb/mysql/makefile index e94c7f1..936d17c 100644 --- a/odb/mysql/makefile +++ b/odb/mysql/makefile @@ -11,6 +11,7 @@ database.cxx \ enum.cxx \ error.cxx \ exceptions.cxx \ +prepared-query.cxx \ query.cxx \ query-const-expr.cxx \ simple-object-statements.cxx \ diff --git a/odb/mysql/prepared-query.cxx b/odb/mysql/prepared-query.cxx new file mode 100644 index 0000000..05139d1 --- /dev/null +++ b/odb/mysql/prepared-query.cxx @@ -0,0 +1,16 @@ +// file : odb/mysql/prepared-query.cxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +namespace odb +{ + namespace mysql + { + prepared_query_impl:: + ~prepared_query_impl () + { + } + } +} diff --git a/odb/mysql/prepared-query.hxx b/odb/mysql/prepared-query.hxx new file mode 100644 index 0000000..da83aee --- /dev/null +++ b/odb/mysql/prepared-query.hxx @@ -0,0 +1,33 @@ +// file : odb/mysql/prepared-query.hxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_MYSQL_PREPARED_QUERY_HXX +#define ODB_MYSQL_PREPARED_QUERY_HXX + +#include + +#include + +#include +#include + +#include + +namespace odb +{ + namespace mysql + { + struct LIBODB_MYSQL_EXPORT prepared_query_impl: odb::prepared_query_impl + { + virtual + ~prepared_query_impl (); + + mysql::query_base query; + }; + } +} + +#include + +#endif // ODB_MYSQL_PREPARED_QUERY_HXX diff --git a/odb/mysql/query.cxx b/odb/mysql/query.cxx index bc0c4f0..6ca825c 100644 --- a/odb/mysql/query.cxx +++ b/odb/mysql/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); - MYSQL_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/mysql/query.hxx b/odb/mysql/query.hxx index a38b5cb..f08120b 100644 --- a/odb/mysql/query.hxx +++ b/odb/mysql/query.hxx @@ -171,6 +171,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/mysql/query.ixx b/odb/mysql/query.ixx index ce485f1..60affe4 100644 --- a/odb/mysql/query.ixx +++ b/odb/mysql/query.ixx @@ -6,6 +6,12 @@ namespace odb { namespace mysql { + 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/mysql/statement.cxx b/odb/mysql/statement.cxx index 647ec96..b1c156d 100644 --- a/odb/mysql/statement.cxx +++ b/odb/mysql/statement.cxx @@ -23,14 +23,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; @@ -108,7 +108,7 @@ namespace odb } select_statement:: - select_statement (connection& conn, + select_statement (connection_type& conn, const string& t, binding& param, binding& result) @@ -125,7 +125,7 @@ namespace odb } select_statement:: - select_statement (connection& conn, + select_statement (connection_type& conn, const char* t, binding& param, binding& result, @@ -143,7 +143,7 @@ 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), end_ (false), cached_ (false), @@ -156,7 +156,7 @@ namespace odb } select_statement:: - select_statement (connection& conn, + select_statement (connection_type& conn, const char* t, binding& result, bool ct) @@ -338,13 +338,13 @@ namespace odb } insert_statement:: - insert_statement (connection& conn, const string& t, binding& param) + insert_statement (connection_type& conn, const string& t, binding& param) : statement (conn, t), param_ (param), param_version_ (0) { } insert_statement:: - insert_statement (connection& conn, + insert_statement (connection_type& conn, const char* t, binding& param, bool ct) @@ -402,13 +402,13 @@ 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), param_ (param), param_version_ (0) { } update_statement:: - update_statement (connection& conn, + update_statement (connection_type& conn, const char* t, binding& param, bool ct) @@ -460,13 +460,13 @@ 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), param_ (param), param_version_ (0) { } delete_statement:: - delete_statement (connection& conn, + delete_statement (connection_type& conn, const char* t, binding& param, bool ct) diff --git a/odb/mysql/statement.hxx b/odb/mysql/statement.hxx index 700a7f5..66717aa 100644 --- a/odb/mysql/statement.hxx +++ b/odb/mysql/statement.hxx @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -29,6 +30,8 @@ namespace odb class LIBODB_MYSQL_EXPORT statement: public odb::statement { public: + typedef mysql::connection connection_type; + virtual ~statement () = 0; @@ -41,6 +44,12 @@ namespace odb virtual const char* text () const; + virtual connection_type& + connection () + { + return conn_; + } + // Cancel the statement execution (e.g., result fetching) so // that another statement can be executed on the connection. // @@ -48,15 +57,15 @@ namespace odb cancel (); 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 init (std::size_t text_size); protected: - connection& conn_; + connection_type& conn_; std::string text_copy_; const char* text_; auto_handle stmt_; @@ -68,22 +77,22 @@ namespace odb virtual ~select_statement (); - 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); @@ -176,11 +185,11 @@ namespace odb virtual ~insert_statement (); - insert_statement (connection& conn, + insert_statement (connection_type& conn, const std::string& text, binding& param); - insert_statement (connection& conn, + insert_statement (connection_type& conn, const char* text, binding& param, bool copy_text = true); @@ -209,11 +218,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); @@ -236,11 +245,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