From 40ea54d2eda9b7d6d8f3d3f9c2e4dc56a2592538 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 9 Nov 2011 14:09:52 +0200 Subject: Avoid copying statement text if it is statically allocated --- odb/mysql/container-statements.hxx | 7 ++- odb/mysql/object-statements.hxx | 18 ++++-- odb/mysql/statement.cxx | 115 ++++++++++++++++++++++++++++++++----- odb/mysql/statement.hxx | 46 ++++++++++++--- 4 files changed, 156 insertions(+), 30 deletions(-) diff --git a/odb/mysql/container-statements.hxx b/odb/mysql/container-statements.hxx index 9af2ff8..a4bfce2 100644 --- a/odb/mysql/container-statements.hxx +++ b/odb/mysql/container-statements.hxx @@ -167,7 +167,7 @@ namespace odb if (insert_one_ == 0) insert_one_.reset ( new (details::shared) insert_statement_type ( - conn_, insert_one_text_, data_image_binding_)); + conn_, insert_one_text_, data_image_binding_, false)); return *insert_one_; } @@ -181,7 +181,8 @@ namespace odb conn_, select_all_text_, cond_image_binding_, - data_image_binding_)); + data_image_binding_, + false)); return *select_all_; } @@ -192,7 +193,7 @@ namespace odb if (delete_all_ == 0) delete_all_.reset ( new (details::shared) delete_statement_type ( - conn_, delete_all_text_, cond_image_binding_)); + conn_, delete_all_text_, cond_image_binding_, false)); return *delete_all_; } diff --git a/odb/mysql/object-statements.hxx b/odb/mysql/object-statements.hxx index 7991be6..286e597 100644 --- a/odb/mysql/object-statements.hxx +++ b/odb/mysql/object-statements.hxx @@ -302,7 +302,8 @@ namespace odb new (details::shared) insert_statement_type ( conn_, object_traits::persist_statement, - insert_image_binding_)); + insert_image_binding_, + false)); return *persist_; } @@ -316,7 +317,8 @@ namespace odb conn_, object_traits::find_statement, id_image_binding_, - select_image_binding_)); + select_image_binding_, + false)); return *find_; } @@ -329,7 +331,8 @@ namespace odb new (details::shared) update_statement_type ( conn_, object_traits::update_statement, - update_image_binding_)); + update_image_binding_, + false)); return *update_; } @@ -342,7 +345,8 @@ namespace odb new (details::shared) delete_statement_type ( conn_, object_traits::erase_statement, - id_image_binding_)); + id_image_binding_, + false)); return *erase_; } @@ -356,7 +360,8 @@ namespace odb new (details::shared) delete_statement_type ( conn_, object_traits::optimistic_erase_statement, - od_.id_image_binding_)); + od_.id_image_binding_, + false)); } return *od_.erase_; @@ -554,7 +559,8 @@ namespace odb new (details::shared) insert_statement_type ( conn_, object_traits::persist_statement, - insert_image_binding_)); + insert_image_binding_, + false)); return *persist_; } diff --git a/odb/mysql/statement.cxx b/odb/mysql/statement.cxx index 04f59f6..9470daa 100644 --- a/odb/mysql/statement.cxx +++ b/odb/mysql/statement.cxx @@ -3,6 +3,7 @@ // copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file +#include // std::strlen #include #include @@ -24,11 +25,40 @@ namespace odb statement:: statement (connection& conn, const string& text) - : conn_ (conn), text_ (text), stmt_ (conn_.alloc_stmt_handle ()) + : conn_ (conn), text_copy_ (text), text_ (text_copy_.c_str ()) { + init (text_copy_.size ()); + } + + statement:: + statement (connection& conn, const char* text, bool copy) + : conn_ (conn) + { + size_t n; + + if (copy) + { + text_copy_ = text; + text_ = text_copy_.c_str (); + n = text_copy_.size (); + } + else + { + text_ = text; + n = strlen (text_); + } + + init (n); + } + + void statement:: + init (size_t text_size) + { + stmt_.reset (conn_.alloc_stmt_handle ()); + conn_.clear (); - if (mysql_stmt_prepare (stmt_, text_.c_str (), text_.size ()) != 0) + if (mysql_stmt_prepare (stmt_, text_, text_size) != 0) translate_error (conn_, stmt_); { @@ -61,7 +91,7 @@ namespace odb const char* statement:: text () const { - return text_.c_str (); + return text_; } void statement:: @@ -89,10 +119,10 @@ namespace odb select_statement:: select_statement (connection& conn, - const string& s, + const string& t, binding& param, binding& result) - : statement (conn, s), + : statement (conn, t), end_ (false), cached_ (false), rows_ (0), @@ -104,8 +134,40 @@ namespace odb } select_statement:: - select_statement (connection& conn, const string& s, binding& result) - : statement (conn, s), + select_statement (connection& conn, + const char* t, + binding& param, + binding& result, + bool ct) + : statement (conn, t, ct), + end_ (false), + cached_ (false), + rows_ (0), + param_ (¶m), + param_version_ (0), + result_ (result), + result_version_ (0) + { + } + + select_statement:: + select_statement (connection& conn, const string& t, binding& result) + : statement (conn, t), + end_ (false), + cached_ (false), + rows_ (0), + param_ (0), + result_ (result), + result_version_ (0) + { + } + + select_statement:: + select_statement (connection& conn, + const char* t, + binding& result, + bool ct) + : statement (conn, t, ct), end_ (false), cached_ (false), rows_ (0), @@ -276,8 +338,17 @@ namespace odb } insert_statement:: - insert_statement (connection& conn, const string& s, binding& param) - : statement (conn, s), param_ (param), param_version_ (0) + insert_statement (connection& conn, const string& t, binding& param) + : statement (conn, t), param_ (param), param_version_ (0) + { + } + + insert_statement:: + insert_statement (connection& conn, + const char* t, + binding& param, + bool ct) + : statement (conn, t, ct), param_ (param), param_version_ (0) { } @@ -331,8 +402,17 @@ namespace odb } update_statement:: - update_statement (connection& conn, const string& s, binding& param) - : statement (conn, s), param_ (param), param_version_ (0) + update_statement (connection& conn, const string& t, binding& param) + : statement (conn, t), param_ (param), param_version_ (0) + { + } + + update_statement:: + update_statement (connection& conn, + const char* t, + binding& param, + bool ct) + : statement (conn, t, ct), param_ (param), param_version_ (0) { } @@ -380,8 +460,17 @@ namespace odb } delete_statement:: - delete_statement (connection& conn, const string& s, binding& param) - : statement (conn, s), param_ (param), param_version_ (0) + delete_statement (connection& conn, const string& t, binding& param) + : statement (conn, t), param_ (param), param_version_ (0) + { + } + + delete_statement:: + delete_statement (connection& conn, + const char* t, + binding& param, + bool ct) + : statement (conn, t, ct), param_ (param), param_version_ (0) { } diff --git a/odb/mysql/statement.hxx b/odb/mysql/statement.hxx index ee1284b..91faa65 100644 --- a/odb/mysql/statement.hxx +++ b/odb/mysql/statement.hxx @@ -50,10 +50,16 @@ namespace odb protected: statement (connection&, const std::string& text); + statement (connection&, const char* text, bool copy_text); + + private: + void + init (std::size_t text_size); protected: connection& conn_; - const std::string text_; + std::string text_copy_; + const char* text_; auto_handle stmt_; }; @@ -64,14 +70,25 @@ namespace odb ~select_statement (); select_statement (connection& conn, - const std::string& statement, + const std::string& text, binding& param, binding& result); select_statement (connection& conn, - const std::string& statement, + const char* text, + binding& param, + binding& result, + bool copy_text = true); + + select_statement (connection& conn, + const std::string& text, binding& result); + select_statement (connection& conn, + const char* text, + binding& result, + bool copy_text = true); + enum result { success, @@ -147,9 +164,14 @@ namespace odb ~insert_statement (); insert_statement (connection& conn, - const std::string& statement, + const std::string& text, binding& param); + insert_statement (connection& conn, + const char* text, + binding& param, + bool copy_text = true); + // Return true if successful and false if the row is a duplicate. // All other errors are reported by throwing exceptions. // @@ -174,12 +196,15 @@ namespace odb virtual ~update_statement (); - // Asssumes that param.bind is a suffix of data.bind. - // update_statement (connection& conn, - const std::string& statement, + const std::string& text, binding& param); + update_statement (connection& conn, + const char* text, + binding& param, + bool copy_text = true); + unsigned long long execute (); @@ -199,9 +224,14 @@ namespace odb ~delete_statement (); delete_statement (connection& conn, - const std::string& statement, + const std::string& text, binding& param); + delete_statement (connection& conn, + const char* text, + binding& param, + bool copy_text = true); + unsigned long long execute (); -- cgit v1.1