aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--odb/mysql/result.hxx3
-rw-r--r--odb/mysql/result.txx7
-rw-r--r--odb/mysql/statement.cxx33
-rw-r--r--odb/mysql/statement.hxx4
4 files changed, 41 insertions, 6 deletions
diff --git a/odb/mysql/result.hxx b/odb/mysql/result.hxx
index 951ae91..15bcb5d 100644
--- a/odb/mysql/result.hxx
+++ b/odb/mysql/result.hxx
@@ -46,6 +46,9 @@ namespace odb
virtual void
cache ();
+ virtual std::size_t
+ size ();
+
using odb::result_impl<T>::current;
private:
diff --git a/odb/mysql/result.txx b/odb/mysql/result.txx
index d0a65fa..329b75a 100644
--- a/odb/mysql/result.txx
+++ b/odb/mysql/result.txx
@@ -80,5 +80,12 @@ namespace odb
{
statement_->cache ();
}
+
+ template <typename T>
+ std::size_t result_impl<T>::
+ size ()
+ {
+ return statement_->result_size ();
+ }
}
}
diff --git a/odb/mysql/statement.cxx b/odb/mysql/statement.cxx
index 1e822b0..6193f39 100644
--- a/odb/mysql/statement.cxx
+++ b/odb/mysql/statement.cxx
@@ -8,8 +8,6 @@
#include <odb/mysql/connection.hxx>
#include <odb/mysql/exceptions.hxx>
-#include <iostream> // @@ tmp
-
using namespace std;
namespace odb
@@ -68,6 +66,7 @@ namespace odb
: statement (conn),
end_ (false),
cached_ (false),
+ rows_ (0),
image_ (image),
image_version_ (0),
parameters_ (parameters)
@@ -89,6 +88,7 @@ namespace odb
free_result ();
end_ = false;
+ rows_ = 0;
if (mysql_stmt_reset (stmt_))
throw database_exception (stmt_);
@@ -125,18 +125,32 @@ namespace odb
void query_statement::
cache ()
{
- if (!cached_ && !end_)
+ if (!cached_)
{
- if (mysql_stmt_store_result (stmt_))
+ if (!end_)
{
- std::cerr << "store result failed" << std::endl;
- throw database_exception (stmt_);
+ if (mysql_stmt_store_result (stmt_))
+ {
+ throw database_exception (stmt_);
+ }
}
cached_ = true;
}
}
+ std::size_t query_statement::
+ result_size ()
+ {
+ if (!cached_)
+ throw result_not_cached ();
+
+ // mysql_stmt_num_rows() returns the number of rows that have been
+ // fetched by store_result.
+ //
+ return rows_ + static_cast<std::size_t> (mysql_stmt_num_rows (stmt_));
+ }
+
query_statement::result query_statement::
fetch ()
{
@@ -157,6 +171,9 @@ namespace odb
{
case 0:
{
+ if (!cached_)
+ rows_++;
+
return success;
}
case MYSQL_NO_DATA:
@@ -166,6 +183,9 @@ namespace odb
}
case MYSQL_DATA_TRUNCATED:
{
+ if (!cached_)
+ rows_++;
+
return truncated;
}
default:
@@ -198,6 +218,7 @@ namespace odb
{
end_ = true;
cached_ = false;
+ rows_ = 0;
if (mysql_stmt_free_result (stmt_))
throw database_exception (stmt_);
diff --git a/odb/mysql/statement.hxx b/odb/mysql/statement.hxx
index 1684600..bdeee7a 100644
--- a/odb/mysql/statement.hxx
+++ b/odb/mysql/statement.hxx
@@ -89,6 +89,9 @@ namespace odb
void
cache ();
+ std::size_t
+ result_size ();
+
result
fetch ();
@@ -108,6 +111,7 @@ namespace odb
private:
bool end_;
bool cached_;
+ std::size_t rows_;
binding& image_;
std::size_t image_version_;