Although calling Gnome::Gda::Connection::execute_select_command()
,
is one way to receive a Gnome::Gda::DataModel
that
represents parts of a table, as shown in the example above, there are more advanced ways. These are generally provided by derived implementations of the Gnome::Gda::DataModel
base class.
For instance, there is Gnome::Gda::DataModelQuery
. Instead of immediately executing
the query once and storing the result in memory, as
Gnome::Gda::Connection::execute_select_command()
does,
Gnome::Gda::DataModelQuery
executes the query only when it is required. It keeps the connection to the database so that it can make changes.
A Gnome::Gda::DataModelQuery
is initialized with the Gnome::Gda::Query
that
should be executed to get the actual data. A Gnome::Gda::Query
in turn requires a
dictionary whose connection is used to perform the actual query. Note that you should catch any exception thrown by Query::set_sql_text()
.
Additionally, the Gnome::Gda::DataModelQuery
stores three more queries, namely
a delete-query, an insert-query and an update-query. These are used when changes
to the dataset are written into the database. These queries are set via the
Gnome::Gda::DataModelQuery::set_modification_query()
, which
automatically recognizes whether it is an INSERT, an UPDATE or a DELETE query.
In these modification queries, placeholders may be used that are
replaced by old and/or new values. Some examples for these placeholders are:
##/*:name='+0" :type="gint'*/ ##/*:name='-1" :type="gchararray'*/
Such a placeholder always begins with the sequence ##/* and ends with */. In
between, multiple key-value pairs may be specified in the form
:key="value". The special value "+<number>" is
replaced by the new value in the column in the Gnome::Gda::DataModel
with index <number>.
Likewise, "-<number>" is replaced by the old value in
the column in the Gnome::Gda::DataModel
with index <number>.
The type is specified using the type names used by glib's GObject type system. See the Listing Types from a Dict appendix for an example of how to obtain a list from the dict.
Note that in an INSERT query, only new values are available, whereas in a DELETE query, only old values make sense. Only an UPDATE query requires both old and new values, because it uses the old values to identify the record to update. Here are some more complete examples:
model->set_modification_query("INSERT INTO orders (customer) VALUES (##/*:name='+1' :type='gchararray'*/)"); model->set_modification_query("DELETE FROM orders WHERE id=##/*:name='-0' :type='gint'*/"); model->set_modification_query("UPDATE orders SET customer=##/*:name='+1' :type='gchararray'*/ WHERE id=##/*:name='-0' :type='gint'*/)");
The first example shows an INSERT query that adds a new record into the orders table, specifying the value of the customer field as the value from the second column
of the Gnome::Gda::DataModel
, which is a string (gchararray is the GObject
type name).
The second example is a DELETE query that deletes the record from the orders table whose ID matches the one that was in the first column of the DataModel. Again, gint is a GObject type name.
The third line is an example of using both old and new values together. The customer field is updated with the new value from the model but old ID is used to locate the record in the database table whose field should be changed. Remember, if the ID had also changed in the data model, we need the old ID value to identify it correctly.
For a fully-working example, refer to the Grid or Form section. The Loading a Dict from XML appendix shows how to obtain the dictionary from an XML file.