ResultSet

⬅
ResultSet

## Detailed Description

Represents a database result set.

A ResultSet is created by executing a SQL SELECT statement using Connection::executeQuery().

A ResultSet maintains a cursor pointing to its current row of data. Initially, the cursor is positioned before the first row. ResultSet::next() moves the cursor to the next row, and because it returns false when there are no more rows, it can be used in a while loop to iterate through the result set. A ResultSet is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row.

The ResultSet class provides getter methods for retrieving column values from the current row. Values can be retrieved using either the index number of the column or the name of the column. In general, using the column index will be more efficient. Columns are numbered from 1.

Column names used as input to getter methods are case sensitive. When a getter method is called with a column name and several columns have the same name, the value of the first matching column will be returned. The column name option is designed to be used when column names are used in the SQL query that generated the result set. For columns that are NOT explicitly named in the query, it is best to use column indices.

## Examples

The following examples demonstrate how to obtain a ResultSet and how to retrieve values from it.

### Example: Using column names

In this example, columns are named in the SELECT statement, and we retrieve values using the column names (we could of course also use indices if we want):

Connection con = pool.getConnection();
ResultSet result = con.executeQuery("SELECT ssn, name, photo FROM employees");
while (result.next()) {
int ssn = result.getInt("ssn");
auto name = result.getString("name");
auto photo = result.getBlob("photo");
if (photo) {
// Process photo data
}
// Process other data...
}
zdb::Connection
Represents a connection to a SQL database system.
Definition zdbpp.h:1333
zdb::Connection::executeQuery
ResultSet executeQuery(const std::string &sql, Args &&... args)
Executes a SQL query and returns a ResultSet.
Definition zdbpp.h:1563
zdb::ResultSet
Represents a database result set.
Definition zdbpp.h:590
zdb::ResultSet::getInt
int getInt(int columnIndex)
Gets the designated column's value as an int.
Definition zdbpp.h:731
zdb::ResultSet::next
bool next()
Moves the cursor to the next row.
Definition zdbpp.h:677
zdb::ResultSet::getString
std::optional< std::string_view > getString(int columnIndex)
Gets the designated column's value as a string.
Definition zdbpp.h:707
zdb::ResultSet::getBlob
std::optional< std::span< const std::byte > > getBlob(int columnIndex)
Gets the designated column's value as a byte span.
Definition zdbpp.h:795

### Example: Using column indices

This example demonstrates selecting a generated result and printing it. When the SELECT statement doesn't name the column, we use the column index to retrieve the value:

Connection con = pool.getConnection();
ResultSet r = con.executeQuery("SELECT COUNT(*) FROM employees");
if (r.next()) {
std::cout << "Number of employees: "
<< r.getString(1).value_or("none")
<< std::endl;
} else {
std::cout << "No results returned" << std::endl;
}

## Automatic type conversions

A ResultSet stores values internally as bytes and converts values on-the-fly to numeric types when requested, such as when getInt() or one of the other numeric get-methods are called. In the above example, even if count(*) returns a numeric value, we can use getString() to get the number as a string or if we choose, we can use getInt() to get the value as an integer. In the latter case, note that if the column value cannot be converted to a number, an sql_exception is thrown.

## Date and Time

ResultSet provides two principal methods for retrieving temporal column values as C types. getTimestamp() converts a SQL timestamp value to a `time_t` and getDateTime() returns a `tm structure` representing a Date, Time, DateTime, or Timestamp column type. To get a temporal column value as a string, simply use getString()

A ResultSet is reentrant, but not thread-safe and should only be used by one thread (at a time).

Note
  Remember that column indices in ResultSet are 1-based, not 0-based.

Warning
  ResultSet objects are internally managed by the Connection that created them and are not copyable or movable. Always ensure that the originating Connection object remains valid for the entire duration of the ResultSet's use. Basically, keep the Connection and ResultSet objects in the same scope. Do not attempt to use ResultSet objects (including through references or pointers) after their Connection has been closed and returned to the pool.

Represents a database result set. More...

## Public Member Functions
Properties
int | columnCount () const noexcept
| Gets the number of columns in this ResultSet.
std::optional< std::string_view > | columnName (int columnIndex) const noexcept
| Gets the designated column's name.
long | columnSize (int columnIndex)
| Gets the size of a column in bytes.
void | setFetchSize (int rows) noexcept
| Sets the number of rows to fetch from the database.
int | getFetchSize () const noexcept
| Gets the number of rows to fetch from the database.
Functions
bool | next ()
| Moves the cursor to the next row.
Columns
bool | isNull (int columnIndex)
| Checks if the designated column's value is SQL NULL.
std::optional< std::string_view > | getString (int columnIndex)
| Gets the designated column's value as a string.
std::optional< std::string_view > | getString (const std::string &columnName)
| Gets the designated column's value as a string.
int | getInt (int columnIndex)
| Gets the designated column's value as an int.
int | getInt (const std::string &columnName)
| Gets the designated column's value as an int.
long long | getLLong (int columnIndex)
| Gets the designated column's value as a long long.
long long | getLLong (const std::string &columnName)
| Gets the designated column's value as a long long.
double | getDouble (int columnIndex)
| Gets the designated column's value as a double.
double | getDouble (const std::string &columnName)
| Gets the designated column's value as a double.
std::optional< std::span< const std::byte > > | getBlob (int columnIndex)
| Gets the designated column's value as a byte span.
std::optional< std::span< const std::byte > > | getBlob (const std::string &columnName)
| Gets the designated column's value as a byte span.
Date and Time
time_t | getTimestamp (int columnIndex)
| Gets the designated column's value as a Unix timestamp.
time_t | getTimestamp (const std::string &columnName)
| Gets the designated column's value as a Unix timestamp.
tm | getDateTime (int columnIndex)
| Gets the designated column's value as a Date, Time or DateTime.
tm | getDateTime (const std::string &columnName)
| Gets the designated column's value as a Date, Time or DateTime.

## Member Function Documentation

## ◆ columnCount()

int columnCount | ( |  | ) | const
| nodiscardnoexcept

Gets the number of columns in this ResultSet.

Returns
  The number of columns.

## ◆ columnName()

std::optional< std::string_view > columnName | ( | int | columnIndex | ) | const
| nodiscardnoexcept

Gets the designated column's name.

Parameters

  columnIndex | The first column is 1, the second is 2, ...

Returns
  An optional containing the Column name, or std::nullopt if not found.

## ◆ columnSize()

long columnSize | ( | int | columnIndex | ) |
| nodiscard

Gets the size of a column in bytes.

If the column is a blob then this method returns the number of bytes in that blob. No type conversions occur. If the result is a string (or a number since a number can be converted into a string) then return the number of bytes in the resulting string.

Parameters

  columnIndex | The first column is 1, the second is 2, ...

Returns
  Column data size.

Exceptions

  sql_exception | If columnIndex is outside the valid range.

## ◆ setFetchSize()

void setFetchSize | ( | int | rows | ) |
| noexcept

Sets the number of rows to fetch from the database.

ResultSet will prefetch rows in batches of number of `rows` when next() is called to reduce the network roundtrip to the database. This method is only applicable to MySQL and Oracle.

Parameters

  rows | The number of rows to fetch (1..INT_MAX).

## ◆ getFetchSize()

int getFetchSize | ( |  | ) | const
| nodiscardnoexcept

Gets the number of rows to fetch from the database.

Unless previously set with setFetchSize(), the returned value is the same as returned by Connection::getFetchSize()

Returns
  The number of rows to fetch or 0 if N/A.

## ◆ next()

bool next | ( |  | ) |

Moves the cursor to the next row.

A ResultSet cursor is initially positioned before the first row; the first call to this method makes the first row the current row; the second call makes the second row the current row, and so on. When there are no more available rows false is returned. An empty ResultSet will return false on the first call to ResultSet::next().

Returns
  true if the new current row is valid; false if there are no more rows.

Exceptions

  sql_exception | If a database access error occurs.

## ◆ isNull()

bool isNull | ( | int | columnIndex | ) |
| nodiscard

Checks if the designated column's value is SQL NULL.

A ResultSet returns an optional for reference types and 0 for value types. Use this method if you need to differentiate between SQL NULL and std::nullopt/0.

Parameters

  columnIndex | The first column is 1, the second is 2, ...

Returns
  true if column value is SQL NULL, false otherwise.

Exceptions

  sql_exception | If a database access error occurs or columnIndex is invalid.

## ◆ getString() [1/2]

std::optional< std::string_view > getString | ( | int | columnIndex | ) |
| nodiscard

Gets the designated column's value as a string.

The returned string may only be valid until the next call to next() and if you plan to use the returned value longer, you must make a copy.

Parameters

  columnIndex | The first column is 1, the second is 2, ...

Returns
  An optional containing the column value, or std::nullopt if NULL.

Exceptions

  sql_exception | If a database access error occurs or columnIndex is invalid.

## ◆ getString() [2/2]

std::optional< std::string_view > getString | ( | const std::string & | columnName | ) |
| nodiscard

Gets the designated column's value as a string.

The returned string may only be valid until the next call to next() and if you plan to use the returned value longer, you must make a copy.

Parameters

  columnName | The SQL name of the column. case-sensitive.

Returns
  An optional containing the column value, or std::nullopt if NULL.

Exceptions

  sql_exception | If a database access error occurs or columnName does not exist.

## ◆ getInt() [1/2]

int getInt | ( | int | columnIndex | ) |
| nodiscard

Gets the designated column's value as an int.

Parameters

  columnIndex | The first column is 1, the second is 2, ...

Returns
  The column value; if the value is SQL NULL, the value returned is 0.

Exceptions

  sql_exception | If a database error occurs, columnIndex is invalid or value is NaN.

## ◆ getInt() [2/2]

int getInt | ( | const std::string & | columnName | ) |
| nodiscard

Gets the designated column's value as an int.

Parameters

  columnName | The SQL name of the column. case-sensitive.

Returns
  The column value; if the value is SQL NULL, the value returned is 0.

Exceptions

  sql_exception | If a database error occurs, columnName is invalid or value is NaN.

## ◆ getLLong() [1/2]

long long getLLong | ( | int | columnIndex | ) |
| nodiscard

Gets the designated column's value as a long long.

Parameters

  columnIndex | The first column is 1, the second is 2, ...

Returns
  The column value; if the value is SQL NULL, the value returned is 0.

Exceptions

  sql_exception | If a database error occurs, columnIndex is invalid or value is NaN.

## ◆ getLLong() [2/2]

long long getLLong | ( | const std::string & | columnName | ) |
| nodiscard

Gets the designated column's value as a long long.

Parameters

  columnName | The SQL name of the column. case-sensitive.

Returns
  The column value; if the value is SQL NULL, the value returned is 0.

Exceptions

  sql_exception | If a database error occurs, columnName is invalid or value is NaN.

## ◆ getDouble() [1/2]

double getDouble | ( | int | columnIndex | ) |
| nodiscard

Gets the designated column's value as a double.

Parameters

  columnIndex | The first column is 1, the second is 2, ...

Returns
  The column value; if the value is SQL NULL, the value returned is 0.0.

Exceptions

  sql_exception | If a database error occurs, columnIndex is invalid or value is NaN.

## ◆ getDouble() [2/2]

double getDouble | ( | const std::string & | columnName | ) |
| nodiscard

Gets the designated column's value as a double.

Parameters

  columnName | The SQL name of the column. case-sensitive.

Returns
  The column value; if the value is SQL NULL, the value returned is 0.0.

Exceptions

  sql_exception | If a database error occurs, columnName is invalid or value is NaN.

## ◆ getBlob() [1/2]

std::optional< std::span< const std::byte > > getBlob | ( | int | columnIndex | ) |
| nodiscard

Gets the designated column's value as a byte span.

The returned blob may only be valid until the next call to next() and if you plan to use the returned value longer, you must make a copy.

Parameters

  columnIndex | The first column is 1, the second is 2, ...

Returns
  An optional span of bytes containing the blob data, or std::nullopt if NULL.

Exceptions

  sql_exception | If a database access error occurs or columnIndex is invalid.

## ◆ getBlob() [2/2]

std::optional< std::span< const std::byte > > getBlob | ( | const std::string & | columnName | ) |
| nodiscard

Gets the designated column's value as a byte span.

The returned blob may only be valid until the next call to next() and if you plan to use the returned value longer, you must make a copy.

Parameters

  columnName | The SQL name of the column. case-sensitive.

Returns
  An optional span of bytes containing the blob data, or std::nullopt if NULL.

Exceptions

  sql_exception | If a database access error occurs or columnName is invalid.

## ◆ getTimestamp() [1/2]

time_t getTimestamp | ( | int | columnIndex | ) |
| nodiscard

Gets the designated column's value as a Unix timestamp.

The returned value is in Coordinated Universal Time (UTC) and represents seconds since the epoch (January 1, 1970, 00:00:00 GMT).

Even though the underlying database might support timestamp ranges before the epoch and after '2038-01-19 03:14:07 UTC' it is safest not to assume or use values outside this range. Especially on a 32-bit system.

SQLite does not have temporal SQL data types per se and using this method with SQLite assumes the column value in the Result Set to be either a numerical value representing a Unix Time in UTC which is returned as-is or an ISO 8601 time string which is converted to a `time_t` value.

Parameters

  columnIndex | The first column is 1, the second is 2, ...

Returns
  The column value as seconds since the epoch in the GMT timezone. If the value is SQL NULL, the value returned is 0.

Exceptions

  sql_exception | If a database access error occurs, columnIndex is outside the valid range or if the column value cannot be converted to a valid timestamp.

## ◆ getTimestamp() [2/2]

time_t getTimestamp | ( | const std::string & | columnName | ) |
| nodiscard

Gets the designated column's value as a Unix timestamp.

The returned value is in Coordinated Universal Time (UTC) and represents seconds since the epoch (January 1, 1970, 00:00:00 GMT).

Even though the underlying database might support timestamp ranges before the epoch and after '2038-01-19 03:14:07 UTC' it is safest not to assume or use values outside this range. Especially on a 32-bit system.

SQLite does not have temporal SQL data types per se and using this method with SQLite assumes the column value in the Result Set to be either a numerical value representing a Unix Time in UTC which is returned as-is or an ISO 8601 time string which is converted to a `time_t` value.

Parameters

  columnName | The SQL name of the column. case-sensitive.

Returns
  The column value as seconds since the epoch in the GMT timezone. If the value is SQL NULL, the value returned is 0.

Exceptions

  sql_exception | If a database access error occurs, columnName is not found or if the column value cannot be converted to a valid timestamp.

## ◆ getDateTime() [1/2]

tm getDateTime | ( | int | columnIndex | ) |
| nodiscard

Gets the designated column's value as a Date, Time or DateTime.

This method can be used to retrieve the value of columns with the SQL data type, Date, Time, DateTime or Timestamp. The returned `tm` structure follows the convention for usage with mktime(3) where:

- tm_hour = hours since midnight [0-23]
- tm_min = minutes after the hour [0-59]
- tm_sec = seconds after the minute [0-60]
- tm_mday = day of the month [1-31]
- tm_mon = months since January [0-11]

If the column value contains timezone information, tm_gmtoff is set to the offset from UTC in seconds, otherwise tm_gmtoff is set to 0. On systems without tm_gmtoff, (Solaris), the member, tm_wday is set to gmt offset instead as this property is ignored by mktime on input. The exception to the above is tm_year which contains the year literal and not years since 1900 which is the convention. All other fields in the structure are set to zero. If the column type is DateTime or Timestamp all the fields mentioned above are set, if it is a Date or a Time, only the relevant fields are set.

Parameters

  columnIndex | The first column is 1, the second is 2, ...

Returns
  A tm structure with fields for date and time. If the value is SQL NULL, a zeroed tm structure is returned. Use isNull() if in doubt.

Exceptions

  sql_exception | If a database access error occurs, columnIndex is outside the valid range or if the column value cannot be converted to a valid SQL Date, Time or DateTime type.

## ◆ getDateTime() [2/2]

tm getDateTime | ( | const std::string & | columnName | ) |
| nodiscard

Gets the designated column's value as a Date, Time or DateTime.

This method can be used to retrieve the value of columns with the SQL data type, Date, Time, DateTime or Timestamp. The returned `tm` structure follows the convention for usage with mktime(3) where:

- tm_hour = hours since midnight [0-23]
- tm_min = minutes after the hour [0-59]
- tm_sec = seconds after the minute [0-60]
- tm_mday = day of the month [1-31]
- tm_mon = months since January [0-11]

If the column value contains timezone information, tm_gmtoff is set to the offset from UTC in seconds, otherwise tm_gmtoff is set to 0. On systems without tm_gmtoff, (Solaris), the member, tm_wday is set to gmt offset instead as this property is ignored by mktime on input. The exception to the above is tm_year which contains the year literal and not years since 1900 which is the convention. All other fields in the structure are set to zero. If the column type is DateTime or Timestamp all the fields mentioned above are set, if it is a Date or a Time, only the relevant fields are set.

Parameters

  columnName | The SQL name of the column. case-sensitive.

Returns
  A tm structure with fields for date and time. If the value is SQL NULL, a zeroed tm structure is returned. Use isNull() if in doubt.

Exceptions

  sql_exception | If a database access error occurs, columnName is not found or if the column value cannot be converted to a valid Date, Time or DateTime type.

Copyright © Tildeslash Ltd. All rights reserved.