Thomas A. Lowery [[EMAIL PROTECTED]] wrote:
> I've a patch submitted to add PRECISION support, however it's not that useful
> in Pg. For any varchar type column the value is -1.
>
> Two alternatives:
>
> If column_info is supported in the current version:
> my $cols = $dbh_pg->column_info( undef, undef, undef, "column_name" );
>
> Also, perldoc DBD::Pg shows:
> my $attrs = $dbh->func($table, 'table_attributes');
Here's a little quick-and-very-dirty snippet that I put together
to use 'table_attributes' to load a hash with attributes for each
column. I'm sure this can be done better/shorter, but it's just
something I threw together quickly:
my $table = 'my_table';
my $attrib_arrayref = $dbh->func($table, 'table_attributes');
my $ct = 0;
my %columns = ();
my ($name, $type, $size, $notnull, $default, $constraint, $primary_key);
foreach my $column_hashref (@{$attrib_arrayref}) {
$ct++;
print "\n";
while (my($k,$v) = each %{$column_hashref}) {
print "k=<$k>, v=<$v>\n";
if ($k eq 'NAME') { $name = $v; }
elsif ($k eq 'TYPE') { $type = $v; }
elsif ($k eq 'SIZE') { $size = $v; }
elsif ($k eq 'NOTNULL') { $notnull = $v; }
elsif ($k eq 'DEFAULT') { $default = $v; }
elsif ($k eq 'CONSTRAINT') { $constraint = $v; }
elsif ($k eq 'PRIMARY_KEY') { $primary_key = $v; }
}
$columns{$name} = {'NAME' => "$name",
'TYPE' => "$type",
'SIZE' => "$size",
'NOTNULL' => "$notnull",
'DEFAULT' => "$default",
'CONSTRAINT' => "$constraint",
'PRIMARY_KEY' => "$primary_key",
};
}
print "Count = $ct\n";
while (my($col,$col_hashref) = each %columns) {
print "\nColumn <$col>:\n";
print " attrib NAME.........<$col_hashref->{'NAME'}>\n";
print " attrib TYPE.........<$col_hashref->{'TYPE'}>\n";
print " attrib SIZE.........<$col_hashref->{'SIZE'}>\n";
print " attrib NOTNULL......<$col_hashref->{'NOTNULL'}>\n";
print " attrib DEFAULT......<$col_hashref->{'DEFAULT'}>\n";
print " attrib CONSTRAINT...<$col_hashref->{'CONSTRAINT'}>\n";
print " attrib PRIMARY_KEY..<$col_hashref->{'PRIMARY_KEY'}>\n";
}
----------------------------------------------------------------
The output looks like this:
Column <owners>:
attrib NAME.........<owners>
attrib TYPE.........<varchar>
attrib SIZE.........<255>
attrib NOTNULL......<0>
attrib DEFAULT......<>
attrib CONSTRAINT...<>
attrib PRIMARY_KEY..<0>
Column <milestone_date>:
attrib NAME.........<milestone_date>
attrib TYPE.........<varchar>
attrib SIZE.........<255>
attrib NOTNULL......<0>
attrib DEFAULT......<>
attrib CONSTRAINT...<>
attrib PRIMARY_KEY..<0>
Hardy Merrill