DBBrowser/Table/InsertUpdateDelete.pm untested

filter: Statementstmt 0.0% 0 / 184 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 310 CC 37 SCAR 56.5
CC
37
Cov
0%
table_write_access
552.0
__first_column_is_autoincrement
132.0
__build_insert_stmt
12.0
CRAP 284.8
1 package # hide from PAUSE
2 App::DBBrowser::Table::InsertUpdateDelete;
3  
4 use warnings;
5 use strict;
6 use 5.016;
7  
8 use Term::Choose qw();
9 use Term::Choose::Util qw();
10  
11 use App::DBBrowser::Auxil;
12 use App::DBBrowser::GetContent;
13 use App::DBBrowser::Table::CommitWriteSQL;
14 use App::DBBrowser::Table::Substatement;
15  
16  
17 sub new {
18 my ( $class, $info, $options, $d ) = @_;
19 my $sf = {
20 i => $info,
21 o => $options,
22 d => $d
23 };
24 bless $sf, $class;
25 }
26  
27  
28 sub table_write_access {
29 my ( $sf, $sql ) = @_;
30 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
31 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
32 my $cs = App::DBBrowser::Table::CommitWriteSQL->new( $sf->{i}, $sf->{o}, $sf->{d} );
33 my $sb = App::DBBrowser::Table::Substatement->new( $sf->{i}, $sf->{o}, $sf->{d} );
34 my $dbms = $sf->{i}{dbms};
35 my @stmt_types;
36 if ( $sf->{d}{table_origin} eq 'ordinary' ) {
37 push @stmt_types, 'Insert' if $sf->{o}{enable}{insert_into};
38 push @stmt_types, 'Update' if $sf->{o}{enable}{update};
39 push @stmt_types, 'Delete' if $sf->{o}{enable}{delete};
40 }
41 elsif ( $sf->{d}{table_origin} eq 'join' && $dbms =~ /^(?:mysql|MariaDB)\z/ ) {
42 push @stmt_types, 'Update' if $sf->{o}{enable}{update};
43 }
44 if ( ! @stmt_types ) {
45 return;
46 }
47 my $old_idx_type = 0;
48  
49 STMT_TYPE: while ( 1 ) {
50 my $prompt = 'Choose SQL type:';
51 my @pre = ( undef );
52 my $menu = [ @pre, map( "- $_", @stmt_types ) ];
53 # Choose
54 my $idx = $tc->choose(
55 $menu,
56 { %{$sf->{i}{lyt_v}}, prompt => $prompt, index => 1, default => $old_idx_type }
57 );
58 if ( ! defined $idx || ! defined $menu->[$idx] ) {
59 return;
60 }
61 if ( $sf->{o}{G}{menu_memory} ) {
62 if ( $old_idx_type == $idx && ! $ENV{TC_RESET_AUTO_UP} ) {
63 $old_idx_type = 0;
64 next STMT_TYPE;
65 }
66 $old_idx_type = $idx;
67 }
68 my $stmt_type = $menu->[$idx];
69 $stmt_type =~ s/^-\ //;
70 $sf->{d}{stmt_types} = [ $stmt_type ];
71  
72 $ax->reset_sql( $sql ); # Reset SQL
73  
74 if ( $stmt_type eq 'Insert' || $dbms eq 'SQLite' ) {
75 #if ( $sf->{d}{table_origin} eq 'ordinary' ) {
76 # use tablename without alias:
77 my $table_key = $sf->{d}{table_key};
78 $sql->{table} = $ax->qq_table( $sf->{d}{tables_info}{$table_key} );
79 #}
80 }
81 if ( $stmt_type eq 'Insert' ) {
82 my $ok = $sf->__build_insert_stmt( $sql );
83 if ( $ok ) {
84 $cs->commit_sql( $sql );
85 }
86 next STMT_TYPE;
87 }
88 my $sub_stmts = {
89 Delete => [ qw( commit where ) ],
90 Update => [ qw( commit set where ) ],
91 };
92 my %cu = (
93 commit => ' CONFIRM Stmt',
94 set => '- SET',
95 where => '- WHERE',
96 );
97 my $old_idx_cust = 0;
98  
99 CUSTOMIZE: while ( 1 ) {
100 my $menu = [ undef, @cu{@{$sub_stmts->{$stmt_type}}} ];
101 my $info = $ax->get_sql_info( $sql );
102 # Choose
103 my $idx = $tc->choose(
104 $menu,
105 { %{$sf->{i}{lyt_v}}, info => $info, prompt => 'Customize:', index => 1, default => $old_idx_cust }
106 );
107 $ax->print_sql_info( $info );
108 if ( ! defined $idx || ! defined $menu->[$idx] ) {
109 next STMT_TYPE;
110 }
111 if ( $sf->{o}{G}{menu_memory} ) {
112 if ( $old_idx_cust == $idx && ! $ENV{TC_RESET_AUTO_UP} ) {
113 $old_idx_cust = 0;
114 next CUSTOMIZE;
115 }
116 $old_idx_cust = $idx;
117 }
118 my $custom = $menu->[$idx];
119 if ( $custom eq $cu{'set'} ) {
120 $sb->set( $sql );
121 }
122 elsif ( $custom eq $cu{'where'} ) {
123 $sb->where( $sql );
124 }
125 elsif ( $custom eq $cu{'commit'} ) {
126 if ( $stmt_type =~/^(Delete|Update)\z/i && ! length $sql->{where_stmt} ) {
127 my ( $no, $yes ) = ( '- NO', '- YES' );
128 my $prompt = uc( $stmt_type ) . ' without WHERE clause?';
129 my $menu = [ undef, $no, $yes ];
130 # Choose
131 my $choice = $tc->choose(
132 $menu,
133 { %{$sf->{i}{lyt_v}}, info => $info, prompt => $prompt, undef => ' <=' }
134 );
135 if ( ! defined $choice ) {
136 next STMT_TYPE;
137 }
138 elsif ( $choice eq $no ) {
139 $old_idx_cust = 0;
140 next CUSTOMIZE;
141 }
142 }
143 $cs->commit_sql( $sql );
144 next STMT_TYPE;
145 }
146 else {
147 die "$custom: no such value in the hash \%cu";
148 }
149 }
150 }
151 }
152  
153  
154 sub __build_insert_stmt {
155 my ( $sf, $sql ) = @_;
156 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
157 my $gc = App::DBBrowser::GetContent->new( $sf->{i}, $sf->{o}, $sf->{d} );
158 $ax->reset_sql( $sql );
159 my $source = {};
160  
161 REQUIRED_COLS: while ( 1 ) {
162 my $cols_ok = $sf->__insert_into_stmt_columns( $sql );
163 if ( ! $cols_ok ) {
164 return;
165 }
166 my $ok = $gc->get_content( $sql, $source, 0 );
167 if ( ! $ok ) {
168 next REQUIRED_COLS;
169 }
170 return 1;
171 }
172 }
173  
174  
175 sub __insert_into_stmt_columns {
176 my ( $sf, $sql ) = @_;
177 my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} );
178 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
179 $sql->{insert_col_names} = [];
180 my @cols = ( @{$sql->{columns}} );
181 if ( $sf->__first_column_is_autoincrement( $sql ) ) {
182 shift @cols;
183 }
184 my $prompt = "Select columns to fill:";
185 my $info = $sql->{table} . "\n";
186 my $idxs = $tu->choose_a_subset(
187 [ @cols ],
188 { cs_label => 'Cols: ', layout => 0, order => 0, all_by_default => 1,
189 index => 1, confirm => $sf->{i}{ok}, back => '<<', info => $info, prompt => $prompt }
190 );
191 $ax->print_sql_info( $info );
192 if ( ! defined $idxs ) {
193 return;
194 }
195 $sql->{insert_col_names} = [ @cols[@$idxs] ];
196 return 1;
197 }
198  
199  
200 sub __first_column_is_autoincrement { ##
201 my ( $sf, $sql ) = @_;
202 my $dbh = $sf->{d}{dbh};
203 my $schema = $sf->{d}{schema};
204 my $nq_table = $sf->{d}{tables_info}{$sf->{d}{table_key}}[2];
205 my $dbms = $sf->{i}{dbms};
206 if ( $dbms eq 'SQLite' ) {
207 if ( $sf->{d}{db_attached} ) {
208 # return if a table is from an attached database
209 my $main = 'main';
210 my $qt_main = $dbh->quote_identifier( $main );
211 if ( $sql->{table} !~ /^(?:$main|$qt_main)\./i ) {
212 return;
213 }
214 }
215 my $stmt = "SELECT sql FROM sqlite_master WHERE name = ?";
216 my ( $row ) = $sf->{d}{dbh}->selectrow_array( $stmt, {}, $nq_table );
217 my $sth = $dbh->prepare( "SELECT * FROM " . $sql->{table} . " LIMIT 0" );
218 my $nq_col = $sth->{NAME}[0];
219 my $col = $dbh->quote_identifier( $nq_col );
220 my $qt_table = $dbh->quote_identifier( $nq_table ); # quoted but not qualified
221 if ( $row =~ /^\s*CREATE\s+TABLE\s+(?:\Q$nq_table\E|\Q$qt_table\E)\s+\(\s*(?:\Q$nq_col\E|\Q$col\E)\s+INTEGER\s+PRIMARY\s+KEY[^,]*,/i ) {
222 return 1;
223 }
224 }
225 elsif ( $dbms =~ /^(?:mysql|MariaDB)\z/ ) {
226 my $stmt = "SELECT COUNT(*) FROM information_schema.columns WHERE
227 TABLE_SCHEMA = ?
228 AND TABLE_NAME = ?
229 AND ORDINAL_POSITION = 1
230 AND DATA_TYPE = 'int'
231 AND COLUMN_DEFAULT IS NULL
232 AND IS_NULLABLE = 'NO'
233 AND EXTRA like '%auto_increment%'";
234 my ( $first_col_is_autoincrement ) = $dbh->selectrow_array( $stmt, {}, $schema, $nq_table );
235 return $first_col_is_autoincrement;
236 }
237 elsif ( $dbms eq 'Pg' ) {
238 my $stmt = "SELECT COUNT(*) FROM information_schema.columns WHERE
239 TABLE_SCHEMA = ?
240 AND TABLE_NAME = ?
241 AND ORDINAL_POSITION = 1
242 AND DATA_TYPE = 'integer'
243 AND IS_NULLABLE = 'NO'
244 AND (
245 UPPER(column_default) LIKE 'NEXTVAL%'
246 OR UPPER(identity_generation) = 'BY DEFAULT'
247 )";
248 my ( $first_col_is_autoincrement ) = $dbh->selectrow_array( $stmt, {}, $schema, $nq_table );
249 return $first_col_is_autoincrement;
250 }
251 elsif ( $dbms eq 'Firebird' ) {
252 my $stmt = "SELECT COUNT(*) FROM RDB\$RELATION_FIELDS WHERE
253 RDB\$RELATION_NAME = ?
254 AND RDB\$FIELD_POSITION = 0
255 AND (
256 RDB\$IDENTITY_TYPE = 0
257 OR RDB\$IDENTITY_TYPE = 1
258 )";
259 my ( $first_col_is_autoincrement ) = $dbh->selectrow_array( $stmt, {}, $nq_table );
260 return $first_col_is_autoincrement;
261 }
262 elsif ( $dbms eq 'DB2' ) {
263 my $stmt = "SELECT COUNT(*) FROM SYSCAT.COLUMNS WHERE
264 TABSCHEMA = ?
265 AND TABNAME = ?
266 AND COLNO = 0
267 AND TYPENAME = 'INTEGER'
268 AND NULLS = 'N'
269 AND KEYSEQ = 1
270 AND GENERATED = 'A'
271 AND IDENTITY = 'Y'";
272 my ( $first_col_is_autoincrement ) = $dbh->selectrow_array( $stmt, {}, $schema, $nq_table );
273 return $first_col_is_autoincrement;
274 }
275 elsif ( $dbms eq 'Oracle' ) {
276 my $stmt = "SELECT COUNT(*) FROM SYS.ALL_TAB_COLUMNS WHERE
277 OWNER = ?
278 AND TABLE_NAME = ?
279 AND DATA_TYPE = 'NUMBER'
280 AND NULLABLE = 'N'
281 AND COLUMN_ID = 1
282 AND IDENTITY_COLUMN = 'YES'";
283 my ( $first_col_is_autoincrement ) = $dbh->selectrow_array( $stmt, {}, $schema, $nq_table );
284 return $first_col_is_autoincrement;
285 }
286 elsif ( $dbms eq 'MSSQL' ) {
287 my $stmt = "SELECT COUNT(*) FROM sys.columns WHERE
288 object_id = OBJECT_ID( ? )
289 AND column_id = 1
290 AND is_identity = 1";
291 my ( $first_col_is_autoincrement ) = $dbh->selectrow_array( $stmt, {}, "$schema.$nq_table" );
292 return $first_col_is_autoincrement;
293 }
294 return;
295 }
296  
297  
298  
299 1;
300  
301  
302 __END__