Hi list,

The attached patch is the beginning of an implementation of a new
method for the statement class : get_affected_rows(). This method is
used to get the number of rows affected by an update/delete query for
example. I have only implemented the method for MySQL and PostgreSQL,
as I do not master other DBMS API well enough.

What do you think about this patch ? Is there somebody that would like
to help me finish this change for all other backends ?

Best regards,

-- 
Matthieu KERMAGORET | Développeur

[email protected]

MERETHIS est éditeur du logiciel Centreon.
diff --git a/src/backends/empty/soci-empty.h b/src/backends/empty/soci-empty.h
index f5a34da..ffbb288 100644
--- a/src/backends/empty/soci-empty.h
+++ b/src/backends/empty/soci-empty.h
@@ -113,6 +113,7 @@ struct SOCI_EMPTY_DECL empty_statement_backend : details::statement_backend
 
     exec_fetch_result execute(int number);
     exec_fetch_result fetch(int number);
+    unsigned long get_affected_rows();
 
     int get_number_of_rows();
 
diff --git a/src/backends/empty/statement.cpp b/src/backends/empty/statement.cpp
index a93f4de..19ad2d3 100644
--- a/src/backends/empty/statement.cpp
+++ b/src/backends/empty/statement.cpp
@@ -51,6 +51,11 @@ empty_statement_backend::fetch(int /* number */)
     return ef_success;
 }
 
+unsigned long empty_statement_backend::get_affected_rows()
+{
+    return static_cast<unsigned long>(-1);
+}
+
 int empty_statement_backend::get_number_of_rows()
 {
     // ...
diff --git a/src/backends/mysql/session.cpp b/src/backends/mysql/session.cpp
index cd24dba..342da77 100644
--- a/src/backends/mysql/session.cpp
+++ b/src/backends/mysql/session.cpp
@@ -251,7 +251,7 @@ mysql_session_backend::mysql_session_backend(
             db_p ? db.c_str() : NULL,
             port_p ? port : 0,
             unix_socket_p ? unix_socket.c_str() : NULL,
-            CLIENT_MULTI_RESULTS) == NULL)
+            CLIENT_FOUND_ROWS | CLIENT_MULTI_RESULTS) == NULL)
     {
         string errMsg = mysql_error(conn_);
         unsigned int errNum = mysql_errno(conn_);
diff --git a/src/backends/mysql/soci-mysql.h b/src/backends/mysql/soci-mysql.h
index 195f7ca..39097db 100644
--- a/src/backends/mysql/soci-mysql.h
+++ b/src/backends/mysql/soci-mysql.h
@@ -150,6 +150,7 @@ struct mysql_statement_backend : details::statement_backend
 
     virtual exec_fetch_result execute(int number);
     virtual exec_fetch_result fetch(int number);
+    virtual unsigned long get_affected_rows();
 
     virtual int get_number_of_rows();
 
diff --git a/src/backends/mysql/statement.cpp b/src/backends/mysql/statement.cpp
index e857cbf..069861b 100644
--- a/src/backends/mysql/statement.cpp
+++ b/src/backends/mysql/statement.cpp
@@ -340,6 +340,12 @@ mysql_statement_backend::fetch(int number)
     }
 }
 
+unsigned long mysql_statement_backend::get_affected_rows()
+{
+    return static_cast<unsigned long>(
+        mysql_affected_rows(session_.conn_));
+}
+
 int mysql_statement_backend::get_number_of_rows()
 {
     return numberOfRows_ - currentRow_;
diff --git a/src/backends/postgresql/soci-postgresql.h b/src/backends/postgresql/soci-postgresql.h
index 8e2429e..0b04b4f 100644
--- a/src/backends/postgresql/soci-postgresql.h
+++ b/src/backends/postgresql/soci-postgresql.h
@@ -140,6 +140,7 @@ struct postgresql_statement_backend : details::statement_backend
 
     virtual exec_fetch_result execute(int number);
     virtual exec_fetch_result fetch(int number);
+    virtual unsigned long get_affected_rows();
 
     virtual int get_number_of_rows();
 
diff --git a/src/backends/postgresql/statement.cpp b/src/backends/postgresql/statement.cpp
index 3d7a025..8d1202a 100644
--- a/src/backends/postgresql/statement.cpp
+++ b/src/backends/postgresql/statement.cpp
@@ -10,6 +10,7 @@
 #include <libpq/libpq-fs.h> // libpq
 #include <cctype>
 #include <cstdio>
+#include <cstdlib>
 #include <cstring>
 #include <ctime>
 #include <sstream>
@@ -434,6 +435,13 @@ postgresql_statement_backend::fetch(int number)
     }
 }
 
+unsigned long postgresql_statement_backend::get_affected_rows()
+{
+    char* str = PQcmdTuples(result_);
+
+    return (str ? strtoul(str, NULL, 0) : static_cast<unsigned long>(-1));
+}
+
 int postgresql_statement_backend::get_number_of_rows()
 {
     return numberOfRows_ - currentRow_;
diff --git a/src/core/soci-backend.h b/src/core/soci-backend.h
index 9f1f95e..205887d 100644
--- a/src/core/soci-backend.h
+++ b/src/core/soci-backend.h
@@ -152,6 +152,7 @@ public:
 
     virtual exec_fetch_result execute(int number) = 0;
     virtual exec_fetch_result fetch(int number) = 0;
+    virtual unsigned long get_affected_rows() = 0;
 
     virtual int get_number_of_rows() = 0;
 
diff --git a/src/core/statement.cpp b/src/core/statement.cpp
index 738ef6f..3f36d2a 100644
--- a/src/core/statement.cpp
+++ b/src/core/statement.cpp
@@ -429,6 +429,11 @@ bool statement_impl::fetch()
     return gotData;
 }
 
+unsigned long statement_impl::get_affected_rows()
+{
+    return backEnd_->get_affected_rows();
+}
+
 std::size_t statement_impl::intos_size()
 {
     // this function does not need to take into account intosForRow_ elements,
diff --git a/src/core/statement.h b/src/core/statement.h
index b898a65..236b433 100644
--- a/src/core/statement.h
+++ b/src/core/statement.h
@@ -51,6 +51,7 @@ public:
     void undefine_and_bind();
     bool execute(bool withDataExchange = false);
     bool fetch();
+    unsigned long get_affected_rows();
     void describe();
     void set_row(row * r);
     void exchange_for_rowset(into_type_ptr const & i);
@@ -175,6 +176,11 @@ public:
         return gotData_;
     }
 
+    unsigned long get_affected_rows()
+    {
+        return impl_->get_affected_rows();
+    }
+
     bool got_data() const { return gotData_; }
 
     void describe()       { impl_->describe(); }
------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
Soci-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/soci-users

Reply via email to