From 071fe5eef8d88ac51d4ed6436aa2ffc95283ae5b Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Thu, 8 Sep 2011 09:23:47 +0200 Subject: Implement SQLite style select statement interface --- odb/oracle/statement.cxx | 71 ++++++++++++++++++++++-------------------------- odb/oracle/statement.hxx | 43 ++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 46 deletions(-) diff --git a/odb/oracle/statement.cxx b/odb/oracle/statement.cxx index 9398820..f41c161 100644 --- a/odb/oracle/statement.cxx +++ b/odb/oracle/statement.cxx @@ -114,7 +114,7 @@ namespace odb binding& cond, binding& data) : statement (conn, s), - end_ (false), + done_ (false), rows_ (0) { bind_param (cond.bind, cond.count, 0); @@ -124,7 +124,7 @@ namespace odb void select_statement:: execute () { - if (!end_) + if (!done_) free_result (); // @@ Retrieve a single row into the already bound output buffers @@ -144,52 +144,45 @@ namespace odb translate_error (conn_.error_handle (), r); } - select_statement::result select_statement:: - fetch () + void select_statement:: + free_result () { - sword r (OCIStmtFetch2 (stmt_, - conn_.error_handle (), - 1, - OCI_FETCH_NEXT, - 0, - OCI_DEFAULT)); - - switch (r) + if (!done_) { - case OCI_SUCCESS: - { - ++rows_; - return success; - } - case OCI_NO_DATA: - { - end_ = true; - return no_data; - } - default: - { + sword r (OCIStmtFetch2 (stmt_, + conn_.error_handle (), + 0, + OCI_FETCH_NEXT, + 0, + OCI_DEFAULT)); + + if (r == OCI_ERROR || r == OCI_INVALID_HANDLE) translate_error (conn_.error_handle (), r); - return no_data; // Never reached. - } + + done_ = true; } - } + } - void select_statement:: - free_result () + bool select_statement:: + next () { - end_ = true; - rows_ = 0; + if (!done_) + { + sword r (OCIStmtFetch2 (stmt_, + conn_.error_handle (), + 1, + OCI_FETCH_NEXT, + 0, + OCI_DEFAULT)); - sword r (OCIStmtFetch2 (stmt_, - conn_.error_handle (), - 0, - OCI_FETCH_NEXT, - 0, - OCI_DEFAULT)); + if (r == OCI_ERROR || r == OCI_INVALID_HANDLE) + translate_error (conn_.error_handle (), r); + else if (r == OCI_NO_DATA) + done_ = true; + } - if (r == OCI_ERROR || r == OCI_INVALID_HANDLE) - translate_error (conn_.error_handle (), r); + return !done_; } // diff --git a/odb/oracle/statement.hxx b/odb/oracle/statement.hxx index 8dfaa3f..6e183a8 100644 --- a/odb/oracle/statement.hxx +++ b/odb/oracle/statement.hxx @@ -74,26 +74,55 @@ namespace odb void execute (); - // Number of rows already fetched. + result + fetch () + { + return next () ? load () : no_data; + } + + // Never need to deal with truncation, so this is a dummy function. // - std::size_t - fetched () const + void + refetch () { - return rows_; } + void + free_result (); + + // More fine-grained Oracle-specific interface that splits fetch() into + // next() and load(). + // + public: + // Return false if there is no more rows. You should call next() until it + // returns false or, alternatively, call free_result (). Otherwise, the + // statement will remain unfinished. + // + bool + next (); + result - fetch (); + load () + { + if (done_) + return no_data; + + return success; + } + // Never need to deal with truncation, so this is a dummy function. + // void - free_result (); + reload () + { + } private: select_statement (const select_statement&); select_statement& operator= (const select_statement&); private: - bool end_; + bool done_; std::size_t rows_; }; -- cgit v1.1