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;        # primary key äîëæåí áûòü òîëüêî äëÿ ÷òåíèÿ
$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;
    ......
    0;
};

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';

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

DESCRIPTION

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

Edit table

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

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. Åñëè ôóíêöèÿ íå îïðåäåëåíà, íîâàÿ çàïèñü äîáàâëÿòñÿ íå áóäåò. Default undef.

-updfunc

Ðàáîòàåò àíàëîãè÷íî -insfunc, íî ñ îäíèì îòëè÷èåì: ïðè çíà÷åíèè undef âñå ðàâíî ïîçâîëÿåò èçìåíÿòü çíà÷åíèÿ â ÿ÷åéêàõ, íî ýòè èçìåíåíèÿ â áàçó íå ïåðåäàþòñÿ. Default undef.

-delfunc

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

-font

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

-cellformat

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

-fkeys

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

AUTHORS

vadim-lvv@yandex.ru

KEYWORDS

grid, 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