NAME

Tk::DBI::DBGrid - grid äëÿ ïðîñìîòðà è ðåäàêòèðîâàíèÿ áàçû.

SYNOPSIS

use DBI;
use Tk;
use Tk::DBI::DBGrid;


my $dbh = DBI->connect( $dbhstr, $name, $pas, {AutoCommit => 0, RaiseError => 0}) or die DBI::errstr;

my $w = $MW->Toplevel(-title => 'dbgrid');

.....

my $sql = "SELECT field0, field1, field2 FROM table0";

# òîëüêî ïðîñìîò

my $grid = $w->DBGrid( -dbh => $dbh, -sql => $sql )->pack;



# ñ èñïîëüçîâàíèåì ðåäàêòèðîâàíèÿ, áåç âñòàêè è óäàëåíèÿ

my @pkey = ('field0');

my $upd = sub {
    my @row = @_;
    ..... # êàêîé-íèáóäü êîä äî update, åñëè íóæíî
    $dbh->do("UPDATE table0 SET  WHERE field0 = $row[0]");
    $dbh->commit;
    ..... # êàêîé-íèáóäü êîä ïîñëå update, åñëè íóæíî
    0;  
};

my $grid = $w->DBGrid( -dbh => $dbh, -sql => $sql, -maxrow => 10, -edit => 1, -pkey => \@pkey, -updfunc => $upd, -font => 'Courier 10' )->pack;



# with insert, delete and formating sells

my @pkey = ('field0');

my @cell;
$cell[0]{width} = 5;
$cell[0]{edit} = 0;        
$cell[0]{name} = 'title0'	# çàãîëîâîê äëÿ ïåðâîé êîëîíêè
$cell[1]{width} = undef;   # ñ÷èòûâàåòñÿ äëèíà ïîëÿ èç òàáëèöû
$cell[1]{edit} = 1;
$cell[2]{width} = 7;
$cell[2]{edit} = 1;
$cell[2]{justify} = 'left';


my $ins = sub {
    my @row = @_;
    ......
    $dbh->do("INSERT INTO table0 (field0, field1, field2) VALUES ($row[0], $row[1], $row[2])");
    $dbh->commit;
    ......
    1;
};

my $upd = sub {
    my @row = @_;
    ..... 
    $dbh->do("UPDATE table0 SET  WHERE field0 = $row[0]");
    $dbh->commit;
    ..... 
    0;
};

my $del = sub {
    my ($pkey) = @_;
    .....
    $dbh->do("DELETE FROM table0 WHERE field0 = $pkey");
    $dbh->commit;
    .....
    0;
};

my %fkey;                                                                             *
$fkey{ins} = 'Key-F5';
$fkey{del} = 'Key-F8';
$fkey{copy} = 'Control-Key-c';
$fkey{copy2} = 'Control-Key-C';
$fkey{copy3} = 'Control-Insert';

my $encd = sub {
    return decode('cp1251', $_[0]); # from module Encode
};


my $grid = $w->DBGrid( -dbh        => $dbh, 
                       -sql        => $sql, 
                       -maxrow     => 10, 
                       -edit       => 1, 
                       -pkey       => \@pkey, 
                       -insfunc    => $ins, 
                       -updfunc    => $upd, 
                       -delfunc    => $del, 
                       -titlbg     => '#f0f0f0', 
                       -seltitlbg  => 'gray',
                       -font       => 'Courier 10', 
                       -cellformat => \@cell, 
                       -fkeys      => \%fkey, 
                       -encodes    => $encd 
)->pack(-fill => 'x');

DESCRIPTION

DBGrid ïîçâîëÿåò ïðîñìàòðèâàòü/ðåäàêòèðîâàòü ëþáóþ òàáëèöó, ê êîòîðîé ìîæíî îáðàòèòüñÿ èñïîëüçóÿ çàïðîñ. Çàïðîñ ìîæåò îáúåäèíÿòü íåñêîëüêî òàáëèö.

Edit table

Äëÿ ðåäàêòèðîâàíèÿ íàäî ÷òî áû êàæäóþ çàïèñü ìîæíî áûëî îäíîçíà÷íî îïðåäåëèòü êàêèì-ëèáî ïîëåì, êîòîðîå áóäåò âûïîëíÿòü ôóíêöèþ primary key. Äëÿ ïåðåäà÷è èçìåíåíèÿ â áàçó èñïîëüçóþòñÿ ôóíêöèè, óêàçàòåëè íà êîòîðûå ïåðåäàþòñÿ â êîíñòðóêòîðå ( -insfunc, -updfunc & -delfunc ). Ñàìè ôóíêöèè îïèñûâàþòñÿ â îñíîâíîì òåêñòå ïðîãðàììû. Åñëè óêàçàòåëü íå ïåðåäàåòñÿ èëè ïåðåäàåòñÿ undef, òî ñîîòâåòñòâóþùåå äåéñòâèå íå îáðàáàòûâàåòñÿ. Åñëè â çàïðîñå áóäóò íàéäåíû êëþ÷åâûå ñëîâà ãðóïïîâûõ îïåðàöèé èëè UNION, òî DBGrid áóäåò â ðåæèìå ïðîñìîòðà ( àíàëîãè÷íî -edit => 0 ). Ïîñëå âûçîâîâ ôóíêöèé îòîáðàæàåìûå äàííûå îáíîâëÿþòñÿ åñëè âîçâðàùàåòñÿ 1.

Formating cells

Ïàðàìåòðû îòîáðàæåíèÿ êàæäîãî ïîëÿ îïðåäåëÿþòñÿ ïàðàìåòðàìè ñàìîé òàáëèöû (ñ÷èòûâàþòñÿ äëèíà ïîëÿ è òèï - òåêñòîâûé, öèñëîâîé è îñòàëüíûå). Äëÿ ïîëÿ, îïðåäåëåííîãî ÷åðåç -pkey, ðåäàêòèðîâàíèå íå äîñòóïíî. Âñå ÿ÷åéêè DBGrid - ýëåìåíòû óïðàâëåíèÿ Label èëè Entry, è ïîñëå îòîáðàæåíèÿ èìè ìîæíî óïðàâëÿòü îòäåëüíî, íàïðèìåð $grig->{rowframee}->[$i][$j]->configure( -font => 'Courier 12').

OPTIONS

-dbh

äåñêðèïòîð ñîåäèíåíèÿ ñ áàçîé äàííûé. Îáÿçàòåëüíûé ïàðàìåòð.

-sql

Çàïðîñ, ðåçóëüòàò êîòîðîãî îòîáðàæàåò DBGrig. Ðåçóëüòàòû çàïðîñà çàíîñÿòñÿ â ìàññèâ $grid->{table}->{data}->[íîìåð_çàïèñè][íîìåð_ïîëÿ]. Íóìåðàöèÿ èäåò ñ íóëÿ. Îáÿçàòåëüíûé ïàðàìåòð.

-maxrow

Ìàêñèìàëüíîå êîëè÷åñòâî çàïèñåé, îòîáðàæàåìîå íà ýêðàíå. Default 10.

-edit

Óêàçûâàåò â êàêîì ðåæèìå îòîáðàæàåòñÿ DBGrig : òîëüêî ïðîñìîòð ( 0 ) èëè ðåäàêòèðîâàíèÿ ( 1 ). Default 0.

-pkey

Äëÿ ðåæèìà ðåäàêòèðîâàíèÿ äîëæíî áûòü óêàçàíî êëþ÷åâîå ïîëå (primary key) è ñ÷èòûâàåòñÿ îíî èç ïåðâîãî ýëåìåíòà ìàññèâà, íà êîòîðûé ïåðåäàåòñÿ óêàçàòåëü. Åñëè -pkey íå îïðåäåëíåíî èëè íå íàéäåíî â çàïðîñå, òî edit = 0.

-incfunc

Óêàçàòåëü íà ôóíêöèþ, êîòîðàÿ âûçûâàåòñÿ äëÿ âñòàâêè íîâîé ñòðîêè. *  ôóíêöèþ ïåðåäàåòñÿ ìàññèâ, ñîäåðæàùèé çàïèñü äëÿ âñòàâêè â òàáëèöó. Ïîðÿäîê ïîëåé â çàïèñè òàêîé æå, êàê â çàïðîñå, ïîëó÷åííîì ÷åðåç -sql. Åñëè ôóíêöèÿ íå îïðåäåëåíà, íîâàÿ çàïèñü äîáàâëÿòñÿ íå áóäåò. Åñëè ôóíêöèÿ âîçâðàùàåò 1 (èñòèíà), òî ïîñëå åå âûçîâà îáíîâëÿþòñÿ äàííûå èç áàçû. Default undef.

-updfunc

Ðàáîòàåò àíàëîãè÷íî -insfunc, íî ñ îäíèì îòëè÷èåì: ïðè çíà÷åíèè undef * âñå ðàâíî ïîçâîëÿåò èçìåíÿòü çíà÷åíèÿ â ÿ÷åéêàõ, íî ýòè èçìåíåíèÿ â áàçó íå ïåðåäàþòñÿ. Åñëè â ôóíêöèè äàííûå íå ìîäèôèöèðóþòñÿ, òî äàííûå â êýøå ìîäóëÿ ëó÷øå íå îáíîâëÿòü (âîçâðàùàòü 0). Default undef.

-delfunc

Ðàáîòàåò àíàëîãè÷íî -insfunc.

-titlbg

Öâåò ôîíà çàãîëîâêîâ ïîëåé. Default for windows SystemButtonFace, for * other OS is #f0f0f0.

-seltitlbg

Öâåò ôîíà çàãîëîâêà àêòèâíîãî ïîëÿ. Ïî óìî÷àíèþ äëÿ windows SystemHighlight, äëÿ * îñòàëüíûõ gray.

-font

Øðèôò, èñïîëüçóåìûé â ÿ÷åéêàõ. Ò.ê. øèðèíà ýëåìåíòîâ Entry è Label çàäàåòñÿ â ñèìâîëàõ, òî äëÿ øðèôò äîëæåí áûòü monospaced, íàïðèìåð Courier. Default 'Courier 9'

-cellformat

óêàçàòåëü íà ìàññèâ, ïåðâûé ýëåìåíò êîòîðîãî óêàçûâàåò íà íîìåð ïîëÿ (íóìåðàöèÿ ñ íóëÿ), âòîðîé - ïàðàìåòð ôîðìàòèðîâàíèÿ ïîëÿ. width - øèðèíà ïîëÿ â ñèìâîëàõ, edit - äîñòóïíî ëè ðåäàêòèðîâàíèå äàííîãî ïîëÿ ïðè ðàçðåøåíèè ðåäàêòèðîâàíèè âñåé òàáëèöû, justify - öåíòðèðîâàíèå íàäïèñè (left, center, right).

-fkeys *

Óêàçàòåëü íà õýø, â êîòîðîì áóäóò õðàíèòüñÿ íîâûå êîìáàíèöèè êëàâèø äëÿ âñòàâêè è óäàëåíèÿ çàïèñåé: â ïîëå ins - âñòàâêà íîâîé çàïèñè, del - óäàëåíèå òåêóùåé, copy, copy2, copy3 - êîïèðîâàòü çàïèñü â áóôåð îáìåíà. Åñëè çíå÷åíèå -fkeys íå îïðåäåëåíî, òî äëÿ âñòàâêè íà÷çà÷àåòñÿ F5, äëÿ óäàëåíèÿ F8, äëÿ êîïèðîâàíèÿ â áóôåð - Ctrl+C and Ctrl+Ins.

-encodes *

Óêàçàòåëü íà ôóíêöèþ, êîòîðàÿ âûçûâàåòñÿ äëÿ ïåðåêîäèðîâàíèÿ èç êîäîâîé ñòðàíèöû áàçû äàííûõ â êîäîâóþ ñòðàíèöó ãðàôè÷åñêîé îáîëî÷êè, íàïðèìåð, â áàçå èñïîëüçóåòñÿ NLS_LANG=CL8MSWIN1251, à Gnome 2.x - UTF8. Åñëè ôóíêöèÿ íå îïðåäåëåíà, ïåðåêîäèðîâêà íå ïðîèçâîäèòñÿ. Default undef.

AUTHORS

Ëèõîòà Âàäèì vadim-lvv@yandex.ru

KEYWORDS

dbgrid, entry, label, database, table

1 POD Error

The following errors were encountered while parsing the POD:

Around line 3:

Non-ASCII character seen before =encoding in 'äëÿ'. Assuming CP1252