Author image Kevin Ryde
and 1 contributors


App::Chart::Gtk2::Ex::ListStore::DBI -- rows from a DBI table


 use App::Chart::Gtk2::Ex::ListStore::DBI;
 my $ls = App::Chart::Gtk2::Ex::ListStore::DBI->new (dbh => $dbh,
                                         table => 'mytable',
                                         columns => ['c1','c2']);

 # changing the store updates the database
 $ls->set ($ls->get_iter_first, 0 => 'newval');

 # insert updates sequence numbers
 $ls->insert_with_values (3, 0=>'newrow');


App::Chart::Gtk2::Ex::ListStore::DBI is a subclass of Gtk2::ListStore, though perhaps in the future it'll be just a Glib::Object.



A ListStore-DBI holds data values read from a DBI table. For example

    col1  col2
    aaa   first
    bbb   another
    ccc   yet more
    ddd   blah

This is designed for use with data rows that should be kept in a given order, like a user shopping list or "to do" list.

Changes made to the ListStore-DBI in the program are immediately applied to the database. This means the database contents can be edited by the user with a Gtk2::TreeView or similar, and any programmatic changes to the model are reflected in the view too.

The current implementation is a subclass of Gtk2::ListStore because it's got a fairly reasonable set of editing functions, and it's fast when put in a TreeView.

Drag and Drop

A ListStore-DBI inherits drag-and-drop from Gtk2::ListStore but it's worth noting DnD works by inserting and deleting rows rather than a direct re-order. This means a drop will first create an empty row, so even if you normally don't want empty rows in the database you'll have to relax database constraints on that so it can be created first then filled a moment later.


App::Chart::Gtk2::Ex::ListStore::DBI->new (key => value, ...)


dbh (DBI database handle)
table (string)
columns (arrayref of strings)

The DBI handle, table name, and column names to present in the ListStore.

The "seq" column can be included in the presented data if desired, though it's value will always be the same as the row position in the ListStore, which you can get from the TreePath or TreeIter anyway.

where (hashref, default undef)

A set of column values to match in "where" clauses for the data. This allows multiple sequences to be stored in a single table, with a column value keeping them separate. The property here is a hashref of column names and values. For example,

    $ls->set (where => { flavour => 'foo' });

The table could have

    flavour  content
    foo      aaa
    foo      bbb
    foo      ccc
    foo      ddd
    bar      xxx
    bar      yyy

and only the "foo" rows are presented and edited by the ListStore-DBI.

Note that this where cannot select a subset of a sequence and attempting to do so will probably corrupt the sequential numbering.

When setting a where property must be done before setting dbh etc, or (in the current implementation) the ListStore-DBI will try to read without the where clause, which will almost certainly fail (with duplicate seq numbers).