DBBrowser/Table/CommitWriteSQL.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 217 CC 19 SCAR 37.8
CC
19
Cov
0%
commit_sql
72.0
__transaction
72.0
__auto_commit
30.0
CRAP 44.0
1 package # hide from PAUSE
2 App::DBBrowser::Table::CommitWriteSQL;
3  
4 use warnings;
5 use strict;
6 use 5.016;
7  
8 use Term::Choose qw();
9 use Term::Choose::Util qw( insert_sep );
10 use Term::TablePrint qw();
11  
12 use App::DBBrowser::Auxil;
13 use App::DBBrowser::Table;
14  
15  
16 sub new {
17 my ( $class, $info, $options, $d ) = @_;
18 my $sf = {
19 i => $info,
20 o => $options,
21 d => $d
22 };
23 bless $sf, $class;
24 }
25  
26  
27 sub commit_sql {
28 my ( $sf, $sql ) = @_;
29 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
30 my $dbh = $sf->{d}{dbh};
31 my $waiting = 'DB work ... ';
32 my $stmt_type = $sf->{d}{stmt_types}[-1];
33 my $count_affected;
34 if ( $stmt_type eq 'Insert' ) {
35 if ( ! @{$sql->{insert_args}} ) {
36 return 1;
37 }
38 $ax->print_sql_info( $ax->get_sql_info( $sql ), $waiting );
39 $count_affected = @{$sql->{insert_args}};
40 }
41 elsif ( $stmt_type eq 'Update' or $stmt_type eq 'Delete' ) {
42 $ax->print_sql_info( $ax->get_sql_info( $sql ), $waiting );
43 my $all_arrayref = [];
44 if ( ! eval {
45 my $tbl = App::DBBrowser::Table->new( $sf->{i}, $sf->{o}, $sf->{d} );
46 $all_arrayref = $tbl->select_statement_results( $sql );
47 $count_affected = @$all_arrayref - 1;
48 1 }
49 ) {
50 $ax->print_error_message( $@ );
51 return;
52 }
53 my $prompt = "Affected records:";
54 my $info = $ax->get_sql_info( $sql );
55 if ( @$all_arrayref > 1 ) {
56 my $tp = Term::TablePrint->new( $sf->{o}{table} );
57 if ( ! $sf->{o}{G}{warnings_table_print} ) {
58 local $SIG{__WARN__} = sub {};
59 $tp->print_table( $all_arrayref, { info => $info, prompt => $prompt, footer => $sf->{d}{table_footer} } );
60 }
61 else {
62 $tp->print_table( $all_arrayref, { info => $info, prompt => $prompt, footer => $sf->{d}{table_footer} } );
63 }
64 }
65 $ax->print_sql_info( $info, $waiting );
66 }
67 else {
68 die $stmt_type // 'undefined_stmt_type';
69 }
70  
71 my $transaction;
72 eval {
73 $dbh->{AutoCommit} = 1;
74 $transaction = $dbh->begin_work;
75 # https://metacpan.org/pod/DBI#begin_work
76 # begin_work enables transactions by turning AutoCommit off until the next call to commit or rollback.
77 # If AutoCommit is already off when begin_work is called then it does nothing except return an error.
78 # If the driver does not support transactions then when begin_work attempts to set AutoCommit off the driver will trigger a fatal error.
79 } or do {
80 $dbh->{AutoCommit} = 1;
81 $transaction = 0;
82 };
83 if ( $transaction ) {
84 return $sf->__transaction( $sql, $stmt_type, $count_affected, $waiting );
85 }
86 else {
87 return $sf->__auto_commit( $sql, $stmt_type, $count_affected, $waiting );
88 }
89 }
90  
91  
92 sub __transaction {
93 my ( $sf, $sql, $stmt_type, $count_affected, $waiting ) = @_;
94 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
95 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
96 my $dbh = $sf->{d}{dbh};
97 $ax->print_sql_info( $ax->get_sql_info( $sql ), $waiting );
98 my $rolled_back;
99 my $sth;
100 if ( ! eval {
101 $sth = $dbh->prepare( $ax->get_stmt( $sql, $stmt_type, 'prepare' ) );
102 if ( $stmt_type eq 'Insert' ) {
103 $sth->execute( @$_ ) for @{$sql->{insert_args}};
104 }
105 else {
106 $sth->execute();
107 }
108 if ( $stmt_type eq 'Insert' && $sf->{d}{stmt_types}[0] eq 'Create_Table' ) {
109 # already asked for confirmation (create table + insert data) in Create_Table
110 $dbh->commit;
111 }
112 else {
113 my $commit_ok = sprintf qq( %s %s "%s"), 'COMMIT', insert_sep( $count_affected, $sf->{i}{info_thsd_sep} ), $stmt_type;
114 my $menu = [ undef, $commit_ok ];
115 my $info = $ax->get_sql_info( $sql );
116 # Choose
117 my $choice = $tc->choose(
118 $menu,
119 { %{$sf->{i}{lyt_v}}, info => $info, keep => scalar( @$menu ) }
120 );
121 $ax->print_sql_info( $info, $waiting );
122 if ( ! defined $choice || $choice ne $commit_ok ) {
123 $sth->finish if $sf->{i}{driver} eq 'SQLite';
124 $dbh->rollback;
125 $rolled_back = 1;
126 }
127 else {;
128 $dbh->commit;
129 }
130 }
131 1 }
132 ) {
133 $ax->print_error_message( $@ );
134 $sth->finish if defined $sth && $sf->{i}{driver} eq 'SQLite';
135 $dbh->rollback;
136 $rolled_back = 1;
137 }
138 if ( $rolled_back ) {
139 return;
140 }
141 return 1;
142 }
143  
144  
145 sub __auto_commit {
146 my ( $sf, $sql, $stmt_type, $count_affected, $waiting ) = @_;
147 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
148 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
149 my $dbh = $sf->{d}{dbh};
150 if ( $stmt_type eq 'Insert' && $sf->{d}{stmt_types}[0] eq 'Create_Table' ) {
151 # already asked for confirmation (create table + insert data) in Create_Table
152 }
153 else {
154 my $commit_ok = sprintf qq( %s %s "%s"), 'EXECUTE', insert_sep( $count_affected, $sf->{i}{info_thsd_sep} ), $stmt_type;
155 my $menu = [ undef, $commit_ok ];
156 my $info = $ax->get_sql_info( $sql ); #
157 # Choose
158 my $choice = $tc->choose(
159 $menu,
160 { %{$sf->{i}{lyt_v}}, info => $info, prompt => '', keep => scalar( @$menu ) }
161 );
162 $ax->print_sql_info( $info, $waiting );
163 if ( ! defined $choice || $choice ne $commit_ok ) {
164 return;
165 }
166 }
167 if ( ! eval {
168 my $sth = $dbh->prepare( $ax->get_stmt( $sql, $stmt_type, 'prepare' ) );
169 if ( $stmt_type eq 'Insert' ) {
170 $sth->execute( @$_ ) for @{$sql->{insert_args}};
171 }
172 else {
173 $sth->execute();
174 }
175 1 }
176 ) {
177 $ax->print_error_message( $@ );
178 return;
179 }
180 return 1;
181 }
182  
183  
184  
185  
186  
187 1;
188  
189  
190 __END__