Perform/DoTable.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 115 CC 9 SCAR 40.7
CC
9
Cov
0%
generate_per
90.0
BEGIN
2.0
BEGIN
2.0
CRAP 58.6
1 # FIX_ME get a better name for this.
2 use 5.6.0;
3 package DBIx::Perform::DoTable;
4  
5 use strict;
6 use DBIx::Perform::DButils 'open_db';
7 use Exporter;
8  
9 use base 'Exporter';
10  
11 our $DB;
12 our @EXPORT_OK = qw(generate_per);
13  
14 sub generate_per
15 {
16 my $db = shift;
17 my $table_name = shift;
18  
19 $DB = open_db($db);
20 if (0) # Don't think we really need this...
21 {
22 local ($DB->{'RaiseError'},$DB->{'PrintError'}) = (1, 0);
23 my $tblh = eval {$DB->table_info('', '%', $table_name, 'TABLE') };
24 $tblh ||= eval { $DB->table_info('', "'%'", $table_name, 'TABLE')};
25 my $tblrows = $tblh->fetchall_arrayref({}); # the {} means return hashes.
26 my @tblrows = grep { $_->{'TABLE_NAME'} eq $table_name ||
27 $_->{'relname'} eq $table_name} @$tblrows;
28 if (@tblrows > 1) {
29 print STDERR "Please select one table the following:\n";
30 foreach (@tblrows) {
31 print STDERR " ", $_->{'TABLE_NAME'}, $/;
32 }
33 return undef;
34 }
35 }
36  
37 my $colh;
38 {
39 local ($DB->{'RaiseError'},$DB->{'PrintError'}) = (1, 0);
40 # Work around DBD::Pg breakage with wildcards
41 $colh = eval {$DB->column_info('', '%', $table_name, '%')} ||
42 eval {$DB->column_info('', "'%'", $table_name, "'%'")};
43 }
44 my $colrows = $colh->fetchall_arrayref({}); # {} for row hashes.
45 my @colrows = @$colrows;
46 my $maxlen = 0; # length of name, that is.
47 grep { my $l = length($_->{'COLUMN_NAME'});
48 $l > $maxlen && ($maxlen = $l) }
49 @colrows;
50 my $defsize = 75 - $maxlen;
51 my $buf = "database $db\n\nscreen\n{\n";
52 my $fxxx = "f000"; # Field name counter for roomy fields
53 my $ax = "a0"; # Field name counter for short fields
54 my $b = "b"; # Field name counter for VERY short fields.
55 my @attrs; # attributes section listings.
56 foreach my $col (@colrows) {
57 my ($cname, $size, $type) =
58 @$col{'COLUMN_NAME', 'COLUMN_SIZE', 'DATA_TYPE'};
59 if (!defined($size) || $size < 0) {
60 if ($type =~ /char/i) {
61 $size = ($$col{'atttypmod'} && $$col{'atttypmod'} - 4) ||
62 # other database-specific heuristics here...
63 $defsize;
64 }
65 elsif ($type =~ /date/i) {
66 $size = 10; # e.g. 01-02-2000
67 }
68 elsif ($type =~ /bool/i) {
69 $size = 1;
70 }
71 else {
72 $size = $defsize;
73 }
74 }
75 my $fieldname =
76 $size >= 4 ? $fxxx++ :
77 $size >= 2 ? $ax++ :
78 $b++;
79 my $fnpadding = ' ' x ($size - length($fieldname));
80 my $cnpadding = ' ' x ($maxlen - length($cname));
81 $buf .= " $cname:$cnpadding" . " [" . $fieldname . $fnpadding . "]\n";
82 push (@attrs, " $fieldname = $table_name.$cname;\n");
83 } # foreach
84 $buf .= "}\nend\n\n";
85 $buf .= "tables\n\t$table_name\n\n";
86 $buf .= "attributes\n" . join('', @attrs) . "\nend\n\n";
87 return $buf;
88 }