| 1 | package DBIx::dbMan::Extension::Transaction; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | |||
| 6 | our $VERSION = '0.10'; | ||
| 7 | |||
| 8 | 1; | ||
| 9 | |||
| 10 | sub IDENTIFICATION { return "000001-000022-000010"; } | ||
| 11 | |||
| 12 | sub preference { return 0; } | ||
| 13 | |||
| 14 | sub known_actions { return [ qw/TRANSACTION NOTIFY/ ]; } | ||
| 15 | |||
| 16 | sub init { | ||
| 17 | my $obj = shift; | ||
| 18 | |||
| 19 | $obj->{prompt_num} = $obj->{-interface}->register_prompt(1000); | ||
| 20 | $obj->{prompt_title} = $obj->{-config}->prompt_transaction || 'TRANSACTION'; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub menu { | ||
| 24 | my $obj = shift; | ||
| 25 | |||
| 26 | if ($obj->{-dbi}->current and $obj->{-dbi}->in_transaction) { | ||
| 27 | return ( { label => 'Transaction', submenu => [ | ||
| 28 | { label => 'Commit transaction', | ||
| 29 | action => { action => 'TRANSACTION', | ||
| 30 | operation => 'commit' } }, | ||
| 31 | { label => 'Rollback transaction', | ||
| 32 | action => { action => 'TRANSACTION', | ||
| 33 | operation => 'rollback' } }, | ||
| 34 | { label => 'Auto commit transaction', preference => -20, | ||
| 35 | action => { action => 'TRANSACTION', | ||
| 36 | operation => 'end' } }, | ||
| 37 | { separator => 1, preference => -10 } | ||
| 38 | ] } ); | ||
| 39 | } else { | ||
| 40 | return ( { label => 'Transaction', submenu => [ | ||
| 41 | { label => 'Begin transaction', | ||
| 42 | action => { action => 'TRANSACTION', | ||
| 43 | operation => 'begin' } } | ||
| 44 | ] } ); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | sub handle_action { | ||
| 49 | my ($obj,%action) = @_; | ||
| 50 | |||
| 51 | if ($action{action} eq 'TRANSACTION') { | ||
| 52 | if ($action{operation} =~ /^(begin|end|commit|rollback)$/) { | ||
| 53 | $action{action} = 'NONE'; | ||
| 54 | unless ($obj->{-dbi}->current) { | ||
| 55 | $obj->{-interface}->error("No current connection selected."); | ||
| 56 | return %action; | ||
| 57 | } | ||
| 58 | if ($obj->{-dbi}->in_transaction and $action{operation} eq 'begin') { | ||
| 59 | $obj->{-interface}->error('Transaction already started.'); | ||
| 60 | return %action; | ||
| 61 | } elsif (not $obj->{-dbi}->in_transaction and $action{operation} =~ /^(end|commit|rollback)$/) { | ||
| 62 | $obj->{-interface}->error('No transaction started.'); | ||
| 63 | return %action; | ||
| 64 | } | ||
| 65 | |||
| 66 | if ($action{operation} eq 'begin') { | ||
| 67 | $obj->{-dbi}->trans_begin; | ||
| 68 | $action{output} = "Transaction started.\n"; | ||
| 69 | } elsif ($action{operation} eq 'end') { | ||
| 70 | $obj->{-dbi}->rollback; | ||
| 71 | $obj->{-dbi}->trans_end; | ||
| 72 | $action{output} = "Auto commit transaction mode started with implicit rollback.\n"; | ||
| 73 | } elsif ($action{operation} eq 'commit') { | ||
| 74 | $obj->{-dbi}->commit; | ||
| 75 | $action{output} = "Transaction commited.\n"; | ||
| 76 | } elsif ($action{operation} eq 'rollback') { | ||
| 77 | $obj->{-dbi}->rollback; | ||
| 78 | $action{output} = "Transaction rolled back.\n"; | ||
| 79 | } | ||
| 80 | $action{action} = 'OUTPUT'; | ||
| 81 | $obj->{-interface}->rebuild_menu(); | ||
| 82 | } elsif ( $action{action} eq 'NOTIFY' and $action{notify} eq 'connection_change' ) { | ||
| 83 | # prompt will be applied later in this handler | ||
| 84 | } elsif ($action{operation} eq 'change') { | ||
| 85 | $action{action} = 'NONE'; | ||
| 86 | $obj->{-interface}->rebuild_menu(); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | if ($obj->{-dbi}->in_transaction) { | ||
| 91 | $obj->{-interface}->prompt($obj->{prompt_num},$obj->{prompt_title}); | ||
| 92 | } else { | ||
| 93 | $obj->{-interface}->prompt($obj->{prompt_num},''); | ||
| 94 | } | ||
| 95 | |||
| 96 | $action{processed} = 1; | ||
| 97 | return %action; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub done { | ||
| 101 | my $obj = shift; | ||
| 102 | |||
| 103 | $obj->{-interface}->deregister_prompt($obj->{prompt_num}); | ||
| 104 | |||
| 105 | for (@{$obj->{-dbi}->list('active')}) { | ||
| 106 | my $name = $_->{name}; | ||
| 107 | $obj->{-dbi}->set_current($name); # nic nevypisovat | ||
| 108 | if ($obj->{-dbi}->in_transaction()) { | ||
| 109 | $obj->{-dbi}->rollback; | ||
| 110 | $obj->{-dbi}->trans_end; | ||
| 111 | $obj->{-interface}->print("Transaction end with implicit rollback in connection $name.\n"); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } |