diff options
-rw-r--r-- | odb/mysql/connection.cxx | 4 | ||||
-rw-r--r-- | odb/mysql/connection.hxx | 18 | ||||
-rw-r--r-- | odb/mysql/connection.ixx | 30 | ||||
-rw-r--r-- | odb/mysql/database.hxx | 16 | ||||
-rw-r--r-- | odb/mysql/database.ixx | 26 | ||||
-rw-r--r-- | odb/mysql/makefile | 1 | ||||
-rw-r--r-- | odb/mysql/prepared-query.cxx | 16 | ||||
-rw-r--r-- | odb/mysql/prepared-query.hxx | 33 | ||||
-rw-r--r-- | odb/mysql/query.cxx | 19 | ||||
-rw-r--r-- | odb/mysql/query.hxx | 5 | ||||
-rw-r--r-- | odb/mysql/query.ixx | 6 | ||||
-rw-r--r-- | odb/mysql/statement.cxx | 24 | ||||
-rw-r--r-- | odb/mysql/statement.hxx | 35 |
13 files changed, 194 insertions, 39 deletions
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 <odb/mysql/mysql.hxx> #include <odb/mysql/version.hxx> #include <odb/mysql/forward.hxx> +#include <odb/mysql/query.hxx> #include <odb/mysql/tracer.hxx> #include <odb/mysql/transaction-impl.hxx> #include <odb/mysql/auto-handle.hxx> @@ -61,6 +62,21 @@ namespace odb virtual unsigned long long execute (const char* statement, std::size_t length); + // Query preparation. + // + public: + template <typename T> + prepared_query<T> + prepare_query (const char* name, const char*); + + template <typename T> + prepared_query<T> + prepare_query (const char* name, const std::string&); + + template <typename T> + prepared_query<T> + prepare_query (const char* name, const query<T>&); + // SQL statement tracing. // public: @@ -183,6 +199,8 @@ namespace odb } } +#include <odb/mysql/connection.ixx> + #include <odb/post.hxx> #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 <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)); + } + + template <typename T> + inline prepared_query<T> connection:: + prepare_query (const char* n, const query<T>& q) + { + return query_<T, id_mysql>::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<T> query (const mysql::query<T>&, bool cache = true); + // Query preparation. + // + template <typename T> + prepared_query<T> + prepare_query (const char* name, const char*); + + template <typename T> + prepared_query<T> + prepare_query (const char* name, const std::string&); + + template <typename T> + prepared_query<T> + prepare_query (const char* name, const mysql::query<T>&); + + // 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 <odb/mysql/transaction.hxx> + namespace odb { namespace mysql @@ -398,5 +400,29 @@ namespace odb return r; } + + template <typename T> + inline prepared_query<T> database:: + prepare_query (const char* n, const char* q) + { + return prepare_query<T> (n, mysql::query<T> (q)); + } + + template <typename T> + inline prepared_query<T> database:: + prepare_query (const char* n, const std::string& q) + { + return prepare_query<T> (n, mysql::query<T> (q)); + } + + template <typename T> + inline prepared_query<T> database:: + prepare_query (const char* n, const mysql::query<T>& 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 <odb/mysql/prepared-query.hxx> + +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 <odb/pre.hxx> + +#include <odb/prepared-query.hxx> + +#include <odb/mysql/version.hxx> +#include <odb/mysql/query.hxx> + +#include <odb/mysql/details/export.hxx> + +namespace odb +{ + namespace mysql + { + struct LIBODB_MYSQL_EXPORT prepared_query_impl: odb::prepared_query_impl + { + virtual + ~prepared_query_impl (); + + mysql::query_base query; + }; + } +} + +#include <odb/post.hxx> + +#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 <typename T, database_type_id ID> inline void query_base:: append (val_bind<T> 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 <odb/mysql/mysql.hxx> #include <odb/mysql/version.hxx> #include <odb/mysql/binding.hxx> +#include <odb/mysql/connection.hxx> #include <odb/mysql/auto-handle.hxx> #include <odb/mysql/details/export.hxx> @@ -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<MYSQL_STMT> 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); |