| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::DB::SQLite; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use Encode qw( encode decode ); | ||
| 9 | use File::Find qw( find ); | ||
| 10 | use File::Spec::Functions qw( catfile ); | ||
| 11 | |||
| 12 | use DBD::SQLite 1.74; # sqlite 3.42.0 | ||
| 13 | use DBI qw(); | ||
| 14 | use Encode::Locale qw(); | ||
| 15 | |||
| 16 | use Term::Choose qw(); | ||
| 17 | use Term::Choose::Util qw(); | ||
| 18 | |||
| 19 | use App::DBBrowser::Auxil; | ||
| 20 | |||
| 21 | |||
| 22 | sub new { | ||
| 23 | my ( $class, $info, $opt ) = @_; | ||
| 24 | my $sf = { | ||
| 25 | i => $info, | ||
| 26 | o => $opt | ||
| 27 | }; | ||
| 28 | bless $sf, $class; | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | sub get_db_driver { | ||
| 33 | my ( $sf ) = @_; | ||
| 34 | return 'SQLite'; | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub get_db_handle { | ||
| 39 | my ( $sf, $db ) = @_; | ||
| 40 | my $busy_timeout = delete $sf->{o}{connect_attr}{sqlite_busy_timeout}; | ||
| 41 | my $dsn = "dbi:SQLite:dbname=$db"; | ||
| 42 | my $dbh = DBI->connect( $dsn, '', '', { | ||
| 43 | PrintError => 0, | ||
| 44 | RaiseError => 1, | ||
| 45 | AutoCommit => 1, | ||
| 46 | ShowErrorStatement => 1, | ||
| 47 | %{$sf->{o}{connect_attr}//{}}, ## | ||
| 48 | |||
| 49 | } ); | ||
| 50 | if ( DBI::looks_like_number( $busy_timeout ) ) { ## | ||
| 51 | $dbh->sqlite_busy_timeout( 0 + $busy_timeout ); | ||
| 52 | } | ||
| 53 | return $dbh; | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub get_databases { | ||
| 58 | my ( $sf ) = @_; | ||
| 59 | return \@ARGV if @ARGV; | ||
| 60 | my $cache_sqlite_files = sprintf $sf->{i}{db_cache_file_fmt}, $sf->{i}{plugin}; | ||
| 61 | my $ax = App::DBBrowser::Auxil->new( {}, {}, {} ); | ||
| 62 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 63 | my $db_cache = $ax->read_json( $cache_sqlite_files ) // {}; | ||
| 64 | my $dirs = $db_cache->{directories} // [ $sf->{i}{home_dir} ]; | ||
| 65 | my $databases = $db_cache->{databases} // []; | ||
| 66 | if ( ! $sf->{i}{search} && @$databases ) { | ||
| 67 | return $databases; | ||
| 68 | } | ||
| 69 | while ( 1 ) { | ||
| 70 | my ( $ok, $change ) = ( '- Confirm', '- Change' ); | ||
| 71 | # Choose | ||
| 72 | my $choice = $tc->choose( | ||
| 73 | [ undef, $ok, $change ], | ||
| 74 | { %{$sf->{i}{lyt_v}}, prompt => 'Search databases in: ' . join( ', ', @$dirs ), info => 'SQLite' } | ||
| 75 | ); | ||
| 76 | if ( ! defined $choice ) { | ||
| 77 | return $databases; | ||
| 78 | } | ||
| 79 | elsif ( $choice eq $change ) { | ||
| 80 | my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} ); | ||
| 81 | #my $info = 'Curr: ' . join( ', ', @$dirs ); # | ||
| 82 | my $new_dirs = $tu->choose_directories( { confirm => $sf->{i}{_confirm}, back => $sf->{i}{_back} } ); | ||
| 83 | if ( ! @{$new_dirs//[]} ) { | ||
| 84 | next; | ||
| 85 | } | ||
| 86 | $dirs = $new_dirs; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | last; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | $databases = []; | ||
| 93 | local $| = 1; | ||
| 94 | print 'Searching ... '; | ||
| 95 | my $encoding = Encode::find_encoding( 'locale_fs' ); | ||
| 96 | if ( $sf->{o}{G}{file_find_warnings} ) { | ||
| 97 | my $file; | ||
| 98 | for my $dir ( @$dirs ) { | ||
| 99 | File::Find::find( { | ||
| 100 | wanted => sub { | ||
| 101 | #my $file_fs = $_; | ||
| 102 | return if ! -f $_; | ||
| 103 | $file = $encoding->decode( $_ ); | ||
| 104 | print "$file\n"; | ||
| 105 | if ( ! eval { | ||
| 106 | open my $fh, '<:raw', $_ or die "$file: $!"; | ||
| 107 | defined( read $fh, my $string, 13 ) or die "$file: $!"; | ||
| 108 | close $fh; | ||
| 109 | push @$databases, $file if $string eq 'SQLite format'; | ||
| 110 | 1 } | ||
| 111 | ) { | ||
| 112 | utf8::decode( $@ ); | ||
| 113 | print $@; | ||
| 114 | } | ||
| 115 | }, | ||
| 116 | no_chdir => 1, | ||
| 117 | }, | ||
| 118 | $encoding->encode( $dir ) ); | ||
| 119 | } | ||
| 120 | $tc->choose( | ||
| 121 | [ 'Press ENTER to continue' ], | ||
| 122 | { prompt => 'Search finished.' } | ||
| 123 | ); | ||
| 124 | } | ||
| 125 | else { | ||
| 126 | no warnings qw( File::Find ); | ||
| 127 | my $file; | ||
| 128 | for my $dir ( @$dirs ) { | ||
| 129 | File::Find::find( { | ||
| 130 | wanted => sub { | ||
| 131 | #my $file_fs = $_; | ||
| 132 | return if ! -f $_; | ||
| 133 | $file = $encoding->decode( $_ ); | ||
| 134 | print "$file\n"; | ||
| 135 | eval { | ||
| 136 | open my $fh, '<:raw', $_ or die "$file: $!"; | ||
| 137 | defined( read $fh, my $string, 13 ) or die "$file: $!"; | ||
| 138 | close $fh; | ||
| 139 | push @$databases, $file if $string eq 'SQLite format'; | ||
| 140 | }; | ||
| 141 | }, | ||
| 142 | no_chdir => 1, | ||
| 143 | }, | ||
| 144 | $encoding->encode( $dir ) ); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | print 'Ended searching' . "\n"; | ||
| 148 | $db_cache->{directories} = $dirs; | ||
| 149 | $databases = [ sort { $a cmp $b } @$databases ]; | ||
| 150 | $db_cache->{databases} = $databases; | ||
| 151 | $ax->write_json( $cache_sqlite_files, $db_cache ); | ||
| 152 | return $databases; | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | 1; | ||
| 163 | |||
| 164 | |||
| 165 | __END__ |