Hazelcast C++ Client
Hazelcast C++ Client Library
Loading...
Searching...
No Matches
hazelcast::client::sql::sql_result Class Reference

SQL query result. More...

#include <sql_result.h>

Inheritance diagram for hazelcast::client::sql::sql_result:

Classes

class  page_iterator
 Copy is allowed for convenience but it does shallow copy so it should be avoided. More...
class  page_iterator_sync
 Copy is allowed for convenience but it does shallow copy so it should be avoided. More...
class  row_iterator_sync
 Copy is allowed for convenience but it does shallow copy so it should be avoided. More...

Public Member Functions

virtual ~sql_result ()
 The destructor closes the result if it were open.
bool row_set () const
 Return whether this result has rows to iterate using the iterator() method.
const sql_row_metadatarow_metadata () const
 Gets the row metadata.
int64_t update_count () const
 Returns the number of rows updated by the statement or -1 if this result is a row set.
boost::future< void > close ()
 Release the resources associated with the query result.
page_iterator iterator ()
 Returns an iterator over the result pages.
page_iterator_sync pbegin (std::chrono::milliseconds timeout=std::chrono::milliseconds{ -1 })
page_iterator_sync pend ()
row_iterator_sync begin (std::chrono::milliseconds timeout=std::chrono::milliseconds{ -1 })
row_iterator_sync end ()

Friends

class sql_service

Detailed Description

SQL query result.

Depending on the statement type it represents a pages of rows or an update count.

Usage for page of rows

  1. Use iterator() to iterate the pages. see page_iterator
  2. Use close() to release the resources associated with the result.

Code example:

  auto result = hz.get_sql().execute("SELECT * FROM person");
  for (auto itr = result.iterator(); itr.has_next();) {
     auto page = itr.next().get();

     for (auto const &row : page->rows()) {
        // Process the row.
     }
  }
 

Usage for update count

     auto updated = hz.get_sql().execute("UPDATE ...").get().update_count();
 

You don't need to call close() in this case.

Definition at line 67 of file sql_result.h.

Constructor & Destructor Documentation

◆ ~sql_result()

hazelcast::client::sql::sql_result::~sql_result ( )
virtual

The destructor closes the result if it were open.

Definition at line 955 of file sql.cpp.

956{
957 try {
958 close().get();
959 } catch (...) {
960 // ignore
961 HZ_LOG(client_context_->get_logger(),
962 info,
963 (boost::format("[sql_result::~sql_result()] Exception while "
964 "closing the query result. Query id: %1%") %
965 query_id_)
966 .str());
967 }
968}
boost::future< void > close()
Release the resources associated with the query result.
Definition sql.cpp:901

Member Function Documentation

◆ begin()

sql_result::row_iterator_sync hazelcast::client::sql::sql_result::begin ( std::chrono::milliseconds timeout = std::chrono::milliseconds{ -1 })

Definition at line 815 of file sql.cpp.

816{
817 return row_iterator_sync{ pbegin(timeout) };
818}
Copy is allowed for convenience but it does shallow copy so it should be avoided.
Definition sql_result.h:187

◆ close()

boost::future< void > hazelcast::client::sql::sql_result::close ( )

Release the resources associated with the query result.

The query engine delivers the rows asynchronously. The query may become inactive even before all rows are consumed. The invocation of this command will cancel the execution of the query on all members if the query is still active. Otherwise it is no-op. For a result with an update count it is always no-op.

Definition at line 901 of file sql.cpp.

902{
903 if (closed_) {
904 return boost::make_ready_future();
905 }
906
907 auto release_resources = [this]() {
908 {
909 std::lock_guard<std::mutex> guard{ mtx_ };
910 closed_ = true;
911
912 connection_.reset();
913 }
914
915 row_metadata_.reset();
916 first_page_.reset();
917 };
918
919 try {
920 auto f = service_->close(connection_, query_id_);
921
922 release_resources();
923
924 return f;
925 } catch (const std::exception& e) {
926 release_resources();
927
928 service_->rethrow(e);
929 }
930
931 // This should not be reached.
932 return boost::make_ready_future();
933}

◆ end()

sql_result::row_iterator_sync hazelcast::client::sql::sql_result::end ( )

Definition at line 821 of file sql.cpp.

822{
823 return row_iterator_sync{};
824}

◆ iterator()

sql_result::page_iterator hazelcast::client::sql::sql_result::iterator ( )

Returns an iterator over the result pages.

The iterator may be requested only once.

Returns
the iterator to be used over the result. The iterator iterates page by page over the result.
Exceptions
exception::illegal_stateif the iterator is requested more than once or if this result does not have any pages.

Definition at line 783 of file sql.cpp.

784{
785 check_closed();
786
787 if (!first_page_) {
788 BOOST_THROW_EXCEPTION(exception::illegal_state(
789 "sql_result::iterator", "This result contains only update count"));
790 }
791
792 if (iterator_requested_) {
793 BOOST_THROW_EXCEPTION(exception::illegal_state(
794 "sql_result::page_iterator", "Iterator can be requested only once"));
795 }
796
797 iterator_requested_ = true;
798
799 return { shared_from_this(), first_page_ };
800}

◆ pbegin()

sql_result::page_iterator_sync hazelcast::client::sql::sql_result::pbegin ( std::chrono::milliseconds timeout = std::chrono::milliseconds{ -1 })

Definition at line 803 of file sql.cpp.

804{
805 return page_iterator_sync{ iterator(), timeout };
806}
Copy is allowed for convenience but it does shallow copy so it should be avoided.
Definition sql_result.h:114
page_iterator iterator()
Returns an iterator over the result pages.
Definition sql.cpp:783

◆ pend()

sql_result::page_iterator_sync hazelcast::client::sql::sql_result::pend ( )

Definition at line 809 of file sql.cpp.

810{
811 return page_iterator_sync{};
812}

◆ row_metadata()

const sql_row_metadata & hazelcast::client::sql::sql_result::row_metadata ( ) const

Gets the row metadata.

Returns
the metadata of the rows in this result.
Exceptions
illegal_state_exceptionif this result doesn't have rows, but only an update count

Definition at line 945 of file sql.cpp.

946{
947 if (!row_metadata_) {
948 throw exception::illegal_state(
949 "sql_result::row_metadata", "This result contains only update count");
950 }
951
952 return *row_metadata_;
953}

◆ row_set()

bool hazelcast::client::sql::sql_result::row_set ( ) const

Return whether this result has rows to iterate using the iterator() method.

Definition at line 777 of file sql.cpp.

778{
779 return update_count() == -1;
780}
int64_t update_count() const
Returns the number of rows updated by the statement or -1 if this result is a row set.
Definition sql.cpp:771

◆ update_count()

int64_t hazelcast::client::sql::sql_result::update_count ( ) const

Returns the number of rows updated by the statement or -1 if this result is a row set.

In case the result doesn't contain rows but the update count isn't applicable or known, 0 is returned.

Definition at line 771 of file sql.cpp.

772{
773 return update_count_;
774}

◆ sql_service

friend class sql_service
friend

Definition at line 308 of file sql_result.h.


The documentation for this class was generated from the following files: