| 1 | package DBIx::dbMan::Extension::Describe; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | use Term::ANSIColor; | ||
| 6 | |||
| 7 | our $VERSION = '0.07'; | ||
| 8 | |||
| 9 | 1; | ||
| 10 | |||
| 11 | sub IDENTIFICATION { return "000001-000032-000007"; } | ||
| 12 | |||
| 13 | sub preference { return 0; } | ||
| 14 | |||
| 15 | sub known_actions { return [ qw/DESCRIBE/ ]; } | ||
| 16 | |||
| 17 | sub handle_action { | ||
| 18 | my ($obj,%action) = @_; | ||
| 19 | |||
| 20 | if ($action{action} eq 'DESCRIBE') { | ||
| 21 | $action{action} = 'NONE'; | ||
| 22 | unless ($obj->{-dbi}->current) { | ||
| 23 | $obj->{-interface}->error("No current connection selected."); | ||
| 24 | return %action; | ||
| 25 | } | ||
| 26 | |||
| 27 | my $colorized = $obj->{-mempool}->get('output_format') eq 'colortable'; | ||
| 28 | |||
| 29 | if ($action{oper} eq 'complete') { | ||
| 30 | my $sth = $obj->{-dbi}->table_info(); | ||
| 31 | my $ret = $sth->fetchall_arrayref(); | ||
| 32 | my @all = (); | ||
| 33 | if (defined $ret) { | ||
| 34 | for (@$ret) { | ||
| 35 | if ($_->[3] =~ /^(table|view)$/i) { | ||
| 36 | push @all,$_->[2]; | ||
| 37 | push @all,$_->[1].'.'.$_->[2] if $_->[1]; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | $sth->finish; | ||
| 42 | $action{list} = \@all; | ||
| 43 | $action{action} = 'CACHE'; | ||
| 44 | $action{what} = 'list'; | ||
| 45 | $action{cache_type} = 'describe_std'; | ||
| 46 | $action{processed} = 1; | ||
| 47 | return %action; | ||
| 48 | } | ||
| 49 | |||
| 50 | my $table = new Text::FormatTable '| l | l | l | l | l |'; | ||
| 51 | $table->rule; | ||
| 52 | $table->head(map { $colorized ? color( $obj->{-config}->tablecolor_head || 'bright_yellow' ) . $_ . color( $obj->{-config}->tablecolor_lines || 'reset' ) : $_; } 'COLUMN','TYPE','SIZE','SCALE','NULLABLE'); | ||
| 53 | $table->rule; | ||
| 54 | |||
| 55 | my $sth = $obj->{-dbi}->prepare(q!SELECT * FROM !.$action{what}.q! WHERE 0 = 1!); | ||
| 56 | unless (defined $sth) { | ||
| 57 | $action{action} = 'OUTPUT'; | ||
| 58 | $action{output} = ''; | ||
| 59 | $obj->{-interface}->error( $obj->{-dbi}->errstr() ); | ||
| 60 | $action{processed} = 1; | ||
| 61 | return %action; | ||
| 62 | } | ||
| 63 | my $ret = $sth->execute(); | ||
| 64 | if (defined $ret) { | ||
| 65 | my @type = map { (defined $obj->{-dbi}->type_info($_)) | ||
| 66 | ? (scalar $obj->{-dbi}->type_info($_)->{TYPE_NAME}) | ||
| 67 | : $_ } @{$sth->{TYPE}}; | ||
| 68 | my @prec = @{$sth->{PRECISION}}; | ||
| 69 | my @scale = @{$sth->{SCALE}}; | ||
| 70 | my @null = @{$sth->{NULLABLE}}; | ||
| 71 | my %nullcvt = qw/0 no 1 yes 2 unknown/; | ||
| 72 | $nullcvt{''} = 'no'; | ||
| 73 | for (@{$sth->{NAME}}) { | ||
| 74 | $table->row(map { $colorized ? color( $obj->{-config}->tablecolor_content || 'bright_white' ) . $_ . color( $obj->{-config}->tablecolor_lines || 'reset' ) : $_; } $_,shift @type,shift @prec,shift @scale,$nullcvt{shift @null}); | ||
| 75 | } | ||
| 76 | } else { | ||
| 77 | $obj->{-interface}->error("Table $action{what} not found."); | ||
| 78 | return %action; | ||
| 79 | } | ||
| 80 | $sth->finish; | ||
| 81 | $table->rule; | ||
| 82 | $action{action} = 'OUTPUT'; | ||
| 83 | $action{output} = ( $colorized ? color( $obj->{-config}->tablecolor_lines || 'reset' ) : '' ) | ||
| 84 | . $table->render($obj->{-interface}->render_size) . ( $colorized ? color( 'reset' ) : '' ); | ||
| 85 | } | ||
| 86 | |||
| 87 | $action{processed} = 1; | ||
| 88 | return %action; | ||
| 89 | } |