sql.pl untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 90 CC 4 SCAR 24.0
CC
4
Cov
0%
execSQL
20.0
BEGIN
2.0
BEGIN
2.0
CRAP 11.0
1 #!/usr/bin/perl -w
2
3 # MListbox demonstration application.
4
5 # Author: Hans J. Helgesen, December 1999.
6 #
7 # Before March 2000:
8 #
9 # Please send comments, suggestions and error reports to
10 # hans_helgesen@hotmail.com.
11 #
12 # From March 2000: hans.helgesen@novit.no
13 #
14 use Tk;
15 use Tk::MListbox;
16 use Tk::Pane;
17 use DBI;
18
19
20 my $intro = <<EOT;
21 This is a very simple DBI application that demonstrates the use of MListbox $Tk::MListbox::VERSION.
22
23 * To execute a query, type the query in the query window and click "GO".
24 * To resize any of the columns, drag the vertical bar to the RIGHT of the column.
25 * To move any of the columns, drag the column header left or right.
26 * To sort the table, click on any of the column headers. A new click will reverse the sort order.
27
28 Note that this program calls MListbox->insert, MListbox->see and MListbox->update once FOR EACH ROW
29 fetched from the database. This is not very efficient, a better approach would be to store all rows
30 in an array, and then call MListbox->insert once when the query is done.
31
32 EOT
33
34 my $status = 'Idle';
35
36 # Check argument.
37 if (@ARGV != 3) {
38 print STDERR "Usage: $0 source userid password\n";
39 print STDERR "Example: $0 dbi:Oracle:oradb peter secretpwd\n";
40 exit 1;
41 }
42
43 # Connect to the database.
44 my $dbh = DBI->connect(@ARGV) or die "Can't connect: $DBI::errstr\n";
45
46
47 # Create Tk window...
48 my $mw = new MainWindow;
49 $mw->title ("SQL $ARGV[1]\@$ARGV[0]");
50
51 $mw->Label(-text=>$intro,-justify=>'left')->pack(-anchor=>'w');
52
53 my $f=$mw->Frame->pack(-fill=>'x',-anchor=>'w');
54 my $text = $f->Scrolled('Text',-scrollbars=>'osoe',
55 -width=>80,-height=>5)->pack(-side=>'left',
56 -expand=>1,
57 -fill=>'both');
58
59 $text->insert('end',"select * from all_objects where object_type='TABLE'");
60 $f=$f->Frame->pack(-side=>'left');
61
62 $f->Button(-text=>'Go',
63 -command=>sub {
64 $mw->Busy(-recurse=>1);
65 execSQL();
66 $mw->Unbusy;
67 })->pack;
68
69 $f->Button(-text=>'Clear',
70 -command=>sub {
71 $text->delete('0.0','end');
72 })->pack;
73
74 $f->Button(-text=>'Exit',
75 -command=>sub {
76 $dbh->disconnect;
77 exit;
78 })->pack;
79
80 # Put the MListbox in a Pane, since the MListbox don't support horizontal
81 # scrolling by itself.
82 #
83 $f = $mw->Frame->pack(-fill=>'x');
84 $f->Label(-text=>'Status:')->pack(-side=>'left');
85 $f->Label(-textvariable=>\$status)->pack(-side=>'left');
86
87 my $ml = $mw->Scrolled('MListbox',
88 -scrollbars => 'osoe')
89 ->pack(-expand=>1,-fill=>'both');
90
91 MainLoop;
92
93 #--------------------------------------------------------------------
94 #
95 sub execSQL
96 {
97 # Get the query from the text widget.
98 my $sql = $text->get('0.0','end');
99
100 $status='Call prepare()'; $mw->update;
101
102 my $sth = $dbh->prepare($sql);
103 unless (defined $sth) {
104 $text->insert('end', "\nprepare() failed: $DBI::errstr\n");
105 return;
106 }
107 $status='Call execute()'; $mw->update;
108 unless ($sth->execute) {
109 $text->insert('end', "\nexecute() failed: $DBI::errstr\n");
110 return;
111 }
112
113 # Query OK, delete all old columns in $ml.
114 #
115 $ml->columnDelete(0,'end');
116 my $headings_defined=0;
117 $status='Call fetchrow()'; $mw->update;
118 my $rowcnt=0;
119
120 while (my $hashref = $sth->fetchrow_hashref) {
121 unless ($headings_defined) {
122 foreach (sort keys %$hashref) {
123 $ml->columnInsert('end',-text=>$_);
124 }
125 $headings_defined=1;
126 }
127 my @row=();
128 foreach (sort keys %$hashref) {
129 push @row, $hashref->{$_};
130 }
131 $ml->insert('end', [@row]);
132 $ml->see('end');
133 $rowcnt++;
134 $status="$rowcnt rows fetched";
135 $ml->update;
136 }
137 $status='Idle';
138 }
139
140
141
142
143
144