From 6e71fee5bdb0fe338c7b9c3259a07d317ba36679 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/pgsql/statement.cxx | 174 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 148 insertions(+), 26 deletions(-) (limited to 'odb/pgsql/statement.cxx') diff --git a/odb/pgsql/statement.cxx b/odb/pgsql/statement.cxx index 8a20456..026c419 100644 --- a/odb/pgsql/statement.cxx +++ b/odb/pgsql/statement.cxx @@ -93,10 +93,51 @@ namespace odb const Oid* types, size_t types_count) : conn_ (conn), - name_ (name), - text_ (text), + name_copy_ (name), name_ (name_copy_.c_str ()), + text_copy_ (text), text_ (text_copy_.c_str ()), deallocated_ (false) { + init (types, types_count); + + // + // If any code after this line throws, the statement will be leaked + // (on the server) since deallocate() won't be called for it. + // + } + + statement:: + statement (connection& conn, + const char* name, + const char* text, + bool copy, + const Oid* types, + size_t types_count) + : conn_ (conn), deallocated_ (false) + { + if (copy) + { + name_copy_ = name; + name_ = name_copy_.c_str (); + text_copy_ = text; + text_ = text_copy_.c_str (); + } + else + { + name_ = name; + text_ = text; + } + + init (types, types_count); + + // + // If any code after this line throws, the statement will be leaked + // (on the server) since deallocate() won't be called for it. + // + } + + void statement:: + init (const Oid* types, size_t types_count) + { { odb::tracer* t; if ((t = conn_.transaction_tracer ()) || @@ -107,8 +148,8 @@ namespace odb auto_handle h ( PQprepare (conn_.handle (), - name_.c_str (), - text_.c_str (), + name_, + text_, static_cast (types_count), types)); @@ -124,7 +165,7 @@ namespace odb const char* statement:: text () const { - return text_.c_str (); + return text_; } void statement:: @@ -355,13 +396,13 @@ namespace odb select_statement:: select_statement (connection& conn, const std::string& name, - const std::string& stmt, + const std::string& text, const Oid* types, std::size_t types_count, binding& param, native_binding& native_param, binding& result) - : statement (conn, name, stmt, types, types_count), + : statement (conn, name, text, types, types_count), param_ (¶m), native_param_ (&native_param), result_ (result), @@ -372,14 +413,16 @@ namespace odb select_statement:: select_statement (connection& conn, - const std::string& name, - const std::string& stmt, + const char* name, + const char* text, const Oid* types, std::size_t types_count, + binding& param, native_binding& native_param, - binding& result) - : statement (conn, name, stmt, types, types_count), - param_ (0), + binding& result, + bool copy) + : statement (conn, name, text, copy, types, types_count), + param_ (¶m), native_param_ (&native_param), result_ (result), row_count_ (0), @@ -390,9 +433,9 @@ namespace odb select_statement:: select_statement (connection& conn, const std::string& name, - const std::string& stmt, + const std::string& text, binding& result) - : statement (conn, name, stmt, 0, 0), + : statement (conn, name, text, 0, 0), param_ (0), native_param_ (0), result_ (result), @@ -401,6 +444,38 @@ namespace odb { } + select_statement:: + select_statement (connection& conn, + const char* name, + const char* text, + binding& result, + bool copy) + : statement (conn, name, text, copy, 0, 0), + param_ (0), + native_param_ (0), + result_ (result), + row_count_ (0), + current_row_ (0) + { + } + + select_statement:: + select_statement (connection& conn, + const std::string& name, + const std::string& text, + const Oid* types, + std::size_t types_count, + native_binding& native_param, + binding& result) + : statement (conn, name, text, types, types_count), + param_ (0), + native_param_ (&native_param), + result_ (result), + row_count_ (0), + current_row_ (0) + { + } + void select_statement:: execute () { @@ -421,7 +496,7 @@ namespace odb handle_.reset ( PQexecPrepared (conn_.handle (), - name_.c_str (), + name_, in ? native_param_->count : 0, in ? native_param_->values : 0, in ? native_param_->lengths : 0, @@ -493,13 +568,30 @@ namespace odb insert_statement:: insert_statement (connection& conn, const string& name, - const string& stmt, + const string& text, const Oid* types, size_t types_count, binding& param, native_binding& native_param, bool returning) - : statement (conn, name, stmt, types, types_count), + : statement (conn, name, text, types, types_count), + param_ (param), + native_param_ (native_param), + returning_ (returning) + { + } + + insert_statement:: + insert_statement (connection& conn, + const char* name, + const char* text, + const Oid* types, + size_t types_count, + binding& param, + native_binding& native_param, + bool returning, + bool copy) + : statement (conn, name, text, copy, types, types_count), param_ (param), native_param_ (native_param), returning_ (returning) @@ -521,7 +613,7 @@ namespace odb auto_handle h ( PQexecPrepared (conn_.handle (), - name_.c_str (), + name_, native_param_.count, native_param_.values, native_param_.lengths, @@ -595,12 +687,27 @@ namespace odb update_statement:: update_statement (connection& conn, const string& name, - const string& stmt, + const string& text, const Oid* types, size_t types_count, binding& param, native_binding& native_param) - : statement (conn, name, stmt, types, types_count), + : statement (conn, name, text, types, types_count), + param_ (param), + native_param_ (native_param) + { + } + + update_statement:: + update_statement (connection& conn, + const char* name, + const char* text, + const Oid* types, + size_t types_count, + binding& param, + native_binding& native_param, + bool copy) + : statement (conn, name, text, copy, types, types_count), param_ (param), native_param_ (native_param) { @@ -621,7 +728,7 @@ namespace odb auto_handle h ( PQexecPrepared (conn_.handle (), - name_.c_str (), + name_, native_param_.count, native_param_.values, native_param_.lengths, @@ -646,12 +753,27 @@ namespace odb delete_statement:: delete_statement (connection& conn, const string& name, - const string& stmt, + const string& text, const Oid* types, size_t types_count, binding& param, native_binding& native_param) - : statement (conn, name, stmt, types, types_count), + : statement (conn, name, text, types, types_count), + param_ (¶m), + native_param_ (native_param) + { + } + + delete_statement:: + delete_statement (connection& conn, + const char* name, + const char* text, + const Oid* types, + size_t types_count, + binding& param, + native_binding& native_param, + bool copy) + : statement (conn, name, text, copy, types, types_count), param_ (¶m), native_param_ (native_param) { @@ -660,11 +782,11 @@ namespace odb delete_statement:: delete_statement (connection& conn, const string& name, - const string& stmt, + const string& text, const Oid* types, size_t types_count, native_binding& native_param) - : statement (conn, name, stmt, types, types_count), + : statement (conn, name, text, types, types_count), param_ (0), native_param_ (native_param) { @@ -686,7 +808,7 @@ namespace odb auto_handle h ( PQexecPrepared (conn_.handle (), - name_.c_str (), + name_, native_param_.count, native_param_.values, native_param_.lengths, -- cgit v1.1