| 1 | package DBIx::dbMan::Extension::CountPre; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | use DBI ':sql_types'; | ||
| 6 | |||
| 7 | our $VERSION = '0.02'; | ||
| 8 | |||
| 9 | 1; | ||
| 10 | |||
| 11 | sub IDENTIFICATION { return "000001-000079-000002"; } | ||
| 12 | |||
| 13 | sub preference { return 300; } | ||
| 14 | |||
| 15 | sub known_actions { return [ qw/COUNT/ ]; } | ||
| 16 | |||
| 17 | sub handle_action { | ||
| 18 | my ($obj,%action) = @_; | ||
| 19 | |||
| 20 | $action{processed} = 1; | ||
| 21 | if ($action{action} eq 'COUNT' and $obj->{-dbi}->current) { | ||
| 22 | if ($action{count_re}) { | ||
| 23 | my $sth = $obj->{-dbi}->table_info(); | ||
| 24 | my $ret = $sth->fetchall_arrayref(); | ||
| 25 | study $action{count_re}; | ||
| 26 | my @tab = (); | ||
| 27 | if (defined $ret) { | ||
| 28 | for (@$ret) { | ||
| 29 | push @tab,$_->[2] if lc $_->[3] eq 'table' and | ||
| 30 | $_->[2] =~ /$action{count_re}/i; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | $action{count_tables} = \@tab; | ||
| 34 | delete $action{count_re}; | ||
| 35 | } | ||
| 36 | |||
| 37 | unless (@{$action{count_tables}}) { | ||
| 38 | $action{output_info} = $action{error_output}; | ||
| 39 | $action{action} = 'SQL_RESULT'; | ||
| 40 | $action{result} = []; | ||
| 41 | for my $table (sort keys %{$action{count_result}}) { | ||
| 42 | push @{$action{result}}, [ $table, | ||
| 43 | $action{count_result}->{$table}{rows}, | ||
| 44 | $action{count_result}->{$table}{cols} ]; | ||
| 45 | } | ||
| 46 | $action{fieldnames} = [ 'TABLE', 'LINES', 'FIELDS' ]; | ||
| 47 | $action{fieldtypes} = [ SQL_VARCHAR, SQL_INTEGER, SQL_INTEGER ]; | ||
| 48 | $action{output} = ''; | ||
| 49 | unless (@{$action{result}}) { | ||
| 50 | $action{action} = 'OUTPUT'; | ||
| 51 | $action{fieldnames} = []; | ||
| 52 | $action{fieldtypes} = []; | ||
| 53 | $action{sql} = ''; | ||
| 54 | $action{type} = ''; | ||
| 55 | } | ||
| 56 | } else { | ||
| 57 | my $table = shift @{$action{count_tables}}; | ||
| 58 | $action{action} = 'SQL'; | ||
| 59 | $action{count_process} = 1; | ||
| 60 | $action{sql} = 'SELECT COUNT(*) pocet FROM '.$table; | ||
| 61 | $action{type} = 'select'; | ||
| 62 | delete $action{processed}; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | return %action; | ||
| 67 | } |