| 1 | package DBIx::dbMan::Extension::StandardSQL; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | |||
| 6 | our $VERSION = '0.14'; | ||
| 7 | |||
| 8 | 1; | ||
| 9 | |||
| 10 | sub IDENTIFICATION { return "000001-000014-000014"; } | ||
| 11 | |||
| 12 | sub preference { return 100; } | ||
| 13 | |||
| 14 | sub known_actions { return [ qw/SQL/ ]; } | ||
| 15 | |||
| 16 | sub handle_action { | ||
| 17 | my ($obj,%action) = @_; | ||
| 18 | |||
| 19 | $action{processed} = 1; | ||
| 20 | if ($action{action} eq 'SQL') { | ||
| 21 | if ($action{oper} eq 'complete') { | ||
| 22 | $action{action} = 'CACHE'; | ||
| 23 | $action{type} = 'object' if lc $action{type} eq 'context'; | ||
| 24 | $action{cache_type} = 'sql_type_' . lc( $action{type} ); | ||
| 25 | |||
| 26 | if ($action{what} eq 'list') { | ||
| 27 | # return in {list} list of {type} | ||
| 28 | my $sth = $obj->{-dbi}->table_info(); | ||
| 29 | my $ret = $sth->fetchall_arrayref(); | ||
| 30 | my @all = (); | ||
| 31 | if (defined $ret) { | ||
| 32 | for (@$ret) { | ||
| 33 | push @all,$_->[2] if lc $action{type} eq 'object' || lc $action{type} eq lc $_->[3]; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | $sth->finish; | ||
| 37 | $action{list} = \@all; | ||
| 38 | } | ||
| 39 | } elsif ($action{type} eq 'select' or $action{type} eq 'do') { | ||
| 40 | $action{action} = 'NONE'; | ||
| 41 | unless ($obj->{-dbi}->current) { | ||
| 42 | $obj->{-interface}->error("No current connection selected."); | ||
| 43 | return %action; | ||
| 44 | } | ||
| 45 | |||
| 46 | my $explain_id = $$; | ||
| 47 | if ($action{explain} and not $action{explain_2phase}) { | ||
| 48 | $action{sql_save} = $action{sql}; | ||
| 49 | $action{sql} = qq!DELETE FROM plan_table WHERE statement_id = '$explain_id'!; | ||
| 50 | $action{explain_2phase} = 1; | ||
| 51 | } else { | ||
| 52 | $action{sql} =~ s/explain\s+plan\s+for/explain plan set statement_id = '$explain_id' for/i; | ||
| 53 | delete $action{explain_2phase}; | ||
| 54 | } | ||
| 55 | |||
| 56 | $obj->{-interface}->status("Executing SQL...") unless $action{output_quiet}; | ||
| 57 | my $lr = $obj->{-dbi}->longreadlen(); | ||
| 58 | $obj->{-dbi}->longreadlen($action{longreadlen}) if $action{longreadlen}; | ||
| 59 | my $sth = $obj->{-dbi}->prepare($action{sql}); | ||
| 60 | if (exists $action{placeholders}) { | ||
| 61 | my $i = 0; | ||
| 62 | $sth->bind_param(++$i,$_) for @{$action{placeholders}}; | ||
| 63 | } | ||
| 64 | unless (defined $sth) { | ||
| 65 | $action{action} = 'OUTPUT'; | ||
| 66 | $action{output} = ''; | ||
| 67 | $obj->{-interface}->error( $obj->{-dbi}->errstr() ); | ||
| 68 | $action{processed} = 1; | ||
| 69 | |||
| 70 | $obj->{-dbi}->longreadlen($lr) if $action{longreadlen}; | ||
| 71 | $obj->{-interface}->nostatus unless $action{output_quiet}; | ||
| 72 | return %action; | ||
| 73 | } | ||
| 74 | |||
| 75 | my $res = eval { | ||
| 76 | return $sth->execute(); | ||
| 77 | }; | ||
| 78 | if ( $@ ) { | ||
| 79 | $action{action} = 'OUTPUT'; | ||
| 80 | $action{output} = ''; | ||
| 81 | $action{processed} = 1; | ||
| 82 | |||
| 83 | $obj->{-interface}->error( ( $@ =~ /^Catched signal INT/ ) ? "Interrupted by user.\n" : $@ ); | ||
| 84 | |||
| 85 | $obj->{-dbi}->longreadlen($lr) if $action{longreadlen}; | ||
| 86 | $obj->{-interface}->nostatus unless $action{output_quiet}; | ||
| 87 | return %action; | ||
| 88 | } | ||
| 89 | |||
| 90 | $obj->{-dbi}->longreadlen($lr) if $action{longreadlen}; | ||
| 91 | if (not defined $res) { | ||
| 92 | my $errstr = $obj->{-dbi}->errstr(); | ||
| 93 | $errstr =~ s/^ERROR:\s*//; | ||
| 94 | $obj->{-interface}->error($errstr); | ||
| 95 | } else { | ||
| 96 | if ($action{type} eq 'select' and not $action{explain}) { | ||
| 97 | $action{fieldnames} = $sth->{NAME_uc}; | ||
| 98 | eval { | ||
| 99 | $action{fieldtypes} = $sth->{TYPE}; | ||
| 100 | }; | ||
| 101 | if ($@) { | ||
| 102 | $action{fieldtypes} = [ map { -9998 } @{$action{fieldnames}} ]; | ||
| 103 | } | ||
| 104 | $res = eval { | ||
| 105 | my $result = []; | ||
| 106 | while ( my $row = $sth->fetchrow_arrayref() ) { | ||
| 107 | push @$result, [ @$row ]; | ||
| 108 | } | ||
| 109 | return $result; | ||
| 110 | }; | ||
| 111 | if ( $@ ) { | ||
| 112 | $sth->finish; | ||
| 113 | |||
| 114 | $action{action} = 'OUTPUT'; | ||
| 115 | $action{output} = ''; | ||
| 116 | $action{processed} = 1; | ||
| 117 | |||
| 118 | $obj->{-interface}->error( ( $@ =~ /^Catched signal INT/ ) ? "Interrupted by user.\n" : $@ ); | ||
| 119 | |||
| 120 | $obj->{-dbi}->longreadlen($lr) if $action{longreadlen}; | ||
| 121 | $obj->{-interface}->nostatus unless $action{output_quiet}; | ||
| 122 | return %action; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | if ($action{explain}) { | ||
| 126 | $action{action} = 'SQL'; | ||
| 127 | if ($action{explain_2phase}) { | ||
| 128 | $action{sql} = $action{sql_save}; | ||
| 129 | $sth->finish; | ||
| 130 | delete $action{processed}; | ||
| 131 | $obj->{-interface}->nostatus unless $action{output_quiet}; | ||
| 132 | return %action; | ||
| 133 | } | ||
| 134 | my $local_mempool = $obj->{-dbi}->mempool(); | ||
| 135 | if ( $local_mempool && $local_mempool->get( 'oracle_special_xplan' ) ) { | ||
| 136 | $action{sql} = q!SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY('plan_table',!.$explain_id.'))'; | ||
| 137 | } else { | ||
| 138 | $action{sql} = q!SELECT '.' || LPAD(' ',2*LEVEL-1) || operation || ' ' || options || ' ' || object_name "Execution Plan" FROM plan_table WHERE statement_id = '!.$explain_id.q!' CONNECT BY PRIOR id = parent_id AND statement_id = '!.$explain_id.q!' START WITH id = 0 AND statement_id = '!.$explain_id.q!'!; | ||
| 139 | } | ||
| 140 | delete $action{explain}; | ||
| 141 | } else { | ||
| 142 | $action{action} = 'SQL_RESULT'; | ||
| 143 | $action{result} = $res; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | $sth->finish; | ||
| 147 | $obj->{-interface}->nostatus unless $action{output_quiet}; | ||
| 148 | $obj->{-dbi}->discard_profile_data; | ||
| 149 | delete $action{processed}; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | return %action; | ||
| 154 | } |