NAME

Gtk2::Ex::DBITableFilter - A high level widget to present large amounts of data fetched using DBI. Also provides data filtering capabilities.

DESCRIPTION

If you have tons of relational data accessible using DBI, may be you would like to view them in a Gtk2 widget. The ideal widget (in most cases) is the Gtk2::TreeView or its younger cousin, the Gtk2::Ex::Simple::List.

Gtk2::Ex::DBITableFilter is a higher level widget built using Gtk2::Ex::Simple::List to achieve the following.

1. Ensure that arbitrary SQLs can be executed to fetch the data.

2. Ensure that UI does not hang while SQL is being executed.

3. Provide some kind of paging functionality. Do not display all fetched data in one shot, instead spread it into multiple pages with buttons to navigate between pages.

4. Provide some kind of data filtering capability. (Spreadsheets for example allow the user to filter the data by column using a dropdown box).

SYNOPSIS

use Gtk2::Ex::DBITableFilter;

my $mythread = Gtk2::Ex::Threads::DBI->new( {
	dsn		=> "DBI:CSV:f_dir=$datadir;csv_eol=\n;csv_sep_char=,",
	user	=> '',
	passwd	=> '',
	attr	=> { RaiseError => 1, AutoCommit => 1 }
});


my $slist = Gtk2::Ex::Simple::List->new (
	 undef			=> 'bool',
	'ID'			=> 'text',
	'Name'			=> 'text',
	'Description'	=> 'text',
	'Quantity'		=> 'int',
);

my $pagedlist = Gtk2::Ex::DBITableFilter->new($slist);
$pagedlist->make_checkable;
$pagedlist->add_filter(2, 
	[[0,'cat'], [1,'rat'], [1,'dog'], [0,'elephant'], [0,'lion'], [0,'tiger']]
);
$pagedlist->count_using(\&count_records);
$pagedlist->fetch_using(\&fetch_records);
$pagedlist->register_thread($mythread);

# --------------------------------------------- #
# Do not forget to define the callback functions
# --------------------------------------------- #
sub fetch_records {
	my ($dbh, $sqlparams) = @_;
	my $params = thaw $sqlparams;
	my $selectednames = $params->{params}->{columnfilter}->{2};
	my $selecteddescs = $params->{params}->{columnfilter}->{3};
	my $str1 = combine(@$selectednames);
	my $str2 = combine(@$selecteddescs);
	my $sth = $dbh->prepare(qq{
		select selected, id, name, description, quantity
		from table1
		where name in $str1 and description in $str2
		limit $start, $step
	});
	$sth->execute();
	my @result_array;
	while (my @ary = $sth->fetchrow_array()) {
		push @result_array, \@ary;
	}
	return \@result_array;	
}

sub count_records {
	my ($dbh, $sqlparams) = @_;
	my $params = thaw $sqlparams;
	my $selectednames = $params->{params}->{columnfilter}->{2};
	my $selecteddescs = $params->{params}->{columnfilter}->{3};
	my $str1 = combine(@$selectednames);
	my $str2 = combine(@$selecteddescs);
	my $sth = $dbh->prepare(qq{
		select count(*)
		from table1
		where name in $str1 and description in $str2
	});
	$sth->execute();
	my @result_array;
	while (my @ary = $sth->fetchrow_array()) {
		push @result_array, $ary[0];
	}
	return \@result_array;	
}

METHODS

new($slist);

Constructor accepts a Gtk2::Ex::Simple::List as the argument.

count_using(\&call_back);

Define a callback function to count the records. This call back function will be called with certain parameters. Look at the example ...

fetch_using(\&call_back);

Define a callback function to fetch the records. This call back function will be called with certain parameters. Look at the example ...

register_thread($mythread);

The $mythread object here is an instance of Gtk2::Ex::Threads::DBI

SEE ALSO

Gtk2::Ex::Threads::DBI Gtk2::Ex::ComboBox Gtk2::Ex::Simple::List

AUTHOR

Ofey Aikon, <ofey.aikon at cpan dot org>

ACKNOWLEDGEMENTS

To the wonderful gtk-perl-list.

COPYRIGHT & LICENSE

Copyright 2005 Ofey Aikon, All Rights Reserved.

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA.