DBBrowser/DB/DuckDB.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 104 CC 9 SCAR 38.3
CC
9
Cov
0%
get_databases
90.0
new
2.0
get_db_driver
2.0
CRAP 46.0
1 package # hide from PAUSE
2 App::DBBrowser::DB::DuckDB;
3  
4 use warnings;
5 use strict;
6 use 5.016;
7  
8 use DBI qw();
9 use List::MoreUtils qw( uniq );
10  
11 use Term::Choose::Util qw();
12  
13  
14 sub new {
15 my ( $class, $info, $opt ) = @_;
16 my $sf = {
17 i => $info,
18 o => $opt
19 };
20 bless $sf, $class;
21 }
22  
23  
24 sub get_db_driver {
25 my ( $sf ) = @_;
26 return 'DuckDB';
27 }
28  
29  
30 sub get_db_handle {
31 my ( $sf, $db ) = @_;
32 my $dsn = "dbi:DuckDB:dbname=$db";
33 my $dbh = DBI->connect( $dsn, '', '', {
34 PrintError => 0,
35 RaiseError => 1,
36 AutoCommit => 1,
37 ShowErrorStatement => 1,
38 #%{$sf->{o}{connect_attr}//{}},
39 } );
40 return $dbh;
41 }
42  
43  
44 sub get_databases {
45 my ( $sf ) = @_;
46 return \@ARGV if @ARGV;
47 my $file_duckdb_dbs = sprintf $sf->{i}{db_cache_file_fmt}, $sf->{i}{plugin}; ##
48 my $ax = App::DBBrowser::Auxil->new( {}, {}, {} );
49 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
50 my $saved_databases = $ax->read_json( $file_duckdb_dbs ) // [];
51 my $databases = [ @$saved_databases ];
52 if ( ! $sf->{i}{search} && @$databases ) {
53 return $databases;
54 }
55 my @pre = ( undef );
56 my ( $confirm, $add, $remove ) = ( ' Confirm', '- Add Database', '- Remove Database' );
57  
58 while ( 1 ) {
59 my $info = join( "\n", 'Databases: ', @$databases, '' );
60 # Choose
61 my $choice = $tc->choose(
62 [ @pre, $confirm, $add, $remove ],
63 { %{$sf->{i}{lyt_v}}, info => $info, undef => $sf->{i}{_back} }
64 );
65 $ax->print_sql_info( $info );
66 if ( ! defined $choice ) {
67 return $saved_databases;
68 }
69 elsif ( $choice eq $confirm ) {
70 $ax->write_json( $file_duckdb_dbs, $databases );
71 return $databases;
72 }
73 elsif ( $choice eq $add ) {
74 my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} );
75 my $new = $tu->choose_a_file( {
76 info => $info,
77 prompt => 'Choose the database location:',
78 cs_label => 'Database: ',
79 prompt2 => 'Your choice:'
80 } );
81 if ( length $new ) {
82 $databases = [ uniq sort( @$databases, $new ) ];
83 }
84 }
85 elsif ( $choice eq $remove ) {
86 my $info = 'Remove database';
87 my @pre = ( undef );
88 my $idx = $tc->choose( [ @pre, @$databases ], { info => $info, index => 1, layout => 2 } );
89 if ( $idx ) {
90 splice( @$databases, $idx - @pre, 1 );
91 }
92 }
93 }
94 }
95  
96  
97  
98  
99 1;
100  
101 __END__