Commit 68a5f093 authored by Maciej Sobczak's avatar Maciej Sobczak

Applied modified patch from Matthieu Kermagoret for get_affected_rows.

parent 1dcd0313
......@@ -114,6 +114,7 @@ struct SOCI_EMPTY_DECL empty_statement_backend : details::statement_backend
exec_fetch_result execute(int number);
exec_fetch_result fetch(int number);
long long get_affected_rows();
int get_number_of_rows();
std::string rewrite_for_procedure_call(std::string const& query);
......
......@@ -51,6 +51,12 @@ empty_statement_backend::fetch(int /* number */)
return ef_success;
}
long long empty_statement_backend::get_affected_rows()
{
// ...
return -1;
}
int empty_statement_backend::get_number_of_rows()
{
// ...
......
......@@ -179,6 +179,7 @@ struct firebird_statement_backend : details::statement_backend
virtual exec_fetch_result execute(int number);
virtual exec_fetch_result fetch(int number);
virtual long long get_affected_rows();
virtual int get_number_of_rows();
virtual std::string rewrite_for_procedure_call(std::string const &query);
......
......@@ -540,6 +540,12 @@ void firebird_statement_backend::exchangeData(bool gotData, int row)
}
}
long long firebird_statement_backend::get_affected_rows()
{
// ...
return -1;
}
int firebird_statement_backend::get_number_of_rows()
{
return rowsFetched_;
......
......@@ -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_);
......
......@@ -151,6 +151,7 @@ struct mysql_statement_backend : details::statement_backend
virtual exec_fetch_result execute(int number);
virtual exec_fetch_result fetch(int number);
virtual long long get_affected_rows();
virtual int get_number_of_rows();
virtual std::string rewrite_for_procedure_call(std::string const &query);
......
......@@ -340,6 +340,11 @@ mysql_statement_backend::fetch(int number)
}
}
long long mysql_statement_backend::get_affected_rows()
{
return static_cast<long long>(mysql_affected_rows(session_.conn_));
}
int mysql_statement_backend::get_number_of_rows()
{
return numberOfRows_ - currentRow_;
......
......@@ -168,6 +168,7 @@ struct odbc_statement_backend : details::statement_backend
virtual exec_fetch_result execute(int number);
virtual exec_fetch_result fetch(int number);
virtual long long get_affected_rows();
virtual int get_number_of_rows();
virtual std::string rewrite_for_procedure_call(std::string const &query);
......
......@@ -199,6 +199,12 @@ odbc_statement_backend::fetch(int number)
return ef_success;
}
long long odbc_statement_backend::get_affected_rows()
{
// ...
return -1;
}
int odbc_statement_backend::get_number_of_rows()
{
return numRowsFetched_;
......
......@@ -186,6 +186,7 @@ struct oracle_statement_backend : details::statement_backend
virtual exec_fetch_result execute(int number);
virtual exec_fetch_result fetch(int number);
virtual long long get_affected_rows();
virtual int get_number_of_rows();
virtual std::string rewrite_for_procedure_call(std::string const &query);
......
......@@ -116,6 +116,12 @@ statement_backend::exec_fetch_result oracle_statement_backend::fetch(int number)
}
}
long long oracle_statement_backend::get_affected_rows()
{
// ...
return -1;
}
int oracle_statement_backend::get_number_of_rows()
{
int rows;
......
......@@ -142,6 +142,7 @@ struct postgresql_statement_backend : details::statement_backend
virtual exec_fetch_result execute(int number);
virtual exec_fetch_result fetch(int number);
virtual long long get_affected_rows();
virtual int get_number_of_rows();
virtual std::string rewrite_for_procedure_call(std::string const & query);
......
......@@ -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,21 @@ postgresql_statement_backend::fetch(int number)
}
}
long long postgresql_statement_backend::get_affected_rows()
{
const char * resultStr = PQcmdTuples(result_);
char * end;
long long result = strtoll(resultStr, &end, 0);
if (end != resultStr)
{
return result;
}
else
{
return -1;
}
}
int postgresql_statement_backend::get_number_of_rows()
{
return numberOfRows_ - currentRow_;
......
......@@ -450,6 +450,45 @@ void test10()
std::cout << "test 10 passed" << std::endl;
}
// test for number of affected rows
struct table_creator_for_test11 : table_creator_base
{
table_creator_for_test11(session & sql)
: table_creator_base(sql)
{
sql << "create table soci_test(val integer)";
}
};
void test11()
{
{
session sql(backEnd, connectString);
table_creator_for_test11 tableCreator(sql);
for (int i = 0; i != 10; i++)
{
sql << "insert into soci_test(val) values(:val)", use(i);
}
statement st1 = (sql.prepare <<
"update soci_test set val = val + 1");
st1.execute(false);
assert(st1.get_affected_rows() == 10);
statement st2 = (sql.prepare <<
"delete from soci_test where val <= 5");
st2.execute(false);
assert(st2.get_affected_rows() == 5);
}
std::cout << "test 11 passed" << std::endl;
}
// DDL Creation objects for common tests
struct table_creator_one : public table_creator_base
{
......@@ -563,6 +602,7 @@ int main(int argc, char** argv)
test8();
test9();
test10();
test11();
std::cout << "\nOK, all tests passed.\n\n";
return EXIT_SUCCESS;
......
......@@ -172,6 +172,7 @@ struct sqlite3_statement_backend : details::statement_backend
virtual exec_fetch_result execute(int number);
virtual exec_fetch_result fetch(int number);
virtual long long get_affected_rows();
virtual int get_number_of_rows();
virtual std::string rewrite_for_procedure_call(std::string const &query);
......
......@@ -270,6 +270,12 @@ sqlite3_statement_backend::fetch(int number)
return loadRS(number);
}
long long sqlite3_statement_backend::get_affected_rows()
{
// ...
return -1;
}
int sqlite3_statement_backend::get_number_of_rows()
{
return static_cast<int>(dataCache_.size());
......
......@@ -127,6 +127,7 @@ public:
virtual exec_fetch_result execute(int number) = 0;
virtual exec_fetch_result fetch(int number) = 0;
virtual long long get_affected_rows() = 0;
virtual int get_number_of_rows() = 0;
virtual std::string rewrite_for_procedure_call(std::string const& query) = 0;
......
......@@ -1792,6 +1792,13 @@ SOCI_DECL int soci_execute(statement_handle st, int withDataExchange)
}
}
SOCI_DECL long long soci_get_affected_rows(statement_handle st)
{
statement_wrapper * wrapper = static_cast<statement_wrapper *>(st);
return wrapper->st.get_affected_rows();
}
SOCI_DECL int soci_fetch(statement_handle st)
{
statement_wrapper * wrapper = static_cast<statement_wrapper *>(st);
......
......@@ -121,10 +121,11 @@ SOCI_DECL char const * soci_get_use_date (statement_handle st, char const *
// statement preparation and execution
SOCI_DECL void soci_prepare(statement_handle st, char const * query);
SOCI_DECL int soci_execute(statement_handle st, int withDataExchange);
SOCI_DECL int soci_fetch(statement_handle st);
SOCI_DECL int soci_got_data(statement_handle st);
SOCI_DECL void soci_prepare(statement_handle st, char const * query);
SOCI_DECL int soci_execute(statement_handle st, int withDataExchange);
SOCI_DECL long long soci_get_affected_rows(statement_handle st);
SOCI_DECL int soci_fetch(statement_handle st);
SOCI_DECL int soci_got_data(statement_handle st);
SOCI_DECL int soci_statement_state(statement_handle s);
SOCI_DECL char const * soci_statement_error_message(statement_handle s);
......
......@@ -363,6 +363,11 @@ bool statement_impl::execute(bool withDataExchange)
return gotData;
}
long long statement_impl::get_affected_rows()
{
return backEnd_->get_affected_rows();
}
bool statement_impl::fetch()
{
if (fetchSize_ == 0)
......
......@@ -50,6 +50,7 @@ public:
void define_and_bind();
void undefine_and_bind();
bool execute(bool withDataExchange = false);
long long get_affected_rows();
bool fetch();
void describe();
void set_row(row * r);
......@@ -169,6 +170,11 @@ public:
return gotData_;
}
long long get_affected_rows()
{
return impl_->get_affected_rows();
}
bool fetch()
{
gotData_ = impl_->fetch();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment