Macros | Typedefs
ResultSet.h File Reference

Detailed Description

A ResultSet represents a database result set.

A ResultSet is created by executing a SQL SELECT statement using either Connection_executeQuery() or PreparedStatement_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 interface 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.

Example

The following examples demonstrate how to obtain a ResultSet and how to get values from the set:

ResultSet_T r = Connection_executeQuery(con, "SELECT ssn, name, picture FROM employees");
while (ResultSet_next(r)) 
{
     int ssn = ResultSet_getIntByName(r, "ssn");
     const char *name =  ResultSet_getStringByName(r, "name");
     int pictureSize;
     const void *picture = ResultSet_getBlobByName(r, "picture", &pictureSize);
     [..]
}

Here is another example where a generated result is selected and printed:

ResultSet_T r = Connection_executeQuery(con, "SELECT count(*) FROM employees");
printf("Number of employees: %s\n", ResultSet_next(r) ? ResultSet_getString(r, 1) : "none");

Automatic type conversions

A ResultSet store values internally as bytes and convert values on-the-fly to numeric types when requested, such as when ResultSet_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 ResultSet_getString() to get the number as a string or if we choose, we can use ResultSet_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 SQLException is thrown.

Date and Time

Result Set provides two principal methods for retrieving temporal column values as C types. ResultSet_getTimestamp() converts a SQL timestamp value to a time_t and ResultSet_getDateTime() returns a tm structure representing a SQL Date, Time, DateTime or Timestamp column type. To get a temporal column value as a string, simply use ResultSet_getString()

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

See also
Connection.h PreparedStatement.h SQLException.h

Macros

#define T   ResultSet_T
 

Typedefs

typedef struct ResultSet_S * T
 

Functions

Properties
int ResultSet_getColumnCount (T R)
 Returns the number of columns in this ResultSet object. More...
 
const char * ResultSet_getColumnName (T R, int columnIndex)
 Get the designated column's name. More...
 
long ResultSet_getColumnSize (T R, int columnIndex)
 Returns column size in bytes. More...
 
void ResultSet_setFetchSize (T R, int rows)
 Specify the number of rows that should be fetched from the database when more rows are needed for this ResultSet. More...
 
int ResultSet_getFetchSize (T R)
 Get the number of rows that should be fetched from the database when more rows are needed for this ResultSet. More...
 
bool ResultSet_next (T R)
 Moves the cursor down one row from its current position. More...
 
Columns
bool ResultSet_isnull (T R, int columnIndex)
 Returns true if the value of the designated column in the current row of this ResultSet object is SQL NULL, otherwise false. More...
 
const char * ResultSet_getString (T R, int columnIndex)
 Retrieves the value of the designated column in the current row of this ResultSet object as a C-string. More...
 
const char * ResultSet_getStringByName (T R, const char *columnName)
 Retrieves the value of the designated column in the current row of this ResultSet object as a C-string. More...
 
int ResultSet_getInt (T R, int columnIndex)
 Retrieves the value of the designated column in the current row of this ResultSet object as an int. More...
 
int ResultSet_getIntByName (T R, const char *columnName)
 Retrieves the value of the designated column in the current row of this ResultSet object as an int. More...
 
long long ResultSet_getLLong (T R, int columnIndex)
 Retrieves the value of the designated column in the current row of this ResultSet object as a long long. More...
 
long long ResultSet_getLLongByName (T R, const char *columnName)
 Retrieves the value of the designated column in the current row of this ResultSet object as a long long. More...
 
double ResultSet_getDouble (T R, int columnIndex)
 Retrieves the value of the designated column in the current row of this ResultSet object as a double. More...
 
double ResultSet_getDoubleByName (T R, const char *columnName)
 Retrieves the value of the designated column in the current row of this ResultSet object as a double. More...
 
const void * ResultSet_getBlob (T R, int columnIndex, int *size)
 Retrieves the value of the designated column in the current row of this ResultSet object as a void pointer. More...
 
const void * ResultSet_getBlobByName (T R, const char *columnName, int *size)
 Retrieves the value of the designated column in the current row of this ResultSet object as a void pointer. More...
 
Date and Time
time_t ResultSet_getTimestamp (T R, int columnIndex)
 Retrieves the value of the designated column in the current row of this ResultSet object as a Unix timestamp. More...
 
time_t ResultSet_getTimestampByName (T R, const char *columnName)
 Retrieves the value of the designated column in the current row of this ResultSet object as a Unix timestamp. More...
 
struct tm ResultSet_getDateTime (T R, int columnIndex)
 Retrieves the value of the designated column in the current row of this ResultSet object as a Date, Time or DateTime. More...
 
struct tm ResultSet_getDateTimeByName (T R, const char *columnName)
 Retrieves the value of the designated column in the current row of this ResultSet object as a Date, Time or DateTime. More...
 

Macro Definition Documentation

◆ T

#define T   ResultSet_T

Typedef Documentation

◆ T

typedef struct ResultSet_S* T

Function Documentation

◆ ResultSet_getColumnCount()

int ResultSet_getColumnCount ( T  R)

Returns the number of columns in this ResultSet object.

Parameters
RA ResultSet object
Returns
The number of columns

◆ ResultSet_getColumnName()

const char * ResultSet_getColumnName ( T  R,
int  columnIndex 
)

Get the designated column's name.

Parameters
RA ResultSet object
columnIndexThe first column is 1, the second is 2, ...
Returns
Column name or NULL if the column does not exist. You should use the method ResultSet_getColumnCount() to test for the availability of columns in the result set.

◆ ResultSet_getColumnSize()

long ResultSet_getColumnSize ( T  R,
int  columnIndex 
)

Returns column size 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
RA ResultSet object
columnIndexThe first column is 1, the second is 2, ...
Returns
Column data size
Exceptions
SQLExceptionIf columnIndex is outside the valid range
See also
SQLException.h

◆ ResultSet_setFetchSize()

void ResultSet_setFetchSize ( T  R,
int  rows 
)

Specify the number of rows that should be fetched from the database when more rows are needed for this ResultSet.

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

Parameters
RA ResultSet object
rowsThe number of rows to fetch (1..INT.MAX)
Exceptions
SQLExceptionIf a database error occurs
AssertExceptionIf rows is less than 1
See also
Connection_setFetchSize

◆ ResultSet_getFetchSize()

int ResultSet_getFetchSize ( T  R)

Get the number of rows that should be fetched from the database when more rows are needed for this ResultSet.

Unless previously set with ResultSet_setFetchSize(), the returned value is the same as returned by Connection_getFetchSize()

Parameters
RA ResultSet object
Returns
The number of rows to fetch or 0 if N/A
See also
Connection_getFetchSize

◆ ResultSet_next()

bool ResultSet_next ( T  R)

Moves the cursor down one row from its current position.

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 not more available rows false is returned. An empty ResultSet will return false on the first call to ResultSet_next().

Parameters
RA ResultSet object
Returns
true if the new current row is valid; false if there are no more rows
Exceptions
SQLExceptionIf a database access error occurs

◆ ResultSet_isnull()

bool ResultSet_isnull ( T  R,
int  columnIndex 
)

Returns true if the value of the designated column in the current row of this ResultSet object is SQL NULL, otherwise false.

If the column value is SQL NULL, a Result Set returns the NULL pointer for reference types and 0 for value types. Use this method if you need to differ between SQL NULL and the value NULL/0.

Parameters
RA ResultSet object
columnIndexThe first column is 1, the second is 2, ...
Returns
True if column value is SQL NULL, otherwise false
Exceptions
SQLExceptionIf a database access error occurs or columnIndex is outside the valid range
See also
SQLException.h

◆ ResultSet_getString()

const char * ResultSet_getString ( T  R,
int  columnIndex 
)

Retrieves the value of the designated column in the current row of this ResultSet object as a C-string.

If columnIndex is outside the range [1..ResultSet_getColumnCount()] this method throws an SQLException. The returned string may only be valid until the next call to ResultSet_next() and if you plan to use the returned value longer, you must make a copy.

Parameters
RA ResultSet object
columnIndexThe first column is 1, the second is 2, ...
Returns
The column value; if the value is SQL NULL, the value returned is NULL
Exceptions
SQLExceptionIf a database access error occurs or columnIndex is outside the valid range
See also
SQLException.h

◆ ResultSet_getStringByName()

const char * ResultSet_getStringByName ( T  R,
const char *  columnName 
)

Retrieves the value of the designated column in the current row of this ResultSet object as a C-string.

If columnName is not found this method throws an SQLException. The returned string may only be valid until the next call to ResultSet_next() and if you plan to use the returned value longer, you must make a copy.

Parameters
RA ResultSet object
columnNameThe SQL name of the column. case-sensitive
Returns
The column value; if the value is SQL NULL, the value returned is NULL
Exceptions
SQLExceptionIf a database access error occurs or columnName does not exist
See also
SQLException.h

◆ ResultSet_getInt()

int ResultSet_getInt ( T  R,
int  columnIndex 
)

Retrieves the value of the designated column in the current row of this ResultSet object as an int.

If columnIndex is outside the range [1..ResultSet_getColumnCount()] this method throws an SQLException. In general, on both 32 and 64 bits architecture, int is 4 bytes or 32 bits and long long is 8 bytes or 64 bits. A long type is usually equal to int on 32 bits architecture and equal to long long on 64 bits architecture. However, the width of integer types are architecture and compiler dependent. The above is usually true, but not necessarily.

Parameters
RA ResultSet object
columnIndexThe first column is 1, the second is 2, ...
Returns
The column value; if the value is SQL NULL, the value returned is 0
Exceptions
SQLExceptionIf a database access error occurs, columnIndex is outside the valid range or if the value is NaN
See also
SQLException.h

◆ ResultSet_getIntByName()

int ResultSet_getIntByName ( T  R,
const char *  columnName 
)

Retrieves the value of the designated column in the current row of this ResultSet object as an int.

If columnName is not found this method throws an SQLException. In general, on both 32 and 64 bits architecture, int is 4 bytes or 32 bits and long long is 8 bytes or 64 bits. A long type is usually equal to int on 32 bits architecture and equal to long long on 64 bits architecture. However, the width of integer types are architecture and compiler dependent. The above is usually true, but not necessarily.

Parameters
RA ResultSet object
columnNameThe SQL name of the column. case-sensitive
Returns
The column value; if the value is SQL NULL, the value returned is 0
Exceptions
SQLExceptionIf a database access error occurs, columnName does not exist or if the value is NaN
See also
SQLException.h

◆ ResultSet_getLLong()

long long ResultSet_getLLong ( T  R,
int  columnIndex 
)

Retrieves the value of the designated column in the current row of this ResultSet object as a long long.

If columnIndex is outside the range [1..ResultSet_getColumnCount()] this method throws an SQLException. In general, on both 32 and 64 bits architecture, int is 4 bytes or 32 bits and long long is 8 bytes or 64 bits. A long type is usually equal to int on 32 bits architecture and equal to long long on 64 bits architecture. However, the width of integer types are architecture and compiler dependent. The above is usually true, but not necessarily.

Parameters
RA ResultSet object
columnIndexThe first column is 1, the second is 2, ...
Returns
The column value; if the value is SQL NULL, the value returned is 0
Exceptions
SQLExceptionIf a database access error occurs, columnIndex is outside the valid range or if the value is NaN
See also
SQLException.h

◆ ResultSet_getLLongByName()

long long ResultSet_getLLongByName ( T  R,
const char *  columnName 
)

Retrieves the value of the designated column in the current row of this ResultSet object as a long long.

If columnName is not found this method throws an SQLException. In general, on both 32 and 64 bits architecture, int is 4 bytes or 32 bits and long long is 8 bytes or 64 bits. A long type is usually equal to int on 32 bits architecture and equal to long long on 64 bits architecture. However, the width of integer types are architecture and compiler dependent. The above is usually true, but not necessarily.

Parameters
RA ResultSet object
columnNameThe SQL name of the column. case-sensitive
Returns
The column value; if the value is SQL NULL, the value returned is 0
Exceptions
SQLExceptionIf a database access error occurs, columnName does not exist or if the value is NaN
See also
SQLException.h

◆ ResultSet_getDouble()

double ResultSet_getDouble ( T  R,
int  columnIndex 
)

Retrieves the value of the designated column in the current row of this ResultSet object as a double.

If columnIndex is outside the range [1..ResultSet_getColumnCount()] this method throws an SQLException.

Parameters
RA ResultSet object
columnIndexThe 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
SQLExceptionIf a database access error occurs, columnIndex is outside the valid range or if the value is NaN
See also
SQLException.h

◆ ResultSet_getDoubleByName()

double ResultSet_getDoubleByName ( T  R,
const char *  columnName 
)

Retrieves the value of the designated column in the current row of this ResultSet object as a double.

If columnName is not found this method throws an SQLException.

Parameters
RA ResultSet object
columnNameThe SQL name of the column. case-sensitive
Returns
The column value; if the value is SQL NULL, the value returned is 0.0
Exceptions
SQLExceptionIf a database access error occurs, columnName does not exist or if the value is NaN
See also
SQLException.h

◆ ResultSet_getBlob()

const void * ResultSet_getBlob ( T  R,
int  columnIndex,
int *  size 
)

Retrieves the value of the designated column in the current row of this ResultSet object as a void pointer.

If columnIndex is outside the range [1..ResultSet_getColumnCount()] this method throws an SQLException. The returned blob may only be valid until the next call to ResultSet_next() and if you plan to use the returned value longer, you must make a copy.

Parameters
RA ResultSet object
columnIndexThe first column is 1, the second is 2, ...
sizeThe number of bytes in the blob is stored in size
Returns
The column value; if the value is SQL NULL, the value returned is NULL
Exceptions
SQLExceptionIf a database access error occurs or columnIndex is outside the valid range
See also
SQLException.h

◆ ResultSet_getBlobByName()

const void * ResultSet_getBlobByName ( T  R,
const char *  columnName,
int *  size 
)

Retrieves the value of the designated column in the current row of this ResultSet object as a void pointer.

If columnName is not found this method throws an SQLException. The returned blob may only be valid until the next call to ResultSet_next() and if you plan to use the returned value longer, you must make a copy.

Parameters
RA ResultSet object
columnNameThe SQL name of the column. case-sensitive
sizeThe number of bytes in the blob is stored in size
Returns
The column value; if the value is SQL NULL, the value returned is NULL
Exceptions
SQLExceptionIf a database access error occurs or columnName does not exist
See also
SQLException.h

◆ ResultSet_getTimestamp()

time_t ResultSet_getTimestamp ( T  R,
int  columnIndex 
)

Retrieves the value of the designated column in the current row of this ResultSet object as a Unix timestamp.

The returned value is in Coordinated Universal Time (UTC) and represent 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-bits system.

SQLite does not have temporal SQL data types per se and using this method with SQLite assume 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. See also PreparedStatement_setTimestamp()

Parameters
RA ResultSet object
columnIndexThe 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, i.e. January 1, 1970, 00:00:00 GMT
Exceptions
SQLExceptionIf a database access error occurs, if columnIndex is outside the range [1..ResultSet_getColumnCount()] or if the column value cannot be converted to a valid timestamp
See also
SQLException.h PreparedStatement_setTimestamp

◆ ResultSet_getTimestampByName()

time_t ResultSet_getTimestampByName ( T  R,
const char *  columnName 
)

Retrieves the value of the designated column in the current row of this ResultSet object as a Unix timestamp.

The returned value is in Coordinated Universal Time (UTC) and represent 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-bits system.

SQLite does not have temporal SQL data types per se and using this method with SQLite assume 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. See also PreparedStatement_setTimestamp()

Parameters
RA ResultSet object
columnNameThe 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, i.e. January 1, 1970, 00:00:00 GMT
Exceptions
SQLExceptionIf a database access error occurs, if columnName is not found or if the column value cannot be converted to a valid timestamp
See also
SQLException.h PreparedStatement_setTimestamp

◆ ResultSet_getDateTime()

struct tm ResultSet_getDateTime ( T  R,
int  columnIndex 
)

Retrieves the value of the designated column in the current row of this ResultSet object 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] and 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
RA ResultSet object
columnIndexThe 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 ResultSet_isnull() if in doubt.
Exceptions
SQLExceptionIf a database access error occurs, if columnIndex is outside the range [1..ResultSet_getColumnCount()] or if the column value cannot be converted to a valid SQL Date, Time or DateTime type
See also
SQLException.h

◆ ResultSet_getDateTimeByName()

struct tm ResultSet_getDateTimeByName ( T  R,
const char *  columnName 
)

Retrieves the value of the designated column in the current row of this ResultSet object 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] and 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
RA ResultSet object
columnNameThe 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 ResultSet_isnull() if in doubt.
Exceptions
SQLExceptionIf a database access error occurs, if columnName is not found or if the column value cannot be converted to a valid SQL Date, Time or DateTime type
See also
SQLException.h

Copyright © Tildeslash Ltd. All rights reserved.