The C++ QueryResult Class

The QueryResult class is used to control the DataSource object returned by methods executeQuery() and executePreparedQuery() of classes McoSqlEngine, RemoteSqlEngine and McoMultithreadedSqlEngine.

For an overview see page C++ Classes

The QueryResult class inherits its principal methods from classes ResultSet and DataSource. These methods are listed below.

bool isNumberOfRecordsKnown()
Returns true if method nRecords() can be called successfully.
int64_t nRecords(Runtime* runtime)
Returns the number of Record objects in the result set.
Cursor* records()
Returns a Cursor over the result set.
Vector<Field>* fields()
Returns a Vector (a variable length array) of Field objects.
Field* findField(char const * name );
Finds the Field in the DataSource by name (as a char array).
Field* findField(String * name );
Finds the Field in the DataSource by name (as a String).
void extract(...)
Extracts Record components to the corresponding C struct.

Typically a QueryResult object is instantiated in a function or method that releases it on return as the QueryResult object goes out of scope. (Note that failing to release a DataSource object can lead to an application deadlock. The QueryResult class helps avoid this as the DataSource method release() is called in the QueryResult destructor which also correctly releases the DataSource in case of a thrown exception.)

Example

The most common use of the QueryResult class is illustrated in the following code snippet:

    void show_results( char * sql )
    {
        QueryResult result(engine.executeQuery(sql));

        Cursor* cursor = result->records();
        while (cursor->hasNext())
        {
            Record* rec = cursor->next();
            _Person p;
            // Extract the Person record to the corresponding struct
            result->extract(rec, &p, sizeof(p));
            printf("\t\t%d) Name=%s, Ordinal=%u\n", count, p.name, p.ordinal );
        }
    }

(Please see page Processing Query Results for further details.)