Advanced GdaDataSelect usage

Whenever a SELECT statement is successfully executed (using the GdaConnection's methods), a new GdaDataSelect object is created, which can be used as any other GdaDataModel object. However this object has some extra features which are described in this section.

Automatic re-run of the SELECT statement

If the SELECT statement which has been executed contained some parameters, then the auto-reset property controls whether the GdaDataSelect object should re-run the SELECT statement to have an up-to-date contents. This feature is disabled by default but can be enabled anytime.

For example the following code (errors checking omitted for readability):

GdaStatement *stmt;
GdaSqlParser *parser = ...;
GdaDataModel *model;

stmt = gda_sql_parser_parse_string (parser,
       "SELECT * FROM customers WHERE id <= ##theid::gint",
       NULL, NULL);
gda_statement_get_parameters (stmt, &params, NULL);
gda_set_set_holder_value (params, NULL, "theid", 9);
model = gda_connection_statement_execute_select (cnc, stmt, params, NULL);
g_object_set (G_OBJECT (model), "auto-reset", TRUE, NULL);
g_object_unref (stmt);
      

would create a GdaDataSelect object (the 'model' variable) with the following contents:

id | name           | default_served_by | country | city
---+----------------+-------------------+---------+-----
 2 | Ed Lamton      |                 4 | SP      | MDR 
 3 | Lew Bonito     |                 1 | FR      | TLS 
 4 | Mark Lawrencep |                   | SP      | MDR 
 9 | Greg Popoff    |                 2 | SP      | MDR 
      

and with the following changes:

gda_set_set_holder_value (params, NULL, "theid", 4);
      

the contents of the data model will automatically be set to:

id | name           | default_served_by | country | city
---+----------------+-------------------+---------+-----
 2 | Ed Lamton      |                 4 | SP      | MDR 
 3 | Lew Bonito     |                 1 | FR      | TLS 
 4 | Mark Lawrencep |                   | SP      | MDR
      

Important note: with some database providers (such as SQLite), the column's types (if not specified when the statement is run) cannot be determined untill there is a value in the column. This means that a column's type may change over time from the GDA_TYPE_NULL type to its correct type.

Invalid parameters

If the SELECT statement which has been executed contained some parameters, and if it is not possible to give correct values to the parameters when the data model resulting from the execution of the SELECT must be created, then the execution should fail and no data model should be created (see the gda_connection_statement_execute()'s documentation). However that default behaviour can be changed using the GDA_STATEMENT_MODEL_ALLOW_NOPARAM flag: the returned data model will have no row and will automatically update its contents (re-run the SELECT statement) when parameters are changed.

The example above can be modified as follows, note that the value of the 'theid' parameter is not set which makes it invalid:

	GdaStatement *stmt;
GdaSqlParser *parser = ...;
GdaDataModel *model;

stmt = gda_sql_parser_parse_string (parser, 
       "SELECT * FROM customers WHERE id <= ##theid::gint",
        NULL, NULL);
gda_statement_get_parameters (stmt, &params, NULL);
model = gda_connection_statement_execute_select_full (cnc, stmt, params,
                                                      GDA_STATEMENT_MODEL_ALLOW_NOPARAM,
                                                      NULL, NULL);
g_object_unref (stmt);
      

The created data model contains no row, and with the following changes:

gda_set_set_holder_value (params, NULL, "theid", 4);
      

the contents of the data model will automatically be set to:

id | name           | default_served_by | country | city
---+----------------+-------------------+---------+-----
 2 | Ed Lamton      |                 4 | SP      | MDR 
 3 | Lew Bonito     |                 1 | FR      | TLS 
 4 | Mark Lawrencep |                   | SP      | MDR