The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Gtk2::Ex::DbLinker::DbcDataManager - a module used by Form and Datasheet that get data from a database using DBIx::Class objects

VERSION

See Version in Gtk2::Ex::DbLinker::DbTools

SYNOPSIS

        use Gtk2 -init;
        use Gtk2::GladeXML;
        use Gtk2::Ex:Linker::DbcDataManager; 

        my $builder = Gtk2::Builder->new();
        $builder->add_from_file($path_to_glade_file);

        use My::Schema;
        use Gtk2::Ex::DbLinker::DbcDataManager;

Instanciation of a DbcManager object is a two step process:

  • use a ResultSet object from the table(s) you want to display

             my $rs = $self->{schema}->resultset('Jrn'); 
  • Pass this object to the DbcDataManager constructor

             my $dbcm = Linker::DbcDataManager->new( rs => $rs );

To link the data with a Gtk window, the Gtk entries id in the glade file have to be set to the names of the database fields

          $self->{linker} = Linker::Form->new( 
                    data_manager => $dbcm,
                    builder =>  $builder,
                    rec_spinner => $self->{dnav}->get_object('RecordSpinner'),
                    status_label=>  $self->{dnav}->get_object('lbl_RecordStatus'),
                    rec_count_label => $self->{dnav}->get_object("lbl_recordCount"),
            );

To add a combo box in the form, the first field given in fields array will be used as the return value of the combo. noed is the Gtk2combo id in the glade file and the field's name in the table that received the combo values.

        my $dman = Gtk2::Ex::DbLinker::DbcDataManager->new(rs => $self->{schema}->resultset('Ed')->search_rs( undef, {order_by => ['nom']} )  );


        $self->{linker}->add_combo(
        data_manager => $dman,
        id => 'noed',
        fields => ["id", "nom"],
      );

And when all combos or datasheets are added:

      $self->{linker}->update;

To change a set of rows in a subform, use and on_changed event of the primary key in the main form and call

                $self->{subform_a}->on_pk_changed($new_primary_key_value);

In the subform a module:

        sub on_pk_changed {
                 my ($self,$value) = @_;
                # get a new ResultSet object and pass it to query
                my $rs = $self->{schema}->resultset('Table')->search_rs({FieldA=> $fieldA_value},  {order_by => 'FieldB'});
                $self->{subform_a}->get_data_manager->query($rs);
                $self->{subform_a}->update;
        }

DESCRIPTION

This module fetch data from a dabase using DBIx::Class.

METHODS

constructor

The parameter is passed as a list of parameter name => value or as a hash reference with the parameter name as key.

The unique parameter is rs.

The value for rs is a DBIx::Class::ResultSet object.

                my $rs = $self->{schema}->resultset("Table")->search_rs(undef, {order_by => 'title'});
                
                my $dman = Gtk2::Ex::DbLinker::DbcDataManager->new({ rs => $rs});

Array references of primary key names and auto incremented primary keys may also be passed using primary_keys, ai_primary_keys as hash keys. If not given the DbcDataManager uses the primary key from the Ressource object. You have to give the primary key when you use join. For example to have a list of customer's names that have passed orders, you will define a join between the table orders and customers with

        my $href =  { join => ['ComUser'] ,
                distinct => 1 ,
                order_by => ['ComUser.name', 'ComUser.givename'],
                columns => [{id_user => 'ComUser.id_user'},{name => 'ComUser.name'},{givename => 'ComUser.givenname'}],
        };
        my $table =  $self->{schema}->resultset('Order');
        my $rs = $table->search_rs({}, $href);

ComUser and ComCred are the relationships described in the ::Result::Order package. You will build a datamanager from the User and Order tables with

        my $dman = Gtk2::Ex::DbLinker::DbcDataManager->new({ 
                rs => $rs, 
                primary_keys => ['id_user'], 
                columns => {id_user => 'integer', name => 'varchar', givename => 'varchar'}, 
        });

The fields are taken from a call to $resultset-result_source> and when you join two tables this is not always the fields you are intersted in. You may pass a columns as a hash ref where the keys are the names of the fields and the values are the type (varchar, char, integer,boolean, date, serial, text, smallint, mediumint, timestamp, enum). Likewise, '+columns' with a similar hash ref will add the fields (from the join table) to those that are derived from $resultset-result_source>. Use this if you are using fields from the result_source object and fields from join clause. Use columns if you are interested only in fields from a join clause. The fields selected with '+columns' or with 'columns' are readonly: a call to set_field($field_id, $field_value) will not change any value.

query( $rs );

To display an other set of rows in a form, call the query method on the datamanager instance for this form with a new DBIx::Class::ResultSet object. Return 0 if there is no row or 1 if there are any rows found by the query.

        my $rs = $self->{schema}->resultset('Books')->search_rs({no_title => $value});
        $self->{form_a}->get_data_manager->query($rs);
        $self->{form_a}->update;

The methods belows are used by the Form module and you should not have to use them directly.

new_row();

save();

return 1 on success or 0. Check this if you implement a locking mecanism within a Result module with DBIx::Class::OptimisticLocking:

        __PACKAGE__->load_components(qw/OptimisticLocking Core/);
        __PACKAGE__->optimistic_locking_strategy('dirty'); 

This will prevent a row from being changed by two users in the same time.

delete();

set_row_pos( $new_pos );

change the current row for the row at position $new_pos.

get_row_pos();

Return the position of the current row, first one is 0.

set_field ( $field_id, $value);

Sets $value in $field_id. undef as a value will set the field to null.

get_field ( $field_id );

Return the value of a field or undef if null.

get_field_type ( $field_id );

Return one of varchar, char, integer, date, serial, boolean.

row_count();

Return the number of rows.

get_field_names();

Return an array of the field names.

get_primarykeys();

Return an array of primary key(s) (auto incremented or not).

get_autoinc_primarykeys();

Return an array of auto incremented primary key(s) from the underlying result_source or undef.

SUPPORT

Any Gk2::Ex::DbLinker questions or problems can be posted to me (rappazf) on my gmail account.

The current state of the source can be extract using Mercurial from http://sourceforge.net/projects/gtk2-ex-dblinker-dbtools/.

AUTHOR

François Rappaz <rappazf@gmail.com>

COPYRIGHT AND LICENSE

Copyright (c) 2014 by François Rappaz. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Gtk2::Ex::DbLinker::Forms

Gtk2::Ex::DbLinker::Datasheet

DBIx::Class

CREDIT

The authors of DBIx::Class !