| 1 | package DBIx::dbMan::Extension::DescribePg; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | |||
| 6 | our $VERSION = '0.03'; | ||
| 7 | |||
| 8 | 1; | ||
| 9 | |||
| 10 | sub IDENTIFICATION { return "000001-000033-000003"; } | ||
| 11 | |||
| 12 | sub preference { return 50; } | ||
| 13 | |||
| 14 | sub known_actions { return [ qw/DESCRIBE/ ]; } | ||
| 15 | |||
| 16 | sub handle_action { | ||
| 17 | my ($obj,%action) = @_; | ||
| 18 | |||
| 19 | if ($action{action} eq 'DESCRIBE' and $obj->{-dbi}->driver eq 'Pg' and not $action{oper}) { | ||
| 20 | $action{action} = 'NONE'; | ||
| 21 | unless ($obj->{-dbi}->current) { | ||
| 22 | $obj->{-interface}->error("No current connection selected."); | ||
| 23 | return %action; | ||
| 24 | } | ||
| 25 | |||
| 26 | my $table = new Text::FormatTable '| r | l | l | r | l | l | l | l |'; | ||
| 27 | $table->rule; | ||
| 28 | $table->head('ORDER','COLUMN','TYPE','SIZE','ALIGN','BY VAL','NULLABLE','DEFAULT'); | ||
| 29 | $table->rule; | ||
| 30 | my $d = $obj->{-dbi}->selectall_arrayref( | ||
| 31 | q!SELECT a.attnum, a.attname, t.typname, a.attlen, a.attalign, | ||
| 32 | a.attbyval, a.attnotnull, a.atthasdef | ||
| 33 | FROM pg_class c, pg_attribute a, pg_type t | ||
| 34 | WHERE c.relname = ? AND a.attnum > 0 AND | ||
| 35 | a.attrelid = c.oid AND a.atttypid = t.oid | ||
| 36 | ORDER BY attnum!,{},$action{what}); | ||
| 37 | if (defined $d and @$d) { | ||
| 38 | for (@$d) { $table->row($_->[0],$_->[1],$_->[2], | ||
| 39 | ($_->[3]>0)?$_->[3]:'',$_->[4],$_->[5]?'yes':'no', | ||
| 40 | $_->[6]?'no':'yes',$_->[7]?'yes':'no'); } | ||
| 41 | } else { | ||
| 42 | $obj->{-interface}->error("Table $action{what} not found."); | ||
| 43 | return %action; | ||
| 44 | } | ||
| 45 | $table->rule; | ||
| 46 | $action{action} = 'OUTPUT'; | ||
| 47 | $action{output} = $table->render($obj->{-interface}->render_size); | ||
| 48 | } | ||
| 49 | |||
| 50 | $action{processed} = 1; | ||
| 51 | return %action; | ||
| 52 | } |