Hazelcast C++ Client
Hazelcast C++ Client Library
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
 

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. More...
 
int64_t update_count () const
 Returns the number of rows updated by the statement or -1 if this result is a row set. More...
 
boost::future< void > close ()
 Release the resources associated with the query result. More...
 
page_iterator iterator ()
 Returns an iterator over the result pages. More...
 

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 64 of file sql_result.h.

Member Function Documentation

◆ 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 672 of file sql.cpp.

673 {
674  if (closed_) {
675  return boost::make_ready_future();
676  }
677 
678  auto release_resources = [this](){
679  {
680  std::lock_guard<std::mutex> guard{ mtx_ };
681  closed_ = true;
682 
683  connection_.reset();
684  }
685 
686  row_metadata_.reset();
687  first_page_.reset();
688  };
689 
690  try
691  {
692  auto f = service_->close(connection_, query_id_);
693 
694  release_resources();
695 
696  return f;
697  } catch (const std::exception& e) {
698  release_resources();
699 
700  service_->rethrow(e);
701  }
702 
703  // This should not be reached.
704  return boost::make_ready_future();
705 }

◆ 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 640 of file sql.cpp.

641 {
642  check_closed();
643 
644  if (!first_page_) {
645  BOOST_THROW_EXCEPTION(exception::illegal_state(
646  "sql_result::iterator", "This result contains only update count"));
647  }
648 
649  if (iterator_requested_) {
650  BOOST_THROW_EXCEPTION(exception::illegal_state(
651  "sql_result::page_iterator", "Iterator can be requested only once"));
652  }
653 
654  iterator_requested_ = true;
655 
656  return { shared_from_this(), first_page_ };
657 }

◆ 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 717 of file sql.cpp.

718 {
719  if (!row_metadata_) {
720  throw exception::illegal_state(
721  "sql_result::row_metadata", "This result contains only update count");
722  }
723 
724  return *row_metadata_;
725 }

◆ 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 628 of file sql.cpp.

629 {
630  return update_count_;
631 }

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