Managing data models: GNOME Data Access 5 manual

## Managing data models

Each time a SELECT query is executed, it returns a GdaDataModel object, which contains methods to access the returned data. Note that GdaDataModel is in fact an interface and is implemented in very various ways depending on the database provider executing the query; however any GdaDataModel object is used the same way. Note that GdaDataModel objects are also generally used to represent an array of data where all the data in a column is of the same type.

Some data models can be modified, some can't and knowing the features of a specific data model instance can be done using the gda_data_model_get_access_flags () and gda_data_model_get_attributes_at () methods. Data models returned when SELECT queries are executed usually are not modifiable.

The value stored in each cell of a data model (at a (column, row) position) is a read-only GValue pointer. Accessing the data in a GdaDataModel can be done using two methods:

- in a random access way using gda_data_model_get_value_at () if the data model supports random access

- using a data model iterator as a cursor: a GdaDataModelIter object which is always supported by any data model.

### Example using random access

The following example displays the contents of a GdaDataModel using random access:

    void
    show_data_model (GdaDataModel *dm)
    {
              gint row_id;
              gint column_id;
              const GValue *value;

              for (column_id = 0; column_id < gda_data_model_get_n_columns (dm);
                   column_id++)
              g_print("%s\t", gda_data_model_get_column_title (dm, column_id));
              g_print("\n");

              for (row_id = 0; row_id < gda_data_model_get_n_rows (dm); row_id++) {
                        for (column_id = 0; column_id < gda_data_model_get_n_columns (dm);
                             column_id++) {
                                  char *str;

                                  value = gda_data_model_get_value_at (dm, column_id, row_id, NULL);
                                  str = gda_value_stringify (value);
                                  g_print ("%s\t", str);
                                  g_free (str);
                        }
                        g_print("\n");
              }
    }

### Example using an iterator

    void
    show_data_model (GdaDataModel *dm)
    {
              gint column_id;
              GdaDataModelIter *iter;

              for (column_id = 0; column_id < gda_data_model_get_n_columns (dm);
                   column_id++)
              g_print("%s\t", gda_data_model_get_column_title (dm, column_id));
              g_print("\n");

              iter = gda_data_model_create_iter (dm);
              gda_data_model_iter_move_next (iter);
              while (gda_data_model_iter_is_valid (iter)) {
                        GSList *list;
                        for (list = GDA_SET (iter)->holder; list; list = list->next) {
                                  str = gda_holder_get_value_str (GDA_HOLDER (list->data), NULL);
                                  g_print ("%s\t", str);
                                  g_free (str);
                        }
                        g_print("\n");
              }
              g_object_unref (iter);
    }

### Freeing data models

When you finish using data models you must free it, using g_object_unref ().