3.6.7 Optional Result Set Metadata (original) (raw)

3.6.7 Optional Result Set Metadata

When a client executes a statement that produces a result set, MySQL makes available the data the result set contains, and by default also result set metadata that provides information about the result set data. Metadata is contained in theMYSQL_FIELD structure (seeSection 5.2, “C API Basic Data Structures”), which is returned by the mysql_fetch_field(),mysql_fetch_field_direct(), andmysql_fetch_fields() functions.

Clients can indicate on a per-connection basis that result set metadata is optional and that the client will indicate to the server whether to return it. Suppression of metadata transfer by the client can improve performance, particularly for sessions that execute many queries that return few rows each.

There are two ways for a client to indicate that result set metadata is optional for a connection. They are equivalent, so either one suffices:

For metadata-optional connections, the client sets theresultset_metadata system variable to control whether the server returns result set metadata. Permitted values are FULL (return all metadata) and NONE (return no metadata). The default is FULL, so even for metadata-optional connections, the server by default returns metadata.

For metadata-optional connections, themysql_fetch_field(),mysql_fetch_field_direct(), andmysql_fetch_fields() functions return NULL whenresultset_metadata is set toNONE.

For connections that are not metadata-optional, settingresultset_metadata toNONE produces an error.

To check whether a result set has metadata, the client calls themysql_result_metadata() function. This function returnsRESULTSET_METADATA_FULL orRESULTSET_METADATA_NONE to indicate that the result set has full metadata or no metadata, respectively.

mysql_result_metadata() is useful if the client does not know in advance whether a result set has metadata. For example, if a client executes a stored procedure that returns multiple result sets and might change theresultset_metadata system variable, the client can invokemysql_result_metadata() for each result set to determine whether it has metadata.