| 1 | package DBIx::dbMan::Extension::EditOracle; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | use Text::FormatTable; | ||
| 6 | |||
| 7 | our $VERSION = '0.08'; | ||
| 8 | |||
| 9 | 1; | ||
| 10 | |||
| 11 | sub IDENTIFICATION { return "000001-000047-000008"; } | ||
| 12 | |||
| 13 | sub preference { return 0; } | ||
| 14 | |||
| 15 | sub known_actions { return [ qw/EDIT/ ]; } | ||
| 16 | |||
| 17 | sub handle_action { | ||
| 18 | my ($obj,%action) = @_; | ||
| 19 | |||
| 20 | $action{processed} = 1; | ||
| 21 | if ($action{action} eq 'EDIT' and $obj->{-dbi}->driver eq 'Oracle') { | ||
| 22 | if ($action{testerror}) { | ||
| 23 | $action{action} = 'NONE'; | ||
| 24 | my $d = $obj->{-dbi}->selectall_arrayref(q!SELECT line, position, text FROM user_errors WHERE name = ? AND type = ? ORDER BY sequence!,{},$action{what},$action{type}); | ||
| 25 | if (defined $d and @$d) { | ||
| 26 | $action{action} = 'OUTPUT'; | ||
| 27 | $action{output} = "I detect some errors on object $action{what} (type $action{type}):\n"; | ||
| 28 | my $tab = new Text::FormatTable '| r | r | l |'; | ||
| 29 | $tab->rule; | ||
| 30 | $tab->head('ROW','COLUMN','ERROR'); | ||
| 31 | $tab->rule; | ||
| 32 | for (@$d) { $tab->row(@$_); } | ||
| 33 | $tab->rule; | ||
| 34 | $action{output} .= $tab->render($obj->{-interface}->render_size); | ||
| 35 | } | ||
| 36 | return %action; | ||
| 37 | } | ||
| 38 | if ($action{type} =~ /^(FUNCTION|PROCEDURE|PACKAGE|PACKAGE BODY|TRIGGER|VIEW)$/) { | ||
| 39 | $action{action} = 'NONE'; | ||
| 40 | my $d; | ||
| 41 | my $lr = $obj->{-dbi}->longreadlen(); | ||
| 42 | $obj->{-dbi}->longreadlen(100000); | ||
| 43 | my $schema = ''; | ||
| 44 | $schema = uc $1 if $action{what} =~ s/^([^.]+)\.//; | ||
| 45 | my $schema_add = ''; | ||
| 46 | my @schema = (); | ||
| 47 | if ($schema) { | ||
| 48 | $schema_add = ' AND owner = ?'; | ||
| 49 | push @schema,$schema; | ||
| 50 | } | ||
| 51 | my $basetable = $schema?'all':'user'; | ||
| 52 | if ($action{type} eq 'TRIGGER') { | ||
| 53 | $d = $obj->{-dbi}->selectall_arrayref(q!SELECT description,trigger_body FROM !.$basetable.q!_triggers WHERE trigger_name = ?!.$schema_add,{},$action{what},@schema); | ||
| 54 | } elsif ($action{type} eq 'VIEW') { | ||
| 55 | $d = $obj->{-dbi}->selectall_arrayref(q!SELECT text FROM !.$basetable.q!_views WHERE view_name = ?!.$schema_add,{},$action{what},@schema); | ||
| 56 | } else { | ||
| 57 | $d = $obj->{-dbi}->selectall_arrayref(q!SELECT text FROM !.$basetable.q!_source WHERE name = ? AND type = ?!.$schema_add.q! ORDER BY line!,{},$action{what},$action{type},@schema); | ||
| 58 | } | ||
| 59 | $action{what} = "$schema.".$action{what} if $schema; | ||
| 60 | $obj->{-dbi}->longreadlen($lr); | ||
| 61 | if (defined $d and @$d) { | ||
| 62 | my $text = "CREATE OR REPLACE "; | ||
| 63 | if ($action{type} eq 'TRIGGER') { | ||
| 64 | my $desc = $d->[0][0]; | ||
| 65 | $desc =~ s/\n/ /gs; | ||
| 66 | $text .= 'TRIGGER '.$desc."\n".$d->[0][1]; | ||
| 67 | } elsif ($action{type} eq 'VIEW') { | ||
| 68 | $text .= 'VIEW '.$action{what}." AS\n".join '',map { $_->[0] } @$d; | ||
| 69 | } else { | ||
| 70 | $text .= join '',map { $_->[0] } @$d; | ||
| 71 | } | ||
| 72 | my $nt = ''; | ||
| 73 | my $started = 0; | ||
| 74 | for my $t (split /\n/,$text) { | ||
| 75 | next if $t =~ /-- \|dbMan\| /; | ||
| 76 | $t =~ s/\s+$//; | ||
| 77 | next if not $started and $t =~ /^\s*$/; | ||
| 78 | ++$started; | ||
| 79 | $nt .= "$t\n"; | ||
| 80 | } | ||
| 81 | $text = $nt; | ||
| 82 | $text =~ s/\n+$//s; | ||
| 83 | $text =~ s/\s+$//s; | ||
| 84 | $text .= ";" if $text =~ /end$/; | ||
| 85 | $text .= "\n\n"; | ||
| 86 | |||
| 87 | if ($obj->{-mempool}->get('edit_object_errors')) { | ||
| 88 | $d = $obj->{-dbi}->selectall_arrayref(q!SELECT line, position, text FROM user_errors WHERE name = ? AND type = ? ORDER BY sequence!,{},$action{what},$action{type}); | ||
| 89 | if (defined $d and @$d) { | ||
| 90 | my $tab = new Text::FormatTable '| r | r | l |'; | ||
| 91 | $tab->rule; | ||
| 92 | $tab->head('ROW','COLUMN','ERROR'); | ||
| 93 | $tab->rule; | ||
| 94 | for (@$d) { $tab->row(@$_); } | ||
| 95 | $tab->rule; | ||
| 96 | $text = "-- |dbMan| Errors in object $action{what} (type $action{type}):\n" . join("\n", map { "-- |dbMan| ".$_ } split /\n/,$tab->render($obj->{-interface}->render_size)) . "\n-- |dbMan| Note: Rows are numbered from line with CREATE OR REPLACE.\n\n" . $text; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | my $editor = $ENV{DBMAN_EDITOR} || $ENV{EDITOR} || 'vi'; | ||
| 100 | my $t = $action{type}; $t =~ s/ /_/g; | ||
| 101 | my $filename = "/tmp/dbman.edit_object.$t.$action{name}.$$.plsql"; | ||
| 102 | if (open F,">$filename") { | ||
| 103 | if ( $obj->{ -interface }->is_utf8 ) { | ||
| 104 | binmode F, ':utf8'; | ||
| 105 | } | ||
| 106 | print F $text; | ||
| 107 | close F; | ||
| 108 | $text = ''; | ||
| 109 | my $modi = -M $filename; | ||
| 110 | $obj->{-interface}->go_away(); | ||
| 111 | system "$editor $filename"; | ||
| 112 | $obj->{-interface}->come_back(); | ||
| 113 | if (-M $filename ne $modi and open F,$filename) { | ||
| 114 | if ( $obj->{ -interface }->is_utf8 ) { | ||
| 115 | binmode F, ':utf8'; | ||
| 116 | } | ||
| 117 | $text = join '',<F>; | ||
| 118 | close F; | ||
| 119 | } | ||
| 120 | unlink $filename if -e $filename; | ||
| 121 | } else { $text = ''; } | ||
| 122 | if ($text) { | ||
| 123 | my $started = 0; | ||
| 124 | my $nt = ''; | ||
| 125 | for my $t (split /\n/,$text) { | ||
| 126 | next if $t =~ /-- \|dbMan\| /; | ||
| 127 | $t =~ s/\s+$//; | ||
| 128 | next if not $started and $t =~ /^\s*$/; | ||
| 129 | ++$started; | ||
| 130 | $nt .= "$t\n"; | ||
| 131 | } | ||
| 132 | $text = $nt; | ||
| 133 | $text =~ s/\n+$//s; | ||
| 134 | $text .= "\n\n"; | ||
| 135 | |||
| 136 | $action{action} = 'OUTPUT'; | ||
| 137 | $action{output} = "I must save edited object into database.\n"; | ||
| 138 | $obj->{-interface}->add_to_actionlist({ action => 'SQL', type => 'do', sql => $text, longreadlen => 100000}); | ||
| 139 | $obj->{-interface}->add_to_actionlist({ action => 'EDIT', what => $action{what}, type => $action{type}, testerror => 1 }); | ||
| 140 | } else { | ||
| 141 | $action{action} = 'OUTPUT'; | ||
| 142 | $action{output} = "I needn't save edited object into database.\n"; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } else { | ||
| 146 | $action{action} = 'OUTPUT'; | ||
| 147 | $action{output} = "Editing of $action{type} isn't implemented yet.\n"; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | return %action; | ||
| 152 | } |